From 7a6c3af9c8c1267982db39320c86a22279352f5f Mon Sep 17 00:00:00 2001 From: icex2 Date: Thu, 15 Aug 2024 11:34:31 +0200 Subject: [PATCH] feat: Add log-ext, handle win last errors Helper to easily deal with GetLastError, log it and make the application fail --- src/main/core/Module.mk | 1 + src/main/core/log-ext.c | 27 +++++++++++++++++++++++++++ src/main/core/log-ext.h | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/main/core/log-ext.c create mode 100644 src/main/core/log-ext.h diff --git a/src/main/core/Module.mk b/src/main/core/Module.mk index a052b13..2ddb34a 100644 --- a/src/main/core/Module.mk +++ b/src/main/core/Module.mk @@ -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 \ diff --git a/src/main/core/log-ext.c b/src/main/core/log-ext.c new file mode 100644 index 0000000..87c89b3 --- /dev/null +++ b/src/main/core/log-ext.c @@ -0,0 +1,27 @@ + + +#include + +#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); +} \ No newline at end of file diff --git a/src/main/core/log-ext.h b/src/main/core/log-ext.h new file mode 100644 index 0000000..0b90906 --- /dev/null +++ b/src/main/core/log-ext.h @@ -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 \ No newline at end of file