它不是那样工作的。您需要根据文档为 boost::log::trivial 提供过滤功能:
http://www.boost.org/doc/libs/1_61_0/libs/log/doc/html/log/tutorial/trivial_filtering.html
void init()
{
logging::core::get()->set_filter
(
// here they've used a constant but you could use a global or
// a function
logging::trivial::severity >= logging::trivial::info
);
}
int main(int, char*[])
{
init();
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
return 0;
}
传递给的对象logging::core::set_filter
是类型boost::log::filter
你可以很容易地写:
auto filt = logging::filter(logging::trivial::severity >= logging::trivial::info);
logging::core::get()->set_filter(filt);
filt
是一个轻量级函数对象,其工作是检查发送给它的属性并返回是否所有针对这些属性的测试都返回true
。在这种情况下,只有一个测试 - logging::trivial::severity >= logging::trivial::info
。
记录器的工作是构造属性集并将其传递给boost::log::core
它想要发出的东西。
总而言之,您必须在自己的变量中跟踪日志记录级别。这是一种方法:
#include <iostream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
namespace logging = boost::log;
int main(int, char*[])
{
// track your own variable here
logging::trivial::severity_level my_log_level = logging::trivial::trace;
// with this filter
auto filt = logging::filter(logging::trivial::severity >= my_log_level);
logging::core::get()->set_filter(filt);
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
// now you have control
if (my_log_level <= logging::trivial::trace)
{
std::cout << "tracing\n";
}
return 0;
}