glog学习
来源:互联网 发布:杨闻萍审计 知乎 编辑:程序博客网 时间:2024/06/18 07:00
Google glog是一个c++实现的应用层记录日志的库。提供了非常友好的记录日志的方法。可以简单地通过流的方法记录日志。
1、 Hello world
用glog写一个hello world 程序:
int main(int argc, char* argv[]){ google::InitGoogleLogging("argv[0]"); LOG(INFO)<<"Hello world"; return 0;}
编译,运行之后。然而,我们并没有看到“Hello world”。
日志文件的输出位置为:/tmp/ ; 输出文件的名字是由
google::InitGoogleLogging("argv[0]");
决定的,如果argv[0] = "test",那么,输出日志文件的文件名就是:test.INFO(等等)。(其实,这并不是一个真正的文件,而是日志文件的一个链接)
2、日志严重性等级
日志通常会有不同的等级。往往一个程序运行的日志文件并不是一个文件(是多个文件),而是根据等级记录在不同的文件中。一个程序对应的日志文件,一共有:
INFO WARNING ERROR FATAL
严重性等级原来越高。
.INFO 日志文件中只记录INFO日志。
.WARNING 日志文件中只记录 INFO日志和WARNING 日志。
以此类推。
如果程序中产生FATAL 日志,则会将日志记录到文件中,然后,终止程序。
然而,在程序中:
LOG(INFO)<<"Hello world";
LOG(...)中可以写的关键字并不是仅仅只有这么四种,还有DFATAL, 该日志记录在DEBUG模式下,输出为FATAL日志,在非DEBUG模式下,防止程序中断,输出为ERROR日志。
3、设置日志的flags
glog 与 gflags 都是google出品。glog的很多特性都会用到gflags。所以, glog的一些配置,是通过gflags的flag设置的。
例如:
./your_application --logtostderr=1这个命令是,将日志记录到标准输出上。
通过gflags可以在程序中设置:
FLAGS_logtostderr=1;
还有很多其他特性:
logtostderr
(bool
, default=false
)- Log messages to stderr instead of logfiles.
Note: you can set binary flags totrue
by specifying1
,true
, oryes
(case insensitive). Also, you can set binary flags tofalse
by specifying0
,false
, orno
(again, case insensitive). stderrthreshold
(int
, default=2, which isERROR
)- Copy log messages at or above this level to stderr in addition to logfiles. The numbers of severity levels
INFO
,WARNING
,ERROR
, andFATAL
are 0, 1, 2, and 3, respectively. minloglevel
(int
, default=0, which isINFO
)- Log messages at or above this level. Again, the numbers of severity levels
INFO
,WARNING
,ERROR
, andFATAL
are 0, 1, 2, and 3, respectively. log_dir
(string
, default="")- If specified, logfiles are written into this directory instead of the default logging directory.
v
(int
, default=0)- Show all
VLOG(m)
messages form
less or equal the value of this flag. Overridable by --vmodule. See the section about verbose logging for more detail. vmodule
(string
, default="")- Per-module verbose level. The argument has to contain a comma-separated list of <module name>=<log level>. <module name> is a glob pattern (e.g.,
gfs*
for all modules whose name starts with "gfs"), matched against the filename base (that is, name ignoring .cc/.h./-inl.h). <log level> overrides any value given by --v. See also the section about verbose logging.
Sometimes, you may only want to log a message under certain conditions. You can use the following macros to perform conditional logging:
LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";The "Got lots of cookies" message is logged only when the variable
num_cookies
exceeds 10. If a line of code is executed many times, it may be useful to only log a message at certain intervals. This kind of logging is most useful for informational messages.LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
The above line outputs a log messages on the 1st, 11th, 21st, ... times it is executed. Note that the special google::COUNTER
value is used to identify which repetition is happening.
You can combine conditional and occasional logging with the following macro.
LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER << "th big cookie";
Instead of outputting a message every nth time, you can also limit the output to the first n occurrences:
LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
Outputs log messages for the first 20 times it is executed. Again, the google::COUNTER
identifier indicates which repetition is happening.
debug的宏,通常在debug阶段会生成日志,而在正式发布时,会做一些简化,不至于影响程序速度:
DLOG(INFO) << "Found cookies"; DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
在标注C库里,我们经常用到assert 来判断程序是否进入非法状态,以至于尽快abort程序。
CHECK与assert 类似,但是不同。CHECK的执行不会参照NDEBUG,所以,在任何时候,check都会被执行。
check 判断条件是否正确,如果条件不正确,则生成FATAL 日志记录,并且终止程序。
多种check:
整数类型:
CHECK_NE(1, 2)
CHECK_EQ
, CHECK_NE
, CHECK_LE
, CHECK_LT
, CHECK_GE
, and CHECK_GT
字符串类型:
CHECK_STREQ
, CHECK_STRNE
, CHECK_STRCASEEQ
, andCHECK_STRCASENE
.
用来判断c类型字符串,c++类型要转换为c类字符串。
浮点数:
CHECK_DOUBLE_EQ
一般类型:
CHECK(fp->Write(x) == 4)
7、冗长的日志
VLOG(1) << "I'm printed when you run the program with --v=1 or higher"; VLOG(2) << "I'm printed when you run the program with --v=2 or higher";
--v=0
8、用户自定义Fail函数
通过日志终止程序执行,可能是产生了FATAL日志,但是,也可以自定义Fail函数终止程序执行:
void YourFailureFunction() { // Reports something... exit(1); } int main(int argc, char* argv[]) { google::InstallFailureFunction(&YourFailureFunction); }
By default, glog tries to dump stacktrace and makes the program exit with status 1. The stacktrace is produced only when you run the program on an architecture for which glog supports stack tracing (as of September 2008, glog supports stack tracing for x86 and x86_64).
CHECK Macros但是
- glog学习
- glog学习
- glog
- glog
- glog
- glog
- glog
- glog
- glog
- Glog
- glog
- glog
- C++学习之道:glog
- 【学习笔记】Google glog 使用
- 【神经网络与深度学习】GLOG介绍
- 【神经网络与深度学习】GLog使用笔记
- Google glog
- glog 入门教程
- jstl标签
- 微信公众平台最实用的工具和技巧大集合
- javaDAY5
- PAT (Advanced Level) Practise 1114 Family Property (25) 并查集orDFS
- 母函数最终模板(备忘录)
- glog学习
- android之AsyncTask原理分析
- Rust 1.7.0 语法基础 标识符(ident)和分隔符的约束
- 人工智能的新纪元——深度学习
- Spring MVC 中 HandlerInterceptorAdapter的使用
- android服务器Bmob的使用
- 服务器搭建环境
- 石子归并(动规例题)
- 第三周项目4 --猜数字