Add helper to log Windows system errors
It will help to log errors returned by GetLastError() or WSAGetLastError(): - <https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror> - <https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsagetlasterror> - <https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes#system-error-codes> Always log the errors in English to be able to read them in bug reports.
This commit is contained in:
parent
5508c635cb
commit
80bec70852
3 changed files with 31 additions and 8 deletions
|
@ -1,5 +1,8 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
static SDL_LogPriority
|
static SDL_LogPriority
|
||||||
|
@ -51,3 +54,24 @@ sc_get_log_level(void) {
|
||||||
SDL_LogPriority sdl_log = SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION);
|
SDL_LogPriority sdl_log = SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION);
|
||||||
return log_level_sdl_to_sc(sdl_log);
|
return log_level_sdl_to_sc(sdl_log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
bool
|
||||||
|
sc_log_windows_error(const char *prefix, int error) {
|
||||||
|
assert(prefix);
|
||||||
|
|
||||||
|
char *message;
|
||||||
|
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM;
|
||||||
|
DWORD lang_id = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
|
||||||
|
int ret =
|
||||||
|
FormatMessage(flags, NULL, error, lang_id, (char *) &message, 0, NULL);
|
||||||
|
if (ret <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: message already contains a trailing '\n'
|
||||||
|
LOGE("%s: [%d] %s", prefix, error, message);
|
||||||
|
LocalFree(message);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
|
@ -26,4 +26,10 @@ sc_set_log_level(enum sc_log_level level);
|
||||||
enum sc_log_level
|
enum sc_log_level
|
||||||
sc_get_log_level(void);
|
sc_get_log_level(void);
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Log system error (typically returned by GetLastError() or similar)
|
||||||
|
bool
|
||||||
|
sc_log_windows_error(const char *prefix, int error);
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -117,14 +117,7 @@ set_cloexec_flag(sc_raw_socket raw_sock) {
|
||||||
static void
|
static void
|
||||||
net_perror(const char *s) {
|
net_perror(const char *s) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
int error = WSAGetLastError();
|
sc_log_windows_error(s, WSAGetLastError());
|
||||||
char *wsa_message;
|
|
||||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
||||||
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(char *) &wsa_message, 0, NULL);
|
|
||||||
// no explicit '\n', wsa_message already contains a trailing '\n'
|
|
||||||
fprintf(stderr, "%s: [%d] %s", s, error, wsa_message);
|
|
||||||
LocalFree(wsa_message);
|
|
||||||
#else
|
#else
|
||||||
perror(s);
|
perror(s);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue