From 0cd066933511edb7869c5f6ce462b019a140ce3c Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Tue, 20 Dec 2011 11:43:57 -0800 Subject: factor out windows perror equivalent --- src/subprocess-win32.cc | 16 +--------------- src/util.cc | 21 +++++++++++++++++++++ src/util.h | 3 +++ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/subprocess-win32.cc b/src/subprocess-win32.cc index 79c4eba..cf61feb 100644 --- a/src/subprocess-win32.cc +++ b/src/subprocess-win32.cc @@ -23,21 +23,7 @@ namespace { void Win32Fatal(const char* function) { - DWORD err = GetLastError(); - - char* msg_buf; - FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - err, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (char*)&msg_buf, - 0, - NULL); - Fatal("%s: %s", function, msg_buf); - LocalFree(msg_buf); + Fatal("%s: %s", function, GetLastErrorString().c_str()); } } // anonymous namespace diff --git a/src/util.cc b/src/util.cc index e8cd11e..0511806 100644 --- a/src/util.cc +++ b/src/util.cc @@ -210,3 +210,24 @@ const char* SpellcheckString(const string& text, ...) { va_end(ap); return result; } + +#ifdef WIN32 +string GetLastErrorString() { + DWORD err = GetLastError(); + + char* msg_buf; + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (char*)&msg_buf, + 0, + NULL); + string msg = msg_buf; + LocalFree(msg_buf); + return msg; +} +#endif diff --git a/src/util.h b/src/util.h index d36e2e1..f6601d3 100644 --- a/src/util.h +++ b/src/util.h @@ -57,6 +57,9 @@ const char* SpellcheckString(const string& text, ...); #ifdef _WIN32 #define snprintf _snprintf + +/// Convert the value returned by GetLastError() into a string. +string GetLastErrorString(); #endif #endif // NINJA_UTIL_H_ -- cgit v0.12