1 , MSG Node , SYSTIME , GetLocalTime( & SYSTIME );
#include<windows.h> //GetLocalTime
class MsgNode
{
public:
LType loggerType;
std::string Inf;
std::string occTime;
Behavior aff;
std::string getTime()
{
SYSTEMTIME systime;
std::ostringstream os;
GetLocalTime( &systime );
os << systime.wHour <<":"<<systime.wMinute<<":"<<systime.wSecond<<"."<<systime.wMilliseconds;
return os.str();
}
};
2 , override new , delete , new[] , delete[]
//OverLoad global new , new[] and delete
void *operator new(size_t size , const std::string & inf )
{
void *p = ::operator new(size);
if( !p )
{
std::bad_alloc ba;
throw ba;
}
std::ostringstream os;
os << " At Address 0x" << std::hex << p;
os << " Size: " << size << " Bytes";
std::string objname( inf , 0 , inf.find_first_of('(') );
Logger::MsgNode ms;
ms.aff = Logger::bNEW;
ms.Inf = objname + os.str() ;
ms.loggerType = Logger::tUNDEFINE;
ms.occTime = ms.getTime();
Logger::LManager::receiveMsg( ms );
return p;
}
void *operator new[](size_t size , const std::string & inf )
{
void *p;
p = ::operator new[]( size );
if( !p )
{
std::bad_alloc ba;
throw ba;
}
std::ostringstream os;
os << " Array[ ] At Address 0x" << std::hex << p;
os << " Size: " << size << " Bytes";
std::string objname( inf , 0 , inf.find_first_of('(') );
Logger::MsgNode ms;
ms.aff = Logger::bNEW;
ms.Inf = objname + os.str() ;
ms.loggerType = Logger::tUNDEFINE;
ms.occTime = ms.getTime();
Logger::LManager::receiveMsg( ms );
return p;
}
void operator delete( void *p , const std::string & inf)
{
std::ostringstream os;
os << " (Pointer) At Address 0x" << std::hex << p;
Logger::MsgNode ms;
ms.aff = Logger::bDELETE;
ms.Inf = inf + os.str();
ms.loggerType = Logger::tUNDEFINE;
ms.occTime = ms.getTime();
Logger::LManager::receiveMsg( ms );
delete p ;
}
template< typename T>
// delete [] need template so that function know the type of undefault node type
void mydelete(T *p, const std::string & inf)
{
std::ostringstream os;
os << " (Pointer Array) At Address 0x" << std::hex << p;
Logger::MsgNode ms;
ms.aff = Logger::bDELETE;
ms.Inf = inf + os.str() ;
ms.loggerType = Logger::tUNDEFINE;
ms.occTime = ms.getTime();
Logger::LManager::receiveMsg( ms );
delete[] p;
}
3 disable warning 4291
#pragma warning(disable:4291)
4 log manager
///--------------------Logger Manager ------//
class LManager
{
public:
LManager():pOut(0){};
~LManager();
static ILogger* creatLogger( LType type , std::string &fname);
static void receiveMsg( MsgNode &msg );
static void regNewLogger( LType , ILogger *(*fp)( std::string &name ));
static void clearBuf(){ MsgBuf.clear(); }
static void reSet();
bool attachOutFile( const std::string &filename = std::string("./Log.txt") );
void handleMsg();
void toConsole(MsgNode &m);
void toFile( MsgNode &m);
private:
static std::deque< MsgNode > MsgBuf; // receive msgNode
static std::map< LType , ILogger *(*)( std::string &) > Reg; // connect all kinds of logger
std::ofstream *pOut;
};
ILogger* LManager::creatLogger( LType type , std::string &fname)
{
std::map< LType , ILogger *(*)( std::string &name ) >::const_iterator index = Reg.find( type );
if( index != Reg.end() )
{
return index->second( fname );
}
else return NULL;
}