2010 spring OOD proj3 备忘

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值