Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2004-04-15 13:52:25


>Well, to implement extra-simple logging I always can do:
>
>[...]
>
>
we can always do that ;)
I chose to use the popular '<<' syntax.

>The complexity, though, is in processing which happens after the message is
>send to logger -- checking when message should be printed, adding
>timestamps, writing to files/syslog, etc.
>
>
>
True. I looked at log4j (for the nth time ;)) I don't like it. It's way
too complex.

There's no need for Filter, Layout and a lot of other classes (IMHO).

What I want is to create a simple correspondence from a log_id (a level)
into a log function.
Having that, you can always say something like:
LOG(activity) << whatever... << std::endl;

if 'activity' is turned off this nothing gets evaluated (yes, LOG is a
macro ;))
If it's turned on, the whole message is composed and the function
corresponding to 'activity' level is called.

What I assume is that in the end the whole output will end up in one or
two files.

Thread-safety and prefixing each message with timestamp is easy - I've
done it in about 40 lines of code.

>[...]
>1. User gets hold of boost::logger and outputs something
>2. logger emits signal which can be then connected to a number of consumer
>3. First consumer might be 'filter' which determines if the message should
>be output at all. If it should be output, it emits signal again, which
>might be delivered further
>4. 'filter' might be connected to 'formatter', which adds timestamp and
>again emits the signal.
>5. 'formatter' might be connected to 'stream_writer' which stores the
>message to a file and does nothing more.
>
>
>
I think the above is too complicated. Here's how I see it:
1. Given a log level, the user gets hold of the corresponding
boost::logger and tries to output something.
2a. If that log is disabled, nothing else happens.
2b. If that log is enabled, the message to be outputted is written to a
stringstream, then
    converted to a string..
    The function corresponding to its level is called. That's it!

If you want formatter (your step 5.) or prefix (your step 4.), you can
simply do them in your custom function.
The log library will contain classes that allow you to manipulate such
things.

For instance:
void do_log(const std::string & msg) {
    // thread-safe logging on a dedicate thread. Writing each 100 ms
    static ts_logger func( "my_log_file", 100);
    func(msg);
}

As I see it, the it's really easy ;)

Best,
John


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk