Changeset - 1e3fe1bbdd01
[Not reviewed]
Mehmet Aslan - 7 years ago 2019-03-31 07:37:16
aaslan-mehmet@hotmail.com
Becasue commandline options handled in constructor, error logs dont have access to message handler. Solved.
Additionaly when MainWindow destroyed, logs messages still logged properly.
3 files changed with 43 insertions and 38 deletions:
0 comments (0 inline, 0 general)
src/main.cpp
Show inline comments
 
@@ -19,18 +19,52 @@
 

	
 
#include <QApplication>
 
#include <QtGlobal>
 
#include <iostream>
 

	
 
#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);
 

	
src/mainwindow.cpp
Show inline comments
 
@@ -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)
src/mainwindow.h
Show inline comments
 
@@ -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;
0 comments (0 inline, 0 general)