feat: Add log-ext, handle win last errors

Helper to easily deal with GetLastError, log it and
make the application fail
This commit is contained in:
icex2
2024-08-15 11:34:31 +02:00
parent d73ded1895
commit 7a6c3af9c8
3 changed files with 50 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ src_core := \
config-property-node.c \
log-bt-ext.c \
log-bt.c \
log-ext.c \
log-sink-async.c \
log-sink-debug.c \
log-sink-file.c \

27
src/main/core/log-ext.c Normal file
View File

@@ -0,0 +1,27 @@
#include <windows.h>
#include "iface-core/log.h"
void core_log_ext_win_last_error_log(const char *module, bt_core_log_message_t log_message)
{
LPSTR buffer;
DWORD last_error;
last_error = GetLastError();
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
last_error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &buffer,
0,
NULL);
log_message(module, "Last error (%08X): %s", last_error, buffer);
LocalFree(buffer);
}

22
src/main/core/log-ext.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef CORE_LOG_EXT_H
#define CORE_LOG_EXT_H
#include "iface-core/log.h"
#define log_fatal_on_win_last_error(...) \
if (GetLastError() != 0) { \
_bt_core_log_api.v1.fatal( \
LOG_MODULE, \
"%s:%d: function `%s'", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
_bt_core_log_api.v1.fatal(LOG_MODULE, __VA_ARGS__); \
core_log_ext_win_last_error_log(LOG_MODULE, _bt_core_log_api.v1.fatal); \
_bt_core_log_api.v1.fatal(LOG_MODULE, ""); \
abort(); \
}
void core_log_ext_win_last_error_log(const char *module, bt_core_log_message_t log_message);
#endif