boost log库

来源:互联网 发布:python init 编辑:程序博客网 时间:2024/06/05 10:10

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

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <boost/log/core.hpp>  
  3. #include <boost/log/trivial.hpp>  
  4. #include <boost/log/expressions.hpp>  
  5. namespace logging = boost::log;  
  6. using namespace std;  
  7. void SetFilter() {  
  8.   logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);  
  9. }  
  10. int main () {  
  11.   cout << "hello, world" << endl;  
  12.   SetFilter();  
  13.   BOOST_LOG_TRIVIAL(trace) << "A trace severity message";  
  14.   BOOST_LOG_TRIVIAL(debug) << "A debug severity message";  
  15.   BOOST_LOG_TRIVIAL(info) << "An informational severity message";  
  16.   BOOST_LOG_TRIVIAL(warning) << "A warning severity message";  
  17.   BOOST_LOG_TRIVIAL(error) << "An error severity message";  
  18.   BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";  
  19. }  

当运行程序后,输出结果变了,trac和debug级别的信息被过滤掉了。
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ ./main  
  2. hello, world  
  3. [2013-12-03 15:34:47.343223] [0x000007f680e76774] [info]    An informational severity message  
  4. [2013-12-03 15:34:47.343324] [0x000007f680e76774] [warning] A warning severity message  
  5. [2013-12-03 15:34:47.343341] [0x000007f680e76774] [error]   An error severity message  
  6. [2013-12-03 15:34:47.343356] [0x000007f680e76774] [fatal]   A fatal severity message  
但是如果我们在运行时改变这个filter的设置,会怎样?现在试试看,修改一下代码:
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <boost/log/core.hpp>  
  3. #include <boost/log/trivial.hpp>  
  4. #include <boost/log/expressions.hpp>  
  5. namespace logging = boost::log;  
  6. using namespace std;  
  7. void SetFilter1() {  
  8.   logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);  
  9. }  
  10. void SetFilter2() {  
  11.   logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);  
  12. }  
  13. int main () {  
  14.   cout << "hello, world" << endl;  
  15.   SetFilter1();  
  16.   BOOST_LOG_TRIVIAL(trace) << "A trace severity message";  
  17.   BOOST_LOG_TRIVIAL(debug) << "A debug severity message";  
  18.   BOOST_LOG_TRIVIAL(info) << "An informational severity message";  
  19.   BOOST_LOG_TRIVIAL(warning) << "A warning severity message";  
  20.   BOOST_LOG_TRIVIAL(error) << "An error severity message";  
  21.   BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";  
  22.    
  23.   cout << "--------------------" << endl;  
  24.   SetFilter2();  
  25.   BOOST_LOG_TRIVIAL(trace) << "A trace severity message";  
  26.   BOOST_LOG_TRIVIAL(debug) << "A debug severity message";  
  27.   BOOST_LOG_TRIVIAL(info) << "An informational severity message";  
  28.   BOOST_LOG_TRIVIAL(warning) << "A warning severity message";  
  29.   BOOST_LOG_TRIVIAL(error) << "An error severity message";  
  30.   BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";  
  31. }  
好,现在观察结果。
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ ./main  
  2. hello, world  
  3. [2013-12-03 15:37:54.399513] [0x000007fd7709a374] [info]    An informational severity message  
  4. [2013-12-03 15:37:54.399612] [0x000007fd7709a374] [warning] A warning severity message  
  5. [2013-12-03 15:37:54.399630] [0x000007fd7709a374] [error]   An error severity message  
  6. [2013-12-03 15:37:54.399644] [0x000007fd7709a374] [fatal]   A fatal severity message  
  7. --------------------  
  8. [2013-12-03 15:37:54.399666] [0x000007fd7709a374] [debug]   A debug severity message  
  9. [2013-12-03 15:37:54.399680] [0x000007fd7709a374] [info]    An informational severity message  
  10. [2013-12-03 15:37:54.399693] [0x000007fd7709a374] [warning] A warning severity message  
  11. [2013-12-03 15:37:54.399706] [0x000007fd7709a374] [error]   An error severity message  
  12. [2013-12-03 15:37:54.399719] [0x000007fd7709a374] [fatal]   A fatal severity message  

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