0160f338d3009f6380446335171f3c22644bb18f
[invirt/third/libt4.git] / threaded_log.cc
1 #include "threaded_log.h"
2 #include "t4.h"
3
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;
9 int DEBUG_LEVEL = 0;
10
11 using namespace std::chrono;
12
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];
16     if (tid==0)
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;
23     return std::move(f);
24 }
25
26 locked_ostream && _log_member(locked_ostream && f, const void *ptr) {
27     int id = instance_name_map[ptr];
28     if (id == 0)
29         id = instance_name_map[ptr] = ++next_instance_num;
30     f << "#" << std::left << std::setw(2) << id << " ";
31     return std::move(f);
32 }
33
34 lock _log_lock() {
35     return lock(log_mutex);
36 }