【问题标题】:Passing functon object as filter when initializing boost log sink初始化提升日志接收器时将函数对象作为过滤器传递
【发布时间】:2019-01-21 10:06:22
【问题描述】:

我正在创建新的接收器,如下例所示:

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())

但到目前为止,我无法弄清楚“某事”应该是什么。找不到任何例子。你能帮帮我吗?

【问题讨论】:

    标签: c++ boost boost-log boost-logging


    【解决方案1】:

    首先,keywords::format 用于传递格式化程序,而不是过滤器。对于过滤器,请使用 keywords::filter 参数关键字。

    其次,keywords::formatkeywords::filter 关键字目前都只支持字符串参数。接受的字符串根据here 描述的语法分别解释为格式化程序或过滤器。

    如果你想将一个函数对象设置为过滤器,你应该使用你的函数对象在创建的接收器上调用set_filteradd_file_log 返回一个指向已创建接收器的指针,因此您可以这样做:

    auto sink = logging::add_file_log(...);
    sink->set_filter(MyFilter());
    

    同样适用于格式化程序;格式化接收器为此提供了set_formatter 方法。

    【讨论】:

    • 哎呀,写错了标签。应该是 coruse 的过滤器,修复它。
    • 谢谢你的解释,安德烈!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多