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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With