Browse Source

The log prints a truncated id for the current thread

Log messages now have the string "[thrd=%lx]" to indicate which
thread they were logged from. This creates a circular dependency
between the log and thread libraries, but is the simplest way
to achieve this feature.
Steven Engler 2 years ago
parent
commit
9608f905ec
2 changed files with 5 additions and 3 deletions
  1. 2 2
      Makefile.am
  2. 3 1
      src/lib/log/log.c

+ 2 - 2
Makefile.am

@@ -50,12 +50,12 @@ TOR_UTIL_LIBS = \
 	src/lib/libtor-sandbox.a \
 	src/lib/libtor-container.a \
 	src/lib/libtor-net.a \
-	src/lib/libtor-thread.a \
 	src/lib/libtor-memarea.a \
 	src/lib/libtor-math.a \
 	src/lib/libtor-meminfo.a \
 	src/lib/libtor-osinfo.a \
 	src/lib/libtor-log.a \
+	src/lib/libtor-thread.a \
 	src/lib/libtor-lock.a \
 	src/lib/libtor-fdio.a \
 	src/lib/libtor-string.a \
@@ -84,13 +84,13 @@ TOR_UTIL_TESTING_LIBS = \
 	src/lib/libtor-sandbox-testing.a \
 	src/lib/libtor-container-testing.a \
 	src/lib/libtor-net-testing.a \
-	src/lib/libtor-thread-testing.a \
 	src/lib/libtor-memarea-testing.a \
 	src/lib/libtor-math-testing.a \
 	src/lib/libtor-meminfo-testing.a \
 	src/lib/libtor-osinfo-testing.a \
 	src/lib/libtor-term-testing.a \
 	src/lib/libtor-log-testing.a \
+	src/lib/libtor-thread-testing.a \
 	src/lib/libtor-lock-testing.a \
 	src/lib/libtor-fdio-testing.a \
 	src/lib/libtor-string-testing.a \

+ 3 - 1
src/lib/log/log.c

@@ -50,6 +50,7 @@
 #include "lib/wallclock/time_to_tm.h"
 #include "lib/fdio/fdio.h"
 #include "lib/cc/ctassert.h"
+#include "lib/thread/threads.h"
 
 #ifdef HAVE_ANDROID_LOG_H
 #include <android/log.h>
@@ -307,7 +308,8 @@ log_prefix_(char *buf, size_t buf_len, int severity)
 
   n = strftime(buf, buf_len, "%b %d %H:%M:%S",
                tor_localtime_r_msg(&t, &tm, NULL));
-  r = tor_snprintf(buf+n, buf_len-n, ".%.3i [%s] ", ms,
+  r = tor_snprintf(buf+n, buf_len-n, ".%.3i [thrd=%05lx] [%s] ", ms,
+                   tor_get_thread_id() / 4096 % 1048576, /* Make it more readable */
                    sev_to_string(severity));
 
   if (r<0)