3

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

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

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

4

2 回答 2

3

它不是那样工作的。您需要根据文档为 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 >= 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;
}
于 2016-12-14T13:55:36.173 回答
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;
 }
于 2018-09-05T00:15:23.377 回答