Better debug output using QT_MESSAGE_PATTERN

If you are using Qt, you might have some qDebug or qWarning statements in your code. But did you know that you can greatly improve the output of those with the QT_MESSAGE_PATTERN environment variable? This blog post will give you some hints and examples of what you can do.

The default message pattern just prints the message (and the category if one was specified), but qDebug has the possibility to output more information. You can display cool things like the line of code, the function name or more by using some placeholders in the pattern.


Following placeholders are supported:

Placeholder Description
%{appname} QCoreApplication::applicationName()
%{category} Logging category
%{file} Path to source file
%{function} Function
%{line} Line in source file
%{message} The actual message
%{pid} QCoreApplication::applicationPid()
%{threadid} The system-wide ID of current thread (if it can be obtained)
%{qthreadptr} A pointer to the current QThread (result of QThread::currentThread())
%{type} „debug“, „warning“, „critical“ or „fatal“
%{time process} time of the message, in seconds since the process started (the token „process“ is literal)
%{time boot} the time of the message, in seconds since the system boot if that can be determined (the token „boot“ is literal). If the time since boot could not be obtained, the output is indeterminate (see QElapsedTimer::msecsSinceReference()).
%{time [format]} system time when the message occurred, formatted by passing the format to QDateTime::toString(). If the format is not specified, the format of Qt::ISODate is used.
%{backtrace [depth=N] [separator="..."]} A backtrace with the number of frames specified by the optional depth parameter (defaults to 5), and separated by the optional separator parameter (defaults to „|“). This expansion is available only on some platforms (currently only platfoms using glibc). Names are only known for exported functions. If you want to see the name of every function in your application, use QMAKE_LFLAGS += -rdynamic. When reading backtraces, take into account that frames might be missing due to inlining or tail call optimization.

You can also use conditionals on the type of the message using %{if-debug}, %{if-info} %{if-warning}, %{if-critical} or %{if-fatal} followed by an %{endif}. What is inside the %{if-*} and %{endif} will only be printed if the type matches.

Add message pattern in environment settings.
Example pattern:
– „[%{time h:mm:ss.zzz} (%{type})] %{appname} (%{file}:%{line}) – %{message}“
Now the output look like this:
Some other examples:
„[%{type}] %{appname} (%{file}:%{line}) – %{message}“
– „[%{time yyyyMMdd h:mm:ss.zzz t} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} – %{message}“
For more information look documentation

4 Gedanken zu „Better debug output using QT_MESSAGE_PATTERN“

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden /  Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )


Verbinde mit %s