Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to enable and disable qDebug() messages inside a class
Forum Updated to NodeBB v4.3 + New Features

How to enable and disable qDebug() messages inside a class

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 11.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtTester
    wrote on last edited by QtTester
    #11

    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???

    JonBJ 1 Reply Last reply
    0
    • Q QtTester

      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???

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #12

      @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
      
      Q 1 Reply Last reply
      0
      • JonBJ JonB

        @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
        
        Q Offline
        Q Offline
        QtTester
        wrote on last edited by
        #13

        @JonB
        just disable qDebug in a.cpp , not in b.cpp ,c.cpp or other.cpp.

        JonBJ 1 Reply Last reply
        0
        • Q QtTester

          @JonB
          just disable qDebug in a.cpp , not in b.cpp ,c.cpp or other.cpp.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #14

          @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....

          Q 1 Reply Last reply
          0
          • JonBJ JonB

            @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....

            Q Offline
            Q Offline
            QtTester
            wrote on last edited by
            #15

            @JonB Not work when add QT_NO_DEBUG_OUTPUT to cpp head, you can try.

            JonBJ 1 Reply Last reply
            0
            • Q QtTester

              @JonB Not work when add QT_NO_DEBUG_OUTPUT to cpp head, you can try.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #16

              @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)....

              Q 1 Reply Last reply
              0
              • JonBJ JonB

                @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)....

                Q Offline
                Q Offline
                QtTester
                wrote on last edited by
                #17

                @JonB 微信截图_20220608175602.png

                JonBJ 1 Reply Last reply
                0
                • Q QtTester

                  @JonB 微信截图_20220608175602.png

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #18

                  @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.

                  Q 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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.

                    Q Offline
                    Q Offline
                    QtTester
                    wrote on last edited by
                    #19

                    @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";
                    }
                    
                    JonBJ 1 Reply Last reply
                    0
                    • Q QtTester

                      @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";
                      }
                      
                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #20

                      @QtTester
                      Now you are getting demanding!

                      The effect of #if !defined(QT_NO_DEBUG_OUTPUT) is acted on in qloggingcategory.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 switching QT_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.

                      Q 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @QtTester
                        Now you are getting demanding!

                        The effect of #if !defined(QT_NO_DEBUG_OUTPUT) is acted on in qloggingcategory.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 switching QT_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.

                        Q Offline
                        Q Offline
                        QtTester
                        wrote on last edited by
                        #21

                        @JonB
                        we discuss and you give solution, others will be benefited, tripartite win. :-)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved