How to decide which QtMsgType message type to print

1.2k views Asked by At

Is there any way to tell the application to print only qDebug or only qFatal messages?

1

There are 1 answers

0
Greenflow On

Yes, there is. You can install your own message handler with qInstallMessageHandler. This looks something like that (from the Qt docs):

    void myMessageOutput(QtMsgType type, const QMessageLogContext &context, 
const QString &msg)
    {
        QByteArray localMsg = msg.toLocal8Bit();
        switch (type) {
        case QtDebugMsg:
            fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), 
            context.file, context.line, context.function);
            break;
        case QtWarningMsg:
            fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(),
            context.file, context.line, context.function);
            break;
        case QtCriticalMsg:
            fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), 
            context.file, context.line, context.function);
            break;
        case QtFatalMsg:
            fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), 
            context.file, context.line, context.function);
            abort();
        }
    }

    int main(int argc, char **argv)
    {
        qInstallMessageHandler(myMessageOutput);
        QApplication app(argc, argv);
        ...
        return app.exec();
    }

Gives you 100% control of how qDebug and the other q** output functions work.