Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!romp!auschs!awdprime!nostromo.austin.ibm.com!jkimble From: jkimble@nostromo.austin.ibm.com (The Programmer Guy) Newsgroups: comp.lang.c Subject: Re: #define DEBUG... (using printf for debugging) Message-ID: <2297@awdprime.UUCP> Date: 4 May 90 19:48:29 GMT References: <11290@hoptoad.uucp> <40628@cornell.UUCP> <2294@awdprime.UUCP> Sender: news@awdprime.UUCP Reply-To: ibmaus!jkimble@cs.utexas.edu (The Programmer Guy) Distribution: usa Organization: The Pros From Dover Lines: 43 > [quick DEBUG method within programs] Here's how I usually do it: #define DEBUG(l, f, s) if (Debug >= l) fprintf(stderr, f, s) You will have to have "int Debug;" somewhere, usually as a global. Now you just change (int)Debug to be whatever you want; I usually have a command-line option that allows me to specify it in levels (ala UUCP). For example: program -x9 (Debug will be set to 9, maximum debug) program -x5 (Debug will be set to 5, medium debug) program -x1 (Debug will be set to 1, minimum debug) Your DEBUG statements look like so within the program: DEBUG(7, "Number of elements within array = %d\n", nmbr); DEBUG(5, "Pointer a = |%s|\n", a); etc., etc., etc., Caveats: If you just have an information message and don't want any data passed, make sure you do something like this: DEBUG(7, "We're in rmdir()...\n", ""); [note the second set of ""] The final caveat is that you can only pass one parameter per DEBUG() statement; therefore if you have multiple variables to print you're going to have to use more than one DEBUG statement. However, I think this is the cleanest way to handle this sort of stuff. (But I am sure we're going to see plenty of other methods coming back from The Net). Hope this helps! --Jim Kimble, Phone: 512/823-4479 (work) Yet Another IBM Contractor UUCP: ibmaus!jkimble@cs.utexas.edu TCP/IP Development, RISC System/6000, RT++, RS/6000, etc. IBM Austin, TX "ALPO is 99 cents a can. That's almost SEVEN dog dollars!"