log4c helloworld
log4c.c
#include <stdio.h>
#include "log4c.h"
int main(int argc, char** argv){
int rc = 0;
char *test = "test strings";
log4c_category_t* mycat = NULL;
if (log4c_init()){
printf("log4c_init() failed");
rc = 1;
}else{
mycat = log4c_category_get("log4c.examples.helloworld");
log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "hellworld");
log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%d", rc);
log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s", test);
/* Explicitly call the log4c cleanup routine */
if ( log4c_fini()){
printf("log4c_fini() failed");
}
}
return 0;
}
建立配置文件:log4crc
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE log4c SYSTEM "">
<log4c version="1.2.1">
<config>
<bufsize>0</bufsize>
<debug level="2"/>
<nocleanup>0</nocleanup>
<reread>1</reread>
</config>
<category name="root" priority="notice"/>
<appender name="stdout" type="stream" layout="basic"/>
<layout name="basic" type="basic"/>
<category name="log4c.examples.helloworld" priority="debug" appender="stdout"/>
</log4c>
<!DOCTYPE log4c SYSTEM "">
<log4c version="1.2.1">
<config>
<bufsize>0</bufsize>
<debug level="2"/>
<nocleanup>0</nocleanup>
<reread>1</reread>
</config>
<category name="root" priority="notice"/>
<appender name="stdout" type="stream" layout="basic"/>
<layout name="basic" type="basic"/>
<category name="log4c.examples.helloworld" priority="debug" appender="stdout"/>
</log4c>
appender 为指定输出位置
例如:appender="./log4c.c"
编译:
gcc -g -Wall -O0 log4c.c -o log4c -llog4c
指定路径编译:
gcc -g -Wall -O0 log4c.c -o log4c -L/usr/local/log4c/lib/ -I/usr/local/log4c/include /usr/local/log4c/lib/liblog4c.so
如果用--prefix=/usr/local/log4c 指定了路径 则需要在.profile中添加
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/log4c/lib
export LD_LIBRARY_PATH
输出:
[stdout] ERROR log4c.examples.helloworld - hellworld
[stdout] ERROR log4c.examples.helloworld - 0
[stdout] ERROR log4c.examples.helloworld - test strings
[stdout] ERROR log4c.examples.helloworld - 0
[stdout] ERROR log4c.examples.helloworld - test strings
原文地址:http://hi.baidu.com/lin_yingx/blog/item/8c4af2f96a1939999f514651.html
log4c下载地址:http://log4c.sourceforge.net/
下载地址:http://prdownloads.sourceforge.net/log4c/log4c-1.2.1.tar.gz
完