1. 程式人生 > >[置頂] C++開源日誌庫--Glog的使用

[置頂] 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標頭檔案檢視,是一個巨集定義的地方出現了衝突:

[cpp]  view plain copy print ?
  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巨集   儲存,編譯通過~



第五步,自己的專案中使用


[cpp]  view plain copy print ?
  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標頭檔案檢視,是一個巨集定義的地方出現了衝突:

[cpp]  view plain copy print ?
  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巨集   儲存,編譯通過~



第五步,自己的專案中使用


[cpp]  view plain copy print ?
  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. }  


搞定,後面就是將這些日誌在工程中使用起來了。