glog简单使用

本文介绍了glog库在Windows和Linux上的编译步骤,包括CMake的使用,以及在Windows环境下配置Visual Studio项目时的额外包含目录、库目录和依赖项设置。还提及了glog的日志级别管理和性能优化方法。

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

git clone https://github.com/google/glog.git/ .

or:

svn co https://github.com/google/glog.git/ .

windows: download cmake (www.cmake.org) generate cmake .sln file(glog.sln).

linux: cmake . && make && make install

simple testcase:

#if defined(WIN32)||defined(WINDOWS)
#error("Not supported")
#include <windows.h>
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif

#include <string.h>

#define GOOGLE_GLOG_DLL_DECL
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "glog/logging.h"


bool dirExists(const std::string& dirName_in)
{
	return access(dirName_in.c_str(), F_OK) == 0;
}

#include <string>
#include <iostream>

void init_glog(const char* pname, const std::string& logDir)
{
	google::InitGoogleLogging(pname);
	FLAGS_colorlogtostderr = true;
	FLAGS_logbufsecs = 0;
	FLAGS_max_log_size = 1024 * 1024;
	FLAGS_stop_logging_if_full_disk = true;

	bool result = dirExists(logDir);
	if (!result)
	{
		::mkdir(logDir.c_str(), 0755);
	}

	char buffer[256] = {0};

	memset(buffer, 0, sizeof(buffer));
	snprintf(buffer, sizeof(buffer), "%s/info.log", logDir.c_str());
	google::SetLogDestination(google::GLOG_INFO, buffer);

	memset(buffer, 0, sizeof(buffer));
	snprintf(buffer, sizeof(buffer), "%s/warning.log", logDir.c_str());
	google::SetLogDestination(google::GLOG_WARNING, buffer);

	memset(buffer, 0, sizeof(buffer));
	snprintf(buffer, sizeof(buffer), "%s/error.log", logDir.c_str());
	google::SetLogDestination(google::GLOG_ERROR, buffer);

	memset(buffer, 0, sizeof(buffer));
	snprintf(buffer, sizeof(buffer), "%s/fatal.log", logDir.c_str());
	google::SetLogDestination(google::GLOG_FATAL, buffer);
}


int main(int argc, const char** argv)
{
	init_glog(argv[0], "./logs");

	char str[20] = "hello glog!";
	LOG(INFO) << str;
	std::string cStr = "hello google!";
	LOG(INFO) << cStr;
	LOG(INFO) << "info test" << "hello log!";
	LOG(WARNING) << "warning test";
	LOG(ERROR) << "error test";

	std::string strInput;
	std::cin >> strInput;
	while (strInput != "E" && strInput != "e")
	{
		LOG(ERROR) << strInput;
		std::cin >> strInput;
	}

	google::ShutdownGoogleLogging();

	return 0;
}


windows:

Configuration Properties->C/C++->Additional Include Directories:    E:\study\glog\glog_src\trunk\src\windows

Configuration Properties->Linker->Additional Library Directories: E:\study\glog\glog_build\Debug\

Configuration Properties->Linker->Additional Dependences:glog.lib;%(AdditionalDependencies)

make sure Configuration Properties->C/C++->Code Generation->Runtime Library is the same as the library named glog in glog.sln(MTD/MT or MDD/MD)

linux编译:

g++ -g main.cpp -lglog

优化:

按天分日志文件以及不同级别不同文件:  https://www.cppfans.org/1566.html

性能提升10倍: http://www.chinaz.com/news/2016/0414/521903.shtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值