I'm using trivial logging of boost::log library and I want to execute some code depending on currently set logger severity. This is needed for constructing of log message only if it will be outputted. But I cannot find the right way for querying severity. The code must be something like:
if (boost::log::trivial::severity <=
boost::log::trivial::severity_level::trace)
{
// construct log message
BOOST_LOG_TRIVIAL(trace) << message;
}
In addition may be there is some way when I know that the message will be outputted to avoid double checking of severity and output it directly instead of using BOOST_LOG_TRIVIAL macro?
It doesn't work like that. You need to provide a filter function to boost::log::trivial as per the documentation:
http://www.boost.org/doc/libs/1_61_0/libs/log/doc/html/log/tutorial/trivial_filtering.html
The object passed to
logging::core::set_filteris of typeboost::log::filterYou could just as easily write:
filtis a lightweight function object whose job is to inspect the attributes sent to it and return whether or not all tests against those attributes returntrue. In this case there is only one test -logging::trivial::severity >= logging::trivial::info.It is the job of the logger to construct the attribute set and pass it to
boost::log::corewhen it wants to emit something.The long and short of it is that you must track the logging level in your own variable. Here is one way: