4.第一个例子
点击这里下载代码。
工程文件如下图所示:
本工程共7个文件,源码如下:
----------------------------------------------------main.c---------------------------------------------------------------
#include <stdio.h>
#include "mylog.h"
#include "other.h"
int main(int argv, char **argc) {
int i;
if ( mylog_init() == 1 )
{
printf("mylog_init() failed!\n");
}
for(i=0;i<140000;i++)
{
LOG("%s%d","Hello!-",i);
otherFunc();
}
if(mylog_fini() == 1)
{
printf("mylog_fini() failed!\n");
}
return 0;
}
----------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------Makefile-------------------------------------------------------
TARGET=log4ctest
OBJS=main.o other.o mylog.o
INC=./
LOG4CINC=/usr/local/include/log4c
LIBPATH=./
%.o:%.c
g++ -c -I$(LOG4CINC) -I$(INC) $< -o $@
%.o:%.cpp
g++ -Wall -c -g -I$(INC) $< -o $@
$(TARGET):$(OBJS)
g++ $(OBJS) -llog4c -o $(TARGET)
.PHONY:clean
clean:
rm *.o $(TARGET)
----------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------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 priority="notice"/>
<category priority="debug" appender="stderr" />
<category priority="debug" appender="myrollingfileappender" />
<rollingpolicy type="sizewin" maxsize="104857600" maxnum="10" />
<appender type="rollingfile" logdir="./" prefix="mylogfile" layout="dated" rollingpolicy="myrollingpolicy" />
<appender type="stream" layout="basic"/>
<appender type="stream" layout="dated"/>
<appender type="syslog" layout="basic"/>
<appender type="s13_file" layout="basic"/>
<appender type="s13_stderr" layout="none"/>
<appender type="s13_stderr" layout="catlayout"/>
<appender type="s13_stderr" layout="xmllayout"/>
<appender type="s13_stderr" layout="userlayout"/>
<layout type="basic"/>
<layout type="dated"/>
<layout type="s13_cat"/>
<layout type="s13_xml"/>
<layout type="s13_none"/>
<layout type="s13_userloc"/>
</log4c>
----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------other.h-------------------------------------------------------
void otherFunc(void);
----------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------other.c------------------------------------------------------
#include "mylog.h"
#include "other.h"
void otherFunc(void)
{
LOG("%s","Enter");
}
-----------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------mylog.h-----------------------------------------------------
#ifndef _MYLOG_H_
#define _MYLOG_H_
#include <string.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include "log4c.h"
#ifdef __cplusplus
}
#endif
#define MYLOG_CATEGORY_NAME "log4ctest"
#define MYLOG_PRIORITY LOG4C_PRIORITY_WARN
//1.LOG4C_PRIORITY_ERROR
//2.LOG4C_PRIORITY_WARN
//3.LOG4C_PRIORITY_NOTICE
//4.LOG4C_PRIORITY_DEBUG
//5.LOG4C_PRIORITY_TRACE
extern int mylog_init();
extern void log_message(char* file, int line, const char* func,const char* a_format, ...);
extern int mylog_fini();
#define LOG(fmt,args...) log_message(__FILE__, __LINE__, __FUNCTION__,fmt ,## args);
#endif