资源简介
用MFC写的日志类,用于程序打日志
代码片段和文件信息
#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 strLogLogLevel 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 strFileLogLevel 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“);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2335 2013-08-28 15:37 Log.h
文件 4556 2013-08-28 15:37 Log.cpp
- 上一篇:学生成绩管理系统 C、VC++
- 下一篇:图的邻接表存储实现及深度优先遍历
评论
共有 条评论