How to selectively reenable qDebug ?
-
I can disable ALL qDebug messages in .pro file
DEFINES += QT_NO_DEBUG_OUTPUT
I understand qDebug is a actually macro including
#ifdef ..#endif directiveI like to be able to reenable qDebug or derivation of it AFTER all other qDebug code is disabled using the above "DEFINES"
I like to have debug prints for part of the code and just adding
#ifdef DEBUG_THIS
qDebug(....
#endifdoes not do the job if I have DEFINES += QT_NO_DEBUG_OUTPUT in .pro file.
Ideally - if it is possible - I like to wholesale add another flag to the qDebug macro , something to additionally enable it using preprocessor directive ::
#ifdef ENABLE_ ALL_ DEBUG
qDebug...#endif
I think I know how to search and replace
qDebug()with
#ifdef ENABLE_ ALL_ DEBUG
qDebug...but not sure how to add
#endifAny other less chancy ( search and replace ) solution ?
-
Seems like you are just looking for QLoggingCategory
-
Thanks, perfect solution, albeit confusing implementation.
I just need selective debug , do not need "warnings " etc.I did this test :
#define DISABLE_QDEBUG #define CATEGORY_MAIN //#undef DISABLE_DEBUG MainWindow::MainWindow() : mdiArea(new QMdiArea) { // test QLoggingCategory QLoggingCategory category("driver.usb"); qCDebug(category) << "Q_FUNC_INFO" <<Q_FUNC_INFO << "TEST debug message"; QLoggingCategory category_main("MainWindow debug "); #ifdef CATEGORY_MAIN qCDebug(category_main) << "Q_FUNC_INFO" <<Q_FUNC_INFO << "TEST debug message"; #endif qDebug() << Q_FUNC_INFO;
I did read the doc and it really does not explain ( to me and clearly ) the syntax.
So I copied their "sample" and it surprisingly worked
without adding #include.
Then I couldn't find how to disable it so I added "normal "
preprocessor directive #ifdef .. #endifLooks silly, but works.
PS
The test will probably only work only in MainWindow - where it is defined - as local function.Thanks
-
@AnneRanch said in How to selectively reenable qDebug ?:
albeit confusing implementation.
There is a video of Kai explaining how it works: https://www.youtube.com/watch?v=GDtrbIs6JA0&t=1860s
-
Nice , but
DEFINES += QT_NO_DEBUG_OUTPUT
disables QLoggingCategory same as it disables qDebug macro.
I need to visit the QDebug and find out "who is on first" .
As of now it looks these interact and are controlled by
QT_NO_DEBUG_OUTPUT
-
Based on QLoggingCategory documentation I guess your #defines should impact setFilterRules() configuration what should be output. Then there should be no reason to #ifdef around qCDebug.
And you should remove QT_NO_DEBUG_OUTPUT completely if you need the debugging. Otherwise as you already seen you will not get any output even if set up correctly. -
@AnneRanch said in How to selectively reenable qDebug ?:
DEFINES += QT_NO_DEBUG_OUTPUT
disables QLoggingCategory same as it disables qDebug macro.As written in documentation (https://doc.qt.io/qt-5/qloggingcategory.html#configuring-categories), you can filter the debug output with
QLoggingCategory::setFilterRules()
or environment variableQT_LOGGING_RULES
.For example to disable all debug expect the category "mytest":
QLoggingCategory::setFilterRules("*.debug=false\nmytest.debug=true");
each rule has to be separated with carriage return (\n
)- or
QT_LOGGING_RULES="*.debug=false;mytest.debug=true"
, each rule has to be separated with semi-colon.
-
Thank for the replies.
My best guess is QDebug is sort of generic class and it works just fine as is.
QLoggingCategory is NOT directly related to QDebug and the "category" gives option to direct debugging into specific "area" . Somewhat confusing to use both "category" and "area" terms.
I need to read more on QLoggingCategory but it definitely will do what I want.
And yes, there is no need to use preprocessor directive and QT_NO_DEBUG_OUTPUT will enable / disable both QDebug and QLoggingCategory . ( After reading few discussions on how QT_NO_DEBUG_OUTPUT works - most users do not see it is "make / qmake " option and NOT preprocessor directive )
I think I 'll change my qDebug to "qDebug" category .
Then I can add my new "chat debug" as category.
Little scary - I have not counted my "qDebug" messages.... -
@AnneRanch said in How to selectively reenable qDebug ?:
I think I 'll change my qDebug to "qDebug" category .
Then I can add my new "chat debug" as category.
Little scary - I have not counted my "qDebug" messages....To be sure to not omit one, you should use
qCDebug()
instead ofqDebug()
.
This will force you to specify a category. -
@KroMignon Yes, QLoggingCategory has already "default" category and
replacing qDebug with qCDebug is putting all qDebug into a category. Nice.