home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1270 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: travis@cs.columbia.edu (Travis Lee Winfrey)
  2. Newsgroups: comp.lang.c,comp.unix.wizards,alt.sources,comp.sources.d,misc.misc
  3. Subject: Re: #define DEBUG... (using printf for debugging)
  4. Message-ID: <1990May4.161910.1353@cs.columbia.edu>
  5. Date: 4 May 90 16:19:10 GMT
  6.  
  7. In article <40628@cornell.UUCP>, 
  8. >gordon@mimir.cs.cornell.edu (Jeffrey  Adam Gordon) writes:
  9. >
  10. >    I want to have a DEBUG flag which controls whether diagnostic printfs
  11. >    are executed or not.
  12.  
  13. Well, another variation on the debug macro with two parentheses
  14. is to have a first argument that specifies the current level of
  15. interest.  This argument can be compared with whatever you need: a
  16. module-specific #define, a module-specific variable, a global
  17. variable.  The two biggest problems with this method are the complaints
  18. from lint about constants appearing in an if-statement, and that some
  19. compilers may not strip out the code when you don't want the output.
  20.  
  21. A sample follow.  Note that the numbers here range from 1 to 4 because
  22. I define DEBUG on a per-module basis.  Some other people specify their
  23. interest in debugging information in terms of percentages, i.e., show
  24. me 50% of everything you have in this module.  that's not intuitive
  25. for me.
  26.  
  27. (I'm sorry if anyone else has already described this particular
  28. fillip; I looked through all the messages posted so far, as many of
  29. the previous posters should have done.)
  30.  
  31. /* 
  32.  * debugging macros.  the dprintf macro takes two arguments, a
  33.  * debugging level (1, 2, 3, 4) and a list of ordinary printf
  34.  * arguments in parentheses, e.g.,
  35.  *    dprintf(1, ("proc_kill_module: sys_ptr is NULL!\n"));
  36.  * 
  37.  * debugging levels:
  38.  *     level 1: ordinary unexpected events which the programmer may want
  39.  *              to know, but a user will not.  all "oh no!" type
  40.  *         information should be marked with this.
  41.  *     level 2: more detailed, usually per-module information that
  42.  *         will a programmer will not want to know *unless* he or
  43.  *         she is debugging that module.
  44.  *     Level 3: more detailed than 2, usually per-procedure
  45.  *         debugging information.  debugging statements this
  46.  *         low-level will be removed once gross errors are
  47.  *         found and removed.
  48.  *     Level 4: just a level for demarcating extremely low-level,
  49.  *         usually inner-loop information.  removed as level 3 messages.
  50.  *              
  51.  */
  52. # ifdef DEBUG
  53. #       define dprintf(dlevel,printfargs)  if (dlevel <= DEBUG ) \
  54.                         printf printfargs  
  55. # else /* DEBUG */
  56. #    define dprintf(a,b)
  57. # endif /* DEBUG */
  58. --
  59.  
  60. Arpa:    travis@cs.columbia.edu    Usenet: rutgers!columbia!travis
  61.