参考前人引路章节:http://www.codelast.com/?p=3211.
简单地说,log4cxx就是一个记录日志的C++库(程序运行的时候要保存一些日志到文件,以供将来查看),它是从著名的Java日志库log4j移植而来的,并且它是Apache的一个项目,因为自己需要日志系统来对服务器进行记录,并派生出属于自己的日志库
补一句:Apache声称log4cxx的速度快、灵活性好,但是,速度快是第一位的,灵活性是第二位的("Log4cxx claims to be fast and flexible: speed first, flexibility second."),所以,担心日志记录性能的同学更可以选择log4cxx啦。
系统环境:Ubuntu:
首先去log4cxx的官方网站下载源码安装包:http://logging.apache.org/log4cxx/,点击左边的“Download”进入下载页面,当前(2011年8月9日)的最新版本是0.10.0,你可以下载 .tar.gz 压缩包,解压出来即可。
然后就可以直接configure,make,make install了吗?不行,因为log4cxx官方提供的源码安装包不是一个all-in-one的包,它还依赖于Apache的另外两个库:Apache Portable Runtime(APR)和Apache Portable Runtime Utility(APR-Util),你可以在这个链接找到它们:http://apr.apache.org/。
(1)安装
安装顺序不能变。首先要安装APR,下载到源码安装包后,解压出来,然后:
1
2
3
|
. /configure --prefix= /usr/local/apr
make
make install
|
这里把APR安装到了 /usr/local/apr 目录下,注意,千万不要直接./configure,因为那样会把APR的文件安装到若干目录下,非常不利于维护。
再安装APR-Util,和上面一样,解压出来源码安装包,然后:
1
2
3
|
. /configure --prefix= /usr/local/apr-util --with-apr= /usr/local/apr
make
make install
|
这里,通过 --with-apr 参数指定了前一步安装的APR的目录,同时也将APR-Util的安装目录设置在了/usr/local/apr-util 目录下。
再安装libtool: apt-get install libtool
源代码注意:
1.vim src/main/cpp/inputstreamreader.cpp
增加#include <string.h>;
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/inputstreamreader.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/helpers/bytebuffer.h>
+
#include <string.h>
+
否则会出现inputstreamreader.cpp:66: error: 'memmove' was not declared in this scope
make[3]: *** [inputstreamreader.lo] 错误 1
2.vim src/main/cpp/socketoutputstream.cpp
增加#include <string.h>;
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/socketoutputstream.h>
#include <log4cxx/helpers/socket.h>
#include <log4cxx/helpers/bytebuffer.h>
+
#include <string.h>
+
否则会出现socketoutputstream.cpp:52: error: 'memcpy' was not declared in this scope
3.vim src/examples/cpp/console.cpp
增加#include <string.h>,#include <stdio.h>;
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <log4cxx/logger.h>
#include <log4cxx/consoleappender.h>
#include <log4cxx/simplelayout.h>
#include <log4cxx/logmanager.h>
#include <iostream>
#include <locale.h>
否则会出现
console.cpp: In function ‘int main(int, char**)’:
console.cpp:58: 错误:‘puts’在此作用域中尚未声明
最后,就是安装log4cxx了,但是在安装之前,还要再注意一点:configure的时候要指定APR和APR-Util的安装路径:
1
2
3
4
5
|
. /configure --prefix= /usr/local/log4cxx --with-apr= /usr/local/apr --with-apr-util= /usr/local/apr-util
mv libtool libtool.bak
ln -s /usr/bin/libtool libtool
make
make install
|