我正在像这个例子一样创建新的接收器:
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%]: %Message%"
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
我已经实现了我自己的过滤器对象:
struct MyFilter {
...
bool operator()(const boost::log::attribute_value_set& attrs) const noexcept
{
bool result = ....
// Do my filtering
return result;
}
...
};
如何将它作为接收器初始化参数传递? 即我想添加以下参数:
keywords::filter = SOMETHING(MyFilter())
但到目前为止,我无法弄清楚“某事”应该是什么。找不到任何例子。你能帮帮我吗?
最佳答案
首先,keywords::format
用于传递格式化程序,而不是过滤器。对于过滤器,使用 keywords::filter
参数关键字。
其次,keywords::format
和keywords::filter
关键字目前只支持字符串参数。接受的字符串根据描述的语法解释 here分别作为格式化程序或过滤器。
如果你想将一个函数对象设置为过滤器,你应该用你的函数对象在创建的接收器上调用set_filter
。 add_file_log
返回一个指向创建的接收器的指针,因此您可以这样做:
auto sink = logging::add_file_log(...);
sink->set_filter(MyFilter());
同样适用于格式化程序;格式化接收器为此提供了 set_formatter
方法。
关于c++ - 初始化 boost 日志接收器时将函数对象作为过滤器传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54287556/