ログクラスvs 2010ログファイルを書く月ごとに、毎月1つのログファイルを書きます
16121 ワード
#pragma once
class LogFile
{
protected:
CRITICAL_SECTION _csLock;
char * _szFileName;
HANDLE _hFile;
bool OpenFile();// ,
DWORD Write(LPCVOID lpBuffer, DWORD dwLength);
virtual void WriteLog( LPCVOID lpBuffer, DWORD dwLength);// ,
void Lock() {::EnterCriticalSection(&_csLock); }
void Unlock() {::LeaveCriticalSection(&_csLock);}
char szFileSuffix[32]; //
public:
LogFile(char*szFileName);
virtual ~LogFile();
const char * GetFileName()
{
return _szFileName;
}
void SetFileName(const char *szName);// ,
bool IsOpen()
{
return _hFile != INVALID_HANDLE_VALUE;
}
void Close();
void Log(LPCVOID lpBuffer, DWORD dwLength);//
void Log(const char *szText){
Log(szText, strlen(szText));}
void Log(CString str);
void Log(TCHAR *buffer);
private://
LogFile(const LogFile&);
LogFile&operator = (const LogFile&);
};
#include "StdAfx.h"
#include "LogFile.h"
LogFile::LogFile(char*szFileNameParam)
{
memset(szFileSuffix,0,sizeof(szFileSuffix));
strcat_s(szFileSuffix,szFileNameParam); // ,
_szFileName = NULL;
_hFile = INVALID_HANDLE_VALUE;
::InitializeCriticalSection(&_csLock);
//
time_t now;
time(&now);
char szCurDate[256] = {0};
struct tm newtime;
localtime_s(&newtime,&now);
strftime(szCurDate,19,"%Y%m",&newtime);
char szFileName[64] = {0};
strcat_s(szFileName,10,szCurDate); //
strcat_s(szFileName,szFileSuffix); //
SetFileName(szFileName);
}
//-------------------------------------------------------------------------
LogFile::~LogFile()
{
::DeleteCriticalSection(&_csLock);
Close();
if(_szFileName)
delete [] _szFileName;
}
//-------------------------------------------------------------------------
bool LogFile::OpenFile()
{
if(IsOpen())
return true;
if(!_szFileName)
return false;
_hFile = CreateFileA(
_szFileName,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(!IsOpen() && GetLastError() == 2)// , ,
_hFile = CreateFileA(
_szFileName,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(IsOpen())
SetFilePointer(_hFile, 0, NULL, FILE_END);
return IsOpen();
}
//-------------------------------------------------------------------------
DWORD LogFile::Write(LPCVOID lpBuffer, DWORD dwLength)
{
DWORD dwWriteLength = 0;
if(IsOpen())
WriteFile(_hFile, lpBuffer, dwLength, &dwWriteLength, NULL);
return dwWriteLength;
}
//-------------------------------------------------------------------------
void LogFile::WriteLog( LPCVOID lpBuffer, DWORD dwLength)
{
DWORD dwWriteLength;
char buf[1024]={0};
strcat_s(buf,1000,(char*)lpBuffer);
if(IsOpen())
{
time_t now;
time(&now);
char temp[1024];
struct tm newtime;
localtime_s(&newtime,&now);
memset(temp,0,sizeof(temp));
strftime(temp, 64, "%Y-%m-%d %H:%M:%S :", &newtime);
strcat_s(temp,128," :");
strcat_s(temp,1024,buf);
WriteFile(_hFile, temp,strlen(temp), &dwWriteLength, NULL);
WriteFile(_hFile, "\r
",2, &dwWriteLength, NULL);
FlushFileBuffers(_hFile); //
}
else
{
;
}
}
//-------------------------------------------------------------------------
void LogFile::SetFileName(const char *szName)
{
//assert(szName);
if(_szFileName != NULL)
{
//AfxMessageBox(_T("_szFileName is NULL"));
delete []_szFileName;
}
Close();
int len = strlen(szName);
_szFileName = new char[32];
// assert(_szFileName);
strcpy_s(_szFileName,30,szName);
}
//-------------------------------------------------------------------------
void LogFile::Close()
{
if(IsOpen())
{
CloseHandle(_hFile);
_hFile = INVALID_HANDLE_VALUE;
}
}
//-------------------------------------------------------------------------
void LogFile::Log(LPCVOID lpBuffer, DWORD dwLength)
{
// assert(lpBuffer);
__try
{
Lock();
//
time_t now;
time(&now);
char szCurDate[256] = {0};
struct tm newtime;
localtime_s(&newtime,&now);
strftime(szCurDate,19,"%Y%m",&newtime);
char szCurFileName[64] = {0};
strcat_s(szCurFileName,10,szCurDate); //
strcat_s(szCurFileName,szFileSuffix); //
if(!PathFileExistsA(szCurFileName))
SetFileName(szCurFileName);
if(OpenFile())
WriteLog(lpBuffer, dwLength);
}
__finally
{
Unlock();
}
}
void LogFile::Log(TCHAR *buffer)
{
CT2A buf_str(buffer);
Log(buf_str);
}
void LogFile::Log(CString str)
{
Log(str.GetBuffer());
}