📄 logfile.cpp
字号:
// ErrLogFile.cpp: implementation of the CErrLogFile class.
//
#include "stdafx.h"
#include "LogFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLogFile::CLogFile()
{
}
CLogFile::~CLogFile()
{
fLog.Abort();
}
//////////////////////////////////////////////////////////////////////
// Name : InitErrLog
// Function: Init error log file
// Input:
// strFileName(file name)
// Output: BOOL
//////////////////////////////////////////////////////////////////////
BOOL CLogFile::InitLog(CString strFileName)
{
try
{
int iRet = fLog.Open(strFileName, CFile::modeCreate |
CFile::modeWrite | CFile::modeNoTruncate | CFile::typeText | CFile::shareDenyNone);
if (iRet == FALSE)
{
fLog.m_hFile = NULL;
return FALSE;
}
fLog.SeekToEnd();
}
catch(CFileException *pEx)
{
fLog.m_hFile = NULL;
pEx->Delete();
return FALSE;
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// Name : WriteLog
// Function: Init log file
// Input: CString strLogInfo
// Output: BOOL
//////////////////////////////////////////////////////////////////////
BOOL CLogFile::WriteLog(CString strLogInfo)
{
DWORD dwLength = strLogInfo.GetLength();
if (dwLength > 1000)
{
char szTemp[30];
strcpy(szTemp, _T("接收消息大于1000字节"));
fLog.Write(szTemp, 30);
return FALSE;
}
CTime tm = CTime::GetCurrentTime();
char szTime[1050];
CString strTime = tm.Format(" %Y-%m-%d %H:%M:%S ");
strcpy(szTime, strTime); //Add current time
strcat(szTime, strLogInfo);
dwLength = strlen(szTime);
fLog.Write(szTime, dwLength);
fLog.WriteString("\r\n");
fLog.Flush();
BackupFile();
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// Name : WriteErrLog
// Function: Init error log file
// Input: char *szBuffer, int iErrorCode
// szBuffer = 内容, iErrorCode = 状态类型
// Output: void
//////////////////////////////////////////////////////////////////////
void CLogFile::WriteErrLog(char *szBuffer, int iErrorCode = 0)
{
DWORD dwLength = strlen(szBuffer);
if (dwLength > 1000)
{
char szTemp[30];
strcpy(szTemp, _T("接收消息大于1000字节"));
fLog.Write(szTemp, 30);
return;
}
CTime tm = CTime::GetCurrentTime();
char szTime[1050];
CString strTime = tm.Format(" %Y-%m-%d %H:%M:%S ");
char szTemp[10];
sprintf(szTemp, "%4d", iErrorCode);
strcpy(szTime, szTemp); //Add ErrorCode
strcat(szTime, strTime); //Add current time
strcat(szTime, szBuffer); //Add error context
dwLength = strlen(szTime);
fLog.Write(szTime, dwLength);
fLog.WriteString("\r\n");
fLog.Flush();
BackupFile();
}
void CLogFile::ClearContext()
{
fLog.SetLength(0);
}
void CLogFile::BackupFile()
{
CString strOldName,strNewName,strFilePath;
CTime dtTime = CTime::GetCurrentTime();
int iYear,iMonth,iDay;
if(fLog.GetLength() > 10000000)
{
strOldName = fLog.GetFileName();
iYear = dtTime.GetYear();
iMonth = dtTime.GetMonth();
iDay = dtTime.GetDay();
strNewName.Format("%d%d%d",iYear,iMonth,iDay);
strNewName = strNewName + strOldName;
strFilePath = fLog.GetFilePath();
fLog.Close();//文件重命名前必须关闭该文件
fLog.Rename(strOldName,strNewName);//因为它是个CFile的静态函数
InitLog(strOldName);
}
}
void CLogFile::WriteStatNum(char *szBuffer)
{
DWORD dwLength = strlen(szBuffer);
fLog.Write(szBuffer, dwLength);
fLog.WriteString("\r\n");
fLog.Flush();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -