Detailed logging question

A

Angus

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?
 
V

Victor Bazarov

Angus said:
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?

It depends very much on what you mean by "string processing". If you
can find some genericity in your processing, then extract that into
a separate "string processor for purposes of logging", then that class
could be written with those #ifdefs and the clutter is fairly localised.

V
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top