CFile m_fileTestLog;

来源:互联网 发布:vb与西门子plc通讯 编辑:程序博客网 时间:2024/06/07 01:28

CFile m_fileTestLog;

 

void CEventsLog::AddTestLog(CString strContent)
{
 //TRACE(strContent);
 DebugMsg(strContent);
 
 CSingleLock   TestLogLock(&m_TestLogLock);
 m_TestLogLock.Lock();
 
 CFileException e;

 try //////////////////////////////////////////                                                                                                     
 {
  //
  
  COleDateTime dtCurrent = COleDateTime::GetCurrentTime();
  
  CString strTemp = dtCurrent.Format("%Y-%m-%d %H:%M:%S >>");
  strContent = strTemp + strContent;

  if (m_bRebuildTest)//重建文件
  {
   if (!m_fileTestLog.Open("C://ICTTestLog.TXT",CFile::modeCreate | CFile::modeWrite | CFile::modeRead) )
   {
    return ;
   }
   m_bRebuildTest = FALSE ;
  }
  else if (!m_fileTestLog.Open("C://ICTTestLog.TXT", CFile::modeWrite | CFile::modeRead))
  {
   if (!m_fileTestLog.Open("C://ICTTestLog.TXT",CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate | CFile::modeRead) )
   {
    return ;
   }
  }

  m_fileTestLog.Seek(0,CFile::end);
   
  int nLength = strContent.GetLength();
  if (nLength>0)
  {
   m_fileTestLog.Write((LPCTSTR)strContent,nLength);
  }
  char chEnter[2];
  chEnter[0]=0x0d;
  chEnter[1]=0x0a;
  m_fileTestLog.Write(chEnter,2);

  m_nWriteTestfileLogTimes++;
  //////检查文件大小
  if (m_nWriteTestfileLogTimes>100)//每写一百次检查一次
  {
   m_nWriteTestfileLogTimes = 0 ;

   CFileStatus status;

   if( m_fileTestLog.GetStatus( status ) )
   {
    if ( status.m_size > 1024*1024*20 ) m_bRebuildTest = TRUE; //
   }

  }

  m_fileTestLog.Close();
  
 }///////////////////////////////////////////////
 catch( CException* e )                                                                                                                           
 {                                                                                                                                                
  ::Sleep(100); 
  e->Delete();
  //m_SystemExceptionLock.Unlock;
  //AfxMessageBox( _T("错误!"), MB_ICONEXCLAMATION );                                                                       
 }
 catch(...)                                                                                                                           
 {                                                                                                                                                
  //g_EventsLog.AddSystemException("...");
  //m_SystemExceptionLock.Unlock;
 }
 m_TestLogLock.Unlock();

}

 

 

原创粉丝点击