【问题标题】:How to query boost::log severity?如何查询 boost::log 严重性?
【发布时间】:2017-04-29 21:00:39
【问题描述】:

我正在使用boost::log 库的简单日志记录,我想根据当前设置的记录器严重性执行一些代码。仅当将输出日志消息时,才需要构建日志消息。但我找不到查询严重性的正确方法。代码必须是这样的:

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

另外可能有什么办法,当我知道消息会被输出时,避免重复检查严重性,直接输出而不是使用BOOST_LOG_TRIVIAL宏?

【问题讨论】:

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


    【解决方案1】:

    它不是那样工作的。您需要根据文档为 boost::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 &gt;= 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;
    }
    

    【讨论】:

    • 这不能回答问题。一定有办法根据严重程度有条件地执行任意代码。
    • 将充实答案。不幸的是,你假设不正确
    • @bobeff 答案更新了如何跟踪您自己的日志记录级别的示例
    • “总而言之,您必须在自己的变量中跟踪日志记录级别。”这似乎是可行的解决方案。 10 倍。
    • 过滤器复制my_log_level,因此当您更改它时,过滤器仍将使用旧值。您可以使用boost::phoenix::ref(my_log_level) 使过滤器引用该变量,但您必须确保可以线程安全地访问该变量。
    【解决方案2】:

    这是一个选项,它添加了另一层日志记录宏,并将代码包装在提供给这些宏的 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;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 2012-07-04
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      相关资源
      最近更新 更多