Boost logo

Boost :

From: John Eddy (johneddy_at_[hidden])
Date: 2005-01-28 19:14:18


Thanks for the suggestion. I have implemented a version with this
suggestion which would change the example I posted earlier to look like
this:

class default_levels
{
    public:
        enum levels {
            DETAIL,
            DEBUG,
            INFO,
            WARN,
            ERROR,
            SHOUT,
            FATAL,
            DEFAULT = WARN // DEFAULT is required. The rest are up
to the user.
        };
};

#include <boost/logging.hpp>
#include <iterator>
#include <iostream>

using namespace std;
using namespace boost::logging;

struct test1 { int field; };

int main(int argc, char* argv[])
{
    typedef logger_base<default_levels> log_type;
    log_type mylog; // default level is default_levels::DEFAULT (WARN)

    // anything logged at this point would only cause an entry creation
    // if it were logged at the WARN level or greater.
    mylog.log(default_levels::WARN, "This is an anonomous log entry");
    mylog.log(default_levels::WARN, "main is logging a warning");

    // create some items to associate with log entries.
    test1 t11, t12;

    // make the logging level for all not-otherwise-level-set test1
objects DEBUG
    mylog.set_logging_level<test1>(default_levels::DEBUG);

    // now, the first statement below will cause a log entry and the
second will not.
    mylog.log(default_levels::INFO, &t11, "This is an info entry");
    mylog.log(default_levels::DETAIL, &t12, "This is a detail entry");

    // now make the logging level for t11 FATAL
    mylog.set_logging_level(&t11, default_levels::FATAL);

    // now, the first statement below will cause a log entry and the
second will not.
    mylog.log(default_levels::FATAL, &t11, "Call me T11", "This is the
fatal error text");
    mylog.log(default_levels::DEBUG, &t11, "This is a T11 debug message");

    // logging messages can then be retrieved from the log using
predicates, of which
    // a number of simple examples I've already created. For now, I
will just write
    // all logged messages to the screen.
    const log_type::entry_list& all = mylog.get_all_entries();
    for_each(all.begin(), all.end(), entry_printer<log_type::entry>(cerr));
}

The above program causes the output:
2005-Jan-28 19:08:21.464715: Anonymous, L:4 - This is an anonomous log entry
2005-Jan-28 19:08:21.464715: Anonymous, L:4 - main is logging a warning
2005-Jan-28 19:08:21.464715: struct test1, L:3 - This is an info entry
2005-Jan-28 19:08:21.464715: Call me T11, L:7 - This is the fatal error text

I am thinking about requiring a conversion from levels to string reps in
the policy class but have not implemented that.

Thanks,
John

Jason Hise wrote:

> You may want to make your logging levels a policy which defaults to an
> enumeration of your currently listed logging levels. This way, if
> someone has a need for a logger with other levels of output they can
> just specify a different enumeration for the policy. Instead of
> having separate logging functions (log, warn, info) you could have one
> which accepts a value of the enumeration's type as the first param.
> The logging level could be a member of this enumeration as well, so a
> simple less-than comparison could determine whether or not to output
> the message.
>
> -Jason
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost


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