home *** CD-ROM | disk | FTP | other *** search
- From: dig@peritek.UUCP (Dave Gotwisner)
- Newsgroups: comp.lang.c,comp.unix.wizards,alt.sources,comp.sources.d,misc.misc
- Subject: Re: #define DEBUG... (using printf for debugging)
- Message-ID: <1427@peritek.UUCP>
- Date: 4 May 90 04:47:41 GMT
-
- In article <40628@cornell.UUCP>, 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");
-
- 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
-