C++简单的打印日志接口

#ifndef _LOG_H
#define _LOG_H

#include <time.h>
#include <string.h>
#include <stdio.h>

typedef enum LOG_LEVEL
{
	LOG_LEVEL_DEBUG=0,
	LOG_LEVEL_INFO=1,
	LOG_LEVEL_WARNING=2,
	LOG_LEVEL_ERROR=3,
	LOG_LEVEL_CRITICAL=4,
	LOG_LEVEL_EVENT=5
}ENUM_LOG_LEVEL;

extern FILE* fpPrint;
extern time_t rawtime;
extern struct tm* timeinfo;
extern char* LogLevelName[];
extern unsigned char LogLevel;

//日志初始化时使用
//_IOFBF  --满BF  _IONBF --无BF
#define printFileBufType	_IONBF//_IONBF直接从流中读入数据或直接向流中写入数据,而没有缓冲区
#define printFileBufSize	0


#define OpenPrintFile(strFileFullPath) \
	fpPrint=fopen(strFileFullPath,"ab+");\
	setvbuf(fpPrint,NULL,printFileBufType,printFileBufSize)//setvbuf把缓冲区与流相关

//结束时使用
#define ClosePrintFile() if(NULL!=(fpPrint))\
	fclose(fpPrint);fpPrint=NULL

//不带参的打印函数
#define DPIDebugPrint(level,fmt) \
	if(((level)>=LogLevel) && (NULL!=fpPrint))\
{\
	time(&rawtime);\
	timeinfo=localtime(&rawtime);\
	fprintf(fpPrint,"\n[%02d-%02d %02d:%02d:%02d.%03d][FILE:%15s][LINE:%4d][%s] "fmt,\
	timeinfo->tm_mon+1,\
	timeinfo->tm_mday,\
	timeinfo->tm_hour,\
	timeinfo->tm_min,\
	timeinfo->tm_sec,\
	(int)(rawtime%1000),\
	strrchr(__FILE__,'\\')+1,\
	__LINE__,\
	LogLevelName[level]);\
}


//带参的打印函数
#define DPIDebugPrintWithArgs(level,fmt,...) \
	if(((level)>=LogLevel) && (NULL!=fpPrint))\
{\
	time(&rawtime);\
	timeinfo=localtime(&rawtime);\
	fprintf(fpPrint,"\n[%02d-%02d %02d:%02d:%02d.%03d][FILE:%15s][LINE:%4d][%s] "fmt,\
	timeinfo->tm_mon+1,\
	timeinfo->tm_mday,\
	timeinfo->tm_hour,\
	timeinfo->tm_min,\
	timeinfo->tm_sec,\
	(int)(rawtime%1000),\
	strrchr(__FILE__,'\\')+1,\
	__LINE__,\
	LogLevelName[level],__VA_ARGS__);\
}

#endif


 

#include "stdafx.h"
#include "Log.h"

FILE*			fpPrint=NULL;	//日志文件指针
time_t			rawtime=0;
struct tm*		timeinfo=NULL;
char*			LogLevelName[6]={"DEBUG","INFO","WARNING","ERROR","CRITICAL","EVENT"};

#ifdef _DEBUG
unsigned char	LogLevel=LOG_LEVEL_DEBUG;
#else
unsigned char	LogLevel=LOG_LEVEL_ERROR;
#endif

你可能感兴趣的:(log)