如何查询boost :: log severity?

时间:2016-12-14 13:44:20

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

我使用boost::log库的简单日志记录,我想根据当前设置的记录器严重性执行一些代码。只有在输出日志消息时,才需要这样做。但我无法找到查询严重程度的正确方法。代码必须类似于:

if (boost::log::trivial::severity <=
    boost::log::trivial::severity_level::trace)
{
  // construct log message
  BOOST_LOG_TRIVIAL(trace) << message;
}

另外可能有一些方法,当我知道将输出消息以避免重复检查严重性并直接输出而不是使用BOOST_LOG_TRIVIAL宏?

2 个答案:

答案 0 :(得分:1)

它不起作用。您需要根据文档提供过滤函数来提升:: log :: trivial:

http://www.boost.org/doc/libs/1_61_0/libs/log/doc/html/log/tutorial/trivial_filtering.html

void init()
{
    logging::core::get()->set_filter
    (
        // here they've used a constant but you could use a global or
        // a function
        logging::trivial::severity >= logging::trivial::info
    );
}

int main(int, char*[])
{
    init();

    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    return 0;
}

传递给logging::core::set_filter的对象属于boost::log::filter

你可以轻松写下来:

auto filt = logging::filter(logging::trivial::severity >= logging::trivial::info);
logging::core::get()->set_filter(filt);

filt是一个轻量级的函数对象,其作用是检查发送给它的属性,并返回针对这些属性的所有测试是否返回true。在这种情况下,只有一个测试 - logging::trivial::severity >= logging::trivial::info

记录器的工作是构造属性集,并在想要发出内容时将其传递给boost::log::core

它的长短是你必须跟踪自己变量中的日志记录级别。这是一种方式:

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

namespace logging = boost::log;


int main(int, char*[])
{
    // track your own variable here
    logging::trivial::severity_level my_log_level = logging::trivial::trace;

    // with this filter
    auto filt = logging::filter(logging::trivial::severity >= my_log_level);
    logging::core::get()->set_filter(filt);

    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    // now you have control
    if (my_log_level <= logging::trivial::trace)
    {
        std::cout << "tracing\n";
    }


    return 0;
}

答案 1 :(得分:0)

这里是一个选项,它添加了另一层日志记录宏,并将要在有条件调用的代码包装到提供给这些宏的lambda函数中。仅在满足严重性级别约束的情况下,才能评估lambda函数。

据我所知,此方法是线程安全的,但是我对boost :: log的理解是...有限的。我尚未在多线程环境中进行测试。

 //g++ -std=gnu++11  -DBOOST_LOG_DYN_LINK main.cpp -lboost_log -lpthread -o lambda_demo                    

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

 namespace logging = boost::log;

 #define LOG_TRACE(ARG) BOOST_LOG_TRIVIAL(trace) << ARG;
 #define LOG_INFO(ARG) BOOST_LOG_TRIVIAL(info) << ARG;

 const std::string complicated_function(const std::string &message)
 {
     std::cout << "\nInside complicated_function.\n" << std::flush;
     return "Returning from complicated_function (" + message + ").\n";
 }

 int main(int, char*[])
 {
     std::cout << "\n" << complicated_function("called from std::cout") << "\n" << std::flush;

     BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
     BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
     BOOST_LOG_TRIVIAL(info) << "An informational severity message";
     BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
     BOOST_LOG_TRIVIAL(error) << "An error severity message";
     BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message\n";

     LOG_TRACE ("Coming up: a trace message with a lambda function:");

     LOG_TRACE (
         [=]() {
             return complicated_function("(called from LOG_TRACE");
         }()
         );

     logging::core::get()->set_filter
         (
             logging::trivial::severity >= logging::trivial::info
             );

     BOOST_LOG_TRIVIAL(trace) << "After setting filter, another trace severity message";
     BOOST_LOG_TRIVIAL(debug) << "After setting filter, another debug severity message";
     BOOST_LOG_TRIVIAL(info) << "After setting filter, another informational severity message";
     BOOST_LOG_TRIVIAL(warning) << "After setting filter, another warning severity message";
     BOOST_LOG_TRIVIAL(error) << "After setting filter, anothern error severity message";
     BOOST_LOG_TRIVIAL(fatal) << "After setting filter, another fatal severity message\n";

     LOG_TRACE ("Coming up: a trace message with a lambda function:");
     LOG_TRACE (
         [=]() {
             return complicated_function("called from LOG_TRACE");
         }()
         );

     LOG_INFO ("Coming up: an info message with a lambda function:");
     LOG_INFO (
         [=]() {
             return complicated_function("called from LOG_INFO");
         }()
         );

     return 0;
 }