編譯boost:ios
一、打開Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Promotexpress
二、命令CD到boost目錄下,運行 bjam stage --toolset=msvc-10.0 --with-log threading=multi releaseapp
stage 指編譯到stage目錄下,toolset:編譯工具,with-log:指定編譯boost的log模塊frontend
Log 模塊:ide
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 }