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 to true by specifying 1true, or yes (case insensitive). Also, you can set binary flags to false by specifying 0false, or no (again, case insensitive).
stderrthreshold (int, default=2, which is ERROR)
Copy log messages at or above this level to stderr in addition to logfiles. The numbers of severity levels INFOWARNINGERROR, and FATAL are 0, 1, 2, and 3, respectively.
minloglevel (int, default=0, which is INFO)
Log messages at or above this level. Again, the numbers of severity levels INFOWARNINGERROR, and FATAL 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 for m 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.

4. 记录特定条件日志

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.


5、Debug日志

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";

6、CHECK 宏

在标注C库里,我们经常用到assert 来判断程序是否进入非法状态,以至于尽快abort程序。

CHECK与assert 类似,但是不同。CHECK的执行不会参照NDEBUG,所以,在任何时候,check都会被执行。

check 判断条件是否正确,如果条件不正确,则生成FATAL 日志记录,并且终止程序。

多种check:

整数类型:  

CHECK_NE(1, 2)

CHECK_EQCHECK_NECHECK_LECHECK_LTCHECK_GE, and CHECK_GT

字符串类型:

 CHECK_STREQCHECK_STRNECHECK_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";

可以自定义vlog日志的等级。可以自定义显示那些日志

--v=0

与LOG一样,它也有很多条件选择。


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但是


0 0