home *** CD-ROM | disk | FTP | other *** search
- From: kc@oz.rci.dk (Knud Christensen)
- Newsgroups: comp.lang.c,comp.unix.wizards,alt.sources,comp.sources.d,misc.misc
- Subject: Re: #define DEBUG... (using printf for debugging)
- Message-ID: <801@oz.rci.dk>
- Date: 7 May 90 12:17:07 GMT
-
- gordon@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@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).
-