欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

boost log库使用二

程序员文章站 2022-03-03 19:20:43
...
               

这次,尝试filter,首先代码来自于官方文档。

#include <iostream>#include <boost/log/core.hpp>#include <boost/log/trivial.hpp>#include <boost/log/expressions.hpp>namespace logging = boost::log;using namespace std;void SetFilter() {  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);}int main () cout << "hello, world" << endl;  SetFilter();  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";}

当运行程序后,输出结果变了,trac和debug级别的信息被过滤掉了。

$ ./mainhello, world[2013-12-03 15:34:47.343223] [0x000007f680e76774] [info]    An informational severity message[2013-12-03 15:34:47.343324] [0x000007f680e76774] [warning] A warning severity message[2013-12-03 15:34:47.343341] [0x000007f680e76774] [error]   An error severity message[2013-12-03 15:34:47.343356] [0x000007f680e76774] [fatal]   A fatal severity message
但是如果我们在运行时改变这个filter的设置,会怎样?现在试试看,修改一下代码:

#include <iostream>#include <boost/log/core.hpp>#include <boost/log/trivial.hpp>#include <boost/log/expressions.hpp>namespace logging = boost::log;using namespace std;void SetFilter1() {  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);}void SetFilter2() {  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);}int main () cout << "hello, world" << endl;  SetFilter1();  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";   cout << "--------------------" << endl;  SetFilter2();  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";}
好,现在观察结果。

$ ./mainhello, world[2013-12-03 15:37:54.399513] [0x000007fd7709a374] [info]    An informational severity message[2013-12-03 15:37:54.399612] [0x000007fd7709a374] [warning] A warning severity message[2013-12-03 15:37:54.399630] [0x000007fd7709a374] [error]   An error severity message[2013-12-03 15:37:54.399644] [0x000007fd7709a374] [fatal]   A fatal severity message--------------------[2013-12-03 15:37:54.399666] [0x000007fd7709a374] [debug]   A debug severity message[2013-12-03 15:37:54.399680] [0x000007fd7709a374] [info]    An informational severity message[2013-12-03 15:37:54.399693] [0x000007fd7709a374] [warning] A warning severity message[2013-12-03 15:37:54.399706] [0x000007fd7709a374] [error]   An error severity message[2013-12-03 15:37:54.399719] [0x000007fd7709a374] [fatal]   A fatal severity message

起作用了。也就是说我们可以在不重新部署程序的情况下修改日志的过滤级别。在我的TCP服务中,可以监听一个管理端口,专门用来接受用于改变过滤级别的特殊消息。

非常好的特性,我喜欢这个功能。



           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow