#define DEBUG... (using printf for debugging)

Dave Gotwisner dig at peritek.UUCP
Fri May 4 14:47:41 AEST 1990


In article <40628 at cornell.UUCP>, gordon at mimir.cs.cornell.edu (Jeffrey  Adam Gordon) writes:
> I want to have a DEBUG flag which controls whether diagnostic printfs
> are executed or not.
> 
> The obvious way to do this is:
> 
> #ifdef DEBUG
> 	printf ("debugging information");
> #endif DEBUG
> 
> But its a pain to have to type the #ifdef ... #endif all the time, and
> its less readable than simply having:
>
> 	DEBUG ("debugging information");

Agreed.

> 
> Now, I can use the latter format if I
> 
> #define DEBUG printf
> 
> but then how do I turn DEBUG off?

Having read some of the other responses, I have yet to see the one I use:

#ifdef DEBUG
# define	Dprintf	printf
#else
# define	Dprintf if
#endif

This only needs to be included in a common header file,  then, if you do:

	Dprintf("Input value is %d\n", 10);

you get the following if DEBUG is defined:

	printf("Input value is %d\n", 10);

and the following if DEBUG is not defined:

	if ("Input value is %d\n", 10);

The semicolon will terminate the if, so, unless the debug printf has an
else immediatly after it, this will work.  There is some run time overhead
(unless your compiler optimizes "if (statement);" away because it never does
anything), but the overhead should be small.
-- 
------------------------------------------------------------------------------
Dave Gotwisner					UUCP:  ...!unisoft!peritek!dig
Peritek Corporation				       ...!vsi1!peritek!dig
5550 Redwood Road
Oakland, CA 94619				Phone: 1-415-531-6500



More information about the Comp.unix.wizards mailing list