From d3980f7cefe53b069ae598678095db6ad0dbe328 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sun, 28 Apr 2024 15:20:28 +0300 Subject: [PATCH] journal: prepend code location to messages at debug log levels Debug and trace log messages are often written based on the stderr logging, where code location is always visible. journalctl does not show the code location without extra tricks, which makes user-submitted debug logs from journal more cryptic. Make journal log more similar to stderr logs by prepending the code location and log level in the log message when the log topic level is >= DEBUG. --- spa/plugins/support/journal.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/spa/plugins/support/journal.c b/spa/plugins/support/journal.c index 26f2e8dbd..dc4a85f90 100644 --- a/spa/plugins/support/journal.c +++ b/spa/plugins/support/journal.c @@ -47,6 +47,7 @@ impl_log_logtv(void *object, const char *fmt, va_list args) { + static const char * const levels[] = { "-", "E", "W", "I", "D", "T", "*T*" }; struct impl *impl = object; char line_buffer[32]; char file_buffer[strlen("CODE_FILE=") + strlen(file) + 1]; @@ -81,9 +82,26 @@ impl_log_logtv(void *object, break; } - if (topic) + if (spa_log_level_topic_enabled(&impl->log, topic, SPA_LOG_LEVEL_DEBUG)) { + const char *lev = levels[SPA_CLAMP(level, 0u, SPA_N_ELEMENTS(levels) - 1u)]; + const char *tp = topic ? topic->topic : ""; + + if (file && func) { + const char *f = strrchr(file, '/'); + + f = f ? f+1 : file; + sz = spa_scnprintf(message_buffer, sizeof(message_buffer), + "%s %s%s[%s:%d:%s]: ", + lev, tp, topic ? " " : "", + f, line, func); + } else { + sz = spa_scnprintf(message_buffer, sizeof(message_buffer), + "%s %s%s", lev, tp, topic ? ": " : ""); + } + } else if (topic) { sz = spa_scnprintf(message_buffer, sizeof(message_buffer), - "%s: ", topic->topic); + "%s: ", topic->topic); + } /* we'll be using the low-level journal API, which expects us to provide * the location explicitly. line and file are to be passed as preformatted