From be4659469bb3596e1c8629f476a9558095bfdba0 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 24 Apr 2023 12:01:32 +0200 Subject: [PATCH] log_msg_once updates use static, not thread_local storage This has an implications that the message is indeed printed once, even though is called from 2 threads (this can happen eg. when printed once during normal operation and then during module deletion, that may happen in a different thread). --- src/debug.cpp | 22 ++++++++++++++++++---- src/debug.h | 1 - 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/debug.cpp b/src/debug.cpp index 4a0e7a66d..cfe073fc1 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -46,6 +46,7 @@ #include "config_win32.h" #include +#include #include #include #include @@ -58,6 +59,7 @@ #include "utils/string_view_utils.hpp" #include "utils/misc.h" // ug_strerror +using std::atomic; using std::string; using std::unordered_map; @@ -133,14 +135,28 @@ void log_msg(int level, const char *format, ...) { } void log_msg_once(int level, uint32_t id, const char *msg, ...) { - if (Logger::oneshot_messages.count(id) > 0 || log_level < level) { + if (log_level < level) { return; } - Logger::oneshot_messages.insert(id); + static volatile uint32_t oneshot_messages[10]; + static atomic oneshot_messages_cnt; + for (size_t i = 0; i < oneshot_messages_cnt; ++i) { + if (oneshot_messages[i] == id) { + return; + } + } + va_list aq; va_start(aq, msg); log_vprintf(level, msg, aq); va_end(aq); + + size_t last_cnt = oneshot_messages_cnt.fetch_add(1); + if (last_cnt < sizeof oneshot_messages / sizeof oneshot_messages[0]) { + oneshot_messages[last_cnt] = id; + } else { + log_msg(LOG_LEVEL_WARNING, "oneshot_messages full!\n"); + } } /** @@ -336,8 +352,6 @@ void debug_file_dump(const char *key, void (*serialize)(const void *data, FILE * } #endif -thread_local std::set Logger::oneshot_messages; - Log_output::Log_output(){ last_msg.reserve(initial_buf_size); interactive = color_output_init(); diff --git a/src/debug.h b/src/debug.h index f265b9920..063bd1090 100644 --- a/src/debug.h +++ b/src/debug.h @@ -301,7 +301,6 @@ private: std::ostringstream oss; friend void log_msg_once(int level, uint32_t id, const char *msg, ...); - static thread_local std::set oneshot_messages; }; #define LOG(level) \