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

Knud Christensen kc at oz.rci.dk
Mon May 7 22:17:07 AEST 1990


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");

>Now, I can use the latter format if I

>#define DEBUG printf

>but then how do I turn DEBUG off?


>I have though of doing the following (which is not very elegant but I
>thought it would work):

>#ifdef DODEBUG
>#   define DEBUG printf
>#   define ENDDEBUG ;
>#else
>#   define DEBUG /*
>#   define ENDDEBUG */
>#endif DODEBUG


>which would allow the following syntax for debugging

>	DEBUG ("the value is %d", val); ENDDEBUG

>Unfortunately, I can't figure out how to #define something to be equal
>to "/*" sinece "/*" always seems to be interpreted as the start of a
>comment.

>Well, I've been rambling trying to describe the problem.  Basically,
>does anyone have an idea how I can do the above easily and elegantly?

>Thanks for reading.

>- Jeff
>	
The following, which i found in a magazine solves the problem very elegantly i
think:

/*
  debug.h

  This header file gives a number of usefull definitions for debugging
*/

#ifdef debug
#  define DFPRINTF(x) fprintf x
#  define DTRACE fprintf(stderr, "Trace line %d\n", __LINE__)
#  define DTRINT(var) fprintf(stderr, "Trace line %d var = %d\n", __LINE__, var)
#else
#  define DFPRINTF(x)
#  define DTRACE
#  define DTRINT(var)
#endif

/*
  End of debug facility definitions
*/

C-program

#define debug
#include "debug.h"

int i,j;

main()
{
  DFPRINTF((stdout,"This is a test %d", i));
}
----

Knud Christensen                                     RC International, Denmark
kc at rci.dk

It is better to keep your mouth shut, and look like a fool
 than to open it, and remove all doubt!   - Marx - (Groucho not Karl).



More information about the Alt.sources mailing list