logger: Dedup code, fix client Debug Log loglevel
authorShane Synan <digitalcircuit36939@gmail.com>
Thu, 14 Feb 2019 13:58:46 +0000 (14:58 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 14 Feb 2019 21:16:15 +0000 (22:16 +0100)
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.

src/common/logger.cpp
src/common/logger.h
src/qtui/debuglogdlg.cpp

index d17e182..51f1366 100644 (file)
@@ -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;
+}
index 7b88078..c10b0e4 100644 (file)
@@ -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;
     };
 
     /**
index 996f1ac..e535640 100644 (file)
@@ -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)