QtMessageHandler context contains no information
-
We have a QtMessageHandler registered with
qInstallMessageHandlerand when messages are logged thecontext.fileandcontext.functiondo not seem to be defined.void handle_message(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); const char *file = context.file ? context.file : "???"; const char *func = context.function ? context.function : "???"; switch (type) { case QtDebugMsg: fprintf(stderr, "[DEBUG %s:%u#%s] %s\n", file, context.line, func, localMsg.constData()); break; } // ... }This always produces
???in the place of the function and file name when called withqDebug() << "Message";Would anyone know why this might be the case? Thanks in advance!
-
We have a QtMessageHandler registered with
qInstallMessageHandlerand when messages are logged thecontext.fileandcontext.functiondo not seem to be defined.void handle_message(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); const char *file = context.file ? context.file : "???"; const char *func = context.function ? context.function : "???"; switch (type) { case QtDebugMsg: fprintf(stderr, "[DEBUG %s:%u#%s] %s\n", file, context.line, func, localMsg.constData()); break; } // ... }This always produces
???in the place of the function and file name when called withqDebug() << "Message";Would anyone know why this might be the case? Thanks in advance!
@tmag
I would guessqDebug()does not say anywhere it will actually pass a file/function name inQMessageLogContextfrom wherever you call it, does it? (Unlike, say,Q_ASSERT()which I know prints them, whether it goes viaqInstallMessageHandler.) You would have to pass in from caller__FILE__,__LINE__,__WHATEVER_FOR_FILENAMEsomehow to achieve this, probably not viaqDebug()?This is only a guess. But put a breakpoint on
handle_message(), look back up the stack view to whereqDebug()is calling it, and see what parameters are being passed/how theQMessageLogContext contextis set up. -
@tmag
I would guessqDebug()does not say anywhere it will actually pass a file/function name inQMessageLogContextfrom wherever you call it, does it? (Unlike, say,Q_ASSERT()which I know prints them, whether it goes viaqInstallMessageHandler.) You would have to pass in from caller__FILE__,__LINE__,__WHATEVER_FOR_FILENAMEsomehow to achieve this, probably not viaqDebug()?This is only a guess. But put a breakpoint on
handle_message(), look back up the stack view to whereqDebug()is calling it, and see what parameters are being passed/how theQMessageLogContext contextis set up. -
-
@tmag said in QtMessageHandler context contains no information:
Would anyone know why this might be the case? Thanks in advance!
Your project most likely was compiled with either QT_NO_MESSAGELOGCONTEXT or QT_NO_DEBUG defines. If the latter, you can force message log contexts by defining QT_MESSAGELOGCONTEXT.
-
@mpergand
Err, OK, thanks!
The OP should still put a breakpoint on his handler and have a look up the stack/look in thecontextvariable. -
@tmag said in QtMessageHandler context contains no information:
Would anyone know why this might be the case? Thanks in advance!
Your project most likely was compiled with either QT_NO_MESSAGELOGCONTEXT or QT_NO_DEBUG defines. If the latter, you can force message log contexts by defining QT_MESSAGELOGCONTEXT.