Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check boost::log filter explicitly?

I have some trivial logging:

BOOST_LOG_TRIVIAL(trace) << make_trace_record();

Now make_trace_record is a somewhat expensive function to call (don't ask why, it's complicated). I want to call it only if the log currently passes filtering. How can I do that? I don't see a way to call the severity filter explicitly.

like image 209
n. 1.8e9-where's-my-share m. Avatar asked May 15 '18 06:05

n. 1.8e9-where's-my-share m.


2 Answers

Acorn's answer correctly points out that Boost.Log macros already implement conditional execution of the streaming expression. This behavior is documented in the Tutorial.

I will add that you can generate log records manually, avoiding the macros. An example is given here:

logging::record rec = lg.open_record();
if (rec)
{
    logging::record_ostream strm(rec);
    strm << "Hello, World!";
    strm.flush();
    lg.push_record(boost::move(rec));
}

This can be useful if log message formatting is complicated and doesn't fit easily in a streaming expression.

like image 166
Andrey Semashev Avatar answered Nov 13 '22 15:11

Andrey Semashev


Boost.Log filters beforehand; therefore, make_trace_record() will not be called if the severity is not high enough.

In order to set the severity filter for the trivial logger, call:

boost::log::core::get()->set_filter(
    boost::log::trivial::severity >= boost::log::trivial::...
);

For instance, the following example outputs 1, showing that expensive() is only called once:

Live On Coliru

#include <iostream>

#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>

int count = 0;

int expensive()
{
    return ++count;
}

int main()
{
    boost::log::core::get()->set_filter(
        boost::log::trivial::severity >= boost::log::trivial::warning
    );

    BOOST_LOG_TRIVIAL(error) << expensive();
    BOOST_LOG_TRIVIAL(info) << expensive();

    std::cout << count << '\n';

    return 0;
}

Prints:

[2018-05-21 14:33:47.327507] [0x00007eff37aa1740] [error]   1
1

For those wondering how it works, take a look to: How does the "lazy evaluation" of Boost Log's trivial loggers work?

like image 4
Acorn Avatar answered Nov 13 '22 17:11

Acorn