Boost Log 基本使用方法
Boost Log 基本使用方法
flyfish 2014-11-5
依据boost提供的代码演示样例,学习Boost Log 的基本使用方法
前提
boost版本号boost_1_56_0
演示样例代码目录 boost_1_56_0\libs\log\example\basic_usage
使用的单词非常形象。整个过程就像流水一样
如果要输出的日志比作水
水 (Hello, World!)
水槽 (sink)
流向哪里 (console,file)
从哪里取 (source)
水的等级 (severity level)
过滤输出 (filter)
格式输出 (format)
各部分连接者(core)
演示样例
#include <iostream> #include <boost/log/common.hpp>
#include <boost/log/expressions.hpp> #include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp> #include <boost/log/attributes/timer.hpp>
#include <boost/log/attributes/named_scope.hpp> #include <boost/log/sources/logger.hpp> #include <boost/log/support/date_time.hpp> namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords; using boost::shared_ptr; // Here we define our application severity levels.
enum severity_level
{
normal,
notification,
warning,
error,
critical
}; // The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
static const char* const str[] =
{
"normal",
"notification",
"warning",
"error",
"critical"
};
if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast< int >(lvl);
return strm;
} int _tmain(int argc, char* argv[])
{
// This is a simple tutorial/example of Boost.Log usage // The first thing we have to do to get using the library is
// to set up the logging sinks - i.e. where the logs will be written to.
logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %Message%"); // One can also use lambda expressions to setup filters and formatters
logging::add_file_log
(
"sample.log",
keywords::filter = expr::attr< severity_level >("Severity") >= warning,
keywords::format = expr::stream
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
<< " [" << expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")
<< "] [" << expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")
<< "] <" << expr::attr< severity_level >("Severity")
<< "> " << expr::message
/*
keywords::format = expr::format("%1% [%2%] [%3%] <%4%> %5%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
% expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")
% expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")
% expr::attr< severity_level >("Severity")
% expr::message
*/
); // Also let's add some commonly used attributes, like timestamp and record counter.
logging::add_common_attributes();
logging::core::get()->add_thread_attribute("Scope", attrs::named_scope()); BOOST_LOG_FUNCTION(); // Now our logs will be written both to the console and to the file.
// Let's do a quick test and output something. We have to create a logger for this.
src::logger lg; // And output...
BOOST_LOG(lg) << "Hello, World!"; // Now, let's try logging with severity
src::severity_logger< severity_level > slg; // Let's pretend we also want to profile our code, so add a special timer attribute.
slg.add_attribute("Uptime", attrs::timer()); BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to the file";
BOOST_LOG_SEV(slg, warning) << "A warning severity message, will pass to the file";
BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to the file"; return 0;
}
从上到下依次分析
一 日志严重性等级
enum severity_level
{
normal,
notification,
warning,
error,
critical
};
二 日志等级输出
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
...
}
输出已经定义的等级描写叙述,日志等级的数值与字符串一一相应,假设在enum severity_level假设未定义则输出数值。
std::basic_ostream对全部的内建类型,进行了重载,输入各种内置类型
重载operator <<,使得自定的用户定义类型severity_level 集成到IOStream library中
IOStream library的类都带有两个參数,当中一个是字符的类型,一个是与字符类型相关的信息
就像std::cout一样输出各种类型,编译器自己会进行正确的推导输出的什么类型。
static const char* const 表示数组里面的指针不可改变 并且指针所指向的字符串也不可改变
三 日志输出位置
logging::add_console_log
日志输出到控制台
logging::add_file_log
日志输出到文件
四 定义源。像std::cout一样输出
src::logger lg;
BOOST_LOG(lg) << "Hello, World!";
五 结果
文件的输出
2014-11-05, 19:46:19.513082 [00:00:00]
[int __cdecl wmain(int,char *[]) (文件路径:代码行)] <warning> A warning severity message, will pass to the file
2014-11-05, 19:46:19.518082 [00:00:00]
[int __cdecl wmain(int,char *[]) (文件路径:代码行)] <error> An error severity message, will pass to the file
控制台的输出
2014-Nov-05 19:51:30.261856: Hello, World!
2014-Nov-05 19:51:30.268856: A normal severity message, will not pass to the file
2014-Nov-05 19:51:30.275856: A warning severity message, will pass to the file
2014-Nov-05 19:51:30.284857: An error severity message, will pass to the file
Boost Log 基本使用方法的更多相关文章
- boost.log要点笔记
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- boost.asio与boost.log同时使用导致socket不能正常收发数据
现象: 1. 没有使用boost.log前能正常收发数据 2.加入boost.log后async_connect没有回调 fix过程: 1. gdb调试发现程序block在pthread_timed_ ...
- Boost.log
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- Android开发华为手机无法看log日志解决方法
Android开发华为手机无法看log日志解决方法 上班的时候,由于开发工具由Eclipse改成Android Studio后,原本的华为手机突然无法查看崩溃日志了,大家都知道,若是无法查看日志要它毛 ...
- Android开发过程中在sh,py,mk文件中添加log信息的方法
Android开发过程中在sh,py,mk文件中添加log信息的方法 在sh文件中: echo "this is a log info" + $info 在py文件中: print ...
- boost.log在项目中应用
//头文件#pragma once #include <string> #include <boost/log/trivial.hpp> using std::string; ...
- 编译boost.log模块遇到的一些问题
线上日志用到的是日志库,在全局有一个锁,导致在高并发的时候,容易因为锁竞争问题导致时延.在某些情况下,会因为同一个用户,同时访问某个变量,导致读写冲突使线上服务整体core掉(考虑到请求的间隔,为了应 ...
- Boost log中的几个问题
1. 使用动态库时,要定义 BOOST_LOG_DYN_LINK 或者 BOOST_ALL_DYN_LINK 否则会出现如下错误: CMakeFiles/xxxx.dir/xxxx.cpp.o: I ...
- C++ 日志库 boost::log 以及 glog 的对比
日志能方便地诊断程序原因.统计程序运行数据,是大型软件系统必不可少的组件之一.本文将从设计上和功能上对比 C++ 语言常见的两款日志库: boost::log 和 google-glog . 设计 b ...
随机推荐
- 拥抱Mac之码农篇
拥抱Mac之码农篇 使用Mac大概两年时间.之前用着公司配的一台27寸的iMac.无奈机械硬盘严重拖慢速度,影响工作心情.于是入手Macbook Retina 13.这两年的开发工作所有在Mac上完毕 ...
- 再续iOS开发中的这些权限
前言 上篇文章iOS开发中的这些权限,你搞懂了吗?介绍了一些常用权限的获取和请求方法,知道这些方法的使用基本上可以搞定大部分应用的权限访问的需求.但是,这些方法并不全面,不能涵盖住所有权限访问的方法. ...
- ng-repeat 中的 track by $index
用ng-repeat指令遍历一个javascript数组,当数组中有重复元素的时候,angularjs会报错,这是因为ng-Repeat不允许collection中存在两个相同Id的对象. 对于数字或 ...
- zookeeper+kafka集群搭建
一.ZK集群安装. 解压安装包后进入conf目录,conf/zoo_sample.cfg拷贝一份命名为zoo.cfg,同时也放在conf下面. zookeeper配置文件: # The number ...
- hdu 1080 dp(最长公共子序列变形)
题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G - G ...
- echart的tooltip自定义换行
自定义换行,内容很长的时候 tooltip : { trigger: 'axis', axisPointer : { // 坐标轴指示器,坐标轴触发有效 type : 'shadow' // 默认为直 ...
- IOS - [UIDevice currentDevice] name/model/localizedMode/systemName/systemVersion...../userInterfaceIdiom
+ (UIDevice *)currentDevice; @property(nonatomic,readonly,retain) NSString *name; // ...
- pyqt5的QWebEngineView 使用方法
说明1:关于QWebEngineView pyqt5 已经抛弃 QtWebKit和QtWebKitWidgets,而使用最新的QtWebEngineWidgets. QtWebEngineWidget ...
- [LUOGU]2016 Sam数
我本来想看看SAM,就看见了这个.. 这道题很容易让人想到数位DP,用\(f[i][j]\)表示考虑到第\(i\)位,最后一位是\(j\)的方案数.看到1e18,直接矩阵快速幂加速,因为它每位转移都是 ...
- nysql数据库优化
硬件优化 软件优化 my.cnf参数优化,命令监控show global status\G 调优工具mysqlreport sql语句优化 索引的优化 白名单机制--百度,就是让一些不规范的语句执行查 ...