2

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!

Qi W.
  • 706
  • 9
  • 21
  • Just to confirm: you're building in Debug mode, right? – jonspaceharper May 02 '16 at 10:36
  • I think so. I use `install(TARGETS my_exe DESTINATION ${install_dir} CONFIGURATIONS Debug)` in CMakeLists.txt and `make install` to install my executable file. – Qi W. May 02 '16 at 12:26

3 Answers3

2

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

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

I hope this could help you.

Jiahao
  • 76
  • 1
  • 7
  • He's using CMake. This advice is still sound, but notice that there's a good reason for stripping out source information in release builds (they leak information about your application) – peppe Jul 28 '16 at 08:48
  • Thanks for your reminder, It is CPPFLAGS in qmake. So add set(CMAKE_CXX_FLAGS "-DQT_MESSAGELOGCONTEXT") in CMake – Jiahao Jul 28 '16 at 10:03
2

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.

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
1

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
ephemerr
  • 1,833
  • 19
  • 22