Logging: automatically increase max thread name length + properly log thread name if thread unregisters itself

This commit is contained in:
Nikita Lisitsa 2021-03-05 11:21:05 +03:00
parent 725a7a6ea2
commit 3c4e1f99df
2 changed files with 30 additions and 28 deletions

View file

@ -9,8 +9,6 @@
namespace psemek::log namespace psemek::log
{ {
void set_max_thread_name_length(std::size_t length);
void register_thread(std::string name); void register_thread(std::string name);
void register_thread(std::thread::id id, std::string name); void register_thread(std::thread::id id, std::string name);
void unregister_thread(std::thread::id id); void unregister_thread(std::thread::id id);

View file

@ -8,20 +8,18 @@
#include <mutex> #include <mutex>
#include <unordered_map> #include <unordered_map>
#include <thread> #include <thread>
#include <atomic>
namespace psemek::log namespace psemek::log
{ {
static std::size_t max_thread_name_length = 8; static std::atomic<std::size_t> max_thread_name_length = 3;
void set_max_thread_name_length(std::size_t length)
{
max_thread_name_length = length;
}
static std::mutex thread_names_mutex; static std::mutex thread_names_mutex;
static std::unordered_map<std::thread::id, std::string> thread_names; static std::unordered_map<std::thread::id, std::string> thread_names;
static void put_message(level l, std::string const & message, std::string const & thread_name);
void register_thread(std::string name) void register_thread(std::string name)
{ {
register_thread(std::this_thread::get_id(), std::move(name)); register_thread(std::this_thread::get_id(), std::move(name));
@ -36,12 +34,11 @@ namespace psemek::log
if (it != thread_names.end()) if (it != thread_names.end())
throw std::runtime_error("Thread \"" + name + "\" already registered!"); throw std::runtime_error("Thread \"" + name + "\" already registered!");
if (name.size() > max_thread_name_length)
throw std::runtime_error("Thread \"" + name + "\" name is too long");
thread_names[id] = name; thread_names[id] = name;
} }
max_thread_name_length = std::max(max_thread_name_length.load(), name.size());
put_message(level::info, "Thread \"" + name + "\" registered"); put_message(level::info, "Thread \"" + name + "\" registered");
} }
@ -59,11 +56,14 @@ namespace psemek::log
throw std::runtime_error(os.str()); throw std::runtime_error(os.str());
} }
name = it->second; name = std::move(it->second);
thread_names.erase(it); thread_names.erase(it);
} }
put_message(level::info, "Thread \"" + name + "\" unregistered"); if (id == std::this_thread::get_id())
put_message(level::info, "Thread \"" + name + "\" unregistered", name);
else
put_message(level::info, "Thread \"" + name + "\" unregistered");
} }
static ::tm safe_localtime(std::time_t const & time) static ::tm safe_localtime(std::time_t const & time)
@ -73,10 +73,8 @@ namespace psemek::log
return *std::localtime(&time); return *std::localtime(&time);
} }
void put_message(level l, std::string const & message) static void put_message(level l, std::string const & message, std::string const & thread_name)
{ {
static std::string unknown_thread_name = "???";
using clock = std::chrono::system_clock; using clock = std::chrono::system_clock;
auto const time = clock::to_time_t(clock::now()); auto const time = clock::to_time_t(clock::now());
@ -84,10 +82,25 @@ namespace psemek::log
auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now().time_since_epoch()).count() % 1000; auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now().time_since_epoch()).count() % 1000;
std::ostringstream os;
os
<< '[' << std::put_time(&tm, "%Y %b %d %H:%M:%S.") << std::setw(3) << std::setfill('0') << millis << ']'
<< '[' << std::setw(max_thread_name_length) << std::setfill(' ') << thread_name << ']'
<< '[' << std::setw(7) << l << ']'
<< ' ' << message;
static std::mutex cout_mutex;
std::lock_guard lock{cout_mutex};
std::cout << os.str() << std::endl;
}
void put_message(level l, std::string const & message)
{
static std::string unknown_thread_name = "???";
auto const id = std::this_thread::get_id(); auto const id = std::this_thread::get_id();
std::string const * thread_name = nullptr;
std::string const * thread_name;
{ {
std::lock_guard lock{thread_names_mutex}; std::lock_guard lock{thread_names_mutex};
auto const it = thread_names.find(id); auto const it = thread_names.find(id);
@ -97,16 +110,7 @@ namespace psemek::log
thread_name = &(it->second); thread_name = &(it->second);
} }
std::ostringstream os; put_message(l, message, *thread_name);
os
<< '[' << std::put_time(&tm, "%Y %b %d %H:%M:%S.") << std::setw(3) << std::setfill('0') << millis << ']'
<< '[' << std::setw(max_thread_name_length) << std::setfill(' ') << *thread_name << ']'
<< '[' << std::setw(7) << l << ']'
<< ' ' << message;
static std::mutex cout_mutex;
std::lock_guard lock{cout_mutex};
std::cout << os.str() << std::endl;
} }
} }