Boost log 简单使用

该示例是在VC2010 MFC Unicode环境下使用


内容包括

1 启动关闭日志

2 设置日志存储路径

3 设置输出日志等级

4 日志是否立即写入文件

5 设置单个文件的大小

6 设置磁盘最小可利用空间

Logger.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma once  
  2.   
  3. #include <cassert>  
  4. #include <iostream>  
  5. #include <fstream>  
  6. #include <boost/locale/generator.hpp>  
  7. #include <boost/date_time/posix_time/posix_time_types.hpp>  
  8.   
  9. #include <boost/log/common.hpp>  
  10. #include <boost/log/expressions.hpp>  
  11. #include <boost/log/utility/setup/file.hpp>  
  12. #include <boost/log/utility/setup/console.hpp>  
  13. #include <boost/log/utility/setup/common_attributes.hpp>  
  14. #include <boost/log/sources/logger.hpp>  
  15. #include <boost/log/support/date_time.hpp>  
  16.   
  17. #include <boost/filesystem.hpp>   
  18. #include <boost/log/detail/thread_id.hpp>   
  19. #include <boost/log/sources/global_logger_storage.hpp>  
  20. namespace logging = boost::log;  
  21. namespace sinks = boost::log::sinks;  
  22. namespace attrs = boost::log::attributes;  
  23. namespace src = boost::log::sources;  
  24. namespace expr = boost::log::expressions;  
  25. namespace keywords = boost::log::keywords;  
  26.   
  27.   
  28. enum severity_level  
  29. {  
  30.     trace,  
  31.     warning,  
  32.     error  
  33.   
  34. };  
  35.   
  36. templatetypename CharT, typename TraitsT >  
  37. inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)  
  38. {  
  39.     static const charconst str[] =  
  40.     {  
  41.         "trace",  
  42.         "warning",  
  43.         "error"  
  44.     };  
  45.     if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))  
  46.         strm << str[lvl];  
  47.     else  
  48.         strm << static_castint >(lvl);  
  49.     return strm;  
  50. }  
  51.   
  52. BOOST_LOG_ATTRIBUTE_KEYWORD(_severity,  "Severity",  severity_level)  
  53. BOOST_LOG_ATTRIBUTE_KEYWORD(_timestamp, "TimeStamp", boost::posix_time::ptime)  
  54.   
  55.   
  56.   
  57. class CLogger  
  58. {  
  59. public:  
  60.     CLogger(void);  
  61.     ~CLogger(void);  
  62.     static void Init();  
  63.     static void Start();  
  64.     static void Stop();  
  65.   
  66.   
  67.     //日志严重等级过滤输出  
  68.     static void SetFilterTrace();  
  69.     static void SetFilterWarning();  
  70.     static void SetFilterError();  
  71.   
  72.   
  73.     void SetLogFilePath(std::wstring strPath);  
  74.     void SetMinFreeSpace(size_t size);  
  75.     void SetRotationsize(size_t size);  
  76.   
  77.   
  78. private:  
  79.     static size_t m_szMinFreeSpace;  
  80.     static size_t m_szRotationSize;  
  81.     static std::wstring m_strFilePath;  
  82.     static bool m_bAutoFlush;  
  83.   
  84. };  

Logger.cpp

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "StdAfx.h"  
  2. #include "Logger.h"  
  3. #include <iostream>  
  4.   
  5.   
  6.  size_t CLogger::m_szMinFreeSpace=10*1024*1024;  
  7.  size_t CLogger::m_szRotationSize=1*1024*1024;  
  8.  std::wstring CLogger::m_strFilePath=_T("C:\\Log\\");  
  9.  bool CLogger::m_bAutoFlush=true;;  
  10.   
  11. CLogger::CLogger(void)  
  12. {  
  13.   
  14. }  
  15.   
  16. CLogger::~CLogger(void)  
  17. {  
  18. }  
  19. void CLogger::Init()  
  20. {  
  21.     
  22.   std::locale::global(std::locale("chs"));    
  23.   
  24.     auto pSink = logging::add_file_log  
  25.         (  
  26.         keywords::open_mode = std::ios::app,//追加写入  
  27.         keywords::file_name =m_strFilePath+_T("%Y-%m-%d.log"),  
  28.         keywords::rotation_size = m_szRotationSize,  
  29.         keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),   
  30.         keywords::min_free_space=m_szMinFreeSpace,  
  31.   
  32.         keywords::format =(  
  33.         expr::stream  
  34.         <<  "["<<expr::attr<UINT>("RecordID")  
  35.         << "]["<<expr::format_date_time(_timestamp,"%Y-%m-%d %H:%M:%S.%f")  
  36.         << "]["<<_severity  
  37.         << "]" <<expr::wmessage)  
  38.         );  
  39.   
  40.     pSink->locked_backend()->auto_flush(m_bAutoFlush);  
  41.   
  42.     logging::add_common_attributes();  
  43.   
  44.     attrs::counter<UINT> RecordID(1);  
  45.     logging::core::get()->add_global_attribute("RecordID", RecordID);  
  46.   
  47. ;   
  48. }  
  49. void CLogger::Start()  
  50. {  
  51.     logging::core::get()->set_logging_enabled(true);  
  52. }  
  53. void CLogger::Stop()  
  54. {  
  55.     logging::core::get()->set_logging_enabled(false);  
  56. }  
  57.   
  58. void CLogger::SetFilterTrace()  
  59. {  
  60.     logging::core::get()->set_filter(expr::attr<severity_level>("Severity") >= trace);   
  61. }  
  62.   
  63. void CLogger::SetFilterWarning()  
  64. {   
  65.     logging::core::get()->set_filter(expr::attr<severity_level>("Severity")>= warning);   
  66. }  
  67. void CLogger::SetFilterError()  
  68. {   
  69.     logging::core::get()->set_filter(expr::attr<severity_level>("Severity")>= error);  
  70. }  
  71.   
  72.   
  73. void CLogger::SetLogFilePath(std::wstring strPath)  
  74. {  
  75.     m_strFilePath=strPath;   
  76. }  
  77.   
  78. void CLogger::SetMinFreeSpace(size_t size)  
  79. {  
  80.     m_szMinFreeSpace=size* 1024 * 1024;  
  81.   
  82. }  
  83. void CLogger::SetRotationsize(size_t size)  
  84. {  
  85.     m_szRotationSize=size* 1024 * 1024;  
  86. }  

使用

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CLogger::Init():  

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. src::wseverity_logger< severity_level > slg;  
  2. BOOST_LOG_SEV(slg, trace) << _T("trace 输出日志ABC");  
  3. BOOST_LOG_SEV(slg, warning) << _T("warning 输出日志ABC");  
  4. BOOST_LOG_SEV(slg, error) << _T("error 输出日志ABC");  

源码下载地址: http://download.csdn.net/detail/flyfish1986/8135933

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值