//---------------------loger.h--------------------//
#ifndef _LOGER_H #define _LOGER_H #include <stdio.h> #define LOGFILEPATH "./wk.log" #define LEVEL 4 #define PLOG(level,format, ...) \ { \ if(logp==NULL) \ logerinit(&logp); \ fprintf(logp,"| L=%s | %s %s | %s | %4.4dL | %s: ",level,__DATE__,__TIME__,__FILE__,__LINE__,__func__); \ fprintf(logp,format,##__VA_ARGS__) ; \ fprintf(logp,"\n"); \ fflush(logp); \ } \ #if (LEVEL==4) #define DLOG(format, ...) PLOG("DBG",format,##__VA_ARGS__) #define ILOG(format, ...) PLOG("INFO",format,##__VA_ARGS__) #define WLOG(format, ...) PLOG("*WARN",format,##__VA_ARGS__) #define ELOG(format, ...) PLOG("**ERROR",format,##__VA_ARGS__) #elif (LEVEL==3) #define DLOG(format, ...) #define ILOG(format, ...) PLOG("INFO",format,##__VA_ARGS__) #define WLOG(format, ...) PLOG("*WARN",format,##__VA_ARGS__) #define ELOG(format, ...) PLOG("**ERROR",format,##__VA_ARGS__) #elif (LEVEL==2) #define DLOG(format, ...) #define ILOG(format, ...) #define WLOG(format, ...) PLOG("*WARN",format,##__VA_ARGS__) #define ELOG(format, ...) PLOG("**ERROR",format,##__VA_ARGS__) #elif (LEVEL==1) #define DLOG(format, ...) #define ILOG(format, ...) #define WLOG(format, ...) #define ELOG(format, ...) PLOG("**ERROR",format,##__VA_ARGS__) #endif FILE *logp; void logerinit(FILE **fp); #endif /*_LOGER_H*/
//------------------------loger.c-------------------//
#include "loger.h" void logerinit(FILE **fp) { *fp=fopen(LOGFILEPATH,"a"); if(*fp==NULL) perror("fopen logfile"); }
//-----------------------use example--------------//
DLOG("%s","create failure"); WLOG("%s","create failure"); ILOG("%s","create failure"); ELOG("%s","create failure");
//----------------------log------------------------//
| L=INFO | Jan 2 2015 04:45:39 | wk.c | 0124L | loadpi: open file[conf] success! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0135L | loadpi: read portinfo success! | L=DBG | Jan 2 2015 04:45:39 | wk.c | 0106L | loadport: pi->port[0]=22 | L=DBG | Jan 2 2015 04:45:39 | wk.c | 0106L | loadport: pi->port[1]=80 | L=DBG | Jan 2 2015 04:45:39 | wk.c | 0111L | loadport: pi->size=2 | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0150L | loadpi: portinfo is loaded! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0153L | loadpi: loadpi is finish! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0167L | main: load [portinfo] success! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0176L | main: open file[conf] success! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0062L | createdpt: create collpt thread success! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0071L | createdpt: create analy thread success! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0191L | main: create displaypt success! | L=*WARN | Jan 2 2015 04:45:39 | wk.c | 0194L | main: wk is quiting! | L=INFO | Jan 2 2015 04:45:39 | wk.c | 0197L | main: Thread wait over! | L=*WARN | Jan 2 2015 04:45:39 | wk.c | 0200L | main: wk exit!