boost的log使用
Posted zhanghuan_wangkai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了boost的log使用相关的知识,希望对你有一定的参考价值。
以前使用log4cpp,boost增加了log库改用了boost的, 其实在用法上差不了太多
log影响性能最基本的有2个地方 一个是console显示,一个是auto_flush不通过缓存直接写入
1 #include "stdafx.h" 2 #include "SLog.h" 3 4 #include <stdexcept> 5 #include <iostream> 6 #include <boost/smart_ptr.hpp> 7 8 #include <boost/log/common.hpp> 9 #include <boost/log/expressions.hpp> 10 #include <boost/log/utility/setup/file.hpp> 11 #include <boost/log/utility/setup/console.hpp> 12 #include <boost/log/utility/setup/common_attributes.hpp> 13 #include <boost/log/attributes/timer.hpp> 14 #include <boost/log/attributes/named_scope.hpp> 15 #include <boost/log/sources/logger.hpp> 16 #include <boost/log/support/date_time.hpp> 17 18 namespace logging = boost::log; 19 namespace attrs = boost::log::attributes; 20 namespace src = boost::log::sources; 21 namespace sinks = boost::log::sinks; 22 namespace expr = boost::log::expressions; 23 namespace keywords = boost::log::keywords; 24 25 using namespace boost; 26 27 boost::log::sources::severity_logger< severity_levels > SLog::slg; 28 29 SLog::SLog(void) 30 31 32 33 34 SLog::~SLog(void) 35 36 37 38 template< typename CharT, typename TraitsT > 39 inline std::basic_ostream< CharT, TraitsT >& operator<< ( 40 std::basic_ostream< CharT, TraitsT >& strm, severity_levels lvl) 41 42 static const char* const str[] = 43 44 "emergency", 45 "alert", 46 "critical", 47 "error", 48 "warning", 49 "notice", 50 "info", 51 "debug" 52 ; 53 if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str))) 54 strm << str[lvl]; 55 else 56 strm << static_cast< int >(lvl); 57 return strm; 58 ; 59 60 BOOST_LOG_ATTRIBUTE_KEYWORD(_severity, "Severity", severity_levels); 61 BOOST_LOG_ATTRIBUTE_KEYWORD(_timestamp, "TimeStamp", boost::posix_time::ptime); 62 BOOST_LOG_ATTRIBUTE_KEYWORD(_uptime, "Uptime", attrs::timer::value_type); 63 BOOST_LOG_ATTRIBUTE_KEYWORD(_scope, "Scope", attrs::named_scope::value_type); 64 65 void SLog::InitLog(const std::string& filename) 66 67 auto asink = logging::add_console_log(std::clog, keywords::format = expr::stream 68 << expr::format_date_time(_timestamp, "[%Y-%m-%d,%H:%M:%S.%f]") 69 << " <" << _severity 70 << ">: " << expr::message); 71 72 asink->set_filter(expr::attr< severity_levels >("Severity") <= slog_error); 73 74 logging::core::get()->add_sink(asink); 75 76 for (int i = slog_emergency; i<=slog_debug;i++) 77 78 std::stringstream allfilename; 79 allfilename << "logs\\\\" << filename << "_"<<(severity_levels)i << "_%Y%m%d_%H%M%S.log"; 80 auto sink = logging::add_file_log( 81 keywords::file_name = allfilename.str(), 82 keywords::rotation_size = 10*1024*1024, 83 keywords::time_based_rotation=sinks::file::rotation_at_time_point(0,0,0), 84 keywords::open_mode = std::ios::app, 85 keywords::auto_flush = false 86 ); 87 88 89 sink->locked_backend()->set_file_collector(sinks::file::make_collector( 90 keywords::target = "logs", 91 keywords::max_size = 30 * 1024 * 1024, 92 keywords::min_free_space = 100 * 1024 * 1024 93 )); 94 95 sink->locked_backend()->scan_for_files(); 96 97 if(i<=slog_error) 98 99 sink->locked_backend()->auto_flush(); 100 101 102 sink->set_filter(expr::attr< severity_levels >("Severity") == (severity_levels)i); 103 104 sink->set_formatter 105 ( 106 expr::format("(%1%)<%2%_%3%>%4%: %5%") 107 % expr::attr< attrs::current_thread_id::value_type >("ThreadID") 108 % expr::format_date_time(_timestamp, "%Y-%m-%d,%H:%M:%S.%f") 109 % expr::attr< boost::posix_time::time_duration >("Uptime") 110 % expr::format_named_scope(_scope, keywords::format = "%n[%f:%l]", keywords::depth = 1) 111 % expr::smessage 112 ); 113 114 logging::core::get()->add_sink(sink); 115 116 117 logging::add_common_attributes(); 118 logging::core::get()->add_thread_attribute("Scope", attrs::named_scope()); 119 logging::core::get()->add_thread_attribute("Uptime", attrs::timer()); 120 121 122 void SLog::SetLevel(severity_levels sl) 123 124 logging::core::get()->set_filter(expr::attr< severity_levels >("Severity") <= sl); 125
1 #pragma once 2 #include <string> 3 4 #include <boost/log/sources/logger.hpp> 5 6 enum severity_levels 7 8 slog_emergency = 0, //slog_emerg 9 slog_alert = 1, //slog_alert 10 slog_critical = 2, //slog_crit 11 slog_error = 3, //slog_error 大于slog_error的log不会在console显示 并且不会马上写入文件 12 slog_warning = 4, //slog_warning 13 slog_notice = 5, //slog_notice 14 slog_info = 6, //slog_info 15 slog_debug = 7 //slog_debug 16 ; 17 18 class SLog 19 20 public: 21 SLog(void); 22 ~SLog(void); 23 24 static boost::log::sources::severity_logger< severity_levels > slg; 25 26 static void InitLog(const std::string& filename); 27 28 static void SetLevel(severity_levels sl); 29 ; 30 31 #define BOOST_SLOG(slog_lvl) BOOST_LOG_FUNCTION();BOOST_LOG_SEV(SLog::slg, slog_lvl) 32 33 #define SLOG_EMERGENCY BOOST_SLOG(slog_emergency) 34 #define SLOG_ALERT BOOST_SLOG(slog_alert) 35 #define SLOG_CRITICAL BOOST_SLOG(slog_critical) 36 #define SLOG_ERROR BOOST_SLOG(slog_error) 37 #define SLOG_WARNING BOOST_SLOG(slog_warning) 38 #define SLOG_NOTICE BOOST_SLOG(slog_notice) 39 #define SLOG_INFO BOOST_SLOG(slog_info) 40 #define SLOG_DEBUG BOOST_SLOG(slog_debug)
1 // log_test.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "SLog.h" 6 #include <boost/timer.hpp> 7 8 void test2() 9 10 BOOST_SLOG( slog_notice ) << "A normal severity message, test2"; 11 12 13 void test1() 14 15 BOOST_SLOG( slog_notice ) << "A normal severity message, test1"; 16 test2(); 17 18 19 int _tmain(int argc, _TCHAR* argv[]) 20 21 SLog::InitLog("testapp"); 22 23 //SLog::SetLevel(slog_notice); 24 25 SLOG_ALERT << "A normal severity message, will not pass to the file"; 26 27 SLOG_WARNING << "A warning severity message, will pass to the file"; 28 29 SLOG_NOTICE << "An error severity message, will pass to the file"; 30 31 //printf("start:\\n"); 32 test1(); 33 //SLog::SetLevel(slog_emergency); 34 int _count = 10000; 35 boost::timer tt; 36 for (int i =0 ;i<_count;i++) 37 38 SLOG_INFO << "An slog_warning severity message, count:"<<i; 39 40 printf("count:%d time:%lf(s)\\n",_count, tt.elapsed()); 41 42 getchar(); 43 return 0; 44
以上是关于boost的log使用的主要内容,如果未能解决你的问题,请参考以下文章