Boost logo

Boost Users :

Subject: [Boost-users] [Log] How to use a channel that is constant per file?
From: Timo Schmiade (the_isz_at_[hidden])
Date: 2013-09-02 17:10:24


Hi all,

I want to use boost's log library to output messages to the console
which feature a per-message severity and a per-file channel.

I already tried two approaches, none of which was satisfying:

=== 1. approach ===

The class boost::log::sources::severity_channel_logger supports both
severity and channel and I could define it in each source file like so:

--- 8< ---

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(
    log,
    boost::log::sources::severity_channel_logger<
        boost::log::trivial::severity_level,
        std::string
>
)

--- >8 ---

Unfortunately, using it like this requires me to redundantly add the
channel to each log message, which is very inconvenient because it is
the same for all logs in the file.

--- 8< ---

// Message.cpp

BOOST_LOG_CHANNEL_SEV(log, Message, trace) << "Hello, world";

--- >8 ---

=== 2. approach ===

I defined my logger like this:

--- 8< ---

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(
    log,
    boost::log::sources::severity_logger<
        boost::log::trivial::severity_level
>
)

--- >8 ---

Then, in each file, I manually set the "Channel" attribute for the
logger, like so:

--- 8< ---

// Message.cpp

log::get().add_attribute(
    "Channel",
    boost::log::attributes::make_constant("Message"));

--- >8 ---

This works, but as the "log" object seems to be stored globally, I would
have to "invent" new names for the logger object in each file. This is
both insecure and prevents the usage of macros (I think).

So here's the question:

What would be the recommended / a convenient way to add a per-file
constant channel to log messages?

Thanks in advance!

Timo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net