Boost logo

Boost :

Subject: Re: [boost] Boost log issues and workarounds
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2019-01-17 10:29:49

On 1/17/19 11:33 AM, hh h via Boost wrote:
> Hi,
> I like the boost log, but there are many small issues, appreciate your
> kind help:
> I followed the following link to use a good macro LOG:
> But calling LOG(error) caused much trouble for the name space
> collision, alternative is to use full namespace
> boost::log::trivial::error, but that is too cumbersome,I tried to use
> an uppercase ERROR to replace it in an enum ERROR =
> boost::log::trivial::error, obviously that could not be complied,
> although it is defined as enum severity_level in trivial.hpp, it does
> not look like the boost::log::trivial::error is an enum value. Any
> workarounds?

I'm not sure I understand the problem correctly, it would be easier if
you described it in more detail, preferably with code samples.

 From what I gathered, you want a macro `LOG(severity)`, where
`severity` is a value from an enum defined by you. First, Boost.Log
requires a logger (or, generally speaking, a logging source) to emit log
records. You can create either local or global loggers, but since you
don't want a logger to be specified in your macro, I'm assuming you want
a global logger. See here:

Given a global logger named `my_logger`, your macro could be defined
like this:

#define LOG(severity) BOOST_LOG_SEV(my_logger::get(), severity)

Note that the library provides `BOOST_LOG_TRIVIAL` macro which doesn't
require a logger. In this case, a global logger defined by Boost.Log is
used. That logger uses `boost::log::trivial::severity_level` enum for
its severity levels.

Regarding your severity level enum, you have to specify it in template
parameters of the logger you use. In the logging macro, you can add any
namespaces before the enum value so that the macro expansion context is
irrelevant. For example:

namespace my_namespace {
enum my_severity

#define LOG(severity) \
   BOOST_LOG_SEV(my_logger::get(), ::my_namespace::my_severity::severity)

LOG(error) << "Something failed";

> Another issue from that link is it has to call LOGGABLE prior to call
> LOG(error) in each function, that makes more clumsy, software
> developers in my team are not happy about it. Any workaround to have a
> fixed argument in BOOST_LOG_STREAM_WITH_PARAMS, without need to claim
> boost::log::sources::severity_logger<boost::log::trivial::severity_level>
> slg; in each function? You guys are genius that must be a way to do
> it, appreciate your advice.

I would say, `LOGGABLE` from one of the linked answers is a library
misuse. If you don't have a context associated with the logged activity,
you should be using a global logger. Otherwise, you should have a logger
specific to the context, e.g. as a class member.

Boost list run by bdawes at, gregod at, cpdaniel at, john at