glog的使用

编译步骤

请参考下面的链接
https://blog.csdn.net/wei242425445/article/details/87968490

初步使用

编写初始化代码

#include <glog/logging.h>

int main(int argc, char* argv[]) 
{
	// Initialize Google's logging library.
	// argv[0]只是把程序名称传递给日志
	google::InitGoogleLogging(argv[0]);

	// Record.
	LOG(INFO)<<"hello, world";

	return 0;
}

编写CMakeLists.txt

需要关联libglog.a

cmake_minimum_required(VERSION 2.8)

project(demo)

SET(CMAKE_BUILD_TYPE "Release")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -fPIC")

aux_source_directory(. DIRSRCS)

add_executable(demo ${DIRSRCS})
target_link_libraries(demo libglog.a)

编译运行

$ mkdir build && cd build
$ cmake ..
$ make
$ ./demo

生成的日志将会出现在/tmp文件夹中

wilson@ubuntu:/tmp$ cat demo.ubuntu.wilson.log.INFO.20200307-010131.11749 
Log file created at: 2020/03/07 01:01:31
Running on machine: ubuntu
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg
I0307 01:01:31.396569 11749 Main.cpp:9] hello, world

输出到终端

添加全局变量GLOG_logtostderr=1或者在程序执行的时候加上下面的传参

$ GLOG_logtostderr=1 ./demo 
I0307 05:59:04.701386 15216 Main.cpp:12] hello, world

更改日志生成目录

//Set log directory
FLAGS_log_dir = "log/";

让日志同时输出到终端

// Set whether log messages go to stderr in addition to logfiles.
FLAGS_alsologtostderr = true;

设置最大日志文件大小

// Sets the maximum log file size (in MB).
FLAGS_max_log_size = 1;

显示日志在终端

//If set this flag to 1, the log will show in the terminal
FLAGS_logtostderr = 1;

不显示日志在终端

//And if set to 0, the log will show in the file
FLAGS_logtostderr = 0;

条件日志

int count = 1;
LOG_IF(INFO, count > 10) << "count is greater than 10";
//LOG_IF(INFO, count < 10) << "count is less than 10";

前n条记录

for (size_t i = 0; i < 30; i++)
{
	LOG_FIRST_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
}

每n条记录一次

for (size_t i = 0; i < 30; i++)
{
	LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
}

条件次数记录

for (size_t i = 0; i < 30; i++)
{
	LOG_IF_EVERY_N(INFO, (i > 20), 10) << "Got the " << google::COUNTER << "th big cookie";
}

断言

// check 1==4, if not, then write failed and then crash to find the error as soon as possible
CHECK(1 == 4) << "Write failed!";

Verbose日志

判断verbose是否打开

if (VLOG_IS_ON(1))
{
	LOG(INFO) << "level 1";
}
else
{
	LOG(INFO) << "not level 1";
}

打印日志

VLOG(1) << "test 1";
VLOG(2) << "test 2";
VLOG(3) << "test 3";
VLOG_IF(1, 1 > 1024)<<"test 4";
VLOG_EVERY_N(1, 10)<<"test 5";
VLOG_IF_EVERY_N(1, 1 > 1024, 10)<<"test 6";

运行程序

GLOG_v=2 build/demo

如果运行时v=1,那么只打印test 1;如果v=2,那么会同时打印test1和test2

I0310 14:05:03.708504   512 Main.cpp:71] test 1
I0310 14:05:03.708515   512 Main.cpp:72] test 2

接收程序异常信号

google::InstallFailureSignalHandler();

程序汇总

#include <iostream>
#include <glog/logging.h>

void FailureFunction() 
{
	std::cout<<"FailureFunction happened"<<std::endl;
    exit(1);
}

int main(int argc, char *argv[])
{
	//Set log directory
	FLAGS_log_dir = "log/";

	// Set whether log messages go to stderr in addition to logfiles.
	FLAGS_alsologtostderr = true;

	// Sets the maximum log file size (in MB).
	FLAGS_max_log_size = 1;

	// Initialize Google's logging library.
	google::InitGoogleLogging(argv[0]);

	google::InstallFailureFunction(&FailureFunction);
	google::InstallFailureSignalHandler();

	//make error in manual
	//char *a = NULL;
	//std::cout<<a[3]<<std::endl;

	//If set this flag to 1, the log will show in the terminal
	FLAGS_logtostderr = 1;

	//LOG(INFO) << "hello, world1";

	//And if set to 0, the log will show in the file
	FLAGS_logtostderr = 0;

	//LOG(INFO) << "hello, world2";

	FLAGS_logtostderr = 1;

	int count = 1;
	LOG_IF(INFO, count > 10) << "count is greater than 10";
	//LOG_IF(INFO, count < 10) << "count is less than 10";

	for (size_t i = 0; i < 30; i++)
	{
		//LOG_FIRST_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";

		//LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";

		//LOG_IF_EVERY_N(INFO, (i > 20), 10) << "Got the " << google::COUNTER << "th big cookie";
	}

	FLAGS_alsologtostderr = true;
	FLAGS_logtostderr = 0;
	while (true)
	{
		char s = getchar();
		if (s == 'b')
		{
			break;
		}
		else if (s == 'a')
		{
			DLOG(INFO) << "Only show in debug mode";
		}
	}

	// check if 1==4, if not, then write failed and then crash to find the error as soon as possible
	//CHECK(1 == 4) << "Write failed!";

	if (VLOG_IS_ON(1))
	{
		LOG(INFO) << "level 1";
	}
	else
	{
		LOG(INFO) << "not level 1";
	}

	VLOG(1) << "test 1";
	VLOG(2) << "test 2";
	VLOG(3) << "test 3";
	VLOG_IF(1, 1 > 1024)<<"test 4";
	VLOG_EVERY_N(1, 10)<<"test 5";
	VLOG_IF_EVERY_N(1, 1 > 1024, 10)<<"test 6";

	return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值