• Boost.log


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

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

    请通过右侧公告中的“联系邮箱(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>        王林森原创 未经许可请勿转载 侵权必究

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

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

    头文件

     1 #include <iostream>
     2 #include <boost/locale/generator.hpp>
     3 #include <boost/date_time/posix_time/posix_time_types.hpp>
     4 #include <boost/log/common.hpp>
     5 #include <boost/log/expressions.hpp>
     6 #include <boost/log/utility/setup/file.hpp>
     7 #include <boost/log/utility/setup/console.hpp>
     8 #include <boost/log/utility/setup/common_attributes.hpp>
     9 #include <boost/log/sources/logger.hpp>
    10 #include <boost/log/support/date_time.hpp>
    11 #include <cstddef>
    12 #include <string>
    13 #include <fstream>
    14 #include <iomanip>
    15 #include <boost/smart_ptr/shared_ptr.hpp>
    16 #include <boost/smart_ptr/make_shared_object.hpp>
    17 #include <boost/date_time/posix_time/posix_time.hpp>
    18 #include <boost/log/core.hpp>
    19 #include <boost/log/attributes.hpp>
    20 #include <boost/log/sources/basic_logger.hpp>
    21 #include <boost/log/sources/severity_logger.hpp>
    22 #include <boost/log/sources/record_ostream.hpp>
    23 #include <boost/log/sinks/sync_frontend.hpp>
    24 #include <boost/log/sinks/text_ostream_backend.hpp>
    25 #include <boost/log/attributes/scoped_attribute.hpp>
    26 #include <boost/log/expressions/formatters/date_time.hpp>
    27 #include <boost/log/sinks/text_file_backend.hpp>
    28 #include <boost/log/sources/global_logger_storage.hpp>
    29 
    30 
    31 namespace logging = boost::log;
    32 namespace sinks = boost::log::sinks;
    33 namespace attrs = boost::log::attributes;
    34 namespace src = boost::log::sources;
    35 namespace expr = boost::log::expressions;
    36 namespace keywords = boost::log::keywords;
    37 
    38 
    39 enum severity_level
    40 {
    41     normal,
    42     message,
    43     alarm,
    44     error,
    45     fatal
    46 };
    47 
    48 template< typename CharT, typename TraitsT >
    49 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
    50 {
    51     static const char* const str[] =
    52     {
    53         "normal",
    54         "message",
    55         "alarm",
    56         "error",
    57         "fatal"
    58     };
    59     if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
    60         strm << str[lvl];
    61     else
    62         strm << static_cast< int >(lvl);
    63     return strm;
    64 }
    65 
    66 BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
    67 BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
    68 BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
    69 BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
    70 BOOST_LOG_ATTRIBUTE_KEYWORD(timeline, "Timeline", attrs::timer::value_type)
    71 
    72 
    73 void init_logging();

    实现文件

     1 void CHTCollectorDlg::init_logging()
     2 {
     3     CString strFileName=m_strLogLocation+TEXT("\WLS_%Y-%m-%d_FULL.log");
     4 
     5     std::locale::global(std::locale("chs"));
     6 
     7     typedef sinks::synchronous_sink< sinks::text_file_backend > text_sink;
     8 
     9     boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(  keywords::open_mode=std::ios::app,
    10                                                                             keywords::file_name =strFileName,
    11                                                                             keywords::rotation_size = 10 *1 * 1024 * 1024,
    12                                                                             keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0)
    13                                                                             );
    14     sink->set_formatter(expr::stream
    15                             << std::dec << std::setw(8) << std::setfill('0') << line_id << std::dec << std::setfill(' ')
    16                             <<"	"<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
    17                             << "	<"<< severity.or_default(normal) 
    18                             << ">		"<<expr::if_(expr::has_attr(tag_attr))
    19                                                 [
    20                                                     expr::stream << "[" << tag_attr << "]"
    21                                                 ]
    22                             << expr::if_(expr::has_attr(timeline))
    23                                         [
    24                                             expr::stream << "[>"<< timeline << "<]"
    25                                         ]
    26                             << expr::message);
    27 
    28     sink->locked_backend()->auto_flush(true);
    29 
    30     logging::core::get()->add_sink(sink);
    31 
    32     //////////////////////////////////////////////////////////////////////////
    33     strFileName=m_strLogLocation+TEXT("WLS_%Y-%m-%d_IMPORTANT.log");
    34     sink = boost::make_shared< text_sink >(    keywords::open_mode=std::ios::app,
    35         keywords::file_name =strFileName,
    36         keywords::rotation_size = 10 *1 * 1024 * 1024,
    37         keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0)
    38         );
    39     sink->set_formatter(expr::stream
    40         << std::dec << std::setw(8) << std::setfill('0') << line_id << std::dec << std::setfill(' ')
    41         <<"	"<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
    42         << "	<"<< severity.or_default(normal) 
    43         << ">		"<<expr::if_(expr::has_attr(tag_attr))
    44                   [
    45                       expr::stream << "[" << tag_attr << "]"
    46                   ]
    47       << expr::if_(expr::has_attr(timeline))
    48             [
    49                 expr::stream << "[>"<< timeline << "<]"
    50             ]
    51       << expr::message);
    52 
    53     sink->locked_backend()->auto_flush(true);
    54     sink->set_filter(severity>=alarm);
    55     logging::core::get()->add_sink(sink);
    56 
    57     logging::add_common_attributes();
    58 }

     用法

    1 AddRecord(severity_level sl,CString str)
    2 {
    3     src::wseverity_logger< severity_level > slg;
    4     BOOST_LOG_SEV(slg, sl) <<str.GetBuffer(str.GetLength());
    5 }
  • 相关阅读:
    常用功能测试点的测试用例
    如何设计功能测试测试用例
    管理小原则
    政党提供的公共产品是其存在的依据
    为什么人是根本?
    学问总分类
    和孩子沟通的开头常用语
    教育的核心对象是心中的那枚种子
    用目标激发动力,用计划控制落实,用梳理总结进行提高
    要想影响孩子第一位的是保证沟通畅通
  • 原文地址:https://www.cnblogs.com/wlsandwho/p/4666418.html
Copyright © 2020-2023  润新知