=================================版权声明=================================

版权声明:本文为博主原创文章 未经许可不得转载 

请通过右侧公告中的“联系邮箱(wlsandwho@foxmail.com)”联系我

未经作者授权勿用于学术性引用。

未经作者授权勿用于商业出版、商业印刷、商业引用以及其他商业用途。                

本文不定期修正完善,为保证内容正确,建议移步原文处阅读。                                                               <--------总有一天我要自己做一个模板干掉这只土豆

本文链接:http://www.cnblogs.com/wlsandwho/p/4666418.html

耻辱墙:http://www.cnblogs.com/wlsandwho/p/4206472.html

=======================================================================

也不知道写的对不对,目前能用就好。

=======================================================================

自动生成文件名、设置过滤等级、设置日志格式、输出到完备日志和重要日志

(部分功能没用到就不删除了,也不碍事。)

=======================================================================

日志格式:

00000001    2015-07-22_09:14:35.771886    <fatal>        王林森原创 未经许可请勿转载 侵权必究

(  行号 ) (      时间戳      ) (等级)  (         内容        )

=======================================================================

头文件

 #include <iostream>
#include <boost/locale/generator.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <cstddef>
#include <string>
#include <fstream>
#include <iomanip>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/core.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sources/basic_logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sources/global_logger_storage.hpp> namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords; enum severity_level
{
normal,
message,
alarm,
error,
fatal
}; template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
static const char* const str[] =
{
"normal",
"message",
"alarm",
"error",
"fatal"
};
if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast< int >(lvl);
return strm;
} BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(timeline, "Timeline", attrs::timer::value_type) void init_logging();

实现文件

 void CHTCollectorDlg::init_logging()
{
CString strFileName=m_strLogLocation+TEXT("\\WLS_%Y-%m-%d_FULL.log"); std::locale::global(std::locale("chs")); typedef sinks::synchronous_sink< sinks::text_file_backend > text_sink; boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >( keywords::open_mode=std::ios::app,
keywords::file_name =strFileName,
keywords::rotation_size = * * * ,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(, , )
);
sink->set_formatter(expr::stream
<< std::dec << std::setw() << std::setfill('') << line_id << std::dec << std::setfill(' ')
<<"\t"<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
<< "\t<"<< severity.or_default(normal)
<< ">\t\t"<<expr::if_(expr::has_attr(tag_attr))
[
expr::stream << "[" << tag_attr << "]"
]
<< expr::if_(expr::has_attr(timeline))
[
expr::stream << "[>"<< timeline << "<]"
]
<< expr::message); sink->locked_backend()->auto_flush(true); logging::core::get()->add_sink(sink); //////////////////////////////////////////////////////////////////////////
strFileName=m_strLogLocation+TEXT("WLS_%Y-%m-%d_IMPORTANT.log");
sink = boost::make_shared< text_sink >( keywords::open_mode=std::ios::app,
keywords::file_name =strFileName,
keywords::rotation_size = * * * ,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(, , )
);
sink->set_formatter(expr::stream
<< std::dec << std::setw() << std::setfill('') << line_id << std::dec << std::setfill(' ')
<<"\t"<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
<< "\t<"<< severity.or_default(normal)
<< ">\t\t"<<expr::if_(expr::has_attr(tag_attr))
          [
          expr::stream << "[" << tag_attr << "]"
          ]
  << expr::if_(expr::has_attr(timeline))
    [
    expr::stream << "[>"<< timeline << "<]"
    ]
  << expr::message); sink->locked_backend()->auto_flush(true);
sink->set_filter(severity>=alarm);
logging::core::get()->add_sink(sink); logging::add_common_attributes();
}

用法

 AddRecord(severity_level sl,CString str)
{
src::wseverity_logger< severity_level > slg;
BOOST_LOG_SEV(slg, sl) <<str.GetBuffer(str.GetLength());
}

Boost.log的更多相关文章

  1. boost.asio与boost.log同时使用导致socket不能正常收发数据

    现象: 1. 没有使用boost.log前能正常收发数据 2.加入boost.log后async_connect没有回调 fix过程: 1. gdb调试发现程序block在pthread_timed_ ...

  2. boost.log要点笔记

    span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...

  3. boost.log在项目中应用

    //头文件#pragma once #include <string> #include <boost/log/trivial.hpp> using std::string; ...

  4. 编译boost.log模块遇到的一些问题

    线上日志用到的是日志库,在全局有一个锁,导致在高并发的时候,容易因为锁竞争问题导致时延.在某些情况下,会因为同一个用户,同时访问某个变量,导致读写冲突使线上服务整体core掉(考虑到请求的间隔,为了应 ...

  5. Boost log中的几个问题

    1. 使用动态库时,要定义 BOOST_LOG_DYN_LINK  或者 BOOST_ALL_DYN_LINK 否则会出现如下错误: CMakeFiles/xxxx.dir/xxxx.cpp.o: I ...

  6. Boost Log 基本使用方法

    Boost Log 基本使用方法 flyfish 2014-11-5 依据boost提供的代码演示样例,学习Boost Log 的基本使用方法 前提 boost版本号boost_1_56_0 演示样例 ...

  7. C++ 日志库 boost::log 以及 glog 的对比

    日志能方便地诊断程序原因.统计程序运行数据,是大型软件系统必不可少的组件之一.本文将从设计上和功能上对比 C++ 语言常见的两款日志库: boost::log 和 google-glog . 设计 b ...

  8. boost log库

    http://blog.csdn.net/sheismylife/article/category/1820481

  9. 在windows不能正常使用boost og

    现象: 1. 在两个不同的dll中使用static的boost.log.在一个dll中的设置在另一个dll中没有起作用 原因:core::get()返回的是一个单例.在不同的dll中是不同的对象 解决 ...

随机推荐

  1. 【转】数据库无关的GO语言ORM - hood

    项目地址:https://github.com/eaigner/hood 这是一个极具美感的ORM库. 特性 链式的api 事务支持 迁移和名字空间生成 模型变量 模型时间 数据库方言接口 没有含糊的 ...

  2. Scalaz(47)- scalaz-stream: 深入了解-Source

    scalaz-stream库的主要设计目标是实现函数式的I/O编程(functional I/O).这样用户就能使用功能单一的基础I/O函数组合成为功能完整的I/O程序.还有一个目标就是保证资源的安全 ...

  3. python补充最常见的内置函数

    最常见的内置函数是: print("Hello World!") 数学运算 abs(-5)                         # 取绝对值,也就是5 round(2. ...

  4. opencart二次开发小记

    在controller中如果要调用model中数据或说方法可以这样写 $this->load->model('catalog/information');//model中的informat ...

  5. [翻译]:SQL死锁-死锁排除

    As we already saw, the reasons why we have blocking issues and deadlocks in the system are pretty mu ...

  6. 代理模式的java实现

    1.  简介 代理模式(Proxy Pattern)是常用设计模式之一.代理模式的定义:Provide a surrogate or placeholder for another object to ...

  7. wso2esb之代理服务 Proxy Services

    代理服务 顾名思义,代理服务充当了WSO2 ESB服务的代理,通常是一个已经存在的服务端点,代理服务可以使用不同的传输方式. 客户可以直接发送请求代理服务的ESB,客户看到服务代理. 运行示例 配置W ...

  8. 【初探移动前端开发04】jQuery Mobile (中)

    前言 昨天我们一起学习了一部分jquery mobile的知识,今天我们继续. 这些是些很基础的东西,有朋友觉得这个没有其它的好,但是学习下不吃亏嘛,我反正也不会一起学习基础啦. 例子请使用手机查看哦 ...

  9. HTML5拖放(drag and drop)与plupload的懒人上传

    HTML5拖放能够将本地的文件拖放到页面上,plupload又是很好的文件上传插件,而今天就将两者结合,做了个文件拖拽上传的功能. 简述HTML5拖放 拖放是HTML5标准的一部分,任何元素都能够拖放 ...

  10. 利用ARCHPR明文破解获取PDF

    我们经常下载一些rar或zip压缩文件,解压时有时发现要密码,而密码多是为了推广而设置的网址等,如果不知道密码,可 以去来源网站上寻找或在压缩文件的注释中查看. 而并非所有都是如此,例如,网上有些人, ...