rightaway717
rightaway717

Reputation: 2831

Disable qDebug output locally with a macro

I used qDebug all over the code. Now I would like to limit its output by translation units, defining a separate macro to enable/disable qDebug output in a translation unit:

test.pro:

DEFINES += NO_DEBUG_ONE

testone.cpp:

#ifdef NO_DEBUG_ONE
#define QT_NO_DEBUG_OUTPUT
#endif

testtwo.cpp:

#ifdef NO_DEBUG_TWO
#define QT_NO_DEBUG_OUTPUT
#endif

So, setting macros like this I expected to get qDebug output only in testtwo.cpp, but I see the qDebug messages from both translation units.

What am I missing here and how to solve it?

Upvotes: 4

Views: 2598

Answers (3)

CJCombrink
CJCombrink

Reputation: 3950

As Gabriel de Grimouard pointed out, the QT_NO_DEBUG_OUTPUT takes affect when you compile Qt, not your application (as far as I understand).

The only option is to change your code, there are a few options:

  1. Do as suggested in his answer, make use of a Macro check before each debug
  2. Define your own debug macro that you can enable or disable:

    // Top of testone.cpp
    #ifdef NO_DEBUG_ONE
      #define DEBUG_MESSAGE(message) 
    #else
      #define DEBUG_MESSAGE(message) qDebug() << message
    #endif
    // In your code
    DEBUG_MESSAGE("This is my debug message");
    
    • The benefit of this is that you do not have #ifndef TEST everywhere in your code.
  3. Make use of the Logging Categories in Qt (recommended)

Unfortunately all of these will require code changes.

Upvotes: 0

Simon Warta
Simon Warta

Reputation: 11408

Move your code to the top of testone.cpp and testtwo.cpp and you should be good to go.

You can use QT_NO_DEBUG_OUTPUT and QT_NO_WARNING_OUTPUT when you compile your app but those must be set before the Qt headers are included.

Using cmake you could add those defines on a per-file base but I don't see a simple way doing that with qmake.

Upvotes: 3

Gabrielle de Grimouard
Gabrielle de Grimouard

Reputation: 2005

As it's a QMake macro I don't think you can do it locally. the only way is to use a standard macro as -D TEST and after do thing like that.

#ifndef TEST
    qDebug() << "test";
#endif

Upvotes: 0

Related Questions