突然好想你钢琴曲:Google-Glog

来源:百度文库 编辑:偶看新闻 时间:2024/05/02 15:16:10

Google-Glog

邱金武通用编程 No Comments

      Glog是Google主持的一个在开源项目,项目主页在http://code.google.com/p/google-glog/ 。如其名,该项目是一个日志系统,官方的介绍“The glog library implements application-level logging. This library provides logging APIs based on C++-style streams and various helper macros. ”。我们可以通过几个简单的宏实现日志功能。个人总结若干优点:

  1. 使用简单,记录日志通过C++流操作方式进行,相当之方便
  2. 多线程安全
  3. 跨平台
  4. 日志级别和级别过滤
  5. 支持调用堆栈输出
  6. 多后端支持,常用的日志文件、Stderr等可以同时输出或者输出到其中一个,此外还有其他的后端输出(貌似有一个Email输出)。
  7. 良好的可配置性,默认的配置会通过环境变量完成初始化,手册中提到如果有安装Google的另一个工具gflags,可以通过它来配置,不过没有研究。此外也可以在程序中初始化这些参数。
  8. 良好的可控性,可以中止日志,可以设置日志最大大小等等,具体见源码中的配置参数。

Introduction (来自官方手册)

      Google glog is a library that implements application-level logging. This library provides logging APIs based on C++-style streams and various helper macros. You can log a message by simply streaming things to LOG(), e.g.

#include int main(int argc, char* argv[]) {// Initialize Google's logging library.google::InitGoogleLogging(argv[0]);// ...LOG(INFO) << "Found " << num_cookies << " cookies";}

      Google glog defines a series of macros that simplify many common logging tasks. You can log messages by severity level, control logging behavior from the command line, log based on conditionals, abort the program when expected conditions are not met, introduce your own verbose logging levels, and more. This document describes the functionality supported by glog. Please note that this document doesn’t describe all features in this library, but the most useful ones. If you want to find less common features, please check header files under src/glog directory.

 

      默认情况下,GLog和Windows.h会打架,究其原因是Glog定义了一个标识符ERROR,而Windows.h中也有定义。官方给出了办法:

      Google glog defines a severity level ERROR, which is also defined in windows.h There are two known workarounds to avoid this conflict:

      不过个人觉得这个改法不靠谱,很容易导致不明真相的错误和异常。所以尝试着改Glog。原理很简单,既然这个标识符打架,那把Glog中的同名标识符改成其他的咯。打开查找/替换工具,将ERROR 替换GERROR(你希望的名字),不过如果你这么做了,肯定编译不过。

      导致冲突的ERROR标识符定义在文件“glog\log_severity.h”中,类型为const int,不过在里面还定义一个ERROR的字符串,所以在替换时要区别这两个标识符。此外Glog中的一些宏也需要小改,COMPACT_GOOGLE_LOG_ERROR需要改成COMPACT_GOOGLE_LOG_GERROR,具体为啥自己看源码。

      前面提到,GLog使用相当之方便,在程序中使用Glog相关的宏,然后使用类似C++流的方式附上需要记录的内容。不过默认情况下Glog仅仅向Stderr输出,所以为了向日志文件输出,需要对其初始化,这通过函数void InitGoogleLogging(const char* argv0)来完成,至于日志文件的名称,Glog通过 argv0和其他众多参数动态生成一个日志文件名(这点我不是很喜欢,所以修改源码改成固定的格式)

      输出目录作为一个选项存在,默认通过环境变量获取,如果无法获取,则试着尝试往可能的临时目录写。Glog如果发现日志文件已经存在,不会覆盖,如果尝试所以目录都这样,则会失败(这点我也不是很喜欢,所以修改源码改成直接覆盖)。Glog还有众多选项,这些选项最初都是通过读取环境变量进行赋值。这些环境变量都有一个统一的前缀GLOG_。由于历史原因,另一些google_前缀的环境变量也能实现同样的效果。

      不要试着在程序开始时写环境变量来初始化Glog(我之前就这么搞,因为还没有发现其他方式进行初始化),因为它比你还快,这些选项都是静态变量,通过一些宏直接进行初始化。为了在程序中定制Glog,可以对“glog\logging.h”中使用DECLARE_前缀宏进行声明的变量进行直接赋值。例如我希望日志往当前目录输出,可以FLAGS_log_dir = "./"。

 

      The following flags are most commonly used:(来自官方手册)

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 INFO,WARNINGERROR, 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 =. 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). overrides any value given by –v. See also the section about verbose logging.