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.
QByteArray msgWithTime(const Logger::LogEntry& msg)
{
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();
+
+
+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;
+}
QDateTime timeStamp;
LogLevel logLevel;
QString message;
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;
QString DebugLogDlg::toString(const Logger::LogEntry& msg)
{
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)
}
void DebugLogDlg::logUpdated(const Logger::LogEntry& msg)