歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++開源日志庫--Glog的使用

C++開源日志庫--Glog的使用

日期:2017/3/1 9:54:54   编辑:Linux編程

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


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

}


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

Copyright © Linux教程網 All Rights Reserved