From afc3c519a97a08fc4f5398c2a752009888f9c7f1 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Mon, 5 Feb 2024 19:56:00 -0500 Subject: [PATCH] Add a message handler to grab debug / warnings on Windows Sends it to Documents / avogadro2.log Signed-off-by: Geoff Hutchison --- avogadro/avogadro.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/avogadro/avogadro.cpp b/avogadro/avogadro.cpp index 5b27889d..6ef1abd8 100644 --- a/avogadro/avogadro.cpp +++ b/avogadro/avogadro.cpp @@ -18,6 +18,10 @@ #include #include +// install a message handler (for Windows) +#include +#include + #include "application.h" #include "mainwindow.h" @@ -31,6 +35,65 @@ void removeMacSpecificMenuItems(); #define DEBUG false +// Based on https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler +void myMessageOutput(QtMsgType type, const QMessageLogContext& context, + const QString& msg) +{ + QByteArray localMsg = msg.toLocal8Bit(); + QString file = context.file; + QString function = context.function; + // get current date and time + QDateTime dateTime = QDateTime::currentDateTime(); + QString dateTimeString = dateTime.toString("yyyy-MM-dd hh:mm:ss"); + + QString message; + switch (type) { + case QtInfoMsg: + message = QString("Info: %1 (%2:%3, %4)\n") + .arg(localMsg.constData()) + .arg(file) + .arg(context.line) + .arg(function); + break; + case QtWarningMsg: + message = QString("Warning: %1 (%2:%3, %4)\n") + .arg(localMsg.constData()) + .arg(file) + .arg(context.line) + .arg(function); + break; + case QtCriticalMsg: + message = QString("Critical: %1 (%2:%3, %4)\n") + .arg(localMsg.constData()) + .arg(file) + .arg(context.line) + .arg(function); + break; + case QtFatalMsg: + message = QString("Fatal: %1 (%2:%3, %4)\n") + .arg(localMsg.constData()) + .arg(file) + .arg(context.line) + .arg(function); + break; + case QtDebugMsg: + default: + message = QString("Debug: %1 (%2:%3, %4)\n") + .arg(localMsg.constData()) + .arg(file) + .arg(context.line) + .arg(function); + break; + } + + QString writableDocs = + QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); + QFile outFile(writableDocs + "/avogadro2.log"); + outFile.open(QIODevice::WriteOnly | QIODevice::Append); + QTextStream ts(&outFile); + ts << message << endl; +} + int main(int argc, char* argv[]) { #ifdef Q_OS_MAC @@ -51,6 +114,9 @@ int main(int argc, char* argv[]) #ifdef Q_OS_WIN // We need to ensure desktop OpenGL is loaded for our rendering. QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL); + + // install the message handler (goes to Documents / avogadro2.log) + qInstallMessageHandler(myMessageOutput); #endif Avogadro::Application app(argc, argv);