From 5e7a9b7697a4733f5985b2054c3cce314148135c Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 4 Sep 2025 00:54:26 +0300 Subject: [PATCH] Fix saving thread names in logger --- libs/log/source/log.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libs/log/source/log.cpp b/libs/log/source/log.cpp index a452eca2..582f8341 100644 --- a/libs/log/source/log.cpp +++ b/libs/log/source/log.cpp @@ -24,7 +24,7 @@ namespace psemek::log std::atomic max_thread_name_length = 3; std::mutex thread_names_mutex; - util::hash_map thread_names; + util::hash_map> thread_names; std::mutex sinks_mutex; std::vector> sinks; @@ -163,10 +163,14 @@ namespace psemek::log if (it != thread_names.end()) throw util::exception("Thread \"" + name + "\" already registered!"); - thread_names[id] = name; + thread_names[id] = std::make_unique(name); } - max_thread_name_length = std::max(max_thread_name_length.load(), name.size()); + { + std::size_t current_max = max_thread_name_length.load(); + while(current_max < name.size() && !max_thread_name_length.compare_exchange_weak(current_max, name.size())) + {} + } put_message(level::info, "Thread \"" + name + "\" registered"); } @@ -185,7 +189,7 @@ namespace psemek::log throw util::exception(os.str()); } - name = std::move(it->second); + name = std::move(*(it->second)); thread_names.erase(it); } @@ -236,7 +240,7 @@ namespace psemek::log if (it == thread_names.end()) thread_name = &unknown_thread_name; else - thread_name = &(it->second); + thread_name = it->second.get(); } put_message(l, message, *thread_name);