logger: Dedup code, fix client Debug Log loglevel
authorShane Synan <digitalcircuit36939@gmail.com>
Thu, 14 Feb 2019 00:31:50 +0000 (19:31 -0500)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 14 Feb 2019 19:24:48 +0000 (20:24 +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 8092072..72cfa50 100644 (file)
@@ -36,27 +36,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();
 };
 
 }
@@ -266,3 +246,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 af8fa3a..1f11e46 100644 (file)
@@ -51,6 +51,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 130ad7d..73391ce 100644 (file)
@@ -43,7 +43,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";
 }