home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!slsvaat!josef!kanze
- From: kanze@us-es.sel.de (James Kanze)
- Subject: Re: Debugging output
- In-Reply-To: spuler@coral.cs.jcu.edu.au's message of 5 Nov 92 02:37:30 GMT
- Message-ID: <KANZE.92Nov5203443@slsvdnt.us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL
- References: <spuler.720931050@coral>
- Date: 5 Nov 92 20:34:43
- Lines: 73
-
- In article <spuler.720931050@coral> spuler@coral.cs.jcu.edu.au (David
- Spuler) writes:
-
- |> [Sorry if this is a re-post. The first one seemed to get lost.]
-
- |> I was wondering whether anyone has any suggestions as to how to use the neat
- |> operator << to produce debugging output in a way that it can be removed
- |> easily from production code. The obvious method is:
-
- |> #ifdef DEBUG
- |> cerr << .....
- |> #endif
-
- |> but I'd like to avoid the #ifdef -#endif lines.
-
- Example deleted.
-
- |> Is there a preprocessor macro trick to remove the dbg << statements? (doubtful)
-
- |> Can I somehow define the << member fns as empty inline functions so that the
- |> compiler will effectively remove them (i.e. generate no code for such calls).
- |> e.g. something like:
-
- |> inline dbg_stream & operator << (char *c)
- |> {
- |> // do nothing
- |> }
- |> This doesn't work seem to work -- using g++ still generates code.
-
- |> Is there a better method? (e.g. making dbg a reference to an ostream?)
-
- |> Thanks for any suggestions. I'll summarize if there's sufficient response.
-
- Why not the following:
-
- #ifdef DEBUG
- #define TRACE( x ) do \
- { \
- if ( traceFile ) \
- *traceFile << x ; \
- } while( 0 )
- #else
- #define TRACE( x )
- #endif
-
- You then simply write:
-
- TRACE( "Got here, i = " << i ) ;
-
- Whenever you what your debug output.
-
- Two notes:
-
- 1. Don't put the x in the macro expansion in parentheses. (This is an
- exception to the normal rule.)
-
- 2. Because of the if, you can toggle tracing on and off by setting
- traceFile to NULL or to &cerr (or to any other file you want).
-
- In fact, because of point 2, I generally like to leave the debug code
- in, even in the delivered version. (In this case, I'll usually check
- that there is a specific environment variable set to a special value
- before accepting any options/commands to turn it on. No need for the
- customer to get a surprise because he mistyped and option, and got the
- debug option.)
- --
- James Kanze GABI Software, Sarl.
- email: kanze@us-es.sel.de 8 rue du Faisan
- 67000 Strasbourg
- France
-
-
-
-