How to enable and disable qDebug() messages inside a class
-
@QtTester
Now you are getting demanding!The effect of
#if !defined(QT_NO_DEBUG_OUTPUT)
is acted on inqloggingcategory.h
(https://code.woboq.org/qt5/qtbase/src/corelib/io/qloggingcategory.h.html#121).Since that, like all Qt header files, is inside a
#ifndef QLOGGINGCATEGORY_H
guard, that file is only read/included the first time that file is included into any particular source file. So switchingQT_NO_DEBUG_OUTPUT
on & off within one file won't have the effect you seem to want.You could presumably achieve the same effect to "scope" the enablement/disablement with something based on:
// Next line at *beginning* of your header file #undef qCDebug # define qCDebug(category, ...) QT_NO_QDEBUG_MACRO() ... // Next line at *end* of your header file #undef qCDebug # define qCDebug(category, ...) \ for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \ QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
but it's getting messy, and relies on knowing what the definition of
qCDebug
is in Qt, which could change.Better would be to write something other than
qDebug()
for whichever things you want to enable/disable, or use a dedicated category you define for those lines.
21/21