From 2222362f66918fbc9c5f0e0806d53d6b000352b7 Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Sat, 8 Jun 2013 12:49:54 +0100 Subject: [PATCH] tvhlog: add option to display the thread ID in log messages --- src/main.c | 4 ++++ src/tvhlog.c | 7 ++++++- src/tvhlog.h | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index abb8a9e8..44c39bc4 100644 --- a/src/main.c +++ b/src/main.c @@ -418,6 +418,7 @@ main(int argc, char **argv) opt_abort = 0, opt_noacl = 0, opt_fileline = 0, + opt_threadid = 0, opt_ipv6 = 0, opt_tsfile_tuner = 0; const char *opt_config = NULL, @@ -475,6 +476,7 @@ main(int argc, char **argv) { 0, "trace", "Enable trace subsystems", OPT_STR, &opt_log_trace }, #endif { 0, "fileline", "Add file and line numbers to debug", OPT_BOOL, &opt_fileline }, + { 0, "threadid", "Add the thread ID to debug", OPT_BOOL, &opt_threadid }, { 0, "uidebug", "Enable webUI debug (non-minified JS)", OPT_BOOL, &opt_uidebug }, { 'A', "abort", "Immediately abort", OPT_BOOL, &opt_abort }, { 0, "noacl", "Disable all access control checks", @@ -588,6 +590,8 @@ main(int argc, char **argv) } if (opt_fileline) log_options |= TVHLOG_OPT_FILELINE; + if (opt_threadid) + log_options |= TVHLOG_OPT_THREAD; if (opt_log_trace) { log_level = LOG_TRACE; log_trace = opt_log_trace; diff --git a/src/tvhlog.c b/src/tvhlog.c index 5529c31c..c0083933 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -192,7 +192,12 @@ void tvhlogv ( const char *file, int line, } /* Basic message */ - l = snprintf(buf, sizeof(buf), "%s: ", subsys); + l = 0; + if (options & TVHLOG_OPT_THREAD) { + pthread_t tid = pthread_self(); + l += snprintf(buf + l, sizeof(buf) - l, "tid %ld: ", tid); + } + l += snprintf(buf + l, sizeof(buf) - l, "%s: ", subsys); if (options & TVHLOG_OPT_FILELINE && severity >= LOG_DEBUG) l += snprintf(buf + l, sizeof(buf) - l, "(%s:%d) ", file, line); if (args) diff --git a/src/tvhlog.h b/src/tvhlog.h index 761d2c24..5034c1d9 100644 --- a/src/tvhlog.h +++ b/src/tvhlog.h @@ -60,6 +60,7 @@ void _tvhlog_hexdump ( const char *file, int line, #define TVHLOG_OPT_MILLIS 0x0100 #define TVHLOG_OPT_DECORATE 0x0200 #define TVHLOG_OPT_FILELINE 0x0400 +#define TVHLOG_OPT_THREAD 0x0800 #define TVHLOG_OPT_ALL 0xFFFF /* Levels */