home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15832 < prev    next >
Encoding:
Text File  |  1992-11-06  |  2.4 KB  |  86 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: Debugging output
  5. In-Reply-To: spuler@coral.cs.jcu.edu.au's message of 5 Nov 92 02:37:30 GMT
  6. Message-ID: <KANZE.92Nov5203443@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <spuler.720931050@coral>
  10. Date: 5 Nov 92 20:34:43
  11. Lines: 73
  12.  
  13. In article <spuler.720931050@coral> spuler@coral.cs.jcu.edu.au (David
  14. Spuler) writes:
  15.  
  16. |> [Sorry if this is a re-post. The first one seemed to get lost.]
  17.  
  18. |> I was wondering whether anyone has any suggestions as to how to use the neat
  19. |> operator << to produce debugging output in a way that it can be removed
  20. |> easily from production code.  The obvious method is:
  21.  
  22. |> #ifdef DEBUG
  23. |>    cerr << .....
  24. |> #endif
  25.  
  26. |> but I'd like to avoid the #ifdef -#endif lines.
  27.  
  28. Example deleted.
  29.  
  30. |> Is there a preprocessor macro trick to remove the dbg << statements? (doubtful)
  31.  
  32. |> Can I somehow define the << member fns as empty inline functions so that the
  33. |> compiler will effectively remove them (i.e. generate no code for such calls).
  34. |> e.g. something like:
  35.  
  36. |>     inline dbg_stream & operator << (char *c) 
  37. |>         {
  38. |>             // do nothing
  39. |>         }
  40. |> This doesn't work seem to work -- using g++ still generates code.
  41.  
  42. |> Is there a better method?  (e.g. making dbg a reference to an ostream?)
  43.  
  44. |> Thanks for any suggestions.  I'll summarize if there's sufficient response.
  45.  
  46. Why not the following:
  47.  
  48.     #ifdef DEBUG
  49.     #define    TRACE( x )    do                    \
  50.         {                            \
  51.              if ( traceFile )                \
  52.                  *traceFile << x ;            \
  53.         } while( 0 )
  54.     #else
  55.     #define    TRACE( x )
  56.     #endif
  57.  
  58. You then simply write:
  59.  
  60.     TRACE( "Got here, i = " << i ) ;
  61.  
  62. Whenever you what your debug output.
  63.  
  64. Two notes:
  65.  
  66. 1. Don't put the x in the macro expansion in parentheses.  (This is an
  67. exception to the normal rule.)
  68.  
  69. 2. Because of the if, you can toggle tracing on and off by setting
  70. traceFile to NULL or to &cerr (or to any other file you want).
  71.  
  72. In fact, because of point 2, I generally like to leave the debug code
  73. in, even in the delivered version.  (In this case, I'll usually check
  74. that there is a specific environment variable set to a special value
  75. before accepting any options/commands to turn it on.  No need for the
  76. customer to get a surprise because he mistyped and option, and got the
  77. debug option.)
  78. --
  79. James Kanze            GABI Software, Sarl.
  80. email: kanze@us-es.sel.de    8 rue du Faisan
  81.                 67000 Strasbourg
  82.                 France
  83.  
  84.  
  85.  
  86.