如何查询boost::log严重性?
How to query boost::log severity?
我正在使用 boost::log 库的简单日志记录,我想根据当前设置的记录器严重性执行一些代码。只有在输出日志消息时才需要构建日志消息。但是我找不到查询严重性的正确方法。代码必须类似于:
if (boost::log::trivial::severity <=
boost::log::trivial::severity_level::trace)
{
// construct log message
BOOST_LOG_TRIVIAL(trace) << message;
}
此外,当我知道消息将被输出以避免双重检查严重性并直接输出它而不是使用 BOOST_LOG_TRIVIAL
宏时,可能有一些方法?
不是这样的。您需要根据文档向 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
是一个轻量级函数对象,它的工作是检查发送给它的属性和 return 是否所有测试都针对这些属性 return 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;
}
这是一个选项,它添加了另一层日志记录宏,并将要在提供给这些宏的 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;
}
我正在使用 boost::log 库的简单日志记录,我想根据当前设置的记录器严重性执行一些代码。只有在输出日志消息时才需要构建日志消息。但是我找不到查询严重性的正确方法。代码必须类似于:
if (boost::log::trivial::severity <=
boost::log::trivial::severity_level::trace)
{
// construct log message
BOOST_LOG_TRIVIAL(trace) << message;
}
此外,当我知道消息将被输出以避免双重检查严重性并直接输出它而不是使用 BOOST_LOG_TRIVIAL
宏时,可能有一些方法?
不是这样的。您需要根据文档向 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
是一个轻量级函数对象,它的工作是检查发送给它的属性和 return 是否所有测试都针对这些属性 return 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;
}
这是一个选项,它添加了另一层日志记录宏,并将要在提供给这些宏的 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;
}