glog

glog 0.3.5下载编译,使用Cmake选择VS2015 X64编译release,得到glog.dll。再使用VS2015 X64 release创建一个测试代码如下:

// testglog.cpp : 定义控制台应用程序的入口点。
//
//#include <stdafx.h>
#define GLOG_NO_ABBREVIATED_SEVERITIES
#define GOOGLE_GLOG_DLL_DECL
#include <glog/logging.h>
//#include <glog/log_severity.h>


#pragma comment(lib, "glog.lib")

int main(int argc, char* argv[])

{

	google::InitGoogleLogging((const char *)argv[0]);

	google::SetLogDestination(google::GLOG_INFO, "./myInfo");

	LOG(WARNING) << "this is the first warning!";

	getchar();


	return 0;

}


编译器需要配置:

1.C/C++选项 添加 附加包含目录

2.链接器选项 添加 附加库目录

3.链接器选项 输入 添加  附加依赖项 glog.lib:

4.C/C++选项  代码生成项  运行库,把MD 改为 MT

5.预编译头改为不使用预编译头


会在源代码同级目录生成log文件,类似如下:




详细参考网址

===================================20170920======

1.多次调用InitGoogleLogging问题修改:

当在dll中多次引用LOG(INFO)时出现问题:

Check failed: !IsGoogleLoggingInitialized() You called InitGoogleLogging() twice!

声明一个全局变量bool glog_initialized = false; 然后在初始化处做一个类似判断:

......
// glog
	if (!glog_initialized)
	{
		glog_initialized = true;
		google::InitGoogleLogging("log");
	}
......


2.时间修改:

if (FLAGS_log_prefix && (line != kNoLogPrefix)) {
    stream() << LogSeverityNames[severity][0]
			 << ' '
			 << setw(4) << 1900 + data_->tm_time_.tm_year     << '-'
             << setw(2) << 1+data_->tm_time_.tm_mon  << '-'
             << setw(2) << data_->tm_time_.tm_mday
             << ' '
             << setw(2) << data_->tm_time_.tm_hour  << ':'
             << setw(2) << data_->tm_time_.tm_min   << ':'
             << setw(2) << data_->tm_time_.tm_sec   << "."
             << setw(6) << usecs
             << ' '
             << setfill(' ') << setw(5)
             << static_cast<unsigned int>(GetTID()) << setfill('0')
             << ' '
             << data_->basename_ << ':' << data_->line_ << "] ";
  }


输出:



3.wchar_t *转换为char *

	size_t i;
	char ch_glog_command[MAX_PATH];
	wchar_t *cStr = GetCommandLine();
	wcstombs_s(&i, ch_glog_command, (size_t)MAX_PATH, 
               cStr, (size_t)MAX_PATH );


4.WCHAR*转为char*

typedef wchar_t WCHAR;    // wc,   16-bit UNICODE character

	TcharToChar(path, ch_glog);
	LOG(INFO) << "path: " << ch_glog;




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值