歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> log4cxx-win7-調試成功

log4cxx-win7-調試成功

日期:2017/3/1 10:39:13   编辑:Linux編程
1.介紹

Log4cxx是開放源代碼項目Apache Logging Service的子項目之一,是Java社區著名的log4j的c++移植版,用於為C++程序提供日志功能,以便開發者對目標程序進行調試和審計。有關 log4cxx的更多信息可以從 Apache Loggin Service的網站 http://logging.apache.org 獲得。當前的最新版本為0.10.0,本文內容及示例代碼都是基於此版本。

2.獲取軟件包

1、 從官方 http://apr.apache.org/download.cgi下載下面二個軟件包:

Win32 Source: apr-1.4.5-win32-src.zip [PGP] [MD5] [SHA1]

Windows Source: apr-util-1.3.12-win32-src.zip [PGP] [MD5]

官方 http://logging.apache.org/log4cxx/download.html 下載:

Apache log4cxx 0.10.0 (tar.gz) apache-log4cxx-0.10.0.tar.gz apache-log4cxx-0.10.0.tar.gz.md5 apache-log4cxx-0.10.0.tar.gz.asc Apache log4cxx 0.10.0 (zip) apache-log4cxx-0.10.0.zip apache-log4cxx-0.10.0.zip.md5 apache-log4cxx-0.10.0.zip.asc
下載tar.gz 和zip包都可。

2、或者從本人的資源上下載: http://download.csdn.net/detail/Qyee16/3662176 (資源分1分)

3.解壓三個軟件包,得到

1.) apache-log4cxx-0.10.0 2.) apr-1.4.5-win32-src 3.)apr-util-1.3.12-win32-src

因為其解壓目錄下面分別存在其同名文件,因此需要把其分別復制出來,分別重命名為:apache-log4cxx、 arp、 apr-util,置於同一目錄下,我放置在D:\log4cxx目錄下

4. 執行bat文件

打開cmd命令行,切換到D:\log4cxx\apache-log4cxx(因為上面我建立的目錄是 D:\log4cxx

(1)鍵入:configure (執行configure.bat

如下則成功:

已復制 1個文件

已復制 1個文件

其實際完成這個任務:configure.bat copies the prefabricated log4cxx.hw and private/log4cxx_private.hw over to log4cxx.h and private/log4cxx_private.h.

(2)鍵入:configure-aprutil (執行configure-aprutil.bat

如下則成功:

其實際完成的工作是: "sed" to modify apu.hw and apr_ldap.hw to disable APR-Iconv and LDAP which are not necessary for log4cxx and problematic to build. If "sed" is not available, the modifications would be trivial to do in any text editor.

常見錯誤:上面看到了,使用“sed”命令,“sed”命令是unix命令,在執行時window會報不是其“內部或外部命令”。

解決方法:就是下載安裝Cygwin 可以從官方:http://www.cygwin.com/下載,本人提供的下載包裡面也已經包含。安裝很簡單但是安裝完成在把其安裝目錄下的bin包含到系統環境變量中

本人安裝目錄為: C:\Cygwin,則把:C:\Cygwin\bin 包含到系統Path中。

5.編譯生成lib 、dl文件

1、使用vc.net2003打開Projects文件夾下的log4cxx.dsw項目,將log4cxx工程設為啟動項目,然後編譯,

2、漫長的等待, 編譯成功後,就可以在projects的Debuge或者Release文件夾下看到lib和dll文件

備注:如果使用vs2010出現錯誤,請使用vs2008編譯。


6.測試

1.打開vs2008,新建一個win32控制台工程Test

2.右鍵點擊Test工程,選擇”屬性”,然後在C++選項卡中添加附加庫目錄,注意該目錄為../ apache-log4cxx-0.10.0\src\main\include

3.在屬性的鏈接器輸入選項卡的”附加依賴項”中添加” log4cxx.lib” ,同時把上面編譯的log4cxxlib、log4cxx.dll拷貝到Test工程目錄下面

4.在cpp文件中輸入如下測試代碼:

[cpp]
  1. // log4cppTest.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <string>
  5. #include <log4cxx/logger.h>
  6. #include <log4cxx/propertyconfigurator.h>
  7. #include <log4cxx/helpers/exception.h>
  8. using namespace log4cxx;
  9. using namespace log4cxx::helpers;
  10. using namespace std;
  11. int main(int argc, char* argv[])
  12. {
  13. string msg = "log4cxx.properties";
  14. LoggerPtr Logger = Logger::getRootLogger();
  15. log4cxx::PropertyConfigurator::configure(msg);
  16. Logger->info(_T("Hello world"));
  17. LOG4CXX_INFO(Logger, "你好");
  18. getchar();
  19. return 0;
  20. }

5.新建一個文本文件,命名為log4cxx.properties,並鍵入如下內容:(也是放在Test工程目錄下面)

[plain]
  1. # 設置root logger為DEBUG級別,使用了ca和fa兩個Appender
  2. log4j.rootLogger=DEBUG, ca, fa
  3. #對Appender fa進行設置:
  4. # 這是一個文件類型的Appender,
  5. # 其輸出文件(File)為./output.log,
  6. # 輸出方式(Append)為覆蓋方式,
  7. # 輸出格式(layout)為PatternLayout
  8. log4j.appender.fa=org.apache.log4j.FileAppender
  9. log4j.appender.fa.File=./output.log
  10. log4j.appender.fa.Append=false
  11. log4j.appender.fa.layout=org.apache.log4j.PatternLayout
  12. log4j.appender.fa.layout.ConversionPattern=%d [%t] %-5p %.16c - %m%n
  13. #對Appender ca進行設置
  14. # 這是一個控制台類型的Appender
  15. # 輸出格式(layout)為PatternLayout
  16. log4j.appender.ca=org.apache.log4j.ConsoleAppender
  17. log4j.appender.ca.layout=org.apache.log4j.PatternLayout
  18. log4j.appender.ca.layout.ConversionPattern=%d [%t] %-5p %.16c - %m%n
6.運行Test,成功的話,就會在其工程目錄下生成: output.log,文件裡已經記錄了信息了。

log4cxx是強大的可配置的日志記錄工具,詳細的配置可以自己研究了哦。

Copyright © Linux教程網 All Rights Reserved