Global destructor clean-ups and python test fixes
[invirt/third/libt4.git] / threaded_log.cc
1 #include "threaded_log.h"
2
3 static std::mutex log_mutex;
4 static std::map<thread::id, int> thread_name_map;
5 static int next_thread_num = 0;
6 static std::map<const void *, int> instance_name_map;
7 static int next_instance_num = 0;
8 int DEBUG_LEVEL = 0;
9
10 using namespace std::chrono;
11
12 locked_ostream && _log_prefix(locked_ostream && f, const string & file, const string & func) {
13     auto thread = std::this_thread::get_id();
14     int tid = thread_name_map[thread];
15     if (tid==0)
16         tid = thread_name_map[thread] = ++next_thread_num;
17     auto utime = duration_cast<microseconds>(
18             system_clock::now().time_since_epoch()).count() % 1000000000;
19     f << std::setfill('0') << std::dec << std::left << std::setw(9) << utime << " ";
20     f << std::setfill(' ') << log_thread_prefix << std::left << std::setw(2) << tid;
21     f << " " << std::setw(20) << file << " " << std::setw(18) << func;
22     return std::move(f);
23 }
24
25 locked_ostream && _log_member(locked_ostream && f, const void *ptr) {
26     int id = instance_name_map[ptr];
27     if (id == 0)
28         id = instance_name_map[ptr] = ++next_instance_num;
29     f << "#" << std::left << std::setw(2) << id << " ";
30     return std::move(f);
31 }
32
33 lock _log_lock() {
34     return lock(log_mutex);
35 }