Re: Detailed logging question
On Sep 26, 6:28 pm, Angus <anguscom...@gmail.com> wrote:
I want to setup a macro to log informational strings if eg
INFOLOGGING
is defined. So I created a macro which outputs informational strings
to a log file.
I want to have a #define which switches this option on or off.
My problem is that I need to do a load of string processing to build
up the informational string. Then the informational string is passed
to the macro. Now if the define is not set then the macro never gets
called. That is fine. But ideally I don't want all this string
processing if the define is not set.
Is my only way round this to have loads of #ifdef <whatever>'s? It
just looks a bit dirty. But I can't see any way round it?
I am trying to implement detailed logging. Errors are always logged
but I want a #define to switch on a higher level of logging.
Anyone got any bright ideas on how to handle this?
First, you don't want logging controlled by a #define. That
means that you have to recompile to turn it on or off. You need
it to depend on a configuration argument, so that it can be
turned on or off without recompiling.
Other than that, I'm not too sure what you mean by "string
processing". The usual solution to logging involves some sort
of simple wrapper which emulates the ostream, idiom, with the
actual output being done by a templated operator<<, something
like:
template< typename T >
LogStream&
operator<<( LogStream& dest, T const& value )
{
if ( dest.isActive() ) {
dest.stream() << value ;
}
return dest ;
}
All of the "string processing" is in the << operators, which
don't get called unless logging is active.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34