Rewrote threaded log code to be more idiomatic.
[invirt/third/libt4.git] / threaded_log.cc
index c44266e..b450f52 100644 (file)
@@ -3,6 +3,26 @@
 mutex cerr_mutex;
 map<thread::id, int> thread_name_map;
 int next_thread_num = 0;
-map<void *, int> instance_name_map;
+map<const void *, int> instance_name_map;
 int next_instance_num = 0;
 int DEBUG_LEVEL = 0;
+
+locked_ostream && _log_prefix(locked_ostream && f, const string & file, const string & func) {
+    auto thread = this_thread::get_id();
+    int tid = thread_name_map[thread];
+    if (tid==0)
+        tid = thread_name_map[thread] = ++next_thread_num;
+    auto utime = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count() % 1000000000;
+    f << setfill('0') << dec << left << setw(9) << utime << " ";
+    f << setfill(' ') << log_thread_prefix << left << setw(2) << tid;
+    f << " " << setw(20) << file << " " << setw(18) << func;
+    return move(f);
+}
+
+locked_ostream && _log_member(locked_ostream && f, const void *ptr) {
+    int id = instance_name_map[ptr];
+    if (id == 0)
+        id = instance_name_map[ptr] = ++next_instance_num;
+    f << "#" << left << setw(2) << id << " ";
+    return move(f);
+}