检查boost :: log过滤器? [英] Check boost::log filter explicitly?

查看:76
本文介绍了检查boost :: log过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些琐碎的日志记录:

I have some trivial logging:

BOOST_LOG_TRIVIAL(trace) << make_trace_record();

现在 make_trace_record 对于电话(不要问为什么,这很复杂)。我只想在日志当前通过过滤时才调用它。我怎样才能做到这一点?我没有看到明确调用严重性过滤器的方法。

Now make_trace_record is a somewhat expensive function to call (don't ask why, it's complicated). I want to call it only if the log currently passes filtering. How can I do that? I don't see a way to call the severity filter explicitly.

推荐答案

Boost.Log过滤器已经过;因此,如果严重性不够高,将不会调用 make_trace_record()

Boost.Log filters beforehand; therefore, make_trace_record() will not be called if the severity is not high enough.

琐碎记录器的严重性过滤器,请调用:

In order to set the severity filter for the trivial logger, call:

boost::log::core::get()->set_filter(
    boost::log::trivial::severity >= boost::log::trivial::...
);

例如,以下示例输出 1 ,显示 expensive()仅被调用一次:

For instance, the following example outputs 1, showing that expensive() is only called once:

在Coliru上直播

#include <iostream>

#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>

int count = 0;

int expensive()
{
    return ++count;
}

int main()
{
    boost::log::core::get()->set_filter(
        boost::log::trivial::severity >= boost::log::trivial::warning
    );

    BOOST_LOG_TRIVIAL(error) << expensive();
    BOOST_LOG_TRIVIAL(info) << expensive();

    std::cout << count << '\n';

    return 0;
}

打印:

[2018-05-21 14:33:47.327507] [0x00007eff37aa1740] [error]   1
1

对于那些想知道它如何工作的人,请看看:

For those wondering how it works, take a look to: How does the "lazy evaluation" of Boost Log's trivial loggers work?

这篇关于检查boost :: log过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆