Disable (debug) logging in QT and QML
Published: 27-02-2021 | Author: Remy van Elst | Text only version of this article
In QT you can use a few functions from the
qDebug.h header like
qWarning() to log information to the console. In QML you can use the likes of
console.error(). It's also very easy to implement your own logger (eg.
SyslogMessageHandler) if you want something different, like logging to syslog and the console.
In this post I'll show you how to disable both forms of logging in a release build, qml and qt have different ways to manage their output.
Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.
You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.
Information online mostly suggested to define
QT_NO_DEBUG_OUTPUT, but that failed to disable
QML logging. Not sure if that's because my sample project had it's own
MessageHandler, but in
the end I figured out how to disable everything.
The QT documentation pages on debugging and qml logging are helpful, but as most often with their documentation, it's so comprehensive that it is not easy to find how to do just one specific thing, in this case, just disabling logging in a release build.
Disable QT logging
.pro file add the following lines:
# Suppress logging output for release build. CONFIG(release, debug|release): DEFINES += QT_NO_DEBUG_OUTPUT CONFIG(release, debug|release): DEFINES += QT_NO_INFO_OUTPUT CONFIG(release, debug|release): DEFINES += QT_NO_WARNING_OUTPUT
Each flag disables the appropriate
qLog() method, for example,
After editing your
.pro file, remember to clean and rebuild your project.
Why not just
CONFIG(release):? If you have multiple options, this only triggers if the last option is
CONFIG(release, debug|release) evaluates to
release but not
debug, or if it contains both
debug doesn't appear after the last occurrence of
release. For example:
CONFIG += release debug release
Because the last
debug comes after the last
CONFIG(release, debug|release) is
Disable QML logging
main.cpp include the
Before your other logging handlers (like
qInstallMessageHandler()), add this piece of code:
#ifdef QT_NO_DEBUG QLoggingCategory::setFilterRules("*.debug=false\n" "*.info=false\n" "*.warning=false\n" "*.critical=true"); fprintf(stderr, "Disabling QML logging in release build.\n"); #else fprintf(stderr, "QML logging enabled.\n"); #endif
QT_NO_DEBUG is exported automatically by QT when compiling a release build. Filter rules
allow for more control over what is logged when and where, but this code just disables
everything except critical. It must be newline separated (
\n in the method parameters),
comma's do not work.
fprintf line is optional but does help to know what's going on and when there
is logging and when logging is disabled.
I tried using an
#if defined(QT_NO_DEBUG_OUTPUT) check for more granular control,
that failed to work however. Every compile (debug or release) disabled the logging, not
sure why and didn't dig in too much either.
The other way is with an environment variable. Here they suggest the following flag:
This failed to work for me, both exported on the command line or in
main.cpp as below:
#ifdef QT_NO_DEBUG qputenv("QT_LOGGING_RULES", "qml=false"); #endif
Your mileage may vary. If it does work for you, please let me know.Tags: articles , c++ , cpp , debugging , development , gui , logging , qml , qt