📄 _log.h
字号:
/************************************
REVISION LOG ENTRY
Revision By: Alex Turc
Revised on 6/5/00 12:03:17 PM
Comments: Logging facilities
************************************/
#ifndef ___log_h__
#define ___log_h__
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
#include <stdarg.h>
#include "_exception.h"
namespace extension
{
/////////////
// Exceptions
/////////////
class event_log_exception :
public extended_exception
{
public:
event_log_exception( long nCode, const char* pDescription = "Event log exception", const char* pFile = "", long nLine = -1 ) :
extended_exception( nCode, pDescription, pFile, nLine )
{
}
};
/*
May 29 2000
Used for event logging
*/
class event_log
{
public:
// Event types
typedef enum event_type
{
event_type_error,
event_type_warning,
event_type_information
} event_type;
event_log( char* szFile, bool bLogging = true )
{
m_strFileName = szFile;
m_bLogging = bLogging;
log_event( event_type_information, "Connection to log file established" );
}
virtual ~event_log()
{
log_event( event_type_information, "Connection to log file closed" );
}
void log_event( event_type evtType, const char* szFormat, ... )
{
if( !m_bLogging )
return;
static const iBufferSize = 1024 * 5;
char pBuffer[iBufferSize];
// Print the parameters into a memory buffer
va_list arg_list;
va_start( arg_list, szFormat );
::vsprintf( pBuffer, szFormat, arg_list );
va_end( arg_list );
// Write data to the log file
fstream fsLog( m_strFileName.c_str(), ios_base::out | ios_base::app );
time_t t = time( NULL );
string strTime = ctime( &t );
fsLog << endl << strTime.substr( 0, strTime.size() - 1 ) << "\t\t" << event_type_to_string( evtType ) << "\t" << pBuffer;
fsLog.close();
}
void enable_logging( bool bLogging )
{
m_bLogging = bLogging;
}
private:
// Flag used to enable or disable logging
bool m_bLogging;
// Name of the log file
string m_strFileName;
const char* event_type_to_string( event_type evtType )
{
static const char* events[] =
{
"Error ",
"Warning ",
"Information"
};
return events[evtType];
}
};
} // Namespace alx
#endif // ___log_h__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -