c++ - 初始化 boost 日志接收器时将函数对象作为过滤器传递

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

我正在像这个例子一样创建新的接收器:

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::formatkeywords::filter 关键字目前只支持字符串参数。接受的字符串根据描述的语法解释 here分别作为格式化程序或过滤器。

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

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

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

关于c++ - 初始化 boost 日志接收器时将函数对象作为过滤器传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54287556/

相关文章:

c++ - 基类析构函数 C++ 中的段错误

c++ - 在第一个 BST 上插入非空函数错误的结尾?

java - 既不是句法也不是语义的错误?

c++ - 使用 Visual Studio 编译代码时,boost::iostreams::multichar_input_filter 中的异常消失

c++ - 有没有办法用 Boost.Log 实现多个独立的日志系统?

c++ - Conan + CMake + C++ : Linking against Boost. 记录静态库失败

c++ - 根据其他变量设置变量

c++ - 如何使 boost::log::keywords::file_name 使用 UTC 时间?

c++ - Boost::日志和日志编号

c++ - boost::ptr_vector 成员函数列表