system/FatalError: add FatalSystemError() overload with WIN32 error code

This commit is contained in:
Max Kellermann 2014-12-05 00:16:24 +01:00
parent a838a03412
commit 08bf4f74a9
2 changed files with 30 additions and 10 deletions

View File

@ -74,23 +74,31 @@ FatalError(const char *msg, const Error &error)
FormatFatalError("%s: %s", msg, error.GetMessage()); FormatFatalError("%s: %s", msg, error.GetMessage());
} }
#ifdef WIN32
void
FatalSystemError(const char *msg, DWORD code)
{
char buffer[256];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, code, 0,
buffer, sizeof(buffer), nullptr);
FormatFatalError("%s: %s", msg, buffer);
}
#endif
void void
FatalSystemError(const char *msg) FatalSystemError(const char *msg)
{ {
const char *system_error;
#ifdef WIN32 #ifdef WIN32
char buffer[256]; FatalSystemError(msg, GetLastError());
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, GetLastError(), 0,
buffer, sizeof(buffer), nullptr);
system_error = buffer;
#else #else
system_error = strerror(errno); const char *system_error = strerror(errno);
#endif
FormatError(fatal_error_domain, "%s: %s", msg, system_error); FormatError(fatal_error_domain, "%s: %s", msg, system_error);
Abort(); Abort();
#endif
} }
void void

View File

@ -23,6 +23,10 @@
#include "check.h" #include "check.h"
#include "Compiler.h" #include "Compiler.h"
#ifdef WIN32
#include <windef.h>
#endif
class Error; class Error;
/** /**
@ -53,6 +57,14 @@ gcc_noreturn
void void
FatalSystemError(const char *msg); FatalSystemError(const char *msg);
#ifdef WIN32
gcc_noreturn
void
FatalSystemError(const char *msg, DWORD code);
#endif
gcc_noreturn gcc_noreturn
void void
FormatFatalSystemError(const char *fmt, ...); FormatFatalSystemError(const char *fmt, ...);