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

  1. From: kc@oz.rci.dk (Knud Christensen)
  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: <801@oz.rci.dk>
  5. Date: 7 May 90 12:17:07 GMT
  6.  
  7. gordon@mimir.cs.cornell.edu (Jeffrey  Adam Gordon) writes:
  8.  
  9. >I want to have a DEBUG flag which controls whether diagnostic printfs
  10. >are executed or not.
  11.  
  12. >The obvious way to do this is:
  13.  
  14. >#ifdef DEBUG
  15. >    printf ("debugging information");
  16. >#endif DEBUG
  17.  
  18. >But its a pain to have to type the #ifdef ... #endif all the time, and
  19. >its less readable than simply having:
  20.  
  21. >    DEBUG ("debugging information");
  22.  
  23. >Now, I can use the latter format if I
  24.  
  25. >#define DEBUG printf
  26.  
  27. >but then how do I turn DEBUG off?
  28.  
  29.  
  30. >I have though of doing the following (which is not very elegant but I
  31. >thought it would work):
  32.  
  33. >#ifdef DODEBUG
  34. >#   define DEBUG printf
  35. >#   define ENDDEBUG ;
  36. >#else
  37. >#   define DEBUG /*
  38. >#   define ENDDEBUG */
  39. >#endif DODEBUG
  40.  
  41.  
  42. >which would allow the following syntax for debugging
  43.  
  44. >    DEBUG ("the value is %d", val); ENDDEBUG
  45.  
  46. >Unfortunately, I can't figure out how to #define something to be equal
  47. >to "/*" sinece "/*" always seems to be interpreted as the start of a
  48. >comment.
  49.  
  50. >Well, I've been rambling trying to describe the problem.  Basically,
  51. >does anyone have an idea how I can do the above easily and elegantly?
  52.  
  53. >Thanks for reading.
  54.  
  55. >- Jeff
  56. >    
  57. The following, which i found in a magazine solves the problem very elegantly i
  58. think:
  59.  
  60. /*
  61.   debug.h
  62.  
  63.   This header file gives a number of usefull definitions for debugging
  64. */
  65.  
  66. #ifdef debug
  67. #  define DFPRINTF(x) fprintf x
  68. #  define DTRACE fprintf(stderr, "Trace line %d\n", __LINE__)
  69. #  define DTRINT(var) fprintf(stderr, "Trace line %d var = %d\n", __LINE__, var)
  70. #else
  71. #  define DFPRINTF(x)
  72. #  define DTRACE
  73. #  define DTRINT(var)
  74. #endif
  75.  
  76. /*
  77.   End of debug facility definitions
  78. */
  79.  
  80. C-program
  81.  
  82. #define debug
  83. #include "debug.h"
  84.  
  85. int i,j;
  86.  
  87. main()
  88. {
  89.   DFPRINTF((stdout,"This is a test %d", i));
  90. }
  91. ----
  92.  
  93. Knud Christensen                                     RC International, Denmark
  94. kc@rci.dk
  95.  
  96. It is better to keep your mouth shut, and look like a fool
  97.  than to open it, and remove all doubt!   - Marx - (Groucho not Karl).
  98.