gflag使用小案例:
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include "glog/logging.h"
#include <gflags/gflags.h>
#include "helloWorld.h"
using namespace std;
set<int> person::intrest;
void testGlog(){
google::InitGoogleLogging("daqing"); //初始化一个log
FLAGS_logtostderr = 1; //输出到控制台
LOG(INFO) << "info test"; //输出一个Info日志
LOG(WARNING) << "warning test"; //输出一个Warning日志
LOG(ERROR) << "error test"; //输出一个Error日志
// LOG(FATAL) << "fatal test"; //输出一个Fatal日志,这是最严重的日志并且输出之后会中止程序,暂时注掉
LOG(INFO)<<"info test lastone";
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(5) << "I'm printed when you run the program with --v=5 or higher";
google::ShutdownGoogleLogging(); //不用log的时候应该释放掉,不然会内存溢出
}
int main(int argc, char* argv[]) {
testGlog();
return 0;
}
运行结果:
[root@second-1492d9693 ClionProject]# make
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/helloWorld.cpp.o
Linking CXX executable main
[100%] Built target main
[root@second-1492d9693 ClionProject]# GLOG_v=5 ./main
I0818 16:21:48.238721 55864 helloWorld.cpp:24] info test
W0818 16:21:48.238860 55864 helloWorld.cpp:25] warning test
E0818 16:21:48.238867 55864 helloWorld.cpp:26] error test
I0818 16:21:48.238871 55864 helloWorld.cpp:28] info test lastone
I0818 16:21:48.238880 55864 helloWorld.cpp:29] I'm printed when you run the program with --v=1 or higher
I0818 16:21:48.238884 55864 helloWorld.cpp:30] I'm printed when you run the program with --v=2 or higher
I0818 16:21:48.238889 55864 helloWorld.cpp:31] I'm printed when you run the program with --v=5 or higher
[root@second-1492d9693 ClionProject]# GLOG_v=3 ./main
I0818 16:21:52.806113 55936 helloWorld.cpp:24] info test
W0818 16:21:52.806288 55936 helloWorld.cpp:25] warning test
E0818 16:21:52.806296 55936 helloWorld.cpp:26] error test
I0818 16:21:52.806303 55936 helloWorld.cpp:28] info test lastone
I0818 16:21:52.806314 55936 helloWorld.cpp:29] I'm printed when you run the program with --v=1 or higher
I0818 16:21:52.806320 55936 helloWorld.cpp:30] I'm printed when you run the program with --v=2 or higher
可以看到通过GLOG_v=num就可以根据数字控制打印的log,当然还有更加复杂的设置可以通过gflag来进行设置。可以参考:glog#verbose-logging
还可以通过设置其他的设置:glog#setting-flags来对glog进行配置。