当前位置:   article > 正文

VC++6.0自定义实现日志记录到文件及界面显示

VC++6.0自定义实现日志记录到文件及界面显示

1,实现日志记录到文件,支持日志文件大小限制,节省空间

  1. void CController::LogWriteRecord(CString strText,CString strFileName)
  2. {
  3. CStdioFile csFile;
  4. try
  5. {
  6. BOOL bRet = csFile.Open(strFileName,CFile::modeCreate
  7. |CFile::modeNoTruncate
  8. |CFile::modeReadWrite|CFile::shareDenyNone|CFile::typeText);
  9. csFile.SeekToEnd();
  10. CString strWriteText=strText+"\r\n";
  11. csFile.WriteString(strWriteText);
  12. //csFile.Write(strWriteText.GetBuffer(strWriteText.GetLength()),1);
  13. strWriteText.ReleaseBuffer();
  14. DWORD64 dwFileLen = csFile.GetLength();
  15. if(!bRet)
  16. {
  17. TRACE("Open failed exception\r\n");
  18. }
  19. if(dwFileLen > constIFileMaxLen)
  20. {
  21. csFile.Seek(-constIFileMaxLen,CFile::end);
  22. char *pcBuf = new char[constIFileMaxLen];
  23. memset(pcBuf,0,constIFileMaxLen);
  24. csFile.Read(pcBuf,constIFileMaxLen);
  25. int iLoop = 0;
  26. for( iLoop = 0;iLoop<constIFileMaxLen;iLoop++)
  27. {
  28. if(0x0a == pcBuf[iLoop])
  29. {
  30. break;
  31. }
  32. }
  33. iLoop++;
  34. csFile.SeekToBegin();
  35. csFile.Write(pcBuf+iLoop,constIFileMaxLen-iLoop);
  36. csFile.SetLength((DWORD)constIFileMaxLen-iLoop);
  37. delete [] pcBuf;
  38. pcBuf = NULL;
  39. }
  40. csFile.Flush();
  41. csFile.Close();
  42. }
  43. catch (...)
  44. {
  45. csFile.Close();
  46. TRACE("Write log exception\r\n");
  47. }
  48. }
  1. void CController::LogWriteOneRecord(CString strFunction,CString strType,CString strText)
  2. {
  3. CDuplicatefilesclearDlg *pDlg = (CDuplicatefilesclearDlg*)m_pDlg;
  4. //LogWriteRecord("["+ CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S")+"]"+strText,GetLogFileName());
  5. TRACE(strText);
  6. TRACE("\r\n");
  7. m_strWriteText=("["+ CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S")+"]"+strFunction+"() "+strType+strText);
  8. LogWriteRecord(m_strWriteText,GetLogFileName());
  9. //显示到界面
  10. pDlg->AppendLog(strText);
  11. }

2,实现记录到UI Edit控件

2.1使用Edit控件,使用多行模式

2.2 编写ApendLog UI函数

  1. void CDuplicatefilesclearDlg::AppendLog(CString str)
  2. {
  3. CString strText;
  4. GetDlgItemText(IDC_EDIT_Msg,strText);
  5. strText += "\r\n";
  6. strText += str;
  7. if(strText.GetLength() > 10240)
  8. {
  9. strText= strText.Right(strText.GetLength()/2);
  10. }
  11. SetDlgItemText(IDC_EDIT_Msg,strText);
  12. int nLen = ((CEdit*)GetDlgItem(IDC_EDIT_Msg))->SendMessage(WM_GETTEXTLENGTH);
  13. ((CEdit*)GetDlgItem(IDC_EDIT_Msg))->SetSel(nLen, nLen, FALSE);
  14. }

2.3 使用代码

  1. m_pControl = new CController(this);
  2. m_pControl->LogWriteOneRecord("CDuplicatefilesclearDlg::OnInitDialog()","[I] "," CDuplicatefilesclearDlg::OnInitDialog()函数...");

2.5运行结果

完整代码上传到资源,欢迎下载!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/600975
推荐阅读
相关标签
  

闽ICP备14008679号