boost 相关

时间:2023-03-10 01:19:56
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库 使用

 #include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/thread/thread.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/attributes/named_scope.hpp>
#include <boost/log/expressions/keyword.hpp> #include <fstream>
#include <iostream> using namespace std; namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords; using namespace logging::trivial; enum sign_severity_level {
trace,
debug,
info,
warning,
error,
fatal,
report
}; BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::severity_logger_mt<sign_severity_level>) void InitLog()
{
typedef sinks::synchronous_sink<sinks::text_file_backend> TextSink; // 设置旋转日志,每天0时添加一个日志文件,或日志文件大于10MB时添加另一个日志文件,磁盘必须大于3G
/*boost::shared_ptr<sinks::text_file_backend>*/
auto backend = boost::make_shared<sinks::text_file_backend>(
keywords::file_name = "%Y-%m-%d.log", // 日志文件
keywords::rotation_size = * * , // 日志文件上限10MB
keywords::time_based_rotation = sinks::file::rotation_at_time_point(, , ), // 每天一个日志文件
keywords::min_free_space = * * , // 磁盘最小容量
keywords::open_mode = ios::app, // 文件追加
keywords::auto_flush = true // 自动刷新(立刻写入日志文件)
); //// 自动刷新(立刻写入日志文件)
//backend->auto_flush(true); boost::shared_ptr<TextSink> sink(new TextSink(backend)); // 格式化日志格式 [日期]<日志级别>: 日志内容
sink->set_formatter(
//// 两种格式化写法
//// 1:
//expr::stream
//<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]"
//<< "<" << expr::attr<SeverityLevel::sign_severity_level>("Severity") << ">"
//<< "(" << expr::format_named_scope("Scopes", boost::log::keywords::format = "%n (%f : %l)") << "):"
//<< expr::smessage // 2:
expr::format("[%1%]<%2%>(%3%): %4%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::attr<sign_severity_level>("Severity") /*logging::trivial::severity*/
// %n:Scope name(void foo());%f:Source file name of the scope;%l:Line number in the source file
% expr::format_named_scope("Scopes", boost::log::keywords::format = "%n (%f : %l)")
//% expr::attr<attrs::current_thread_id::value_type >("ThreadID") // 单线程没有ID
% expr::smessage
); logging::core::get()->add_global_attribute("Scopes", attrs::named_scope()); // 设置过滤器
sink->set_filter(expr::attr< sign_severity_level >("Severity") >= sign_severity_level::error);
//sink->set_filter(expr::attr< severity_level >("Severity") >= logging::trivial::severity_level::error); logging::add_common_attributes(); // 要添加,否则线程ID和日期等一些属性都打印不出来
logging::core::get()->add_sink(sink);
} void foo(void)
{
//BOOST_LOG_FUNCTION(); // 打印更详细的scope BOOST_LOG_NAMED_SCOPE("Scopes"); // 一定要这句,否则打印不出scope
src::severity_logger_mt<sign_severity_level>& lg = my_logger::get();
BOOST_LOG_SEV(lg, sign_severity_level::error) << "A trace severity message";
} int main()
{
InitLog();
foo();
logging::core::get()->remove_all_sinks();
}

Boost log样例