配置 Boost.Log v2

Configure Boost.Log v2

我正在使用 Boost.Log V2 1.75.0 并使用 boost::log::init_from_stream(config);

从配置文件设置我的环境

是否可以通过配置文件声明彩色控制台接收器,或者是否有任何方法可以将彩色输出添加到控制台?

现在,我在 Boost Trivial Logger 中添加尽可能多的控制台接收器,并按不同级别过滤它们,但我认为这不是正确的方法。示例:

[Sinks.ConsoleSinkTrace]
Destination=Console
Filter="%Severity% = trace"
Format="3[0;35m[%TimeStamp%] [%ProcessId%] [%ThreadId%] [%Severity%] - %Message%3[0m"
Asynchronous=false
AutoFlush=true

[Sinks.ConsoleSinkDebug]
Destination=Console
Filter="%Severity% = debug"
Format="3[0;34m[%TimeStamp%] [%ProcessId%] [%ThreadId%] [%Severity%] - %Message%3[0m"
Asynchronous=false
AutoFlush=true

...等等...

更新

我找到了@AndreySemashev 建议的 SO post,但我不太明白,如何将它融入我的项目: 我的主要期望是我想通过文件配置 Boost.Log,所以:

  1. 如果我从配置文件中完全删除控制台接收器,如何将格式化程序设置到接收器? (我猜 sink 是一个控制台类型的接收器,如果配置文件中没有这样的部分,它将不会被实例化)

sink->set_formatter(&coloring_formatter);

  1. 如果我提供一个新的格式化程序方法,我假设处理配置文件中给出的整个格式字符串是我的责任。我也想避免这种情况

这些观察是否正确,还是我遗漏了什么?

谢谢

中描述了创建具有着色支持的格式化程序,这里我将重点介绍如何将该格式化程序合并到设置文件中。

Boost.Log 支持通过 registering sink factories, which you can leverage in your case. Since you can reuse the text_ostream_backend 扩展其设置文件解析器以进行输出,您不必实施新的接收器,但您需要自定义其配置。

class colored_console_factory :
    public logging::sink_factory< char >
{
public:
    // Creates the sink with the provided parameters
    boost::shared_ptr< sinks::sink > create_sink(settings_section const& settings)
    {
        typedef sinks::text_ostream_backend backend_t;
        auto backend = boost::make_shared< backend_t >();

        backend->add_stream(
            boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));

        // Read settings and configure the backend accordingly
        if (auto auto_flush = settings["AutoFlush"])
            backend->auto_flush(*auto_flush == "1" || *auto_flush == "true");

        auto sink =
            boost::make_shared< sinks::synchronous_sink< backend_t > >(backend);

        // You can reuse filter and formatter parsers provided by Boost.Log
        if (auto filter = settings["Filter"])
            sink->set_filter(logging::parse_filter(*filter));

        if (auto format = settings["Format"])
        {
            logging::formatter fmt = logging::parse_formatter(*format);
            // Wrap the parsed formatter with coloring
            sink->set_formatter(coloring_formatter(std::move(fmt)));
        }

        return sink;
    }
};

在上面,coloring_formatter 是一个函数对象,它在输出中用着色前缀和后缀包装已解析的格式化程序。函数对象必须具有格式化程序的标准签名。

class coloring_formatter
{
public:
    typedef void result_type;
    typedef basic_formatting_ostream< char > stream_type;

public:
    explicit coloring_formatter(logging::formatter&& fmt) :
        m_fmt(std::move(fmt))
    {}

    result_type operator() (
        logging::record_view const& rec, stream_type& strm) const
    {
        // Output coloring prefix
        auto severity = rec[logging::trivial::severity];
        if (severity)
        {
            switch (severity.get())
            {
                ...
            }
        }

        // Let the wrapped formatter produce its output
        m_fmt(rec, strm);

        // Output coloring suffix
        if (severity)
        {
            ...
        }
    }

private:
    logging::formatter m_fmt;
};

coloring_formatter具体实现请参考我之前的回答