📄 loghndlr.cpp
字号:
// LogHndlr.cpp - Implementation of CLogHndlr class
// CLogHndlr is derived from CMsgHndlr and it is responsible for logging the message on to
// the log file, when HandleMessage method is called
// Written by : T. Kulathu Sarma
// This file is part of Singleton application to demonstrate the concept of Singleton classes.
// This file is provided "as is" with no expressed or implied warranty.
#include "stdafx.h"
#include "LogHndlr.h"
#include <io.h>
#include "MsgHndlrReg.h"
// Register CLogHndlr agent in the message handler registry
static CMsgHndlrRegistry< CLogHndlr > gLogHndlr( RUNTIME_CLASS( CLogHndlr ), "LOGHNDLR" );
IMPLEMENT_DYNCREATE( CLogHndlr, CMsgHndlr )
// CLogHndlr - Implementation
CLogHndlr::CLogHndlr()
{
// Step 1 - Initialize class members
m_csLogFile = "TEST.LOG";
}
CLogHndlr::~CLogHndlr()
{
}
INT CLogHndlr::SetLogFile( LPCSTR lpszLogFile )
{
// Step 1 - If log file is present, remove it
if( _access( lpszLogFile, 00 ) != -1 )
{
DeleteFile( lpszLogFile );
}
// Step 2 - Set m_csLogFile to the parameter passed
m_csLogFile = lpszLogFile;
return SUCCESS;
}
INT CLogHndlr::GetLogFile( CString & rcsLogFile )
{
// Step 1 - Set m_csLogFile to the parameter passed
rcsLogFile = m_csLogFile;
return SUCCESS;
}
// HandleMessage logs the message to the given log file
// It displays the message to the user after logging it to the file
// Application data is ignored in this method
INT CLogHndlr::HandleMessage( LPCSTR lpszMessage, INT nType, DWORD /* dwAppData */ )
{
INT nIcon = 0;
// Step 1 - Check, if log file name is provided
if( m_csLogFile.IsEmpty() == TRUE )
{
return FAILURE;
}
// Step 2 - Open the log file in append mode
FILE * pFPtr = fopen( m_csLogFile, "a+" );
if( pFPtr == NULL )
{
return FAILURE;
}
// Step 3 - Write the message to the log file
if( nType == INFO_TYPE )
{
fprintf( pFPtr, "Information : " );
nIcon = MB_ICONINFORMATION;
}
else if( nType == WARNING_TYPE )
{
fprintf( pFPtr, "Warning : " );
nIcon = MB_ICONEXCLAMATION;
}
else if( nType == ERROR_TYPE )
{
fprintf( pFPtr, "Error : " );
nIcon = MB_ICONSTOP;
}
fprintf( pFPtr, "%s\n", lpszMessage );
// Step 4 - Close the file
fclose( pFPtr );
// Step 5 - Display the message using AfxMessageBox with appropriate icons
AfxMessageBox( CString( "Log Message Handler : " ) + lpszMessage, MB_OK | nIcon );
return SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -