📄 log.cpp
字号:
// Log.cpp: implementation of the CLog class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Log.h"
#include "Resource.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define CRLF "\x0d\x0a"
CRITICAL_SECTION LogCriticalSection; // Protection from multiple threads
UINT MessageBoxThreadProcFunc(LPVOID pParam);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLog::CLog
(
CString strLogFileName,
DWORD dwMaxFileLength/* = 1000*/,
BOOL bDisplayLogSizeWarning /*= TRUE*/
) :m_strLogfileName(strLogFileName),
m_dwMaxLen(dwMaxFileLength),
m_bDisplayDlg(bDisplayLogSizeWarning)
{
m_bUseLog =TRUE;
m_pLogFile =NULL;
ASSERT (!strLogFileName.IsEmpty());
Initialize ();
}
void CLog::Initialize ()
{
InitializeCriticalSection(&LogCriticalSection);
CFileException e;
if (m_dwMaxLen == 0) // if it is Zero there will be a deluge of errors !!
m_dwMaxLen = 1000;
m_dwMaxLen *= 1024; // convert lenght supplied into kb's
if (m_bUseLog)
{
m_pLogFile = new CFile();
ASSERT (m_pLogFile != NULL);
if (m_strLogfileName.IsEmpty ())
{
delete m_pLogFile;
m_pLogFile = NULL;
return;
}
if(!m_pLogFile->Open( m_strLogfileName,
CFile::modeCreate | // if not there create it
CFile::modeNoTruncate| // Dont truncate
CFile::shareDenyNone | // Let others also read
CFile::modeWrite, &e ) )// Write into it
{
delete m_pLogFile;
m_pLogFile = NULL; // we were not able to create the file so set ptr to NULL
#ifdef _DEBUG
afxDump << "不能打开文件 " << e.m_cause << "\n";
#endif
}
if (m_pLogFile != NULL)
{
m_pLogFile->SeekToEnd () ;
}
}
}
CLog::~CLog()
{
if (m_pLogFile != NULL)
{
m_pLogFile->Close ();
delete m_pLogFile;
m_pLogFile = NULL;
}
}
void CLog::LogMessage (UINT uMsgID)
{
if (!m_bUseLog) // Does the user want a log ?
return;
if (m_pLogFile == NULL)
return;
CString strMsg;
if (strMsg.LoadString (uMsgID)) // Load the String from the string table
{
CTime tmTime = CTime::GetCurrentTime ();
//strMsg =((tmTime.Format ("%m/%d/%y %H:%M:%S ")) + strMsg +CRLF);
strMsg =strMsg +CRLF;
m_pLogFile->Write (strMsg,strMsg.GetLength());
DisplayWarningMessage();
}
else
ASSERT (FALSE);
}
void CLog::LogMessage (CString strMsg)
{
if (!m_bUseLog)
return;
if (m_pLogFile == NULL)
return;
if (strMsg.IsEmpty ())
return;
CTime tmTime = CTime::GetCurrentTime();
//strMsg =((tmTime.Format ("%m/%d/%y %H:%M:%S ")) + strMsg +CRLF);
strMsg =strMsg +CRLF;
m_pLogFile->Write (strMsg,strMsg.GetLength());
DisplayWarningMessage();
}
void CLog::DisplayWarningMessage()
{
if (!m_bDisplayDlg) // if a Warning Message Is already being shown
return; // dont populate the screen with more messages
if(m_pLogFile == NULL)
return;
if (m_pLogFile->GetLength () <= m_dwMaxLen) // Has file length exceded the limit?
return;
AfxBeginThread(MessageBoxThreadProcFunc,(LPVOID)&m_bDisplayDlg);
m_bDisplayDlg = FALSE ; // dont come back until user asks
}
UINT MessageBoxThreadProcFunc(LPVOID pParam)
{
EnterCriticalSection(&LogCriticalSection); // We are setting a variable here so
BOOL *pbDisplayDlg = (BOOL*)pParam;
(*pbDisplayDlg) = FALSE; // take some precaution
CString strMessage,strHeading;
strMessage="日志文件的长度超过设定的文件最大长度";//.LoadString("IDS_LOG_FILE_LEN_WARNING");
strHeading="日志-警告";//.LoadString("IDS_HEAD_BP_WARNING");
if (MessageBox(NULL,strMessage,strHeading,MB_OKCANCEL) == IDOK)
(*pbDisplayDlg) = TRUE;
LeaveCriticalSection(&LogCriticalSection);
return 0;
}
/*****************************************************************************************
Function Name : DeleteLogFile
Purpose : Deletes the Log File
Parameters : None
Return Type : BOOL
Date : 05-Nov-98
Modification History:
11/11/98 Converted Delete to Truncate the file to Zero length
*****************************************************************************************/
BOOL CLog::ClearLogFile()
{
if (m_pLogFile != NULL)
m_pLogFile->SetLength (0); // The Log is gone !!!
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -