41 snprintf(s_thread_name,
sizeof(s_thread_name),
"%s", name.c_str());
42 s_message_counter = 0;
49 using StringCount = std::pair<std::string, size_t>;
51 template <
typename Type>
55 template <
typename Key,
typename T,
typename Compare = std::less<Key> >
56 using logger_map = std::map<Key,
T, Compare,
57 LoggerAllocator<std::pair<const Key, T> > >;
60 static std::mutex s_mutex;
63 static logger_map<std::thread::id, StringCount> s_threads;
67 std::lock_guard<std::mutex> lock(s_mutex);
68 s_threads[std::this_thread::get_id()] = StringCount(name, 0);
79 ThreadLoggerPrefixHook();
82 ~ThreadLoggerPrefixHook();
85 void add_log_prefix(std::ostream& os)
final;
94 ThreadLoggerPrefixHook::ThreadLoggerPrefixHook() {
98 ThreadLoggerPrefixHook::~ThreadLoggerPrefixHook() {
102 void ThreadLoggerPrefixHook::add_log_prefix(std::ostream& os) {
106 if (*s_thread_name != 0) {
107 os << s_thread_name <<
' ';
110 os <<
"unknown " << std::this_thread::get_id() <<
' ';
113 std::ios::fmtflags flags(os.flags());
114 os << std::setfill(
'0') << std::setw(6) << s_message_counter++;
120 std::lock_guard<std::mutex> lock(s_mutex);
122 auto it = s_threads.find(std::this_thread::get_id());
123 if (it != s_threads.end()) {
124 StringCount& sc = it->second;
125 std::ios::fmtflags flags(os.flags());
126 os << sc.first <<
' ' 127 << std::setfill(
'0') << std::setw(6) << sc.second++;
131 os <<
"unknown " << std::this_thread::get_id();
mem::Manager g_logger_mem_manager(nullptr, "Logger")
memory manager singleton for Logger
static thread_local size_t s_message_counter
thread message counter
static ThreadLoggerPrefixHook s_default_logger
default logger singleton
void NameThisThread(const std::string &name)
Defines a name for the current thread, only if no name was set previously.
static thread_local char s_thread_name[64]
thread name
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Abstract class to implement prefix output hooks for logging.
LoggerPrefixHook * set_logger_prefix_hook(LoggerPrefixHook *hook)