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