/***************************************
@ file: CLogFile.h
@ desc: 日志打印类
@ created: 2011/03/30 20:59
@ author: ICEFOXY
@ history:
****************************************/
#ifndef CLogFile_h__
#define CLogFile_h__
#include <fstream>
#include <string>
#include <iostream>
#include <Windows.h>
using namespace std;
#define LOGX mySpace::log<<__FILE__<<" line:"<<__LINE__<<ends
#define LOGBYTE mySpace::log<<__FILE__<<" line:"<<__LINE__<<ends;mySpace::log.LogBytes
/**************************************
Level DEBUG 指出细粒度信息事件对调试应用程序是非常有帮助的。
Level INFO 表明消息在粗粒度级别上突出强调应用程序的运行过程。
Level WARN 表明会出现潜在错误的情形。
Level ERROR 指出虽然发生错误事件,但仍然不影响系统的继续运行。
Level FATAL 指出每个严重的错误事件将会导致应用程序的退出。
Level ALL 是最低等级的,用于打开所有日志记录。
Level OFF 是最高等级的,用于关闭所有日志记录。
**************************************/
#define LOG_DEBUG mySpace::log<<__FILE__<<" line:"<<__LINE__<<" [DEBUG]"<<ends
#define LOG_INFO mySpace::log<<__FILE__<<" line:"<<__LINE__<<" [INFO]"<<ends
#define LOG_WARN mySpace::log<<__FILE__<<" line:"<<__LINE__<<" [WARN]"<<ends
#define LOG_ERROR mySpace::log<<__FILE__<<" line:"<<__LINE__<<" [ERROR]"<<ends
#define LOG_FATAL mySpace::log<<__FILE__<<" line:"<<__LINE__<<" [FATAL]"<<ends
class CLogFile
{
public:
CLogFile();
CLogFile( string logName );
~CLogFile();
ofstream & operator << ( char t );
ofstream & operator << ( char* t );
ofstream & operator << ( int t );
ofstream & operator << ( float t );
ofstream & operator << ( double t );
ofstream & operator << ( string t );
ofstream & LogBytes ( const char *, int );
///< 默认为此格式"[%0d %0d %0d %0d:%0d:%0d]"
bool SetTimeFmt( const char * );
protected:
bool TypeTimeNow();
///< 单字节转换字符到16进制整形
char * atoi_hex( char );
private:
ofstream m_ofs;
char m_format[32]; //时间显示格式
char m_fmtTime[32]; //格式化之后的时间
SYSTEMTIME sysTime;
bool bTimeNeed;
string m_fileName;
string m_filePath;
char m_Arr[3];
};
namespace mySpace
{
static CLogFile log("log1");
}
#endif // CLogFile_h__
/***************************************
@ file: CLogFile.cpp
@ desc:
@ created: 2011/03/30 21:00
@ author: ICEFOXY
@ history:
****************************************/
#include "CLogFile.h"
CLogFile::CLogFile()
{
char * fmt = "[%d %d %d %d:%d:%d]";
memcpy( m_format, fmt, strlen(fmt) );
m_filePath = "./LogFiles/";
m_filePath += "DefaultLog.log";
m_ofs.open( m_filePath.c_str(), ios_base::app|ios_base::out );
}
CLogFile::CLogFile( string logName )
{
char * fmt = "[%d %d %d %d:%d:%d]";
memcpy( m_format, fmt, sizeof(m_format) );
m_filePath = "./LogFiles/";
m_filePath += ( logName + ".log" );
m_ofs.open( m_filePath.c_str(), ios_base::app|ios_base::out );
}
CLogFile::~CLogFile()
{
m_ofs.close();
}
ofstream & CLogFile::operator << ( char t )
{
TypeTimeNow();
m_ofs << t;
return m_ofs;
}
ofstream & CLogFile::operator<< ( char* t )
{
TypeTimeNow();
m_ofs << t;
return m_ofs;
}
ofstream & CLogFile::operator << ( int t )
{
TypeTimeNow();
m_ofs << t;
return m_ofs;
}
ofstream & CLogFile::operator << ( float t )
{
TypeTimeNow();
m_ofs << t;
return m_ofs;
}
ofstream & CLogFile::operator << ( double t )
{
TypeTimeNow();
m_ofs << t;
return m_ofs;
}
ofstream & CLogFile::operator << ( string t )
{
TypeTimeNow();
m_ofs << t;
return m_ofs;
}
ofstream & CLogFile::LogBytes( const char * pChar, int nLength )
{
//TypeTimeNow();
if ( nLength < 0 || nLength > 9999 )
{
m_ofs << " nBytes Error ! " << endl;
return m_ofs;
}
m_ofs << "Length[" << nLength << "]" << ends;
for ( int i = 0; i < nLength; ++i )
{
m_ofs << atoi_hex( *(pChar + i) ) << ends;
}
m_ofs << endl;
return m_ofs;
}
bool CLogFile::TypeTimeNow()
{
GetLocalTime( &sysTime );
sprintf_s( m_fmtTime, sizeof(m_fmtTime), m_format,
sysTime.wYear, sysTime.wMonth, sysTime.wDay,
sysTime.wHour, sysTime.wMinute, sysTime.wSecond );
m_ofs << m_fmtTime << ends;
return true;
}
bool CLogFile::SetTimeFmt( const char * fmt )
{
memcpy( m_format, fmt, sizeof(m_format) );
return true;
}
char * CLogFile::atoi_hex( char c )
{
ZeroMemory( m_Arr, sizeof(m_Arr) );
int iVar( c & 0x7f );
if ( iVar < 0x10 )
{
m_Arr[0] = '0';
_itoa_s( iVar, m_Arr + 1,2, 16 );
}
else
{
_itoa_s( iVar, m_Arr,10, 16 );
}
_strupr_s ( m_Arr, sizeof(m_Arr) );
return m_Arr;
}