From 76cc4203e321f9ee44a4373bcd92fcabb13e331c Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Tue, 12 Nov 2019 20:43:21 +0100 Subject: [PATCH] Logging: Log time + file + line + message and not just the message. Write Qt-Logging (qDebug(), qWarning(), ...) to file and the console. --- src/Lichtsteuerung.pro | 3 +++ src/main.cpp | 32 ++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Lichtsteuerung.pro b/src/Lichtsteuerung.pro index d038597c..6e411d4a 100644 --- a/src/Lichtsteuerung.pro +++ b/src/Lichtsteuerung.pro @@ -10,6 +10,9 @@ RESOURCES += qml.qrc DEFINES += CONVERT_FROM_SPOTIFY_OBJECTS +# we always want to have context information in log messages. +DEFINES += QT_MESSAGELOGCONTEXT + # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = diff --git a/src/main.cpp b/src/main.cpp index c5b89328..149e96b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,6 +76,16 @@ extern "C" void signal_handler(int /*sig*/) { } #endif +QFile logOutput; +QtMessageHandler defaultMessageHandler = nullptr; + +void toFileMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { + logOutput.write(qFormatLogMessage(type, context, msg).replace(QLatin1String("file://"), "").toUtf8()); + logOutput.write("\n"); + logOutput.flush(); + defaultMessageHandler(type, context, msg); +} + int main(int argc, char *argv[]) { QSharedMemory mem(QStringLiteral("Lichteuerung Leander Schulten")); { // check if the app is alreandy running or should be restarted @@ -119,15 +129,21 @@ int main(int argc, char *argv[]) { std::signal(SIGABRT, signal_handler); std::signal(SIGFPE, signal_handler); #endif + // set logging pattern: https://doc.qt.io/qt-5/qtglobal.html#qSetMessagePattern + qSetMessagePattern(QStringLiteral("[%{time h:mm:ss.zzz}] %{type} %{if-category}%{category}: %{endif}file://%{file}:%{line} (%{function}): %{message}")); + // init DrMinGW and file logger base path + const auto basePath = QStandardPaths::writableLocation(QStandardPaths::QStandardPaths::AppDataLocation) + QStringLiteral("/Lichtsteuerung"); + if (!QDir().mkpath(basePath)) { + qWarning() << "Error creating dirs : " << basePath; + } else { + logOutput.setFileName(basePath + "/log_" + QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy HH.mm.ss")) + ".txt"); + if (logOutput.open(QFile::WriteOnly)) { + defaultMessageHandler = qInstallMessageHandler(toFileMessageHandler); + } + } #ifdef DrMinGW ExcHndlInit(); - auto path = QStandardPaths::writableLocation(QStandardPaths::QStandardPaths::AppDataLocation); - path += QLatin1String("/Lichtsteuerung"); - QDir dir(path); - if(!dir.mkpath(path)){ - qWarning() << "Error creating dirs : " << path; - } - path += QLatin1String("/crash_dump_"); + auto path = basePath + QStringLiteral("/crash_dump_"); path += QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy HH.mm.ss")); path += QLatin1String(".txt"); qDebug() << "The crash report file is : " << path; @@ -144,7 +160,7 @@ int main(int argc, char *argv[]) { }); #ifdef DrMinGW // send crash reports - auto files = dir.entryInfoList(QDir::Filter::Files); + auto files = QDir(basePath).entryInfoList(QDir::Filter::Files); for(auto & file : files){ if(file.fileName().startsWith(QLatin1String("crash_dump"))){ auto newFileName = file.absolutePath() + "/sended_" +file.fileName(); -- GitLab