log4cpp安装使用

一,下载:http://log4cpp.sourceforge.net

二,安装:打开终端,以此执行下列命令:       

    ./configure
    make
    make check
    make install

三,在eclipse中测试:

    在eclipse中建立一个C++工程,输入测试代码:

#include <iostream>
using namespace std;


#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"


void simpleDemo();


int main() {
cout << "This is log4cpp example" << endl; 


simpleDemo();


return 0;
}


void simpleDemo() {
log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console",
&std::cout);
appender1->setLayout(new log4cpp::BasicLayout());


log4cpp::Appender *appender2 = new log4cpp::FileAppender("default",
"program.log");
appender2->setLayout(new log4cpp::BasicLayout());


log4cpp::Category& root = log4cpp::Category::getRoot();
root.setPriority(log4cpp::Priority::WARN);
root.addAppender(appender1);


log4cpp::Category& sub1 = log4cpp::Category::getInstance(
std::string("sub1"));
sub1.addAppender(appender2);


// use of functions for logging messages
root.error("root error");
root.info("root info");
sub1.error("sub1 error");
sub1.warn("sub1 warn");


// printf-style for logging variables
root.warn("%d + %d == %s ?", 1, 1, "two");


// use of streams for logging messages
root << log4cpp::Priority::ERROR << "Streamed root error";
root << log4cpp::Priority::INFO << "Streamed root info";
sub1 << log4cpp::Priority::ERROR << "Streamed sub1 error";
sub1 << log4cpp::Priority::WARN << "Streamed sub1 warn";


// or this way:
root.errorStream() << "Another streamed error";
}

这个时候应该会出错,显示没有找到库信息,解决方法如下:

首先,打开工程属性->"c/c++Build"->"settings"->"tool settings"。

1,"GCC C++ Complier"->"includes"->"/usr/local/include"。

2,“GCC C++ Linker”->"Libraries",增加两项:“log4cpp”;“pthread”。

再次运行,如果提示共享库找不到问题,解决办法如下:

1,修改/etc/ld.so.conf,在文件末尾加入一行:“/usr/local/lib”。

2,在终端中输入命令:ldconfig

再次运行,我们会发现已经可以正常使用log4cpp了!

 

posted on 2013-05-13 13:47  sunylat  阅读(311)  评论(0编辑  收藏  举报