特点
1 采用双文件记录日志信息
2 当一个文件写满后,清空另外一个文件继续写入
3 可设置最大日志文件大小
4 文本文件使用unicode进行编码
DoubleLog(void);
~DoubleLog(void);
bool Init(const string& logFileName);
void SetMaxBytes(int nbyte);
//void Close(void);
bool WriteLog(LPCWSTR lpszFormat,…);
private:
static char lineEnd[4];
static char newufile[2];
static bool bclog;
fstream fs;
string filename1;
string filename2;
int maxLength;
//bool isOpen;
wchar_t szMsg[DEFAULT_STR_BUFF_LEN];
//streampos curFilePos;
bool bfile1;
bool WriteFileHead();
void ChangePoint();
bool OpenFile();
bool CheckFStream();
bool OpenNewFile();
};
LPWSTR getNowTimeW(LPWSTR pbuff)
{
SYSTEMTIME cSystime;
GetLocalTime(&cSystime);
//wchar_t pbuff[24] = {0};
swprintf_s(pbuff, 20, L”%04d-%02d-%02d %02d:%02d:%02d”, cSystime.wYear, cSystime.wMonth, cSystime.wDay, cSystime.wHour, cSystime.wMinute, cSystime.wSecond);
//return wstring(pbuff);
return pbuff;
}
#include <strsafe.h>
#define DEFAULT_MAX_BUFF_LEN (1024*1024)
char DoubleLog::lineEnd[4] = {0x0d,0x00,0x0a,0x00};
char DoubleLog::newufile[2] = {0xff,0xfe};
bool DoubleLog::bclog = false;
DoubleLog::DoubleLog(void)
: maxLength(DEFAULT_MAX_BUFF_LEN)
{
if (!bclog)
{
CreateDirectory(L”log”,NULL);
bclog = true;
}
bfile1 = true;
}
DoubleLog::~DoubleLog(void)
{
}
int GetFstreamLen(fstream& infile)
{
streampos cp = infile.tellg();
infile.seekg( 0, ios_base::end );
streampos len = infile.tellg();
infile.seekg( 0, cp );
return len;
}
bool DoubleLog::WriteFileHead()
{
fs.write(newufile,2);
return true;
}
void DoubleLog::ChangePoint()
{
bfile1 = !bfile1;
}
bool DoubleLog::OpenFile()
{
string fname;
if (bfile1)
{
fname = filename1;
}
else
{
fname = filename2;
}
if (fs.is_open())
{
fs.close();
}
fs.open(fname.c_str(),ios_base::app | ios_base::out | ios_base::binary);
if (! fs.is_open())
{
return false;
}
return true;
}
bool DoubleLog::OpenNewFile()
{
string fname;
if (bfile1)
{
fname = filename1;
}
else
{
fname = filename2;
}
if (fs.is_open())
{
fs.close();
}
fs.open(fname.c_str(), ios::trunc | ios_base::out | ios_base::binary);
if (! fs.is_open())
{
return false;
}
WriteFileHead();
return true;
}
bool DoubleLog::CheckFStream()
{
if (!fs.is_open())
{
return false;
}
WriteFileHead();
streampos curpos = fs.tellp();
streampos zp = 2;
if (curpos == zp)
{
return true;
}
if (curpos >= maxLength)
{
return false;
}
return true;
}
bool DoubleLog::Init(const string& logFileName)
{
string curD(“log/”);
filename1 = curD + logFileName + “.txt”;
filename2 = curD +logFileName + “_2.txt”;
OpenFile();
if (!CheckFStream())
{
fs.close();
ChangePoint();
OpenFile();
CheckFStream();
}
return true;
}
bool DoubleLog::WriteLog(LPCWSTR lpszFormat,…)
{
va_list argList;
va_start(argList, lpszFormat);
HRESULT hr;
hr = StringCchVPrintfW(szMsg,DEFAULT_STR_BUFF_LEN,lpszFormat,argList);
if (hr == S_OK || hr == STRSAFE_E_INSUFFICIENT_BUFFER)
{
szMsg[DEFAULT_STR_BUFF_LEN – 1]=0;
}
va_end(argList);
wchar_t szfbuff[24] = {0};
getNowTimeW(szfbuff);
szfbuff[wcslen(szfbuff)] = L’ ‘;
szfbuff[wcslen(szfbuff)] = 0;
fs.write((char*)szfbuff, wcslen(szfbuff) * 2);
fs.write((char*)szMsg, wcslen(szMsg) * 2);
fs.write(lineEnd,4);
fs.flush();
streampos curpos = fs.tellp();
if (curpos >= maxLength || !(fs.good()))
{
fs.close();
ChangePoint();
OpenNewFile();
}
return true;
}
void DoubleLog::SetMaxBytes(int nbyte)
{
if (nbyte<512 || nbyte > 500*1024*1024)
{
return;
}
maxLength = nbyte;
}