前面做了不少boost log的例子,现在来总结一下,下面是boost log库的架构图:
下面总结一下和这个架构相关的知识:
#include <boost/log/core.hpp> ... boost::shared_ptr<logging::core> core = logging::core::get();
一个core可以安装多个Sink,下面的代码假定已经有了两个Sink对象,将其安装到core中
core->add_sink(sink1); ... core->add_sink(sink2);
需要先创建一个backend对象,然后在创建sink对象的时候,将backend对象传递给它。
typedef sinks::synchronous_sink<sinks::text_file_backend> TextSink; // init sink1 boost::shared_ptr<sinks::text_file_backend> backend1 = boost::make_shared<sinks::text_file_backend>( keywords::file_name = "sign_%Y-%m-%d_%H-%M-%S.%N.log", keywords::rotation_size = 10 * 1024 * 1024, keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), keywords::min_free_space = 30 * 1024 * 1024); boost::shared_ptr<TextSink> sink1(new TextSink(backend1));
前面的代码中已经演示,注意backend的类型需要制定一个frontend类型作为其模板类。因此,当创建一个backend对象的时候,已经确定了frontend。
这个frontend模板可以用synchronous_sink类,也可以用asynchronous_sink, 后者不会阻塞调用程序,会额外的创建线程去处理log,不过会慢点,内存消耗大点。一般都推荐先用后者。
这里看到一个概念:keywords. 在boost/log/keywords/目录下27个hpp文件:
auto_flush.hpp facility.hpp ident.hpp log_source.hpp open_mode.hpp rotation_size.hpp target.hpp channel.hpp file_name.hpp ip_version.hpp max_size.hpp order.hpp scan_method.hpp time_based_rotation.hpp delimiter.hpp filter.hpp iteration.hpp message_file.hpp ordering_window.hpp severity.hpp use_impl.hpp depth.hpp format.hpp log_name.hpp min_free_space.hpp registration.hpp start_thread.hpp
sinks::text_file_backend
参考文档:
http://boost-log.sourceforge.net/libs/log/doc/html/log/detailed/sink_backends.html
http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/boost/log/sinks/text_file_backend.html
注意,text_file_backend的构造函数语法上支持变参,但是语义上只支持有限的keywords:
template< typename ArgsT >
void construct(ArgsT const& args)
{
construct(
filesystem::path(args[keywords::file_name | filesystem::path()]),
args[keywords::open_mode | (std::ios_base::trunc | std::ios_base::out)],
args[keywords::rotation_size | (std::numeric_limits< uintmax_t >::max)()],
args[keywords::time_based_rotation | time_based_rotation_predicate()],
args[keywords::auto_flush | false]);
}namespace aux {
//! Creates and returns a file collector with the specified parameters
BOOST_LOG_API shared_ptr< collector > make_collector(
filesystem::path const& target_dir,
uintmax_t max_size,
uintmax_t min_free_space
);
template< typename ArgsT >
inline shared_ptr< collector > make_collector(ArgsT const& args)
{
return aux::make_collector(
filesystem::path(args[keywords::target]),
args[keywords::max_size | (std::numeric_limits< uintmax_t >::max)()],
args[keywords::min_free_space | static_cast< uintmax_t >(0)]);
}
} // namespace aux下面到了指定日志格式,这个需要在sink中指定,比如:
sink1->set_formatter (
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")
% expr::attr<attrs::current_thread_id::value_type>("ThreadID")
% expr::smessage
);我使用的是Boost::Format风格。
参考文档:http://boost-log.sourceforge.net/libs/log/doc/html/log/detailed/attributes.html
根据设计,日志记录是由attributes组成的,所以打印内容必须以attribute的方式传给sink对象。
sink1->set_formatter (
expr::format("[%1%]<%2%>(%3%)(%4%): %5%")
% expr::attr<unsigned int>("LineID")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::attr<sign_severity_level>("Severity")
% expr::attr<attrs::current_thread_id::value_type >("ThreadID")
% expr::smessage
);不要忘记添加commont attributes
logging::add_common_attributes();该函数定义在boost/log/utility/setup/common_attributes.hpp文件中, 里面添加了4个属性:
inline void add_common_attributes()
{
shared_ptr< core > pCore = core::get();
pCore->add_global_attribute(
aux::default_attribute_names::line_id(),
attributes::counter< unsigned int >(1));
pCore->add_global_attribute(
aux::default_attribute_names::timestamp(),
attributes::local_clock());
pCore->add_global_attribute(
aux::default_attribute_names::process_id(),
attributes::current_process_id());
#if !defined(BOOST_LOG_NO_THREADS)
pCore->add_global_attribute(
aux::default_attribute_names::thread_id(),
attributes::current_thread_id());
#endif
}filter可以过滤日志级别,还可以更多,下面是个例子:
sink1->set_filter(expr::attr<sign_severity_level>("Severity") >= trace);boost log库使用 十二 架构研究和主要使用总结,布布扣,bubuko.com
原文:http://blog.csdn.net/csfreebird/article/details/20213349