Boost logo

Boost :

From: David A. Greene (greened_at_[hidden])
Date: 2001-11-24 16:11:43


Jesse Jones wrote:

>>I've written a class template that takes as template
>>value parameter the number of streams logging messages
>>should go to.
>
> Personally I don't like using iostreams for debug logging. I
> experimented with this with CodeWarrior Pro 6 (I think it was) and
> got insane amounts of bloat.

I haven't noticed such problems, though admittedly I haven't
looked for them either. :) I find iostreams much too convenient
to just dismiss them out-of-hand. Often I want to dump the
state of some object and iostreams is the only way to do it
nicely.

>>Each stream can be bound to a different
>>ostream, and each stream has its own threshold (to
>>prioritize logging messages).
>
> I do this a bit differently. When clients want to log something they
> can specify a logging category (eg LOG("Sockets", "Sent ", bytes, "
> bytes to ", address, ".\n")). When LOG is called it assembles the
> string (using overloaded ToStr functions) and passes the string and
> the category to the logger.

Here is where I find iostreams nice to use. My simple debug
macro is used like this:

DEBUG("Sent " << bytes << " bytes to " << address, <other_args>);

where <other_args> includes categories, levels, etc. I haven't
implemented anything beyond a simple global debug level since
I didn't want to spend all my time writing debugging code. This
is one of the reasons I asked whether Boost might provide such
facilities. :)

There is no need for ToStr functions as iostreams takes care
of it. Plus it works with objects.

> The nice thing about this is that it's simple to use, messages are
> filtered with a meaningful name instead of a number, there's no bloat
> yet it's still type safe and extensible, and it can be easily
> customized with different sinks.

How do you specify different sinks? Is LOG a vararg macro?
I don't know why, but I feel a bit uncomfortable channelling
based on a string. You're right in that it gives you a
friendlier category tag than a number, but there's something
that just doesn't "feel" right to me. Maybe it's that the
compiler can't check whether a valid channel is being specified.

Has anyone ever come up with a nice solution to the "stringified
enum" idiom? I've seen countless preprocessor hacks but is
there a better way?

                                -Dave

-- 
"Some little people have music in them, but Fats, he was all music,
  and you know how big he was."  --  James P. Johnson

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