Boost logo

Boost :

Subject: Re: [boost] Boost log issues and workarounds
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2019-01-18 08:57:43


On 1/18/19 6:45 AM, hh h wrote:
> Hi Andrey,
>
>> Those are just examples in the docs. These particular examples don't
>> exist as standalone files. You can find global loggers used in a number
>> of other examples in the libs/log/example directory.
>
> The global loggers is working well, but I could not compile the local
> log severity, here is the code:
>
> BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(my_logger,
> boost::log::sources::channel_logger_mt< >,
> (boost::log::keywords::channel = "general"))

If you want a severity level attribute, that should be
severity_channel_logger_mt< ServerityLevel_t >.

> typedef enum {
> INFO,
> DEBUG,
> WARNING,
> ERROR,
> CRITICAL
> } ServerityLevel_t;
>
> template<typename CharT, typename TraitsT> std::basic_ostream< CharT,
> TraitsT >& operator << (std::basic_ostream< CharT, TraitsT >& strm,
> ServerityLevel_t lvl) {
> static const char* const str[] = {
> "INFO",
> "DEBUG",
> "WARNING",
> "ERROR",
> "CRITICAL"
> };
> if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str))) {
> strm << str[lvl];
> } else {
> strm << static_cast< int >(lvl);
> }
> return strm;
> }
>
> #define LOG(severity) BOOST_LOG_STREAM_WITH_PARAMS((my_logger::get()),
> (SetGetAttrib("FileName",
> PathToFilename(__FILE__)))(SetGetAttrib("LineNumber", (unsigned
> int)__LINE__))(ServerityLevel_t::severity))
>
> /usr/include/boost/log/sources/channel_feature.hpp:171:60: error: no
> match for ‘operator[]’ (operand types are ‘const ServerityLevel_t’ and
> ‘boost::parameter::aux::default_<boost::log::v2_mt_posix::keywords::tag::channel,
> const boost::parameter::void_>’)
> return open_record_with_channel_unlocked(args,
> args[keywords::channel | parameter::void_()]);

I'm not sure what SetGetAttrib is, but BOOST_LOG_STREAM_WITH_PARAMS
assumes the parameters are Boost.Parameter named parameters. I.e. to
pass a severity level you need to say:

(::boost::log::keywords::severity = ServerityLevel_t::severity)

and to pass a channel name:

(::boost::log::keywords::channel = (chan))

If you want to use named parameters to add FileName and LineNumber
attributes, you will have to write your own logger feature and add it to
the severity level and channel features, as described here:

https://www.boost.org/doc/libs/1_69_0/libs/log/doc/html/log/extension/sources.html

If you don't need to apply filters to these attributes, you can just use
add_value manipulator in your macro:

https://www.boost.org/doc/libs/1_69_0/libs/log/doc/html/log/detailed/utilities.html#log.detailed.utilities.manipulators.add_value

That will work without having to develop a new logger feature.


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