PDA

View Full Version : Macro used for debugging



sunil.thaha
28th March 2007, 08:31
Hi, Below is a macro I use for debugging. Before I put this in Wiki I thought I would post it here so that if it is useful, we will put it in wiki



// --- Debugging ---
#include <QtDebug>

#define APP_NO_DEBUG_MESSAGES


#if !defined( APP_NO_DEBUG_MESSAGES )

// Flags for Debug Output
# define DEBUG_PRINT_FILEPATH
# define DEBUG_PRINT_SHORT_FN_NAME
# define DEBUG_PRINT_LINENUMBER

// Adjust Length of fields Here
// FileName
# define DEBUG_FILEPATH_LENGTH 27
# define DEBUG_FILENAME_LENGTH 22
// Function
# define DEBUG_LONG_FUNCTION_LENGTH 40
# define DEBUG_SHORT_FUNCTION_LENGTH 25

// LineNumber
# define DEBUG_LINENUMBER_LENGTH 4

// file names
# if defined( DEBUG_PRINT_FILEPATH )
# define DEBUG_FILENAME ( QString( "%1").arg(__FILE__ , \
-DEBUG_FILEPATH_LENGTH, QLatin1Char(' ')) )

# elif defined( DEBUG_PRINT_FILENAME )
# include <QFileInfo>
# define DEBUG_FILENAME ( QString( "%1").arg( \
QFileInfo(__FILE__).fileName(), \
-DEBUG_FILENAME_LENGTH, QLatin1Char(' ')) )

# elif defined( DEBUG_NO_FILENAME )
# define DEBUG_FILENAME ""
# else
# error No debug file flags defined
# endif

// function Names
# if defined( Q_CC_GNU )
# if defined ( DEBUG_PRINT_LONG_FN_NAME )
# define DEBUG_FUNCTION_NAME QString( " %1").arg( __PRETTY_FUNCTION__, \
-DEBUG_FULL_FUNCTION_LENGTH, QLatin1Char(' '))
#
# elif defined ( DEBUG_PRINT_SHORT_FN_NAME )
# define DEBUG_FUNCTION_NAME QString( " %1").arg( __FUNCTION__ +QString("()"), \
-DEBUG_SHORT_FUNCTION_LENGTH, QLatin1Char(' '))
#
# elif defined ( DEBUG_NO_FUNCTION_NAME )
# define DEBUG_FUNCTION_NAME ""
# else
# error No debug fuction flags defined
# endif
# else
# define DEBUG_FUNCTION_NAME ""
# endif

// Line numbers
# if defined ( DEBUG_PRINT_LINENUMBER )
# define DEBUG_LINENUMBER ( QString("[%1]")\
.arg( QString::number(__LINE__), DEBUG_LINENUMBER_LENGTH, QLatin1Char(' ')) )
# else
# define DEBUG_LINENUMBER ""
# endif

// Debug Macro
# define DEBUG() qDebug() << QString( DEBUG_FILENAME + DEBUG_LINENUMBER + DEBUG_FUNCTION_NAME + " :" )
#else
# define DEBUG() if(true);else qDebug()
#endif


How do I use it


#include "Debug.h"

void foo(){
DEBUG() << "Blah " << "Blah "
}

sunil.thaha
29th March 2007, 06:16
Any comments ??
Or, Is it worth having this in Wiki ?

wysota
29th March 2007, 07:12
Sorry, I'm a bit sleepy right now... but what's the point of this macro?

sunil.thaha
29th March 2007, 07:40
Well,

The macro allow you to print a debug message which has the filename, line number and the function name. this macro can be enable/disabled using


#define APP_NO_DEBUG_MESSAGES
You can disble/enable the Filename, Function name and the line number
Both the function name and the filename can be short or more descriptive


# define DEBUG_PRINT_FILEPATH
# define DEBUG_PRINT_SHORT_FN_NAME
# define DEBUG_PRINT_LINENUMBER
The output is aligned and the width of the field can be configured


// file name
# define DEBUG_FILEPATH_LENGTH 27
# define DEBUG_FILENAME_LENGTH 22
#
// Function
#
# define DEBUG_LONG_FUNCTION_LENGTH 40
#
# define DEBUG_SHORT_FUNCTION_LENGTH 25
#
// LineNumber
#
# define DEBUG_LINENUMBER_LENGTH 4
so for line


void foo(){
DEBUG() << "I am here ";
}

void foobar(){
DEBUG() << "Yupppy ";
}
the output would be



src/filename.cpp [ 2] void foo() : "I am here "
src/filename.cpp [ 6] void foobar() : "Yuppy"
Since the output is aligned, it is easier to comprehend

wysota
29th March 2007, 09:14
C00l... although complicated :) Can't you make it work so that it is called by using qDebug() (so that one doesn't have to replace all qDebug() calls)?

sunil.thaha
29th March 2007, 09:30
C00L.... although complicated
Thanks :)
Anyone can do that, replacing the DEBUG with qDebug in the #define DEBUG() should do I think . But I personally would not want to do that . Hmmm, Just doesn't look fine. what if I do not want the extra information for a some set of debug messages

sunil.thaha
29th March 2007, 10:29
the question still remains....

Is it helpful to have this in our wiki ?

wysota
29th March 2007, 11:58
Anyone can do that, replacing the DEBUG with qDebug in the #define DEBUG() should do I think .

I don't think so, because you use qDebug() in your debug macro, so you can't substitute qDebug using a different macro. What I'd like to see is to substitute the qDebug macro to add information you add in your macro. Sorry if the explanation sounds complicated...

sunil.thaha
29th March 2007, 12:14
How about having this on top :cool:


#if defined( qDebug )
# undef qDebug
#endif



PFA driver file.

So is it fine to have it in our wiki ?

wysota
29th March 2007, 12:59
I think you don't understand... Your macro uses qDebug(), so you can't undef it or it won't work.

sunil.thaha
29th March 2007, 13:09
Did you try out the driver file. it works for me ...

sunil.thaha
30th March 2007, 18:32
Since there has been three downloads ... Did anyone find it useful ?