From: Shane Synan Date: Thu, 14 Feb 2019 13:58:46 +0000 (+0100) Subject: logger: Dedup code, fix client Debug Log loglevel X-Git-Tag: test-travis-01~65 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=1aa0c911c54698f5757f403ff0b6a1021386d348 logger: Dedup code, fix client Debug Log loglevel Deduplicate code by moving LogEntry formatting into a separate function, LogEntry::toString(), which applies timestamps and the log level string. Fix client Debug Log dialog to print log level using this new function. Modify msgWithTime() to use this new function, too. --- diff --git a/src/common/logger.cpp b/src/common/logger.cpp index d17e1822..51f1366a 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -37,27 +37,7 @@ namespace { QByteArray msgWithTime(const Logger::LogEntry& msg) { - 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(); + return (msg.toString() + "\n").toUtf8(); } } // namespace @@ -244,3 +224,29 @@ void Logger::outputMessage(const LogEntry& message) } #endif } + + +QString Logger::LogEntry::toString() const +{ + QString levelString; + + switch (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 timeStamp.toString("yyyy-MM-dd hh:mm:ss ") + levelString + message; +} diff --git a/src/common/logger.h b/src/common/logger.h index 7b88078f..c10b0e41 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -55,6 +55,13 @@ public: QDateTime timeStamp; LogLevel logLevel; QString message; + + /** + * Gets this log entry in a printable format, with timestamp and log level + * + * @return the log entry, formatted with timestamp and log level + */ + QString toString() const; }; /** diff --git a/src/qtui/debuglogdlg.cpp b/src/qtui/debuglogdlg.cpp index 996f1ac4..e5356403 100644 --- a/src/qtui/debuglogdlg.cpp +++ b/src/qtui/debuglogdlg.cpp @@ -41,7 +41,7 @@ DebugLogDlg::DebugLogDlg(QWidget* parent) QString DebugLogDlg::toString(const Logger::LogEntry& msg) { - return msg.timeStamp.toString("yyyy-MM-dd hh:mm:ss ") + msg.message + "\n"; + return msg.toString() + "\n"; } void DebugLogDlg::logUpdated(const Logger::LogEntry& msg)