[置顶] C++开源日志库--Glog的使用

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
 

[置顶] C++开源日志库--Glog的使用

分类: C/C++   1213人阅读  评论(0)  收藏  举报

公司其他同事大多做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


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


[plain]  view plain copy print ?
  1. 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.  
  2. 1>  SessionMgr.cpp  
  3. 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.  
  4. 1>  SessionFactory.cpp  
  5. 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.  
  6. 1>  RealTimeStreamSession.cpp  
  7. 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.  
  8. 1>  main.cpp  
  9. 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.  
  10. 1>  GNumGenerator.cpp  
  11. 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.  
  12. 1>  DevicControlSession.cpp  
  13. 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.  
  14. 1>  CatalogSesssion.cpp  
  15. 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头文件查看,是一个宏定义的地方出现了冲突:

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

解决方法:

在工程加上预编译宏GLOG_NO_ABBREVIATED_SEVERITIES

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



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


  1. #include "glog/logging.h"  
  2. int _tmain(int argc, _TCHAR* argv[])  
  3. {     
  4.     google::InitGoogleLogging((const char *)argv[0]);  //参数为自己的可执行文件名  
  5.   
  6.     google::SetLogDestination(google::GLOG_INFO,"./myInfo");  
  7.   
  8.     LOG(INFO) << "This is a <Warn> log message..." << ;  
  9.   
  10.   
  11.          .....................  
  12.   
  13. }  


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

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

公司其他同事大多做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


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


[plain]  view plain copy print ?
  1. 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.  
  2. 1>  SessionMgr.cpp  
  3. 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.  
  4. 1>  SessionFactory.cpp  
  5. 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.  
  6. 1>  RealTimeStreamSession.cpp  
  7. 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.  
  8. 1>  main.cpp  
  9. 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.  
  10. 1>  GNumGenerator.cpp  
  11. 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.  
  12. 1>  DevicControlSession.cpp  
  13. 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.  
  14. 1>  CatalogSesssion.cpp  
  15. 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头文件查看,是一个宏定义的地方出现了冲突:

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

解决方法:

在工程加上预编译宏GLOG_NO_ABBREVIATED_SEVERITIES

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



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


  1. #include "glog/logging.h"  
  2. int _tmain(int argc, _TCHAR* argv[])  
  3. {     
  4.     google::InitGoogleLogging((const char *)argv[0]);  //参数为自己的可执行文件名  
  5.   
  6.     google::SetLogDestination(google::GLOG_INFO,"./myInfo");  
  7.   
  8.     LOG(INFO) << "This is a <Warn> log message..." << ;  
  9.   
  10.   
  11.          .....................  
  12.   
  13. }  


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

猜你喜欢

转载自blog.csdn.net/hggjgff/article/details/83828404