// Usage: g++ glogtest.cpp -o glogtest -lglog
// reference: * https://blog.csdn.net/Solomon1558/article/details/52558503
// * https://zhuanlan.zhihu.com/p/26025722
#include<iostream>#include<string>#include<glog/logging.h>intmain(intargc,char**argv){FLAGS_alsologtostderr=1;google::InitGoogleLogging(argv[0]);//通过SetLogDestination可能没有设置log_dir标志位的方式方便(会遗漏一些日志)
//google::SetLogDestination(google::GLOG_INFO, "/tmp/today");
//标志位
FLAGS_colorlogtostderr=true;//设置输出颜色
FLAGS_v=std::atoi(argv[1]);//设置最大显示等级(超过该等级将不记录log)
FLAGS_log_dir="./logs";LOG(INFO)<<"Found "<<google::COUNTER<<" arguments!";// assert
CHECK(access(argv[2],0)!=-1)<<"No such file: "<<argv[2];LOG(INFO)<<"I am INFO!";LOG(WARNING)<<"I am WARNING!";LOG(ERROR)<<"I am ERROR!";//VLOG用来自定义日志, 可以在括号内指定log级别
VLOG(1)<<"[Custom log(VLOG)] Level 1!";VLOG(2)<<"[Custom log(VLOG)] Level 2!";VLOG(3)<<"[Custom log(VLOG)] Level 3!";VLOG(4)<<"[Custom log(VLOG)] Level 4! This is used for detailed message which need not to be printed each time.";VLOG(5)<<"[Custom log(VLOG)] Level 5! On this level messages are print as detail as possible for debugging.";LOG(FATAL)<<"I am FATAL!";return0;}
LOG:内置日志,可以看做一个ostream,向屏幕输出的同时可以重定向到其他位
INFO / WARNING / ERROR / FATAL:代表了不同的log级别。其中FATAL的出现会直接导致程序中断。ERROR不会中断程序,但是对应的LOG会显示为红色;WARNING对应LOG会显示为黄色。
LOG_IF(INFO,num_cookies>10)<<"Got lots of cookies";LOG_EVERY_N(INFO,10)<<"Got the "<<google::COUNTER<<"th cookie";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";
Debug Mode
1
2
3
4
5
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";
It is a good practice to check expected conditions in your program frequently to detect errors as early as possible. The CHECK macro provides the ability to abort the application when a condition is not met, similar to the assert macro defined in the standard C library.
1
2
3
4
5
6
7
8
9
CHECK(fp->Write(x)==4)<<"Write failed!";// macros for equality/inequality checks - CHECK_EQ,
// CHECK_NE, CHECK_LE, macros for equality/inequality checks - CHECK_EQ, CHECK_NE, CHECK_LE, CHECK_LT, CHECK_GE, and CHECK_GT.CHECK_LT, CHECK_GE, and CHECK_GT.
CHECK_NE(1,2)<<": The world must be ending!";CHECK_EQ(string("abc")[1],'b');CHECK_NOTNULL(some_ptr);some_ptr->DoSomething();