#include "StdAfx.h" #include "DisplayMessage.h" CDisplayMessage::CDisplayMessage(void) { m_pDM2P = NULL; m_pFileLog = NULL; InitializeCriticalSection(&m_csLog); } CDisplayMessage::~CDisplayMessage(void) { if (m_pFileLog) { delete m_pFileLog; m_pFileLog = NULL; } DeleteCriticalSection(&m_csLog); } void CDisplayMessage::SetPath(const CString& strPath) { m_strLogPath = strPath; CreateDirectory(m_strLogPath, NULL); m_FSLog.SetRemainDelete(m_strLogPath, TARGETTYPE_FILE, 300); MakeLogFile(); } BOOL CDisplayMessage::MakeLogFile() { // Log 贸府 if (m_strLogPath.IsEmpty()) return FALSE; m_FSLog.CommitSchedule(); m_TimeLogFile = CTime::GetCurrentTime(); m_strLogFile.Format(_T("%s\\%04d-%02d-%02d.log"), m_strLogPath, m_TimeLogFile.GetYear(), m_TimeLogFile.GetMonth(), m_TimeLogFile.GetDay()); if (m_pFileLog) { delete m_pFileLog; m_pFileLog = NULL; } m_pFileLog = new CStdioFile(); return TRUE; } void CDisplayMessage::DisplayMessage(const CString& strMessage) { CTime time = CTime::GetCurrentTime(); CString strValue = _T(""); strValue.Format(_T("[%02d:%02d:%02d] %s"), time.GetHour(), time.GetMinute(), time.GetSecond(), strMessage); WriteToFile(strValue); if(m_pDM2P) { m_pDM2P->DisplayMessage((TCHAR*)(LPCTSTR)strValue); } } void CDisplayMessage::DisplayMessage(const TCHAR* lpstrFormat, ...) { va_list list; TCHAR strText[2000] = {0}; va_start(list, lpstrFormat); _vstprintf_s(strText, lpstrFormat, list); va_end(list); CTime time = CTime::GetCurrentTime(); CString strValue = _T(""); strValue.Format(_T("[%02d:%02d:%02d] %s"), time.GetHour(), time.GetMinute(), time.GetSecond(), strText); WriteToFile(strValue); if(m_pDM2P) { m_pDM2P->DisplayMessage((TCHAR*)(LPCTSTR)strValue); } } BOOL CDisplayMessage::WriteToFile(const CString& strMessage) { if(m_pFileLog == NULL) return FALSE; EnterCriticalSection(&m_csLog); CTime time = CTime::GetCurrentTime(); if ((time.GetMonth() != m_TimeLogFile.GetMonth()) || (time.GetDay() != m_TimeLogFile.GetDay()) || !m_pFileLog) { MakeLogFile(); } if ( !m_pFileLog->Open(m_strLogFile, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate | CFile::shareDenyNone, &m_fileException) ) { // #ifdef _DEBUG // TCHAR szCause[255] = {0}; // ex.GetErrorMessage(szCause, 255); // TRACE(szCause); // #endif LeaveCriticalSection(&m_csLog); return FALSE; } CString strText; strText.Format(_T("%s\r\n"), strMessage); m_pFileLog->SeekToEnd(); m_pFileLog->WriteString((TCHAR*)(LPCTSTR)strText); m_pFileLog->Close(); LeaveCriticalSection(&m_csLog); return TRUE; }