From 0ebfd0866d7cc9e3dabccf088d8ef1339dfe51a6 Mon Sep 17 00:00:00 2001 From: Alejandro Exojo Date: Mon, 10 Mar 2014 11:40:26 +0100 Subject: [PATCH] Use category names when logging to the journal Use the custom field QT_CATEGORY to store the name of the QLoggingCategory used when writing to systemd's journal. To pass custom fields sd_journal_send() is needed, and is used in combination with #define SD_JOURNAL_SUPPRESS_LOCATION to store the metadata that is already in the QMessageLogContext. Change-Id: I6a120701f7012aaa46451dd3d91586a419c5f803 Reviewed-by: Kai Koehne Reviewed-by: Thiago Macieira Reviewed-by: Robin Burchell --- config.tests/unix/journald/journald.c | 8 +++++++- src/corelib/global/qlogging.cpp | 16 +++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/config.tests/unix/journald/journald.c b/config.tests/unix/journald/journald.c index 470d526e680..32f0de683b5 100644 --- a/config.tests/unix/journald/journald.c +++ b/config.tests/unix/journald/journald.c @@ -40,9 +40,15 @@ ****************************************************************************/ #include +#include int main(int argc, char **argv) { - sd_journal_print_with_location(LOG_INFO, "CODE_FILE=foo.c", "CODE_LINE=0", "unknown_function", "test message"); + sd_journal_send("MESSAGE=%s", "test message", + "PRIORITY=%i", LOG_INFO, + "CODE_FUNC=%s", "unknown", + "CODE_LINE=%d", 0, + "CODE_FILE=%s", "foo.c", + NULL); return 0; } diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 8c1d8b867db..da26490d186 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -64,7 +64,9 @@ #endif #if defined(QT_USE_JOURNALD) && !defined(QT_BOOTSTRAPPED) +# define SD_JOURNAL_SUPPRESS_LOCATION # include +# include # include #endif @@ -1170,13 +1172,13 @@ static void systemd_default_message_handler(QtMsgType type, break; } - char filebuf[PATH_MAX + sizeof("CODE_FILE=")]; - snprintf(filebuf, sizeof(filebuf), "CODE_FILE=%s", context.file ? context.file : "unknown"); - - char linebuf[20]; - snprintf(linebuf, sizeof(linebuf), "CODE_LINE=%d", context.line); - - sd_journal_print_with_location(priority, filebuf, linebuf, context.function ? context.function : "unknown", "%s", message.toUtf8().constData()); + sd_journal_send("MESSAGE=%s", message.toUtf8().constData(), + "PRIORITY=%i", priority, + "CODE_FUNC=%s", context.function ? context.function : "unknown", + "CODE_LINE=%d", context.line, + "CODE_FILE=%s", context.file ? context.file : "unknown", + "QT_CATEGORY=%s", context.category ? context.category : "unknown", + NULL); } #endif