##日志级别

  • INFO(0), WARNING(1), ERROR(2), FATAL(3,会导致进程结束)
  • 高级别的日志会同时记录到低级别的日志文件中
  • glog默认会将ERROR和FATAL日志输出到stderr中
  • 日志文件的默认路径为:
    /tmp/<program name>.<hostname>.<user name>.log.<severity level>.<date>.<time>.<pid>
    

##参数设置

  • GLOG_logtostderr (bool, default=false)
    输出到stderr而不是记录到文件中

  • GLOG_stderrthreshold (int, default=2, which is ERROR)
    >=此级别的日志在记录到文件的同时,也会输出到stderr

  • GLOG_minloglevel (int, default=0, which is INFO)
    只输出>=此级别的日志

  • GLOG_log_dir (string, default=””)
    修改默认的日志文件保存路径,必须在调用google::InitGoogleLogging之前设置

  • GLOG_v (int, default=0)
    输出所有<=此级别(m)的VLOG(m)

##条件日志

  • LOG_IF(INFO, num_cookies > 10) « “Got lots of cookies”;
  • LOG_EVERY_N(INFO, 10) « “Got the “ « google::COUNTER « “th cookie”; // 在第1, 11, 21, … 次执行的时候输出
  • LOG_IF_EVERY_N(INFO, (size > 1024), 10) « “Got the “ « google::COUNTER « “th big cookie”;
  • LOG_FIRST_N(INFO, 20) « “Got the “ « google::COUNTER « “th cookie”;// 只输出前N次日志

##调试输出

  • 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”;

##断言

  • CHECK: 若条件为false,则终止进程(release版本同样生效)
    CHECK(fp->Write(x) == 4) << "Write failed!";
    
  • CHECK_EQ, CHECK_NE, CHECK_LE, CHECK_LT, CHECK_GE, CHECK_GT
    CHECK_NE(1, 2) << ": The world must be ending!";
    
  • CHECK_NOTNULL(some_ptr);
  • CHECK_STREQ, CHECK_STRNE, CHECK_STRCASEEQ, CHECK_STRCASENE. 带CASE的大小写敏感
  • CHECK_DOUBLE_EQ: 检测小数是否相等
  • CHECK_NEAR: 检测小数是否相等,传入第三个参数作为最大忽略差值

##冗余输出

  • 命令行参数–v控制冗余输出的级别:
    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";
    
  • 级别越低,越可能被输出
  • 级别的值可以任意指定(-100,1,2,4,500, …)
  • VLOG只记录在INFO日志中
  • VLOG_IS_ON(n): 当–v >= n时返回true
    if (VLOG_IS_ON(2)) {
      // do some logging preparation and logging
      // that can't be accomplished with just VLOG(2) << ...;
    }
    
  • VLOG_IF, VLOG_EVERY_N, VLOG_IF_EVERY_N和LOG_IF, LOG_EVERY_N, LOF_IF_EVERY类似
    VLOG_IF(1, (size > 1024))
      << "I'm printed when size is more than 1024 and when you run the "
          "program with --v=1 or more";
    VLOG_EVERY_N(1, 10)
      << "I'm printed every 10th occurrence, and when you run the program "
          "with --v=1 or more. Present occurence is " << google::COUNTER;
    VLOG_IF_EVERY_N(1, (size > 1024), 10)
      << "I'm printed on every 10th occurence of case when size is more "
          " than 1024, when you run the program with --v=1 or more. ";
          "Present occurence is " << google::COUNTER;
    

##崩溃日志

  • 可通过google::InstallFailureSignalHandler()设置崩溃日志,崩溃记录如下:
    *** Aborted at 1225095260 (unix time) try "date -d @1225095260" if you are using GNU date ***
    *** SIGSEGV (@0x0) received by PID 17711 (TID 0x7f893090a6f0) from PID 0; stack trace: ***
    PC: @           0x412eb1 TestWaitingLogSink::send()
      @     0x7f892fb417d0 (unknown)
      @           0x412eb1 TestWaitingLogSink::send()
      @     0x7f89304f7f06 google::LogMessage::SendToLog()
      @     0x7f89304f35af google::LogMessage::Flush()
      @     0x7f89304f3739 google::LogMessage::~LogMessage()
      @           0x408cf4 TestLogSinkWaitTillSent()
      @           0x4115de main
      @     0x7f892f7ef1c4 (unknown)
      @           0x4046f9 (unknown)
    
  • 崩溃日志默认输出到stderr,可通过InstallFailureWriter()修改

##其他 ###自定义错误日志行为(FATAL, CHECK)

void YourFailureFunction() {
    // Reports something...
    exit(1);
}

int main(int argc, char* argv[]) {
    google::InstallFailureFunction(&YourFailureFunction);
}

###Google风格的perror()

  • PLOG(), PLOG_IF(), PCHECK()与它们的LOG*, CHECK类似,只是在输出的最后添加了错误描述(类似于perror):
    PCHECK(write(1, NULL, 2) >= 0) << "Write NULL failed";
    F0825 185142 test.cc:22] Check failed: write(1, NULL, 2) >= 0 Write NULL failed: Bad address [14]
    

###去除日志中的字符串

  • 用GOOGLE_STRIP_LOG设置去除字符串的级别
    #define GOOGLE_STRIP_LOG 1    // this must go before the #include!
    #include <glog/logging.h>
    // 将会去除等级低于一的日志中的字符串,由于VLOG的等级是INFO(0),所以VLOG也将被去除
    

详见官方文档:How To Use Google Logging Library (glog)