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

  1. From: passaret@brahe.crd.ge.com ("Mr. Mike" Passaretti)
  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: <7377@crdgw1.crd.ge.com>
  5. Date: 4 May 90 13:18:42 GMT
  6.  
  7.  
  8. In article <40628@cornell.UUCP> 
  9. gordon@mimir.cs.cornell.edu (Jeffrey  Adam Gordon) writes:
  10. #
  11. #    I want to have a DEBUG flag which controls whether diagnostic printfs
  12. #    are executed or not.
  13. #
  14. #     [....]
  15. #
  16. #    - Jeff
  17.  
  18. Fred Fish has produced an excellent set of macros for C
  19. which do just this and a lot more.  The basic form of use is
  20. as follows (forgive me if this is a bit long).
  21.  
  22.     #include "dbug.h"
  23.  
  24.     main(argc, argv)
  25.     int argc;
  26.     char *argv[];
  27.     {
  28.     int foo;
  29.  
  30.     DBUG_ENTER("main");
  31.     DBUG_PROCESS(argv[0]);
  32.     for (i=1;i<argc && argv[i][0]=='-';i++)
  33.             {
  34.         switch(argv[i][1])
  35.         {
  36.         case '#' :
  37.             {
  38.             DBUG_PUSH(&(argv[i][2]));
  39.             }
  40.             break;
  41.         [...]
  42.         }
  43.         }
  44.  
  45.     foo = bar_bar_bar();
  46.     DBUG_PRINT("bar", ("Foo = %d\n", foo));
  47.  
  48.     foo = blah_blah_blah();
  49.     DBUG_PRINT("blah", ("Foo = %d\n", foo));
  50.     DBUG_VOID_RETURN(0);
  51.     }    
  52.  
  53. Now, at run-time the program can be called with a command
  54. line like 'a.out #d:t mumble'.   This is a flag which the
  55. DBUG macros use (through DBUG_PROCESS and DBUG_PUSH) to
  56. enable and disable various functions.  There is tracing,
  57. which is an output like
  58.  
  59.     >main
  60.     ->bar_bar_bar
  61.     -<bar_bar_bar
  62.     ->blah_blah_blah
  63.     -<blah_blah_blah
  64.     <main
  65.  
  66. for simple level tracing.  There is also "flag" type printf
  67. enabling, where a command line like 'a.out #dbar,:t mumble'
  68. would cause all DBUG_PRINTFs with the keyword (1st arg)
  69. "bar" to get printed.  There are also global enables and
  70. disables, including the #define DBUG_OFF, which removes all
  71. of the debug code entirely (for production usage).
  72.  
  73. my dbug.h has the following author notice in it, but I think
  74. it's outdated.  Fred's whereabouts should be easy to
  75. determine, if by no other method than eavesdropping on
  76.  comp.sys.amiga for a while.
  77.  
  78.   *  AUTHOR
  79.   *
  80.   *    Fred Fish
  81.   *    (Currently employed by Motorola Computer Division, Tempe, Az.)
  82.   *    hao!noao!mcdsun!fnf
  83.   *    (602) 438-3614
  84.  
  85.                                                         - MM
  86.  
  87. -- 
  88. passaretti@crd.ge.com                     {whatever}!crdgw1!brahe!passaret
  89.