C LOG 日志生成

//---------------------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!


转载于:https://my.oschina.net/u/2246696/blog/323102

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值