Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to enable and disable qDebug() messages inside a class
-
Hi,
In my Qt5 project i have multiple classes, inside each class they are a fewqDebug() << message;
is it possible to enable/disable this qDebug() lines in a particular classe ?
exemple of what i want to do :
class A: public QObject { Q_OBJECT public: A(); ~A(){} Q_INVOKABLE void connecToServer(){ conn->start(); if(dbg){ qDebug()<<"started !" } } private: bool dbg = true; };
Is there a way to do this without de 'if' statement ?
Thx in advance
LA
-
@jsulm Perfect ! Thx :)
-
@LeLev See https://doc.qt.io/qt-5.10/debug.html
"qDebug(), qInfo(), and qWarning() are debugging tools. They can be compiled away by defining QT_NO_DEBUG_OUTPUT, QT_NO_INFO_OUTPUT, or QT_NO_WARNING_OUTPUT during compilation."
-
@jsulm Thx, but as i said I want do disable qDebug() for a particular class.
with QT_NO_DEBUG_OUTPUT i will disable for the whole application no ?
-
@LeLev I don't understand the use case. How do you decide whether to activate debug output for a specific class and when do you decide (compile time? run time?)?
-
@jsulm sorry..
My probleme is : i have lot of messages, so i can't focus on the message i'm interested.For the moment i'm testing my application, i just want to enable/disable output messages in a particular class, just before running the app.
So lets say i have a classes A, B and C. I want to disable messages comming from B and C, and only see messages from A.
Am I more understandable ?
Thx
-
#define DEBUG class A: public QObject { Q_OBJECT public: A(); ~A(){} Q_INVOKABLE void connecToServer(){ conn->start(); #ifdef DEBUG qDebug()<<"started !" #endif } } private: bool dbg = true; };
-
@jsulm Perfect ! Thx :)
-
Hi @LeLev,
you might also be interested in Qt's categorized logging function: http://blog.qt.io/blog/2014/03/11/qt-weekly-1-categorized-logging/
With that, you can enable/disable logging at runtime. You just need to replace
qDebug
byqCDebug
and add appropriate categories.Regards.
-
Thx @aha_1980 ! i will check that too.
-
@LeLev
You should probably also be aware of https://doc.qt.io/qt-5.10/qtglobal.html#qInstallMessageHandlerqInstallMessageHandler
allows you to control what happens at run-time (instead of compile time) to anything going viaqDebug()
,qWarn()
etc., whether from your own code or Qt internals.