Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 10

Qt-interest Archive, March 2002
how to suppress qDebug on release build?


Message 1 in thread

hi, I have a lot of qDebug(...) calls in my code. is there a way I can
suppress them during release build? I don't want to remove the qDebug calls,
and I try to avoid using #if (too messy). I suppose there is a flag I can
stop them from printing during release build. any idea?

thanks
ben


Message 2 in thread

Op dinsdag 26 maart 2002 15:49, schreef u:
> hi, I have a lot of qDebug(...) calls in my code. is there a way I can
> suppress them during release build? I don't want to remove the qDebug
> calls, and I try to avoid using #if (too messy). I suppose there is a flag
> I can stop them from printing during release build. any idea?
>
> thanks
> ben

Hi,

have a look at qInstallMsgHandler, that should do the trick :-)

regards,

Klaas


Message 3 in thread

Ben Hui <bhui@724.com>
> hi, I have a lot of qDebug(...) calls in my code. is there a way I can
> suppress them during release build? I don't want to remove the qDebug calls,
> and I try to avoid using #if (too messy). I suppose there is a flag I can
> stop them from printing during release build. any idea?

You can collect them in a single #if, or redirect them to a logfile using
code like this:

//...

static void breakHere()
{
    // a breakpoint in this empty function hits immediately _after_
    // printing a qWarning() or qFatal()
}


static void errorHandler( QtMsgType t, const char * message )
{
    // the implementation of this can be #ifdef'd out for a production
    // build, or a different implementation can be substituted
    if ( message && *message )
	fprintf( stderr, "%s\n", message );
    if ( t != QtDebugMsg )
	breakHere();
}

//...

int main( int argc, char ** argv ) {
    qInstallMsgHandler( errorHandler );
//...


--ARnt


Message 4 in thread

On Tue, 26 Mar 2002, Ben Hui wrote:

> hi, I have a lot of qDebug(...) calls in my code. is there a way I can
> suppress them during release build? I don't want to remove the qDebug calls,
> and I try to avoid using #if (too messy). I suppose there is a flag I can
> stop them from printing during release build. any idea?

The gcc ist capable to add a include file befor all other includes of a
*.cpp . Copy the attached files to your project and add the following
lines to your *.pro file.

QMAKE_CXXFLAGS += -include lfqdebug.h
SOURCES += lfqdebug.cpp

You may add a global symbol to change the behaviour of the "new" qDebug.

Beware that's an ugy macro hack!
#include "lfqdebug.h"

#undef qDebug

#include <stdarg.h>
#include <stdio.h>

#include <qstring.h>

QString __MY_FILE__;
int __MY_LINE__;

void qMyDebug( const char *msg, ... )                                                                                                                                           
{                                                                                                                                                                             
    char buf[1024];
    va_list ap;
    va_start( ap, msg );
    vsnprintf( buf, 1024, msg, ap );
	qDebug("%s:%i - %s", __MY_FILE__.latin1(), __MY_LINE__, buf);
    va_end( ap );
}
#ifndef __qMyDebug__
#define __qMyDebug__

#include <qglobal.h>

class QString;

extern QString __MY_FILE__;
extern int __MY_LINE__;

void qMyDebug( const char *msg, ... );

#ifndef qDebug
#define qDebug __MY_FILE__=__FILE__; __MY_LINE__=__LINE__; qMyDebug
#endif // qDebug

#endif // __qMyDebug__