From 5f771f4a6942248ebe0a5a690f19e509328265cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wolfgang=20M=C3=BCller?= Date: Wed, 13 Feb 2019 18:27:18 -0500 Subject: [PATCH] logger: Make output to syslog nicer Resolve the level string in msgWithTime. This means that the log level string is included in logs to files and stdout even if syslog output is enabled. Resolve the program name using RunMode and BuildInfo, distinguishing between client, core, and monolithic. [Backported from 0.14/git-master] --- src/common/logger.cpp | 63 +++++++++++++++++++++++++++---------------- src/common/logger.h | 1 + 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/common/logger.cpp b/src/common/logger.cpp index 81031d2b..80920720 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -36,7 +36,27 @@ namespace { QByteArray msgWithTime(const Logger::LogEntry &msg) { - return (msg.timeStamp.toString("yyyy-MM-dd hh:mm:ss ") + msg.message + "\n").toUtf8(); + QString levelString; + + switch (msg.logLevel) { + case Logger::LogLevel::Debug: + levelString = "[Debug] "; + break; + case Logger::LogLevel::Info: + levelString = "[Info ] "; + break; + case Logger::LogLevel::Warning: + levelString = "[Warn ] "; + break; + case Logger::LogLevel::Error: + levelString = "[Error] "; + break; + case Logger::LogLevel::Fatal: + levelString = "[FATAL] "; + break; + } + + return (msg.timeStamp.toString("yyyy-MM-dd hh:mm:ss ") + levelString + msg.message + "\n").toUtf8(); }; } @@ -114,6 +134,23 @@ bool Logger::setup(bool keepMessages) #ifdef HAVE_SYSLOG _syslogEnabled = Quassel::isOptionSet("syslog"); + + Quassel::RunMode mode = Quassel::runMode(); + Quassel::BuildInfo info = Quassel::buildInfo(); + QString prgname = info.applicationName; + + if (mode == Quassel::RunMode::ClientOnly) { + prgname = info.clientApplicationName; + } else if (mode == Quassel::RunMode::CoreOnly) { + prgname = info.coreApplicationName; + } + + _prgname = prgname.toLocal8Bit(); + + // set up options, program name, and facility for later calls to syslog(3) + if (_syslogEnabled) { + openlog(_prgname.constData(), LOG_PID, LOG_USER); + } #endif _initialized = true; @@ -166,28 +203,8 @@ void Logger::handleMessage(QtMsgType type, const QString &msg) void Logger::handleMessage(LogLevel level, const QString &msg) { - QString logString; - - switch (level) { - case LogLevel::Debug: - logString = "[Debug] "; - break; - case LogLevel::Info: - logString = "[Info ] "; - break; - case LogLevel::Warning: - logString = "[Warn ] "; - break; - case LogLevel::Error: - logString = "[Error] "; - break; - case LogLevel::Fatal: - logString = "[FATAL] "; - break; - } - // Use signal connection to make this method thread-safe - emit messageLogged({QDateTime::currentDateTime(), level, logString += msg}); + emit messageLogged({QDateTime::currentDateTime(), level, msg}); } @@ -229,7 +246,7 @@ void Logger::outputMessage(const LogEntry &message) case LogLevel::Fatal: prio = LOG_CRIT; } - syslog(prio|LOG_USER, "%s", qPrintable(message.message)); + syslog(prio, "%s", qPrintable(message.message)); } #endif diff --git a/src/common/logger.h b/src/common/logger.h index 57d55886..af8fa3af 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -111,6 +111,7 @@ private: std::vector _messages; bool _keepMessages{true}; bool _initialized{false}; + QByteArray _prgname; }; Q_DECLARE_METATYPE(Logger::LogEntry) -- 2.20.1