feat(logging): log qt messages into logger, rather than into terminal

This commit is contained in:
maelstrom 2025-04-17 15:22:00 +02:00
parent 5564fddc21
commit 66d3f80073

View file

@ -1,11 +1,15 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "common.h"
#include "logger.h"
#include "objects/snap.h"
#include <map>
#include <memory>
#include <qclipboard.h>
#include <qglobal.h>
#include <qmessagebox.h>
#include <qmimedata.h>
#include <qstylefactory.h>
#include <qstylehints.h>
#ifdef _NDEBUG
@ -28,6 +32,22 @@ inline bool isDarkMode() {
#endif // QT_VERSION
}
QtMessageHandler defaultMessageHandler = nullptr;
std::map<QtMsgType, Logger::LogLevel> QT_MESSAGE_TYPE_TO_LOG_LEVEL = {
{ QtMsgType::QtInfoMsg, Logger::LogLevel::INFO },
{ QtMsgType::QtSystemMsg, Logger::LogLevel::INFO },
{ QtMsgType::QtDebugMsg, Logger::LogLevel::DEBUG },
{ QtMsgType::QtWarningMsg, Logger::LogLevel::WARNING },
{ QtMsgType::QtCriticalMsg, Logger::LogLevel::ERROR },
{ QtMsgType::QtFatalMsg, Logger::LogLevel::FATAL_ERROR },
};
void logQtMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
Logger::log("[Qt] " + msg.toStdString(), QT_MESSAGE_TYPE_TO_LOG_LEVEL[type]);
// if (defaultMessageHandler) defaultMessageHandler(type, context, msg);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@ -46,6 +66,8 @@ MainWindow::MainWindow(QWidget *parent)
else
QIcon::setFallbackThemeName("editor");
// qApp->setStyle(QStyleFactory::create("fusion"));
defaultMessageHandler = qInstallMessageHandler(logQtMessage);
setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);