[Follows up Check boost::log filter explicitly? ]
The following example uses the trivial logger from Boost Log. It outputs 1
, showing that expensive()
is only called once. How does it work? Why is expensive()
not called?
#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;
}
Output:
[2018-05-21 14:33:47.327507] [0x00007eff37aa1740] [error] 1
1
BOOST_LOG_TRIVIAL
(also love that you did what I never get around to) – Bothersome