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 -
@LeLev
#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; };
-
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@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." -
@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?)?
-
@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 -
@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@LeLev
#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; };
-
@LeLev
#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; };
-
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.
-
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.
-
@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. -
Still not perfect.
for example , I write a library and distribute to someone.
1、 I donot know what to set QCDebug() for final user's categorize.
2、I can tell someone set DEFINES += MYLIB_DEBUG to turn on the debug info, if we use :#ifdef MYLIB_DEBUG qDebug() <<"debug info"; #endif
it will get you crazy to add more and more #ifdef /#endif.
So i try:#ifdef MYLIB_DEBUG #define DBG std::cout #else #define DBG 0 && std::cout #endif // use like this, DBG <<"my string";
Now it woks!
But sadly , when using qdebug(), it doesnot have a return value, so it compiled fail ?.// compile fail: #define DBG 0 && qDebug()
Or we can use :
#ifdef NO_DEBUG_ONE #define DBG(...) #else #define DBG(x,...) qDebug(x,##__VA_ARGS__) #endif
it's like printf, but we will be not able to use operator <<.
Is there a way to define qDebug like std::cout above???
-
Still not perfect.
for example , I write a library and distribute to someone.
1、 I donot know what to set QCDebug() for final user's categorize.
2、I can tell someone set DEFINES += MYLIB_DEBUG to turn on the debug info, if we use :#ifdef MYLIB_DEBUG qDebug() <<"debug info"; #endif
it will get you crazy to add more and more #ifdef /#endif.
So i try:#ifdef MYLIB_DEBUG #define DBG std::cout #else #define DBG 0 && std::cout #endif // use like this, DBG <<"my string";
Now it woks!
But sadly , when using qdebug(), it doesnot have a return value, so it compiled fail ?.// compile fail: #define DBG 0 && qDebug()
Or we can use :
#ifdef NO_DEBUG_ONE #define DBG(...) #else #define DBG(x,...) qDebug(x,##__VA_ARGS__) #endif
it's like printf, but we will be not able to use operator <<.
Is there a way to define qDebug like std::cout above???
@QtTester said in How to enable and disable qDebug() messages inside a class:
#define DBG 0 && std::cout
Pretty dodgy, IMHO!
Is there a way to define qDebug like std::cout above???
Did you try defining
QT_NO_DEBUG_OUTPUT
to the compiler? E.g. in the.pro
file:CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT
-
@QtTester said in How to enable and disable qDebug() messages inside a class:
#define DBG 0 && std::cout
Pretty dodgy, IMHO!
Is there a way to define qDebug like std::cout above???
Did you try defining
QT_NO_DEBUG_OUTPUT
to the compiler? E.g. in the.pro
file:CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT
-
@QtTester said in How to enable and disable qDebug() messages inside a class:
just disable qDebug in a.cpp
So since you know it's a
#define
you know you can put#define QT_NO_DEBUG_OUTPUT
as the first line in any.cpp
file, before you#include
any Qt stuff.... -
@QtTester said in How to enable and disable qDebug() messages inside a class:
just disable qDebug in a.cpp
So since you know it's a
#define
you know you can put#define QT_NO_DEBUG_OUTPUT
as the first line in any.cpp
file, before you#include
any Qt stuff.... -
@QtTester
Before I offer any further suggestions:#define QT_NO_DEBUG_OUTPUT #include <QApplication> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); qDebug() << "Hello world"; }
suppresses the
qDebug()
output for me (Linux, Qt 5.12).... -
@QtTester
Before I offer any further suggestions:#define QT_NO_DEBUG_OUTPUT #include <QApplication> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); qDebug() << "Hello world"; }
suppresses the
qDebug()
output for me (Linux, Qt 5.12).... -
@QtTester
Please read and act on suggestions.you know you can put
#define QT_NO_DEBUG_OUTPUT
as the first line in any.cpp
file, before you#include
any Qt stuff....See the italics I had put in to make sure you got it right.
#define QT_NO_DEBUG_OUTPUT
#include <QApplication>
Please follow the instructions instead of ignoring them and doing your own thing.
-
@QtTester
Please read and act on suggestions.you know you can put
#define QT_NO_DEBUG_OUTPUT
as the first line in any.cpp
file, before you#include
any Qt stuff....See the italics I had put in to make sure you got it right.
#define QT_NO_DEBUG_OUTPUT
#include <QApplication>
Please follow the instructions instead of ignoring them and doing your own thing.
@JonB
you are right, it seems work in cpp, how about if we provide only a head file?lib.h
#ifndef lib_h #define lib_h #define QT_NO_DEBUG_OUTPUT class MyClass{ public : MyClass(){ qDebug() <<"init"; } }; #endif
in a.cpp include the lib.h
#include "lib.h" void main() { // so we cannot enable this line anymore? qDebug()<<"main will shut down qdebug all"; }
-
@JonB
you are right, it seems work in cpp, how about if we provide only a head file?lib.h
#ifndef lib_h #define lib_h #define QT_NO_DEBUG_OUTPUT class MyClass{ public : MyClass(){ qDebug() <<"init"; } }; #endif
in a.cpp include the lib.h
#include "lib.h" void main() { // so we cannot enable this line anymore? qDebug()<<"main will shut down qdebug all"; }
@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.