WriteLog

作者在 2010-01-22 13:36:04 发布以下内容

void WriteLog(CString content)
{
 g_cs.Lock();
 SYSTEMTIME st;
 CString strtm, strout;
 ::GetLocalTime(&st);
 strtm.Format("%d-%.2d-%.2d %.2d:%.2d:%.2d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
 strout.Format("<%s>  %s\r\n",strtm,content);
 OutputDebugString(strout);
 char moduleFileName[MAX_PATH];
 GetModuleFileName(NULL,moduleFileName,MAX_PATH);
 (_tcsrchr(moduleFileName,'\\'))[1] = 0;
 CString logDirectory;
 BOOL bNeedCreate = FALSE;
 logDirectory.Format("%s%s",moduleFileName,"logs");
 CFileFind ff;
 BOOL bFind = ff.FindFile(logDirectory);
 if(!bFind)
 {
  bNeedCreate = TRUE;
 }
 else if(ff.FindNextFile())
 {
  if(!ff.IsDirectory())
  {
   bNeedCreate = TRUE;
  }
 }
 if(bNeedCreate)
 {
  BOOL bSucc = CreateDirectory(logDirectory,NULL);
  if(!bSucc)
  {
   TRACE("创建日志目录失败.\n");
   g_cs.Unlock();
   return;
  }
 }
 CString logFileName;
 logFileName.Format("%s\\%d%.2d%.2d.log",logDirectory,st.wYear,st.wMonth,st.wDay);
 CFile logFile;
 try
 {
  logFile.Open(logFileName,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
  logFile.SeekToEnd();
  logFile.Write(strout.GetBuffer(0),strout.GetLength());
  logFile.Close();
 }
 catch(...)
 {
  TRACE("写日志文件出错.\n");
 }
 g_cs.Unlock();
}
 
默认分类 | 阅读 1116 次
文章评论,共0条
游客请输入验证码
浏览52324次
文章分类
最新评论