Qi W.
Qi W.

Reputation: 786

qSetMessagePattern("%{file}(%{line}): %{message}") output "unkown(0)"

I'm using qSetMessagePattern to format the output.

The weird thing is even I set %{file} and %{line}, I could only get unknown and 0 in my output. And so does the %{function}. The source and output are below:

qSetMessagePattern("%{file}(%{line}): %{message}");
qDebug() << "msg";

output:

unknown(0): msg

Any ideas? Thanks!

Upvotes: 3

Views: 2728

Answers (3)

ephemerr
ephemerr

Reputation: 1983

I used this hint https://stackoverflow.com/a/24012195/2656799 to bypass the problem. In qlogging.h (see there) you can see how debug macros defined:

#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
#define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal

So my solution is to redefine them using standard file, line context macros:

#define logDebug    QMessageLogger(__FILE__,__LINE__, QT_MESSAGELOG_FUNC).debug
#define logInfo     QMessageLogger(__FILE__,__LINE__, QT_MESSAGELOG_FUNC).info
#define logWarning  QMessageLogger(__FILE__,__LINE__, QT_MESSAGELOG_FUNC).warning
#define logCritical QMessageLogger(__FILE__,__LINE__, QT_MESSAGELOG_FUNC).critical
#define logFatal    QMessageLogger(__FILE__,__LINE__, QT_MESSAGELOG_FUNC).fatal

Upvotes: 1

Tomilov Anatoliy
Tomilov Anatoliy

Reputation: 16711

Try to add for the target you build:

target_compile_definitions(${PROJECT_NAME} PRIVATE -DQT_DEBUG -DQT_MESSAGELOGCONTEXT)

Seems -DQT_DEBUG has priority when used along with -DQT_NO_DEBUG.

Upvotes: 3

Jiahao
Jiahao

Reputation: 76

add DEFINES += QT_MESSAGELOGCONTEXT to your xxx.pro, and then, rebuild

make clean && make distclean && qmake && make 

I hope this could help you.

Upvotes: 2

Related Questions