#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\n",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()); }