Qt Development‎ > ‎

Application Log

You can redirect all the output produced by the methods qDebug(), qWarning(), qCritical(), qFatal() to a file very easily.

Create a static method like this:

// ...

void
logMessageHandler(QtMsgType type, const char *msg)
{
    QString txt;
    switch (type) {
        case QtDebugMsg:
            txt = QString("(DBG) %1").arg(msg);
            break;
        case QtWarningMsg:
            txt = QString("(WRN) %1").arg(msg);
            break;
        case QtCriticalMsg:
            txt = QString("(CRT) %1").arg(msg);
            break;
        case QtFatalMsg:
            txt = QString("(FAT) %1").arg(msg);
            break;
    }
    QFile outFile("path-to-your-log-file-somewhere");
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    ts << txt << endl;
    outFile.close();
    if ( type==QtFatalMsg ) abort();
}

// ...


and then make it active by using on the same cpp file. You can do that for example on the main.cpp file:

// ...

qInstallMsgHandler(logMessageHandler);

// ...



Comments