Log4cpp使用

来源:互联网 发布:网络上流行的英文歌曲 编辑:程序博客网 时间:2024/05/17 06:49

下载地址:https://github.com/ietjxdl/log4cpp

系统环境:windows 7 32

开发环境:vs2010

软件版本:log4cpp-1.1.tar.gz

 

一 编译

解压log4cpp-1.1.tar.gz到D: \log4cpp,解压缩后在目录下找到log4cpp\msvc10文件夹,进入后打开msvc10.sln工程。该工程下有log4cpp的静态库、动态库及相应的一些demo工程,在本文中使用的是静态库,只需要编译log4cppLIB工程即可。在debug模式和release模式下分别编译生成相应的库文件,至此库文件编译完成。

为方便使用把需要的文件拷贝到C:\Program Files\log4cpp(这个文件是新添加的)文件夹下。

1.   拷贝库文件,把log4cppLIB工程生成的库文件:log4cppD.lib、log4cppLIB.lib拷贝至C:\Program Files\log4cpp\lib文件夹下;

2.   拷贝头文件,把该库用到的头文件拷贝到C:\Program Files\log4cpp\include文件夹下,库中头文件在D: \log4cpp\include下。

二 配置环境变量

为了代码的可在不同的电脑进行编译,避免引用问题,建议使用环境变量对文件引用路径进行配置。

> 打开系统的环境变量中添加:

变量名:LOG4CPP_ROOT

变量值:C:\Program Files\log4cpp

> 在系统的path环境变量的变量值中添加

变量值:;%LOG4CPP_ROOT%\lib

重启电脑配置的环境变量生效。

三 使用demo

eclipse环境进行android开发有Log类提供系统的log方法,用起来非常方便。查看了log4cpp有定义相关的宏LOG4CPP_DEBUG、LOG4CPP_ERROR等,在使用上没有Log类这么方便,因此打算仿照Log对log4cpp进行再次封装。

class LogUtil

{

public:

    LogUtil() {}

    ~LogUtil() {}

    static inline int init(stringinitFileName ="./log4cpp.nt.property") {

        try {

            log4cpp::PropertyConfigurator::configure(initFileName);

        } catch(log4cpp::ConfigureFailure&f) {

            std::cout <<"Configure Problem " <<f.what() <<std::endl;

            return -1;

        }

        return 0;

    }

 

    static inline void close() {

        log4cpp::Category::shutdown();

    }

 

    /// debug

    static inline void d(stringTag,stringmsg) {

       log4cpp::Category&sub1 =log4cpp::Category::getInstance(Tag);// GetCategory(Tag);

        LOG4CPP_DEBUG(sub1,msg);

    }

 

    /// debug

    static inline void D(stringTag,stringmsg) {

        d(Tag,msg);

}

}

调用方法:

         LogUtil::init();

         LogUtil::d("sub1","testststststtstst");

         LogUtil::d("sub1","teststst55555ststtstst");

         LogUtil::d("sub2","teststststdfasttstst");

         LogUtil::close();

……

 

补充:

1.         包含目录

2.         包含使用库

该domo工程共享路径为:http://download.csdn.net/detail/xinhuo11/6209637