1 #include "threaded_log.h"
4 static std::mutex log_mutex;
5 static std::map<thread::id, int> thread_name_map;
6 static int next_thread_num = 0;
7 static std::map<const void *, int> instance_name_map;
8 static int next_instance_num = 0;
11 using namespace std::chrono;
13 locked_ostream && _log_prefix(locked_ostream && f, const string & file, const string & func) {
14 auto thread = std::this_thread::get_id();
15 int tid = thread_name_map[thread];
17 tid = thread_name_map[thread] = ++next_thread_num;
18 auto utime = duration_cast<microseconds>(
19 system_clock::now().time_since_epoch()).count() % 1000000000;
20 f << std::setfill('0') << std::dec << std::left << std::setw(9) << utime << " ";
21 f << std::setfill(' ') << global->log_thread_prefix << std::left << std::setw(2) << tid;
22 f << " " << std::setw(20) << file << " " << std::setw(18) << func;
26 locked_ostream && _log_member(locked_ostream && f, const void *ptr) {
27 int id = instance_name_map[ptr];
29 id = instance_name_map[ptr] = ++next_instance_num;
30 f << "#" << std::left << std::setw(2) << id << " ";
35 return lock(log_mutex);