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

Robin Rosenberg INF rosenber at ra.abo.fi
Sat May 5 01:44:48 AEST 1990


>I want to have a DEBUG flag which controls whether diagnostic printfs 
>are executed or not.                                                  

I have used the following macros for a while now and they work fine.

/* tdebug.h  -- tracing & debugging macros
 *
 */
#if TDEBUG
    #define T(s)        {dprintf("\033[33m(\033[0m%s\033[0m",#s);{s} dprintf("\033[33m)\033[0m\r\n");}
    #define D(s)        s
    #define P(a)        dprintf a
    #define V(n,x)      {if (debuglevel>=n) dprintf("%s\r\n",x);}
#else
    #define T(s)        {s}     /* ! trace execution of statements */
    #define D(s)        ;       /* ! execute only when debugging */
    #define P(a)                /* ! printf when debugging */
    #define V1(n,x)     {}
#endif TDEBUG
/* eof */

dprintf has the same synopsis as printf, however if may be some other
function. Which function it is is defined at link time. Usually dprintf asks
another process to print the debugging info for me. If your system does not
allow you to define symbols at link time, replace dprintf with the funtion
you want in tdebug.h

Note however that the T() macro doesn't work in all cases. Most notably it
may have to be written differently for other compilers and may not work if
the statement beging traced contains double quotes.

Also, the ANSI escape sequences used in the T() macro is to help matching
parentheses in the trace output.

Here is a sample (not useful) program using these macros

#define TDEBUG 1	/* or 0 if debugging is not wanted */
#include "tdebug.h"

int debuglevel;

main(int argc,char **argv)
{
	debuglevel = atoi(argv[1]);
	P(("This is a demo. Prog =%s\n",argv[0]));
	T(subroutine(argc,argv);)
	V(1,"I will now fall off the e");
}

You may use or modify these macros as you like according to your needs.
Enjoy!

-----------
Robin Rosenberg



More information about the Comp.unix.wizards mailing list