LogFile.cpp
资源名称:SmartMailSvr [点击查看]
上传用户:qdlutongda
上传日期:2007-01-14
资源大小:133k
文件大小:3k
源码类别:
Email客户端
开发平台:
Visual C++
- // LogFile.cpp: implementation of the CLogFile class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "LogFile.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- //extern CCriticalSection m_CritSection;
- //extern CSingleLock m_SingleLock(&m_CritSection);
- CLogFile::CLogFile()
- {
- m_bWriteLog = TRUE;
- m_strLogFilePath=_T("");
- }
- CLogFile::~CLogFile()
- {
- }
- void CLogFile::SetLogFilePath(CString strLogPath)
- {
- if(strLogPath.Right(1) != '\')
- m_strLogFilePath = strLogPath + _T("\");
- else
- m_strLogFilePath = strLogPath;
- m_strLogFilePath = m_strLogFilePath + "log";
- CreateDirectory(m_strLogFilePath, NULL);
- }
- void CLogFile::WriteLog(CString strALine_)
- {
- if(m_strLogFilePath == "")
- return ;
- if (this->m_bWriteLog == FALSE)
- return;
- CStdioFile fileLog;
- CString strFilename=_T("");
- GetLogDateFileName(strFilename);
- //get current time
- time_t t;
- struct tm *date;
- t = time(NULL);
- date = localtime(&t);
- CString strTime;
- strTime.Format("%d-%d-%d %d:%d:%d", (date->tm_year+1900), (date->tm_mon+1),
- date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec);
- if(fileLog.Open(strFilename,CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate|
- CFile::shareDenyNone))
- {
- TRY{
- fileLog.SeekToEnd();
- fileLog.WriteString(strTime + CString("t") + strALine_ + CString(_T("n")));
- }CATCH( CFileException, e )
- {
- fileLog.Close();
- }
- END_CATCH
- fileLog.Close();
- }
- }
- void CLogFile::SetValueToRegistry(CString strWriteLog_,CString strLogFilePath_)
- {
- // m_pApp->WriteProfileString(_T("Log"),_T("WriteLog"),strWriteLog_);
- // m_pApp->WriteProfileString(_T("Log"),_T("LogFile"),strLogFilePath_);
- }
- bool CLogFile::GetValueFromRegistry()
- {
- /* CString strWriteLog;
- CString strLogFilePath;
- strWriteLog = m_pApp->GetProfileString(_T("Log"),_T("WriteLog"));
- if (strWriteLog.IsEmpty())
- return false;
- else
- {
- strWriteLog.MakeUpper();
- if (strWriteLog == _T("YES"))
- m_bWriteLog = TRUE;
- else
- m_bWriteLog = FALSE;
- }
- strLogFilePath = m_pApp->GetProfileString(_T("Log"),_T("LogFile"));
- if (! strLogFilePath.IsEmpty())
- m_strLogFilePath = strLogFilePath;
- return true;
- */
- return false;
- }
- /* 根据当前的日期和时间设定日志文件名YYYY:MM:DD HH:MM:SS */
- void CLogFile::GetLogDateFileName(CString& str)
- {
- time_t t;
- struct tm *date;
- t = time(NULL);
- date = localtime(&t);
- str.Format("%s\%d%02d%02d.txt", m_strLogFilePath,(1900+date->tm_year), date->tm_mon+1,date->tm_mday);
- return;
- }