GLOG中的(Verbose Logging)VLOG的使用

本文展示了glog库在C++中的使用,包括初始化、INFO、WARNING、ERROR等级别的日志输出,以及通过GLOG_v参数控制日志详细级别的功能。通过示例代码和运行结果,说明了如何通过gflag设置来调整日志输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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进行配置。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值