C++开源日志库--Glog的使用

公司其他同事大多做C#的,公司内部暂时也没用提供自己的C++日志库,由于项目较紧急,所以就准备选一个开源日志库使用,由于以前做过java,用的Log4j比较强大,但是查了下,其使用起来有点复杂。所以就想到最伟大的公司google了,其Glog使用还是比较简单的,源码下下来直接用VS编译生成lib和dll库,源码文件中都有现成的vs工程。


开源项目首页:https://code.google.com/p/google-glog/

Glog项目路径: https://code.google.com/p/google-glog/downloads/list


第一步,下载glog-0.3.3.tar.gz,解压,直接打开google-glog.sln工程文件,如果vs版本不对,让其自动转换


第二步,编译,在Debug下生成libglog.dll、 libglog.lib、libglog_static.lib


第三步,将头文件和lib库拷贝到自己的工程下,由于我暂时是window下使用,头文件使用 \glog-0.3.3\src\windows\glog


第四步,引用到自己工程下,编译发现报错:


1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>  SessionMgr.cpp
1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>  SessionFactory.cpp
1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>  RealTimeStreamSession.cpp
1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>  main.cpp
1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>  GNumGenerator.cpp
1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>  DevicControlSession.cpp
1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
1>  CatalogSesssion.cpp
1>d:\workspace\video\videomanage\devicemgr\lib\glog\glog\log_severity.h(55): fatal error C1189: #error :  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.

进入log_severity.h头文件查看,是一个宏定义的地方出现了冲突:

#ifndef GLOG_NO_ABBREVIATED_SEVERITIES
# ifdef ERROR
#  error ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h. See the document for detail.
# endif
const int INFO = GLOG_INFO, WARNING = GLOG_WARNING,
  ERROR = GLOG_ERROR, FATAL = GLOG_FATAL;
#endif

解决方法:

在工程加上预编译宏GLOG_NO_ABBREVIATED_SEVERITIES

C/C++   -->  预处理器   -->   预处理器定义   -->  加上GLOG_NO_ABBREVIATED_SEVERITIES宏   保存,编译通过~



第五步,自己的项目中使用


#include "glog/logging.h"
int _tmain(int argc, _TCHAR* argv[])
{	
	google::InitGoogleLogging((const char *)argv[0]);  //参数为自己的可执行文件名

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

	LOG(INFO) << "This is a <Warn> log message..." << ;


         .....................

}


搞定,后面就是将这些日志在工程中使用起来了。





附件是VS2010的工程,C++日志类,谷歌的东西,很好用,也很强大哦! glog简介 Google glog是一个基于程序级记录日志信息的c++,编程使用方式与c++的stream操作类似,例: LOG(INFO) << "Found " << num_cookies << " cookies"; “LOG”宏为日志输出关键字,“INFO”为严重性程度。 主要支持功能: 1, 参数设置,以命令行参数的方式设置标志参数来控制日志记录行为; 2, 严重性分级,根据日志严重性分级记录日志; 3, 可有条件地记录日志信息; 4, 条件中止程序。丰富的条件判定宏,可预设程序终止条件; 5, 异常信号处理。程序异常情况,可自定义异常处理过程; 6, 支持debug功能。可只用于debug模式; 7, 自定义日志信息; 8, 线程安全日志记录方式; 9, 系统级日志记录; 10, google perror风格日志信息; 11, 精简日志字符串信息。 开源代码托管 开源代码地址:https://github.com/google/glog 其实官方开源代码已经有大量demo可以参考了,也提供了VS可以直接打开编译的项目。 如何使用 1:把glog文件夹拷贝到源代码目录 2:在工程设置中添加附加包含目录(glog\include;)和附加目录(glog\lib;),在附件依赖项中添加对应lib文件,一一对应关系如下: MDd libglog32MDd.lib MD libglog32MD.lib MTd libglog32MTd.lib MT libglog32MT.lib 建议使用MDd和MD方式,带上对应的dll(在glog\bin目录,需要时拷贝到bin文件输出目录)可以避免使用MTd,MT引起的内存泄露是值得的。 #include #include using namespace std; //包含glog头文件,建议放在stdafx.h中 //#define GOOGLE_GLOG_DLL_DECL // 使用静态的时候用这个,不过我测试静态有内存泄露,所以不推荐使用静态 #define GLOG_NO_ABBREVIATED_SEVERITIES #include "glog/logging.h" //获取当前程序的运行目录 string GetAppPathA() { char szExePath[MAX_PATH] = {0}; GetModuleFileNameA(NULL,szExePath,MAX_PATH); char *pstr = strrchr(szExePath,'\\'); memset(pstr+1,0,1); string strAppPath(szExePath); return strAppPath; } void main() { //glog初始化 google::InitGoogleLogging("重签程序"); string strLogPath = GetAppPathA().append("LogInfo\\"); CreateDirectoryA(strLogPath.c_str(),NULL); google::SetLogDestination(google::GLOG_INFO,strLogPath.c_str()); //功能测试 LOG(INFO)<<"log start...";//普通日志 LOG(WARNING)<<"Warning log";//警告日志 LOG(ERROR)<<"Error log";//错误日志 int i = 4; LOG_IF(INFO,i == 4)<<"Log if Test"; //以上就是我常用的几个日志函数了,当然还有很多更加强大的日志相关函数,大家如有有兴趣,可以参照官方给的示例使用, //开源代码地址:https://github.com/google/glog MessageBoxA(NULL,"Test Over",":)",MB_ICONINFORMATION); } 测试程序中,我使用的动态链接方式。(Debug模式中代码生成为MDd,Release为MD)。lib是截止现在2015-11-04-21:35是最新的。采用VS2010编译,MTd,MT,MDd,MD方式编译在测试项目中都有提供。 博文地址: http://blog.csdn.net/sunflover454/article/details/49643625
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值