boost 相关

编译boost:

1、打开Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Promot

2、命令CD到boost目录下,运行 bjam stage --toolset=msvc-10.0 --with-log threading=multi release

stage 指编译到stage目录下,toolset:编译工具,with-log:指定编译boost的log模块

 

Log 模块:

boost log库 使用

 

  1 #include <boost/shared_ptr.hpp>

  2 #include <boost/make_shared.hpp>

  3 #include <boost/thread/thread.hpp>

  4 #include <boost/log/core.hpp>

  5 #include <boost/log/sinks/sync_frontend.hpp>

  6 #include <boost/log/sinks/text_ostream_backend.hpp>

  7 #include <boost/log/sources/record_ostream.hpp>

  8 #include <boost/log/support/date_time.hpp>

  9 #include <boost/log/common.hpp>

 10 #include <boost/log/expressions.hpp>

 11 #include <boost/log/attributes.hpp>

 12 #include <boost/log/sinks.hpp>

 13 #include <boost/log/sources/logger.hpp>

 14 #include <boost/log/utility/setup/common_attributes.hpp>

 15 #include <boost/log/utility/setup/formatter_parser.hpp>

 16 #include <boost/log/sources/severity_logger.hpp>

 17 #include <boost/log/sources/severity_feature.hpp>

 18 #include <boost/log/trivial.hpp>

 19 

 20 #include <boost/log/attributes/named_scope.hpp>

 21 #include <boost/log/expressions/keyword.hpp>

 22 

 23 #include <fstream>

 24 #include <iostream>

 25 

 26 using namespace std;

 27 

 28 namespace logging = boost::log;

 29 namespace attrs = boost::log::attributes;

 30 namespace src = boost::log::sources;

 31 namespace sinks = boost::log::sinks;

 32 namespace expr = boost::log::expressions;

 33 namespace keywords = boost::log::keywords;

 34 

 35 using namespace logging::trivial;

 36 

 37 

 38 enum sign_severity_level {

 39     trace,

 40     debug,

 41     info,

 42     warning,

 43     error,

 44     fatal,

 45     report

 46 };

 47 

 48 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::severity_logger_mt<sign_severity_level>)

 49 

 50 void InitLog()

 51 {

 52     typedef sinks::synchronous_sink<sinks::text_file_backend> TextSink;

 53 

 54     // 设置旋转日志,每天0时添加一个日志文件,或日志文件大于10MB时添加另一个日志文件,磁盘必须大于3G

 55     /*boost::shared_ptr<sinks::text_file_backend>*/

 56     auto backend = boost::make_shared<sinks::text_file_backend>(

 57         keywords::file_name = "%Y-%m-%d.log",                // 日志文件

 58         keywords::rotation_size = 10 * 1024 * 1024,                                        // 日志文件上限10MB

 59         keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),    // 每天一个日志文件

 60         keywords::min_free_space = 30 * 1024 * 1024,                                    // 磁盘最小容量

 61         keywords::open_mode = ios::app,                                                    // 文件追加

 62         keywords::auto_flush = true                                                        // 自动刷新(立刻写入日志文件)

 63         );

 64 

 65     //// 自动刷新(立刻写入日志文件)

 66     //backend->auto_flush(true);

 67 

 68     boost::shared_ptr<TextSink> sink(new TextSink(backend));

 69 

 70 

 71     // 格式化日志格式   [日期]<日志级别>: 日志内容

 72     sink->set_formatter(

 73         //// 两种格式化写法

 74         //// 1:

 75         //expr::stream

 76         //<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]"

 77         //<< "<" << expr::attr<SeverityLevel::sign_severity_level>("Severity") << ">"

 78         //<< "(" << expr::format_named_scope("Scopes", boost::log::keywords::format = "%n (%f : %l)") << "):"

 79         //<< expr::smessage

 80 

 81         // 2:

 82         expr::format("[%1%]<%2%>(%3%): %4%")

 83         % expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")

 84         % expr::attr<sign_severity_level>("Severity")        /*logging::trivial::severity*/

 85         // %n:Scope name(void foo());%f:Source file name of the scope;%l:Line number in the source file

 86         % expr::format_named_scope("Scopes", boost::log::keywords::format = "%n (%f : %l)")

 87         //% expr::attr<attrs::current_thread_id::value_type >("ThreadID")        // 单线程没有ID

 88         % expr::smessage

 89         );

 90 

 91     logging::core::get()->add_global_attribute("Scopes", attrs::named_scope());

 92 

 93     // 设置过滤器

 94     sink->set_filter(expr::attr< sign_severity_level >("Severity") >= sign_severity_level::error);

 95     //sink->set_filter(expr::attr< severity_level >("Severity") >= logging::trivial::severity_level::error);

 96 

 97     logging::add_common_attributes();        // 要添加,否则线程ID和日期等一些属性都打印不出来

 98     logging::core::get()->add_sink(sink);

 99 }

100 

101 void foo(void)

102 {

103     //BOOST_LOG_FUNCTION();        // 打印更详细的scope

104 

105     BOOST_LOG_NAMED_SCOPE("Scopes");        // 一定要这句,否则打印不出scope

106     src::severity_logger_mt<sign_severity_level>& lg = my_logger::get();

107     BOOST_LOG_SEV(lg, sign_severity_level::error) << "A trace severity message";

108 }

109 

110 int main()

111 {

112     InitLog();

113     foo();

114     logging::core::get()->remove_all_sinks();

115 }
Boost log样例

 

你可能感兴趣的:(boost)