資源簡介
用于MFC程序日志記錄類,方面錯誤的查找

代碼片段和文件信息
#include?“StdAfx.h“
#include?“Log.h“
#define?LOG_EVENT?_T(“Global\\ChyLogWrite“)??
CString?CLog::s_strLogFile?=?_T(““);??
CString?CLog::s_strLogPrefix?=?_T(““);
CString?CLog::s_errorInfo=_T(““);
HANDLE?CLog::s_hWriteEvent?=?NULL;??
CLog::CLog(void)
{
}
CLog::~CLog(void)
{
}
//設置路徑和名稱分為兩種模式
//如果給出的不是絕對路徑:在EXE所在的目錄下,新建一個LOG文件夾,log文件夾下日志文件名稱為SMMS2014-10-28.log
//如果給出的是絕對路徑:則以絕對路徑為準。
short?CLog::SetLogFile(LPCTSTR?strPath)
{
//如果給的路徑為空,則設置為xxx.log目錄為EXE同目錄
if?(strPath==NULL)
{
//?得到當前的文件名
CString?strFileName;
GetModuleFileName(AfxGetInstanceHandle()strFileName.GetBuffer(_MAX_PATH)_MAX_PATH);
strFileName.ReleaseBuffer();
//?得到當前目錄
strFileName.Replace(“.exe““.log“);
s_strLogFile=strFileName;
}
else
{
s_strLogFile.Format(_T(“%s.log“)strPath);
}?
return?1;
}
short?CLog::SetPrefix(LPCTSTR?strPrefix)
{
if(strPrefix?&&?strPrefix[0])
{
s_strLogPrefix?=?strPrefix;
}
return?1;
}
//清空當前的文件,新建
void?CLog::CreateNewFile()
{
//-打開關閉文件-
if(s_strLogFile.IsEmpty())
SetLogFile(NULL);
CStdioFile?file;
if(!file.Open(s_strLogFile?CFile::modeCreate))
{
CString?str;
str.Format(_T(“打開文件失敗!路徑為:%s“)s_strLogFile);
AfxMessageBox(str);
exit(0);
}
file.Close();
}
short?CLog::GetDugInfo(LPCSTR?pDugInfo/*?=NULL?*/)
{
// char?file[16];??
//// char?func[16];??
// int?line;???
// sprintf(file__FILE__);?//文件名??
//// sprintf(func__FUNCTION__);//函數名????
// line=__LINE__; //行號
s_errorInfo.Format(_T(“%s“)pDugInfo);
return?1;
}
short?CLog::WriteMsg(LPCTSTR?strFormat)
{
//--
if(s_hWriteEvent==NULL)
{
s_hWriteEvent?=?OpenEvent(0?FALSELOG_EVENT);
if(s_hWriteEvent==NULL)
s_hWriteEvent?=?CreateEvent(NULL?FALSE?TRUE?LOG_EVENT);?
}
WaitForSingleobject(s_hWriteEvent?INFINITE);
//-打開關閉文件-
if(s_strLogFile.IsEmpty())
SetLogFile(NULL);
CStdioFile?file;
if(file.Open(s_strLogFile?CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite))
{
file.SeekToEnd();
CString???strPart_Time;
{
SYSTEMTIME?sysTime??=?{0};
GetLocalTime(&sysTime);
strPart_Time.Format(_T(“\n%2d-%2d-%2d?%2d:%2d:%2d“)??
sysTime.wYearsysTime.wMonthsysTime.wDaysysTime.wHour?sysTime.wMinute?sysTime.wSecond);?
}
CString??str;
str=strPart_Time+_T(“\t“)+s_errorInfo;
file.WriteString(str);
s_errorInfo=““;
str=_T(“\n[“)+s_strLogPrefix+_T(“]“)+strFormat+_T(“\n“);
file.WriteString(str);
file.Close();
}
SetEvent(s_hWriteEvent);
return?1;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2704??2014-12-30?14:50??Log.cpp
?????文件????????726??2014-12-30?14:41??Log.h
-----------?---------??----------?-----??----
?????????????????3430????????????????????2
- 上一篇:數據結構算法與應用 c++語言描述(第二版代碼
- 下一篇:LBM潰壩算例代碼
評論
共有 條評論