花了一晚上時間編譯log4cxx, 總算通過了,這鳥工具,整S我了。
使用log4cxx的版本為0.97 , OS為Fedora 6. 在make的過程中,首先碰到的問題就是:
../include/log4cxx/xml/domconfigurator.h:249: error: extra qualification
'log4cxx::xml::DOMConfigurator::' on member 'subst' |
以上問題花了近二個小時,才找到答案, 答案見下:
Index: include/log4cxx/xml/domconfigurator.h
=========================================
--- include/log4cxx/xml/domconfigurator.h (revision 384243)
+++ include/log4cxx/xml/domconfigurator.h (working copy)
@@ -246,7 +246,7 @@
protected:
static LogString getAttribute(apr_xml_elem*,
const std::string& attrName);
- LogString DOMConfigurator::subst(const LogString& value);
+ LogString subst(const LogString& value); //要在原始碼中把以上函式宣告改成這句
protected:
helpers::Properties props; |
然後,編譯到 /tests/src/util/filter.cpp 時,提示“‘RegEx’ 在此範圍中尚未聲明”
進入到filter.cpp檔案中,看到該檔案使用的是boost庫中的regex功能。
#include
#include "filter.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace boost;
String Filter::merge(const String& pattern, const String& in, const String& fmt)
{
USES_CONVERSION;
std::string convPattern = T2A(pattern.c_str());
std::string convIn = T2A(in.c_str());
std::string convFmt = T2A(fmt.c_str());
std::string result = RegEx(convPattern).Merge(convIn, convFmt);
return A2T(result.c_str());
} |
首先想到的是: gcc的時候“-lboost_regex”沒有添加,在Makfile.in中添加編譯選項:"-lboost_regex",還是出現該錯誤。
最後,查看boost庫原始碼,發現RegEx函數定義在cregex.hpp中,而不是regex.hpp中,可能是boost庫已經更新(把RegEx函數移到cregex.hpp中定義),而log4cxx卻還是預設使用舊boost庫的原因。
這個錯誤更改一下標頭檔編譯就OK了。
#include
以上兩個錯誤,花起來解決的時間都要4個小時。真是無語了
本文來自紅聯
今天無聊想試試log4cxx的用法,從網上找了個例子,小修改了一下:
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"
using namespace std;
using namespace log4cxx;
using namespace log4cxx::helpers;
int main(int argc, char* argv[])
{
LoggerPtr logger(Logger::getLogger("MyApp"));
try
{
BasicConfigurator::configure();
LOG4CXX_INFO(logger, _T("你說hello"));
LOG4CXX_DEBUG(logger, "world");
}
catch (Exception&)
{
}
return 0;
} |
然後寫了個makefile:
test: test_log4cpp1.o
g++ -o test_log4cpp test_log4cpp1.o |
一運行,發現編譯通過,但是link出錯,找不到log4cxx相關的引用。網上到處搜尋資料,基本上都是說要設定LD_LIBRARY_PATH 或者是LIBRARY_PATH,可是我設定了也沒有用,還是同樣的錯誤。然後我把makefile中的命令改為:
| g++ -L/usr/local/lib -o test_log4cpp test_log4cpp1.o |
最後還是一樣的錯誤。。。
在瀕臨絕望的時候,我去查了一下gcc的文檔,無意間發現一段話,就是-l(小寫L)選項可以用來指定庫名。我抱著死馬當活馬醫的心態試了試:
| g++ -llog4cxx -o test_log4cpp test_log4cpp1.o |
竟然成功了!!真是大喜過望阿!
經過簡單的試用發現,log4cxx和log4j還是相當的像的,設定檔基本上可以一模一樣,代碼方面稍有區別,但總得還是在每個類設定一個static的logger對象,然後用LOG4CXX_LEVEL宏來寫log