您的位置:首页 > 编程语言 > C语言/C++

c++ 多线程写日志的一个很实用的日志类源码(支持 c++ builder)

2011-11-05 11:04 316 查看

1.日志基类

.h文件

//--------------------------------------------------------------------------- #ifndefUnitLogWriterH #defineUnitLogWriterH #include<vcl.h> #include<time.h> #include<assert.h> //--------------------------------------------------------------------------- classLogFile { protected: CRITICAL_SECTION_csLock; char*_szFileName; HANDLE_hFile; boolOpenFile();//打开文件,指针到文件尾 DWORDWrite(LPCVOIDlpBuffer,DWORDdwLength); virtualvoidWriteLog(LPCVOIDlpBuffer,DWORDdwLength);//写日志,可以扩展修改 voidLock(){::EnterCriticalSection(&_csLock);} voidUnlock(){::LeaveCriticalSection(&_csLock);} public: LogFile(constchar*szFileName="Log.log");//设定日志文件名 virtual~LogFile(); constchar*GetFileName() { return_szFileName; } voidSetFileName(constchar*szName);//修改文件名,同时关闭上一个日志文件 boolIsOpen() { return_hFile!=INVALID_HANDLE_VALUE; } voidClose(); voidLog(LPCVOIDlpBuffer,DWORDdwLength);//追加日志内容 voidLog(constchar*szText) { Log(szText,strlen(szText)); } private://屏蔽函数 LogFile(constLogFile&); LogFile&operator=(constLogFile&); }; #endif

基类cpp文件

//--------------------------------------------------------------------------- #pragmahdrstop #include"UnitLogWriter.h" //--------------------------------------------------------------------------- #pragmapackage(smart_init) LogFile::LogFile(constchar*szFileName) { _szFileName=NULL; _hFile=INVALID_HANDLE_VALUE; ::InitializeCriticalSection(&_csLock); SetFileName(szFileName); } //------------------------------------------------------------------------- LogFile::~LogFile() { ::DeleteCriticalSection(&_csLock); Close(); if(_szFileName) delete[]_szFileName; } //------------------------------------------------------------------------- boolLogFile::OpenFile() { if(IsOpen()) returntrue; if(!_szFileName) returnfalse; _hFile=CreateFile( _szFileName, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(!IsOpen()&&GetLastError()==2)//打开不成功,且因为文件不存在,创建文件 _hFile=CreateFile( _szFileName, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(IsOpen()) SetFilePointer(_hFile,0,NULL,FILE_END); returnIsOpen(); } //------------------------------------------------------------------------- DWORDLogFile::Write(LPCVOIDlpBuffer,DWORDdwLength) { DWORDdwWriteLength=0; if(IsOpen()) WriteFile(_hFile,lpBuffer,dwLength,&dwWriteLength,NULL); returndwWriteLength; } //------------------------------------------------------------------------- voidLogFile::WriteLog(LPCVOIDlpBuffer,DWORDdwLength) { time_tnow; chartemp[21]; DWORDdwWriteLength; if(IsOpen()) { time(&now); strftime(temp,20,"%Y-%m-%d%H:%M:%S",localtime(&now)); WriteFile(_hFile,"\xd\xa#-----------------------------",32,&dwWriteLength,NULL); WriteFile(_hFile,temp,19,&dwWriteLength,NULL); WriteFile(_hFile,"-----------------------------#\xd\xa",32,&dwWriteLength,NULL); WriteFile(_hFile,lpBuffer,dwLength,&dwWriteLength,NULL); WriteFile(_hFile,"\xd\xa",2,&dwWriteLength,NULL); FlushFileBuffers(_hFile); } } //------------------------------------------------------------------------- //------------------------------------------------------------------------- voidLogFile::SetFileName(constchar*szName) { assert(szName); if(_szFileName) delete[]_szFileName; Close(); _szFileName=newchar[strlen(szName)+1]; assert(_szFileName); strcpy(_szFileName,szName); } //------------------------------------------------------------------------- voidLogFile::Close() { if(IsOpen()) { CloseHandle(_hFile); _hFile=INVALID_HANDLE_VALUE; } } //------------------------------------------------------------------------- voidLogFile::Log(LPCVOIDlpBuffer,DWORDdwLength) { assert(lpBuffer); __try { Lock(); if(!OpenFile()) return; WriteLog(lpBuffer,dwLength); } __finally { Unlock(); } }

2.日志派生类

.h文件

//---------------------------------------------------------------------------

#ifndefLogFileExH
#defineLogFileExH
#include<assert.h>

#include"UnitLogWriter.h"

//---------------------------------------------------------------------------
classLogFileEx:publicLogFile
{
protected:
char*_szPath;
char_szLastDate[9];
int_iType;
voidSetPath(constchar*szPath);
public:
enumLOG_TYPE{YEAR=0,MONTH=1,DAY=2};
LogFileEx(constchar*szPath=".",LOG_TYPEiType=MONTH);
~LogFileEx();
constchar*GetPath();
voidLog(LPCVOIDlpBuffer,DWORDdwLength);
voidLog(constchar*szText);
voidLog(constAnsiString&szText);
private://屏蔽函数
LogFileEx(constLogFileEx&);
LogFileEx&operator=(constLogFileEx&);
};
#endifcpp文件//---------------------------------------------------------------------------

#pragmahdrstop

#include"LogFileEx.h"

//---------------------------------------------------------------------------

#pragmapackage(smart_init)
//-------------------------------------------------------------------------

voidLogFileEx::SetPath(constchar*szPath)
{
assert(szPath);

WIN32_FIND_DATAwfd;
chartemp[MAX_PATH+1]={0};

if(FindFirstFile(szPath,&wfd)==INVALID_HANDLE_VALUE&&CreateDirectory(szPath,NULL)==0)
{
strcat(strcpy(temp,szPath),"CreateFail.ExitNow!ErrorID:");
ltoa(GetLastError(),temp+strlen(temp),10);
MessageBox(NULL,temp,"ClassLogFileEx",MB_OK);
exit(1);
}
else
{
GetFullPathName(szPath,MAX_PATH,temp,NULL);
_szPath=newchar[strlen(temp)+1];
assert(_szPath);
strcpy(_szPath,temp);
}
}
//-------------------------------------------------------------------------
LogFileEx::LogFileEx(constchar*szPath,LOG_TYPEiType)
{
_szPath=NULL;
SetPath(szPath);
_iType=iType;
memset(_szLastDate,0,9);
}
//-------------------------------------------------------------------------
LogFileEx::~LogFileEx()
{
if(_szPath)
delete[]_szPath;
}
//-------------------------------------------------------------------------

constchar*LogFileEx::GetPath()
{
return_szPath;
}
//-------------------------------------------------------------------------

voidLogFileEx::Log(LPCVOIDlpBuffer,DWORDdwLength)
{
assert(lpBuffer);

chartemp[10];
staticconstcharformat[3][10]={"%Y","%Y-%m","%Y%m%d"};

__try
{
Lock();

time_tnow=time(NULL);

strftime(temp,9,format[_iType],localtime(&now));

if(strcmp(_szLastDate,temp)!=0)//更换文件名
{
strcat(strcpy(_szFileName,_szPath),"\\");
strcat(strcat(_szFileName,temp),".log");
strcpy(_szLastDate,temp);
Close();
}
if(!OpenFile())
return;

WriteLog(lpBuffer,dwLength);
}
__finally
{
Unlock();
}
}
//-------------------------------------------------------------------------
voidLogFileEx::Log(constchar*szText)
{
Log(szText,strlen(szText));
}
//-------------------------------------------------------------------------
voidLogFileEx::Log(constAnsiString&szText)
{
Log(szText.c_str(),szText.Length());
}

3.随便测试的代码

//---------------------------------------------------------------------------

#include<vcl.h>
#include<conio.h>
#include"LogFileEx.h"
#pragmahdrstop

//---------------------------------------------------------------------------

#pragmaargsused
intmain(intargc,char*argv[])
{
LogFileExlog;
log.Log("哈哈");
AnsiStringtemp="adsfsadfsadfsaf";
log.Log(temp);
log.Log(temp);
getch();
return0;
}
//---------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: