how to use qInstallMessageHandler to ignore specific debug messages?
-
How i could allow it to only print
qDebug()
messages?
In my attempt below its not printingqDebug()
.void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { switch (type) { case QtDebugMsg: qDebug() << message; // <- doesnt work break; // Stop these msg from being printed to debug. case QtWarningMsg: case QtCriticalMsg: case QtFatalMsg: case QtInfoMsg: default: break; } }
-
How i could allow it to only print
qDebug()
messages?
In my attempt below its not printingqDebug()
.void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { switch (type) { case QtDebugMsg: qDebug() << message; // <- doesnt work break; // Stop these msg from being printed to debug. case QtWarningMsg: case QtCriticalMsg: case QtFatalMsg: case QtInfoMsg: default: break; } }
@Ylvy You can't use qDebug inside the handler. That's recursive.
qInstallMessageHandler
returns a previous handler (or the default one if you didn't set any yet). If you want to just filter out everything but debug you can store the old handler and call it from yours, e.g.// put it somewhere accessible to your handler QtMessageHandler defaultHandler {}; // install new and get the old one defaultHandler = qInstallMessageHandler(myMessageHandler);
and in your handler:
void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { if (type == QtDebugMsg) defaultHandler(type, context, message); }
-
How i could allow it to only print
qDebug()
messages?
In my attempt below its not printingqDebug()
.void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { switch (type) { case QtDebugMsg: qDebug() << message; // <- doesnt work break; // Stop these msg from being printed to debug. case QtWarningMsg: case QtCriticalMsg: case QtFatalMsg: case QtInfoMsg: default: break; } }
@Ylvy
If this is installed as the message handler, it gets called byqDebug()
. But you issue aqDebug()
in this case from inside it. Sounds dangerously like infinite recursion to me?!Try something like
printf()
orfprintf(stderr
orcerr <<
for theqDebug()
you have, does that get fired OK?What you probably ought do here is call the message handler you replaced by this in the
case QtDebugMsg
route. -
@Ylvy
If this is installed as the message handler, it gets called byqDebug()
. But you issue aqDebug()
in this case from inside it. Sounds dangerously like infinite recursion to me?!Try something like
printf()
orfprintf(stderr
orcerr <<
for theqDebug()
you have, does that get fired OK?What you probably ought do here is call the message handler you replaced by this in the
case QtDebugMsg
route. -
How i could allow it to only print
qDebug()
messages?
In my attempt below its not printingqDebug()
.void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { switch (type) { case QtDebugMsg: qDebug() << message; // <- doesnt work break; // Stop these msg from being printed to debug. case QtWarningMsg: case QtCriticalMsg: case QtFatalMsg: case QtInfoMsg: default: break; } }
@Ylvy You can't use qDebug inside the handler. That's recursive.
qInstallMessageHandler
returns a previous handler (or the default one if you didn't set any yet). If you want to just filter out everything but debug you can store the old handler and call it from yours, e.g.// put it somewhere accessible to your handler QtMessageHandler defaultHandler {}; // install new and get the old one defaultHandler = qInstallMessageHandler(myMessageHandler);
and in your handler:
void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { if (type == QtDebugMsg) defaultHandler(type, context, message); }
-