win: Use FormatMessage[W]() instead of FormatMessageA() for logging (issue 1429953004 by mark@chromium.org)

359 views
Skip to first unread message

ma...@chromium.org

unread,
Nov 9, 2015, 3:25:53 PM11/9/15
to sco...@chromium.org, crashp...@chromium.org
Reviewers: scottmg
CL: https://codereview.chromium.org/1429953004/

Message:
This includes two other changes:
- It switches the GetLastError() code formatting to decimal, to match
the declarations in <winerror.h>.
- It gets rid of the double-spacing in PLOG() messages. I wouldn’t
normally have done anything about this, but since the code was right
here…

Messages change from this:

[p:t:yyyymmdd,hhmmss.mmm:FATAL exception_handler_server.cc:357] Check
failed:
pipe != INVALID_HANDLE_VALUE. CreateNamedPipe: The specified path is
invalid.
(0xA1)

to this:

[p:t:yyyymmdd,hhmmss.mmm:FATAL exception_handler_server.cc:357] Check
failed:
pipe != INVALID_HANDLE_VALUE. CreateNamedPipe: The specified path is
invalid.
(161)

Description:
win: Use FormatMessage[W]() instead of FormatMessageA() for logging

FormatMessageA() returns a message in the system's native code page,
but the logging interface expects and assumes UTF-8. UTF-8 can be
obtained by calling the UTF-16 FormatMessage() variant and converting.

Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master

Affected files (+12, -6 lines):
M base/logging.cc


Index: base/logging.cc
diff --git a/base/logging.cc b/base/logging.cc
index
d4a250bd8525cf0e566304dad1a67b42f6fcde4a..b2df48f686623f96de9073eca19fdd775c6628c0
100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -54,15 +54,21 @@ LogMessageHandlerFunction GetLogMessageHandler() {

#if defined(OS_WIN)
std::string SystemErrorCodeToString(unsigned long error_code) {
- char msgbuf[256];
+ wchar_t msgbuf[256];
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS
|
FORMAT_MESSAGE_MAX_WIDTH_MASK;
- DWORD len = FormatMessageA(
- flags, NULL, error_code, 0, msgbuf, arraysize(msgbuf), NULL);
+ DWORD len = FormatMessage(
+ flags, nullptr, error_code, 0, msgbuf, arraysize(msgbuf), nullptr);
if (len) {
- return msgbuf + base::StringPrintf(" (0x%X)", error_code);
+ // Most system messages end in a period and a space. Remove the space
if
+ // it’s there, because the following StringPrintf() includes one.
+ if (len >= 1 && msgbuf[len - 1] == ' ') {
+ msgbuf[len - 1] = '\0';
+ }
+ return base::StringPrintf("%s (%u)",
+ base::UTF16ToUTF8(msgbuf).c_str(),
error_code);
}
- return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)",
+ return base::StringPrintf("Error %u while retrieving error %u",
GetLastError(),
error_code);
}
@@ -157,7 +163,7 @@ void LogMessage::Init(const char* function) {

#if defined(OS_POSIX)
timeval tv;
- gettimeofday(&tv, NULL);
+ gettimeofday(&tv, nullptr);
tm local_time;
localtime_r(&tv.tv_sec, &local_time);
stream_ << std::setw(4) << local_time.tm_year + 1900


sco...@chromium.org

unread,
Nov 9, 2015, 3:43:14 PM11/9/15
to ma...@chromium.org, crashp...@chromium.org

mark@chromium.org via codereview.chromium.org

unread,
Nov 9, 2015, 3:48:40 PM11/9/15
to sco...@chromium.org, ma...@chromium.org, crashp...@chromium.org
Committed patchset #1 (id:1) manually as
06e0fa1309a04bca3d785cc5635840366ba73682 (presubmit successful).

https://codereview.chromium.org/1429953004/
Reply all
Reply to author
Forward
0 new messages