Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a leveled debug system?

Tags:

c

debugging

by design, in the environment I'm working right now I can't use a debugger to try to detect bugs, so pretty much always when I need to debug a functionality I end up outputting some info.

To do that I've done the following:

#ifdef DEBUG
    #define printd(x) printf x
#else
    #define printd(x)
#endif

So when I need to print some debug info I use printd() instead of printf().

The problem I've found is that I need a leveled system, there are messages that may be important in a determined debug level, but irrelevant when debugging other parts of the code.

So my question is, how can I implement a leveled debug system? I value simplicity, I mean, I prefer my actual system than needing a lot of code or confusing code when using it. Something like printd(level, format, ...) would be awesome.

like image 989
Carla Álvarez Avatar asked Feb 02 '10 02:02

Carla Álvarez


People also ask

What is debug level?

A debug level is a set of log levels for debug log categories, such as Database , Workflow , and Validation . A trace flag includes a debug level, a start time, an end time, and a log type. The log types are DEVELOPER_LOG , USER_DEBUG , and CLASS_TRACING .

How are debug levels adjusted in the developer console?

To configure trace flags and debug levels from the Developer Console, click Debug | Change Log Levels. Then complete these actions. To create a trace flag, click Add. To edit an existing trace flag's duration, double-click its start or end time.


1 Answers

Sure, there are more elegant ways to do this, of course, but this works just fine

#ifdef DEBUG
 extern int g_debuglevel;
 #define printd(level, x) (level <= g_debuglevel) ? 0 : printf x
#else
 #define printd(level, x)
#endif

Although personally I prefer this

#ifdef DEBUG
 extern void printdf(level, fmt, ...);
 #define printd printfd
#else
 #define printd
#endif

where printdf is a function that tests the level and then calls vprintf passing along the fmt and va_args.

like image 192
John Knoeller Avatar answered Oct 18 '22 07:10

John Knoeller