So many changes. Broken.
[invirt/third/libt4.git] / debug.cc
1 #include "include/t4.h"
2 #include "include/types.h"
3 #include "include/debug.h"
4
5 using namespace std::chrono;
6
7 locked_ostream && _log_prefix(locked_ostream && f, const string & file, const string & func) {
8     auto thread = std::this_thread::get_id();
9     int tid = global->thread_name_map[thread];
10     if (tid==0)
11         tid = global->thread_name_map[thread] = ++global->next_thread_num;
12     auto utime = (system_clock::now().time_since_epoch() / 1us) % 1000000000;
13     f << std::setfill('0') << std::dec << std::left << std::setw(9) << utime << " ";
14     f << std::setfill(' ') << global->log_thread_prefix << std::left << std::setw(2) << tid;
15     f << " " << std::setw(20) << file << " " << std::setw(18) << func;
16     return std::move(f);
17 }
18
19 locked_ostream && _log_member(locked_ostream && f, const void *ptr) {
20     int id = global->instance_name_map[ptr];
21     if (id == 0)
22         id = global->instance_name_map[ptr] = ++global->next_instance_num;
23     f << "#" << std::left << std::setw(2) << id << " ";
24     return std::move(f);
25 }
26
27 int _log_debug_level() {
28     return global->DEBUG_LEVEL;
29 }
30
31 lock _log_lock() {
32     return lock(global->log_mutex);
33 }
34
35 string hex_string(const string & s) {
36     string bytes;
37     for (char ch : s) {
38         bytes.push_back("0123456789abcdef"[(uint8_t)ch >> 4]);
39         bytes.push_back("0123456789abcdef"[(uint8_t)ch & 15]);
40     }
41     return bytes;
42 }