📄 errorlog.cpp
字号:
#include "StdAfx.h"
#include ".\errorlog.h"
CErrorLog* CErrorLog::m_pInstance = NULL;
int CErrorLog::m_nApiErr = 0;
int CErrorLog::m_nComErr = S_OK;
int CErrorLog::m_nDllErr = 0;
CErrorLog* CErrorLog::Instance()
{
return m_pInstance == NULL ? m_pInstance = new CErrorLog : m_pInstance;
}
void CErrorLog::Release()
{
if (m_pInstance != NULL)
{
delete m_pInstance;
m_pInstance =NULL;
}
}
CErrorLog::CErrorLog(void)
{
}
CErrorLog::~CErrorLog(void)
{
}
///////////////////////////////////////////////////////////////////////
//
// description:
// 将错误字符串写入日志文件函数
// parameter:
// strClass 捕捉到错误的类的名称
// strFunc 捕捉到错误的函数的名称
// strError 错误原因描述
// return: none
// time: 2006-01-22
// history:
//
///////////////////////////////////////////////////////////////////////
void CErrorLog::WriteErrLogStr(const CString& strClass,
const CString& strFunc,
const CString& strError)
{
//日期时间
CTime tmeNow = CTime::GetCurrentTime();
CString strNow = tmeNow.Format("%Y-%m-%d\t%H:%M:%S");
CString strBuffer = strNow + '\t' + strClass + '\t'
+ strFunc + '\t' + strError + '\r' + '\n';
//获得执行文件目录
CString strPath = GetExePath();
if (strPath.GetLength() == 0)
{
return;
}
if (strPath.Right(1) != '\\')
{
strPath += '\\';
}
//如果错误日志文件不存在,则创建
CFile filError;
if (CheckFileIfExist(strPath + "Error.log"))
{
if (filError.Open(strPath + "Error.log", CFile::modeWrite))
{
//如果日志文件>0.5M字节,则清空它
if (filError.GetLength() > 524288)
{
filError.SetLength(0);
}
}
else
{
return;
}
}
else
{
if (!filError.Open(strPath + "Error.log", CFile::modeWrite | CFile::modeCreate))
{
return;
}
}
filError.Seek(0, CFile::end);
filError.Write(strBuffer, strBuffer.GetLength());
filError.Close();
return;
}
///////////////////////////////////////////////////////////////////////
//
// description:
// 将错误代码写入日志文件函数
// parameter:
// strClass 捕捉到错误的类
// strFunc 捕捉到错误的函数
// unError 错误代码
// return: none
// time: 2006-01-22
// history:
//
///////////////////////////////////////////////////////////////////////
void CErrorLog::WriteErrLogNum(const CString& strClass,
const CString& strFunc,
unsigned int unError)
{
//日期时间
CTime tmeNow = CTime::GetCurrentTime();
CString strNow = tmeNow.Format("%Y-%m-%d\t%H:%M:%S");
CString strBuffer;
//发生错误的最底层的函数如果返回1,说明是一个SDK错误,保存SDK错误代码
if ((unError & 0xF) == 1)
{
strBuffer.Format("%s\t%s\t%s\tFunc:%X\tAPI:%X\tCom:%X\tDll:%X\r\n",
strNow, strClass, strFunc, unError, m_nApiErr, m_nComErr, m_nDllErr);
}
else
{
strBuffer.Format("%s\t%s\t%s\tFunc:%X\r\n",
strNow, strClass, strFunc, unError);
}
//获得执行文件目录
CString strPath = GetExePath();
if (strPath.GetLength() == 0)
{
return;
}
if (strPath.Right(1) != '\\')
{
strPath += '\\';
}
//如果错误日志文件不存在,则创建
CFile filError;
if (CheckFileIfExist(strPath + "Error.log"))
{
if (filError.Open(strPath + "Error.log", CFile::modeWrite))
{
//如果日志文件>0.5M字节,则清空它
if (filError.GetLength() > 524288)
{
filError.SetLength(0);
}
}
else
{
return;
}
}
else
{
if (!filError.Open(strPath + "Error.log", CFile::modeWrite | CFile::modeCreate))
{
return;
}
}
filError.Seek(0, CFile::end);
filError.Write(strBuffer, strBuffer.GetLength());
filError.Close();
return;
}
///////////////////////////////////////////////////////////////////////
//
// description:
// 将SDK错误代码(WIN32 API错误、DLL函数返回错误值、COM错误)写入
// 全局变量m_nApiErr、m_nComErr、m_nDllErr
// parameter:
// strClass 捕捉到错误的类
// strFunc 捕捉到错误的函数
// nError 错误代码
// return: none
// time: 2006-01-22
// history:
//
///////////////////////////////////////////////////////////////////////
void CErrorLog::SetSdkError(int nApiError, int nComError, int nDllError)
{
m_nApiErr = 0;
m_nComErr = S_OK;
m_nDllErr = 0;
if (nApiError != 0)
{
m_nApiErr = nApiError;
}
else if (nComError != S_OK)
{
m_nComErr = nComError;
}
else if (nDllError != 0)
{
m_nDllErr = nDllError;
}
}
///////////////////////////////////////////////////////////////////////
//
// description:
// 级联错误号:如果子函数返回0xE,则将父函数的错误号0xF添加在子函数
// 错误号之上:0xFE,0xFE用来返回给上一级函数,如果级联层数超过8层,
// 则子函数的错误号不变(即父函数的错误号被忽略)
// parameter:
// unErrorF 父函数的错误号
// unErrorS 子函数的错误号
// return: 级联后的错误号
// time: 2006-01-22
// history:
//
///////////////////////////////////////////////////////////////////////
unsigned int CErrorLog::AddError(unsigned int unErrorF, unsigned int unErrorS)
{
for (int nCount = 1; nCount <= 7; nCount++)
{
if ((unErrorS >> nCount * 4) == 0)
{
unErrorS += unErrorF << nCount * 4;
return unErrorS;
}
}
return unErrorS;
}
///////////////////////////////////////////////////////////////////////
//
// description:
// 获得当前执行文件的文件名称和路径
// parameter:
// hModule EXE执行文件句柄
// strPath EXE执行文件路径(未尾不带\)
// strFile EXE执行文件名称
// return: EXE执行文件路径(未尾不带\),如果出错,则返回空字符串
// time 2006-01-22
// history:
//
///////////////////////////////////////////////////////////////////////
CString CErrorLog::GetExePath(HMODULE hModule, CString *pstrPath, CString *pstrFile)
{
//临时变量,保存\字符的位置
int nPos;
//保存返回的EXE文件名称
CString strExe;
//保存返回的路径名称
CString strPath;
//临时缓冲区,传给GetModuleFileName函数
TCHAR pbytBuffer[MAX_PATH];
//获得应用程序所在目录
pbytBuffer[0] = '\0';
if (GetModuleFileName(hModule, pbytBuffer, MAX_PATH))
{
strExe = pbytBuffer;
}
else
{
return "";
}
//保存EXE文件路径
nPos = strExe.ReverseFind('\\');
if (nPos > 0)
{
strPath = strExe.Left(nPos);
}
else
{
return "";
}
//保存结果
if (pstrPath != NULL)
{
*pstrPath = strPath;
}
if (pstrFile != NULL && strExe.GetLength() - nPos - 1 > 0)
{
*pstrFile = strExe.Right(strExe.GetLength() - nPos - 1);
}
return strPath;
}
///////////////////////////////////////////////////////////////////////
//
// description:
// 检查文件或目录是否存在,如果存在返回真,不存在或者出错返回假
// 如果blnCheckDir为真,则检查是否为目录,如果不是或者不存在都返回假,是则返回真
// parameter:
// strFile 需要检查的文件或者目录名
// blnCheckDir 是否是检查目录
// return: true 存在
// false 不存在或者出错
// time: 2006-01-09
// history:
//
///////////////////////////////////////////////////////////////////////
bool CErrorLog::CheckFileIfExist(const CString& strFile, bool blnCheckDir)
{
CFileFind fndFile;
if (strFile.GetLength() == 3)
{
if (blnCheckDir)
{
if (fndFile.FindFile(strFile + "*.*"))
{
fndFile.Close();
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (strFile.GetLength() > 3)
{
CString strObject;
//去除未尾"\"
if (strFile.Right(1) == '\\')
{
strObject = strFile.Left (strFile.GetLength() - 1);
}
else
{
strObject = strFile;
}
if (strObject.GetLength() <= 3)
{
//防止"C:\\"情况
return false;
}
if (fndFile.FindFile(strObject))
{
if (blnCheckDir)
{
fndFile.FindNextFile();
if (!fndFile.IsDirectory())
{
fndFile.Close();
return false;
}
}
fndFile.Close();
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -