diff --git a/src/main.cpp b/src/main.cpp --- a/src/main.cpp +++ b/src/main.cpp @@ -19,18 +19,52 @@ #include #include +#include #include "mainwindow.h" #include "tooltipfilter.h" #include "version.h" -MainWindow* pMainWindow; +MainWindow* pMainWindow = nullptr; void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { - // TODO: don't call MainWindow::messageHandler if window is destroyed - pMainWindow->messageHandler(type, context, msg); + QString logString; + + switch (type) + { +#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) + case QtInfoMsg: + logString = "[Info] " + msg; + break; +#endif + case QtDebugMsg: + logString = "[Debug] " + msg; + break; + case QtWarningMsg: + logString = "[Warning] " + msg; + break; + case QtCriticalMsg: + logString = "[Error] " + msg; + break; + case QtFatalMsg: + logString = "[Fatal] " + msg; + break; + } + + std::cerr << logString.toStdString() << std::endl; + + if (pMainWindow != nullptr) + { + // TODO: don't call MainWindow::messageHandler if window is destroyed + pMainWindow->messageHandler(type, logString, msg); + } + + if (type == QtFatalMsg) + { + __builtin_trap(); + } } int main(int argc, char *argv[]) @@ -38,11 +72,11 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); QApplication::setApplicationName(PROGRAM_NAME_STRING); QApplication::setApplicationVersion(VERSION_STRING); + + qInstallMessageHandler(messageHandler); MainWindow w; pMainWindow = &w; - qInstallMessageHandler(messageHandler); - ToolTipFilter ttf; a.installEventFilter(&ttf); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -478,44 +478,16 @@ PlotViewSettings MainWindow::viewSetting } void MainWindow::messageHandler(QtMsgType type, - const QMessageLogContext &context, + const QString &logString, const QString &msg) { - QString logString; - - switch (type) - { -#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) - case QtInfoMsg: - logString = "[Info] " + msg; - break; -#endif - case QtDebugMsg: - logString = "[Debug] " + msg; - break; - case QtWarningMsg: - logString = "[Warning] " + msg; - break; - case QtCriticalMsg: - logString = "[Error] " + msg; - break; - case QtFatalMsg: - logString = "[Fatal] " + msg; - break; - } - - if (ui != NULL) ui->ptLog->appendPlainText(logString); - std::cerr << logString.toStdString() << std::endl; + if (ui != NULL) + ui->ptLog->appendPlainText(logString); if (type != QtDebugMsg && ui != NULL) { ui->statusBar->showMessage(msg, 5000); } - - if (type == QtFatalMsg) - { - __builtin_trap(); - } } void MainWindow::saveAllSettings(QSettings* settings) diff --git a/src/mainwindow.h b/src/mainwindow.h --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -62,8 +62,7 @@ public: PlotViewSettings viewSettings() const; - void messageHandler(QtMsgType type, const QMessageLogContext &context, - const QString &msg); + void messageHandler(QtMsgType type, const QString &logString, const QString &msg); private: Ui::MainWindow *ui;