Skip to content

Commit

Permalink
cleanup: Prefix multi-line log messages with the same timestamp.
Browse files Browse the repository at this point in the history
Right now, multi-line messages will start with a prefix but then have no
prefix for the following lines.
  • Loading branch information
iphydf committed Dec 4, 2024
1 parent 36a5390 commit fdff6e1
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/appmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,34 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt

// Time should be in UTC to save user privacy on log sharing
QTime time = QDateTime::currentDateTime().toUTC().time();
QString LogMsg =
QString logPrefix =
QStringLiteral("[%1 UTC] %2:%3 : ").arg(time.toString("HH:mm:ss.zzz")).arg(file).arg(ctxt.line);
switch (type) {
case QtDebugMsg:
LogMsg += "Debug";
logPrefix += "Debug";
break;
case QtInfoMsg:
LogMsg += "Info";
logPrefix += "Info";
break;
case QtWarningMsg:
LogMsg += "Warning";
logPrefix += "Warning";
break;
case QtCriticalMsg:
LogMsg += "Critical";
logPrefix += "Critical";
break;
case QtFatalMsg:
LogMsg += "Fatal";
logPrefix += "Fatal";
break;
default:
break;
}

LogMsg += ": " + canonicalLogMessage(msg) + "\n";
const QByteArray LogMsgBytes = LogMsg.toUtf8();
QString logMsg;
for (const auto& line : msg.split('\n')) {
logMsg += logPrefix + ": " + canonicalLogMessage(line) + "\n";
}

const QByteArray LogMsgBytes = logMsg.toUtf8();
fwrite(LogMsgBytes.constData(), 1, LogMsgBytes.size(), stderr);

#ifdef LOG_TO_FILE
Expand Down Expand Up @@ -278,8 +282,11 @@ int AppManager::run()
mainLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");
}

if (!mainLogFilePtr)
if (!mainLogFilePtr) {
qCritical() << "Couldn't open logfile" << logfile;
} else {
qDebug() << "Logging to" << logfile;
}

logFileFile.storeRelaxed(mainLogFilePtr); // atomically set the logFile
#endif
Expand Down

0 comments on commit fdff6e1

Please sign in to comment.