13

After searching around for a reason that qDebug() statements work fine with Qt's standard message handler but fail when I switch to my own, I'm appealing here to see if anyone else has any experience with the problem.

Things I know about / have tried, that do nothing...

1) CONFIG += console

2) DEFINES -= QT_NO_WARNING_OUTPUT QT_NO_DEBUG_OUTPUT

3) ::fprintf(stderr, "ERROR\n"); ::fflush(stderr);

4) ::fprintf(stdout, "OUTPUT\n"); ::fflush(stdout);

5) std::cerr << "CERROR" << std::endl; std::cerr.flush();

However it works correctly when using the built in handler (ie it prints the message to the QtCreator console)

int main(int argc, char *argv[]) {
    // Use my handler
    qInstallMessageHandler(MyCustomLogger);
    qDebug() << "Not Printed";

    // Use standard handler
    qInstallMessageHandler(0);
    qDebug() << "Correctly Printed";

    // Use my handler again
    qInstallMessageHandler(MyCustomLogger);
    qDebug() << "Not Printed Again...";
}

The most recent test was allocating myself a console using WinAPI commands this results in the correct behavior all output to stderr and stdout are visible on the console I created. However, this is not the behavior I want, I want to be able to view this output in QtCreator.

Any thoughts on how the standard message handler prints to the debugger? I've not managed to find it in the Qt sources yet.

2
  • 5
    On windows, qDebug() uses the debug channel, not stderr. Feb 1, 2013 at 9:52
  • I must admit I've never heard of the debug channel before, off to google.
    – gpdaniels
    Feb 1, 2013 at 10:09

2 Answers 2

15

As Frank Osterfeld mentioned in his comment:

On windows, qDebug() uses the debug channel, not stderr.

After delving into the QDebug code and QMessageLogger I've found my answer. The handy WinAPI function OutputDebugString.

Usage (Modified from peppe's):

#include <QApplication>
#include <QtDebug>
#include <QtGlobal>

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

void MyMessageOutput(QtMsgType Type, const QMessageLogContext& Context, const QString &Message)
{
    OutputDebugString(reinterpret_cast<const wchar_t *>(Message.utf16()));
}

int main(int argc, char **argv)
{
    // A GUI application
    QApplication app(argc, argv);

    // Custom handler
    qInstallMessageHandler(myMessageOutput);
    qDebug() << "Printed in the console using my message handler in a windows GUI application";

    // Default handler
    qInstallMessageHandler(0);
    qDebug() << "Also printed in the console!";

    // Show GUI here
    //MainForm *MF = new MainForm();
    //MF->show();

    return app.exec();
}
1
  • 1
    Interestingly, this is also the method to make Creator print the messages from the custom handler while debugging (otherwise the handler is entered, but nothing ends up in the Creator console).
    – mlvljr
    Dec 4, 2014 at 13:18
2

I can't reproduce your issue: this works correctly for me.

#include <QCoreApplication>
#include <QtDebug>
#include <QtGlobal>

#include <stdio.h>
#include <stdlib.h>

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    fprintf(stderr, "MESSAGE (%s:%u %s): %s\n", context.file, context.line, context.function, localMsg.constData());
    fflush(stderr);
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    qInstallMessageHandler(myMessageOutput);
    qDebug() << "Printed in the console";
    qInstallMessageHandler(0);
    qDebug() << "Also printed in the console";
    return app.exec();
}
3
  • That's very weird. Could it be to do with other lib's I am using? As when I run that handler in my code I still only get one line...
    – gpdaniels
    Feb 1, 2013 at 10:01
  • Ah maybe not, as I'm using a QApplication rather than a QCoreApplication so that I can have a gui it doesn't work in your example.
    – gpdaniels
    Feb 1, 2013 at 10:07
  • 1
    Problem solved, use OutputDebugString(...) I'll add my answer in 5 hours when the system lets me...
    – gpdaniels
    Feb 1, 2013 at 11:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.