设计成单独一个头文件
#pragma once
#include <windows.h>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;
#define DEBUGLOG "Debug"
#define EXCEPTION "Exception"
#define RESULT "Result"
#define TASKLOG "Task"
#define XML "Xml"
#define MAX_FILES 3
#define MAX_SIZE 1024*1024*4
class log4nb
{
private:
log4nb( const char* logfileName )
{
memset(localID,0,sizeof(localID));
memset(exception,0,sizeof(exception));
memset(result,0,sizeof(result));
memset(debug,0,sizeof(debug));
memset(xml,0,sizeof(xml));
memset(task,0,sizeof(task));
sprintf_s(debug,sizeof(debug),"%s.log",logfileName);
sprintf_s(exception,sizeof(exception),"%s.log",EXCEPTION);
sprintf_s(result,sizeof(result),"%s.log",RESULT);
sprintf_s(task,sizeof(task),"%s.log",TASKLOG);
sprintf_s(xml,sizeof(xml),"%s.log",XML);
}
static log4nb* _instance;
ofstream* pf_normal;
ofstream* pf_exception;
ofstream* pf_result;
ofstream* pf_task;
ofstream* pf_xml;
private:
char debug[50];
char exception[50];
char result[50];
char task[50];
char xml[50];
char localID[50];
public:
~log4nb()
{
_instance = NULL;
}
//设置当前日志信息类型
void inline setLocalID(const char* id)
{
if( strlen(id) > 0 )
strcpy_s(localID,sizeof(localID),id);
}
static log4nb* GetPtr( const char* fileName )
{
if(!_instance)
_instance = new log4nb(fileName);
return(_instance);
}
static log4nb& GetObj( const char* fileName )
{
if(!_instance)
_instance = new log4nb(fileName);
return(*_instance);
}
//输出不含时间戳的DEBUG日志,可传入任意重载了<<操作符的对象
template<class T> inline log4nb& Write(T val)
{
return Write(val,pf_normal);
}
//输出含时间戳的DEBUG日志,可传入任意重载了<<操作符的对象
template<class T> inline log4nb& tmWrite(T val)
{
pf_normal = new ofstream (debug, ios::app);
log4nb& log = tmWrite(val,pf_normal);
logscroller(pf_normal,debug);
return log;
}
//输出含时间戳的Exception日志,可传入任意重载了<<操作符的对象
template<class T> inline log4nb& tmException(T val)
{
pf_exception = new ofstream(exception,ios::app);
log4nb& log = tmWrite(val,pf_exception);
logscroller(pf_exception,exception);
return log;
}
//输出含时间戳的Result日志,可传入任意重载了<<操作符的对象
template<class T> inline log4nb& tmResult(T val)
{
pf_result = new ofstream(result,ios::app);
log4nb& log = tmWrite(val,pf_result);
logscroller(pf_result,result);
return log;
}
//输出含时间戳的Task日志,可传入任意重载了<<操作符的对象
template<class T> inline log4nb& tmTask(T val)
{
pf_task = new ofstream(task,ios::app);
log4nb& log = tmWrite(val,pf_task);
logscroller(pf_task,task);
return log;
}
//输出含时间戳的Xml日志,可传入任意重载了<<操作符的对象
template<class T> inline log4nb& tmXml(T val)
{
pf_xml = new ofstream(xml,ios::app);
log4nb& log = tmWrite(val,pf_xml);
logscroller(pf_xml,xml);
return log;
}
//输出不含时间戳的DEBUG日志,可传入任意重载了<<操作符的对象
template<class T> inline log4nb& operator<< (T val)
{
pf_normal = new ofstream(debug,ios::app);
(*pf_normal) << val ;
pf_normal->flush();
logscroller(pf_normal,debug);
return *this;
}
private:
template<class T> inline log4nb& Write(T val,ofstream* pf)
{
(*pf) << val;
pf->flush();
return *this;
}
template<class T> inline log4nb& tmWrite(T val,ofstream* pf)
{
char strBuf[100];
time_t ltime;
time( <ime );
struct tm today;
localtime_s(&today,<ime );
sprintf_s(strBuf,sizeof(strBuf), "%4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d | %s > ",
today.tm_year+1900, today.tm_mon+1, today.tm_mday, today.tm_hour,
today.tm_min, today.tm_sec,localID);
(*pf) << strBuf << val << "\r\n";
pf->flush();
return *this;
}
void logscroller(ofstream* pf,const char* name)
{
int beg = 0;
int end = 0;
pf->seekp(0,ios::beg);
beg = pf->tellp();
pf->seekp(0,ios::end);
end = pf->tellp();
if( (end-beg) > MAX_SIZE )
{
pf->close();
char* first = new char[MAX_PATH];
char* second = new char[MAX_PATH];
for(int i = MAX_FILES;i >= 2;i--)
{
sprintf_s(first,MAX_PATH,"%s.%d",name,i-1);
sprintf_s(second,MAX_PATH,"%s.%d",name,i);
::MoveFileEx(first,second,MOVEFILE_REPLACE_EXISTING);
}
sprintf_s(first,MAX_PATH,"%s.%d",name,1);
::MoveFileEx(name,first,MOVEFILE_REPLACE_EXISTING);
delete first;
delete second;
}else pf->close();
}
};
log4nb* log4nb::_instance;