#include "StdAfx.h"
#include "Log.h"
Log::Log(CString strFileName/* = ""*/, CString strPostfix/* = "log"*/)
{
m_logLevel = LOG_DEBUG;
m_strFileName = strFileName;
strPostfix.Replace(".", "");
m_strPostfix = strPostfix;
m_nType = 0;//日志类型:0-按天生成,1-单个日志文件
//日志文件夹
m_strLogFolder.Format("%sLog\\",GetModulePath());
CreateDirectory(m_strLogFolder, NULL);//生成文件夹
::InitializeCriticalSection(&m_criticalSection);
}
Log::~Log(void)
{
::DeleteCriticalSection(&m_criticalSection);
}
bool Log::WriteLog(CString strLog,LogLevel logLevel)
{
if (logLevel > m_logLevel)
{
return false;
}
m_hMutex.Lock();
CString strLogDir = m_strLogFolder + GetDT() + "\\";
CreateDirectory(strLogDir, NULL);
CString strLogFile;
if (0 == m_nType)//日志类型:0-按天生成
{
strLogFile.Format("%s%s%s.%s", strLogDir, m_strFileName, GetDT(), m_strPostfix);
}
else if (1 == m_nType)//日志类型:1-单个日志文件
{
strLogFile.Format("%s%s.%s", strLogDir, m_strFileName, m_strPostfix);
}
//m_strFile = strLogFile;
::EnterCriticalSection(&m_criticalSection);
//locale::global(locale(""));//将全局区域设为操作系统默认区域--用于中文文件夹
//m_fileLog.open(strLogFile, ios::app);
//locale::global(locale("C"));//还原全局区域设定
m_pFile = fopen(strLogFile,"a+");
if(m_pFile)
{
CString strTemp;
CString strDayTime = GetCurrentDateTime();
strTemp.Format("[%s]: \n%s\n", strDayTime, strLog);
fputs(strTemp.GetBuffer(), m_pFile);
fflush(m_pFile);
fclose(m_pFile);
//m_fileLog << strTemp;//write to log file
}
//m_fileLog.close();
::LeaveCriticalSection(&m_criticalSection);
m_hMutex.Unlock();
return true;
}
bool Log::WriteLog(CString strLog, CString strFile,LogLevel logLevel)
{
if (logLevel > m_logLevel)
{
return false;
}
m_hMutex.Lock();
CString strLogDir = m_strLogFolder + GetDT() + "\\";
CreateDirectory(strLogDir, NULL);
CString strLogFile;
strLogFile.Format("%s%s.%s", strLogDir, strFile, m_strPostfix);
::EnterCriticalSection(&m_criticalSection);
//locale::global(locale(""));//将全局区域设为操作系统默认区域--用于中文文件夹
//m_fileLog.open(strLogFile, ios::app);
//locale::global(locale("C"));//还原全局区域设定
m_pFile = fopen(strLogFile,"a+");
if(m_pFile)
{
CString strTemp;
CString strDayTime = GetCurrentDateTime();
strTemp.Format("[%s]: \n%s\n", strDayTime, strLog);
fputs(strTemp.GetBuffer(), m_pFile);
fflush(m_pFile);
fclose(m_pFile);
//m_fileLog << strTemp;//write to log file
}
//m_fileLog.close();
::LeaveCriticalSection(&m_criticalSection);
m_hMutex.Unlock();
return true;
}
//获取系统当前日期,格式如:2010-06-22
CString Log::GetCurrentDay()
{
CString strDay;//2010-06-22
COleDateTime currentTime = COleDateTime::GetCurrentTime();
strDay = currentTime.Format("%Y-%m-%d");
return strDay;
}
//获取系统当前日期,格式如:20100622
CString Log::GetDT()
{
CString strDT;//20100622
COleDateTime currentTime = COleDateTime::GetCurrentTime();
strDT = currentTime.Format("%Y%m%d");
return strDT;
}
//获取系统当前日期与时间,格式如:2009-06-29 17:16:20
CString Log::GetCurrentDateTime()
{
CString strTime;//2009-06-29 17:16:20
COleDateTime currentTime = COleDateTime::GetCurrentTime();
strTime = currentTime.Format("%Y-%m-%d %H:%M:%S");
return strTime;
}
//获取执行程序所在路径
CString Log::GetModulePath()
{
CString strPath;
//----------------------------------------------------------
char cFile[MAX_CHAR];
GetModuleFileName(NULL, cFile, sizeof(cFile));
CString strTemp = cFile;
int nPos = strTemp.ReverseFind('\\');
strPath = strTemp.Left(nPos+1);
//----------------------------------------------------------
return strPath;
}
//设置日志等级
void Log::SetLogLevel(LogLevel logLevel)
{
m_logLevel = logLevel;
}
//删除过期日志文件
void Log::DeleteLog(int nLogSaveDays)
{
CString strLogFile;
strLogFile.Format("%s\\*.log", m_strLogFolder);
CFileFind finder;
BOOL bWorking = finder.FindFile(strLogFile);
while (bWorking)
{
bWorking = finder.FindNextFile();
CTime currentTime = CTime::GetCurrentTime();
CTime timeFile;
finder.GetLastAccessTime(timeFile);
CTimeSpan ts = currentTime - timeFile;
int nSecond = (int)ts.GetTotalSeconds();
if (nSecond > nLogSaveDays*24*60*60)//超时,过期
{
CString strFile;
strFile.Format("%s\\%s", m_strLogFolder, finder.GetFileName());
DeleteFile(strFile);
}
}
}