Remove duplicated code on Windows
This also gives a litle more info when FormatMessageW fails. R=whesse@google.com BUG= Review URL: https://codereview.chromium.org//23463044 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@27639 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
@@ -61,5 +61,6 @@
|
||||
'utils_linux.cc',
|
||||
'utils_macos.cc',
|
||||
'utils_win.cc',
|
||||
'utils_win.h',
|
||||
],
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "bin/log.h"
|
||||
#include "bin/thread.h"
|
||||
#include "bin/utils.h"
|
||||
#include "bin/utils_win.h"
|
||||
|
||||
|
||||
namespace dart {
|
||||
@@ -305,21 +306,7 @@ static int SetOsErrorMessage(char** os_error_message) {
|
||||
int error_code = GetLastError();
|
||||
static const int kMaxMessageLength = 256;
|
||||
wchar_t message[kMaxMessageLength];
|
||||
DWORD message_size =
|
||||
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL,
|
||||
error_code,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
message,
|
||||
kMaxMessageLength,
|
||||
NULL);
|
||||
if (message_size == 0) {
|
||||
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
||||
Log::PrintErr("FormatMessage failed %d\n", GetLastError());
|
||||
}
|
||||
_snwprintf(message, kMaxMessageLength, L"OS Error %d", error_code);
|
||||
}
|
||||
message[kMaxMessageLength - 1] = '\0';
|
||||
FormatMessageIntoBuffer(error_code, message, kMaxMessageLength);
|
||||
*os_error_message = StringUtils::WideToUtf8(message);
|
||||
return error_code;
|
||||
}
|
||||
|
||||
@@ -9,15 +9,14 @@
|
||||
#include <time.h> // NOLINT
|
||||
|
||||
#include "bin/utils.h"
|
||||
#include "bin/utils_win.h"
|
||||
#include "bin/log.h"
|
||||
|
||||
|
||||
namespace dart {
|
||||
namespace bin {
|
||||
|
||||
static void FormatMessageIntoBuffer(DWORD code,
|
||||
wchar_t* buffer,
|
||||
int buffer_length) {
|
||||
void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length) {
|
||||
DWORD message_size =
|
||||
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL,
|
||||
@@ -28,11 +27,14 @@ static void FormatMessageIntoBuffer(DWORD code,
|
||||
NULL);
|
||||
if (message_size == 0) {
|
||||
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
||||
Log::PrintErr("FormatMessage failed %d\n", GetLastError());
|
||||
Log::PrintErr("FormatMessage failed for error code %d (error %d)\n",
|
||||
code,
|
||||
GetLastError());
|
||||
}
|
||||
_snwprintf(buffer, buffer_length, L"OS Error %d", code);
|
||||
}
|
||||
buffer[buffer_length - 1] = '\0';
|
||||
// Ensure string termination.
|
||||
buffer[buffer_length - 1] = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
#ifndef BIN_UTILS_WIN_H_
|
||||
#define BIN_UTILS_WIN_H_
|
||||
|
||||
#include "platform/globals.h"
|
||||
|
||||
namespace dart {
|
||||
namespace bin {
|
||||
|
||||
void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length);
|
||||
|
||||
} // namespace bin
|
||||
} // namespace dart
|
||||
|
||||
#endif // BIN_UTILS_WIN_H_
|
||||
Reference in New Issue
Block a user