Fix saving thread names in logger
This commit is contained in:
parent
3e4d4f39ea
commit
5e7a9b7697
1 changed files with 9 additions and 5 deletions
|
|
@ -24,7 +24,7 @@ namespace psemek::log
|
||||||
std::atomic<std::size_t> max_thread_name_length = 3;
|
std::atomic<std::size_t> max_thread_name_length = 3;
|
||||||
|
|
||||||
std::mutex thread_names_mutex;
|
std::mutex thread_names_mutex;
|
||||||
util::hash_map<std::thread::id, std::string> thread_names;
|
util::hash_map<std::thread::id, std::unique_ptr<std::string>> thread_names;
|
||||||
|
|
||||||
std::mutex sinks_mutex;
|
std::mutex sinks_mutex;
|
||||||
std::vector<std::unique_ptr<sink>> sinks;
|
std::vector<std::unique_ptr<sink>> sinks;
|
||||||
|
|
@ -163,10 +163,14 @@ namespace psemek::log
|
||||||
if (it != thread_names.end())
|
if (it != thread_names.end())
|
||||||
throw util::exception("Thread \"" + name + "\" already registered!");
|
throw util::exception("Thread \"" + name + "\" already registered!");
|
||||||
|
|
||||||
thread_names[id] = name;
|
thread_names[id] = std::make_unique<std::string>(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");
|
put_message(level::info, "Thread \"" + name + "\" registered");
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +189,7 @@ namespace psemek::log
|
||||||
throw util::exception(os.str());
|
throw util::exception(os.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
name = std::move(it->second);
|
name = std::move(*(it->second));
|
||||||
thread_names.erase(it);
|
thread_names.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,7 +240,7 @@ namespace psemek::log
|
||||||
if (it == thread_names.end())
|
if (it == thread_names.end())
|
||||||
thread_name = &unknown_thread_name;
|
thread_name = &unknown_thread_name;
|
||||||
else
|
else
|
||||||
thread_name = &(it->second);
|
thread_name = it->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
put_message(l, message, *thread_name);
|
put_message(l, message, *thread_name);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue