"John Smith" <jo********@x-formation.com> wrote in message
news:42*********************@dread11.news.tele.dk. ..
Lets say you have a function "debug printf" which works like printf but
will get left out if _DEBUG flag is not defined along with the strings which
are to be printed.
Now I defined this:
#ifdef _DEBUG
void dbgprintf(char *szFormat, ...);
#else
#define dbgprintf
#endif
1. You could use in your code
dbgprintf("Print this string");
and define
#ifdef _DEBUG
void dbgprintf(whatever_arguments ...)
{
printf(...
}
#else // #ifdef _DEBUG
void dbgprintf(whatever_arguments ...)
{
// Do nothing.
}
#endif // #ifdef _DEBUG
2. Personally, I'm apprehensive about this approach at all. Consider the
following
dbgprintf("%d", my_terribly_expensive_operation());
or
dbgprintf("%d", my_operation_with_various_side_effects());
Then the compiler would call my_terribly_expensive_operation() or
my_operation_with_various_side_effects() even in release mode - probably not
what I want.
So I usually define dbgprintf as a macro itsel - this way the compiler can
eleminate its arguments completely.
3. Another matter which is possibly personal taste - it might be better to
distinguish between debug and trace: the first checks internal invariants;
the second outputs diagnostic information. Personally, I think these should
be orthogonal. There are cases where I find bugs in release mode only (e.g.,
because of thread races, which debug mode can change entirely). At the very
least, I usually try to have orthogonal debug and trace macros.