博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Boost Log
阅读量:4685 次
发布时间:2019-06-09

本文共 4771 字,大约阅读时间需要 15 分钟。

boost log支持以下配置宏,只列出一些常用的,如下表所示:

Macro name Effect
BOOST_LOG_DYN_LINK If defined in user code, the library will assume the binary is built as a dynamically loaded library (“dll” or “so”). Otherwise it is assumed that the library is built in static mode. This macro must be either defined or not defined for all translation units of user application that uses logging. This macro can help with auto-linking on platforms that support it.
BOOST_ALL_DYN_LINK Same as BOOST_LOG_DYN_LINK but also affects other Boost libraries the same way.
BOOST_USE_WINAPI_VERSION Affects compilation of both the library and user’s code. This macro is Windows-specific. Selects the target Windows version for various Boost libraries, including Boost.Log. Code compiled for a particular Windows version will likely fail to run on the older Windows versions, but may improve performance because of using newer OS features. The macro is expected to have an integer value equivalent to _WIN32_WINNT.
BOOST_LOG_NO_THREADS If defined, disables multithreading support. Affects the compilation of both the library and users’ code. The macro is automatically defined if no threading support is detected.

有一点要注意:如果你的程序工程中由多个模块构成(例如,由一个.exe和多个.dll构成),当你使用boost log

库时必须built as a shared object。如果只是单个模块(例如:单个.exe或单个.dll)则可以build the library as a static library.

boost log涉及的重点概念或术语定义解释

日志记录:一个独立的消息包,这个消息包还不是实际写到日志里的消息,它只是一个候选的消息。

属性 : 日志记录中的一个消息片。
属性值:那就是上面所说的属性的值了,可以是各种数据类型。
日志槽(LOG SINK):日志写向的目标,它要定义日志被写向什么地方,以及如何写。
日志源:应用程序写日志时的入口,其实质是一个logger对象的实例。
日志过滤器:决定日志记录是否要被记录的一组判断。
日志格式化:决定日志记录输出的实际格式。
日志核心:维护者日志源、日志槽、日志过滤器等之间的关系的一个全局中的实体。主要在初始化logging library时用到。

最简单例子:

#include 
int main(int, char*[]){ BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; BOOST_LOG_TRIVIAL(debug) << "A debug severity message"; BOOST_LOG_TRIVIAL(info) << "An informational severity message"; BOOST_LOG_TRIVIAL(warning) << "A warning severity message"; BOOST_LOG_TRIVIAL(error) << "An error severity message"; BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message"; return 0;}

添加过滤:

#include 
#include
#include
namespace logging = boost::log;void init(){ logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info );}int main(int, char*[]){ init(); BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; BOOST_LOG_TRIVIAL(debug) << "A debug severity message"; BOOST_LOG_TRIVIAL(info) << "An informational severity message"; BOOST_LOG_TRIVIAL(warning) << "A warning severity message"; BOOST_LOG_TRIVIAL(error) << "An error severity message"; BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message"; return 0;}

 输出到文件:

#include 
#include
#include
#include
#include
#include
#include
#include
namespace logging = boost::log;namespace src = boost::log::sources;namespace sinks = boost::log::sinks;namespace keywords = boost::log::keywords;#if 0//[ example_tutorial_file_simplevoid init(){ logging::add_file_log("sample.log"); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info );}//]// We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416//[ example_tutorial_file_advanced_no_calloutsvoid init(){ logging::add_file_log ( keywords::file_name = "sample_%N.log", keywords::rotation_size = 10 * 1024 * 1024, keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), keywords::format = "[%TimeStamp%]: %Message%" ); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info );}//]#else//[ example_tutorial_file_advancedvoid init(){ logging::add_file_log ( keywords::file_name = "sample_%N.log", /*< file name pattern >*/ keywords::rotation_size = 10 * 1024 * 1024, /*< rotate files every 10 MiB... >*/ keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/ keywords::format = "[%TimeStamp%]: %Message%" /*< log record format >*/ ); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info );}//]#endifint main(int, char*[]){ init(); logging::add_common_attributes(); using namespace logging::trivial; src::severity_logger< severity_level > lg; BOOST_LOG_SEV(lg, trace) << "A trace severity message"; BOOST_LOG_SEV(lg, debug) << "A debug severity message"; BOOST_LOG_SEV(lg, info) << "An informational severity message"; BOOST_LOG_SEV(lg, warning) << "A warning severity message"; BOOST_LOG_SEV(lg, error) << "An error severity message"; BOOST_LOG_SEV(lg, fatal) << "A fatal severity message"; return 0;}

 

转载于:https://www.cnblogs.com/kohlrabi/p/9131487.html

你可能感兴趣的文章
mysql查询一个表的字段,添加或修改到另外一个表的数据
查看>>
CL.exe的 /D 选项, Preprocessor Macro预处理器宏定义
查看>>
[Pytorch]Pytorch中tensor常用语法
查看>>
ZOJ 1008 Gnome Tetravex
查看>>
Jenkin远程部署Tomcat8.5总结
查看>>
编写Linux中sh文件执行时出现莫名字符的问题
查看>>
简单数论(一)
查看>>
CXF和Axis的比较【转】
查看>>
设计一个函数,它接受不定数量的参数,这是参数都是函数。这些函数都接受一个回调函数作为参数,按照回调函数被调用的顺序返回函数名...
查看>>
MyBatis学习总结_06_调用存储过程
查看>>
SEO知识图一
查看>>
[开源JVM] yvm - 自制Java虚拟机
查看>>
Open vSwitch安装
查看>>
【Android】 No Activity found to handle Intent.
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
C++ sort简单用法
查看>>
IIS的ISAPI接口简介
查看>>
python:open/文件操作
查看>>
16 乘法口诀输出
查看>>
mac 常用地址
查看>>