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.
This commit is contained in:
Pauli Virtanen 2024-04-28 15:20:28 +03:00
parent 3ae2ad59d2
commit ac35ecf329

View file

@ -45,6 +45,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];
@ -80,9 +81,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