⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 errormanager.cpp

📁 iocp vc例子,自己是学DELPHI
💻 CPP
字号:
// ErrorManager.cpp: implementation of the CErrorManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PublicFunc.h"
#include "ItemBase.h"
#include "ErrorManager.h"
#include "XmlManager.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////////
// CErrorInfo
//////////////////////////////////////////////////////////////////////////
CErrorInfo::CErrorInfo()
{
	m_strErrorCode = _T("");
	m_nErrorLevel = ERRORLEVEL_NONE;
	m_strErrorText = _T("");
	m_strDomain = _T("");
	m_strClass = _T("");
	m_nErrorLine = 0;
	m_strErrorFile = _T("");
}

CErrorInfo::CErrorInfo(int nErrorCode, CString strDomain, CString strClass, 
					   int nLine, CString strFile)
{
	CErrorInfo ErrInfo;
	CErrorManager::Instance()->GetErrorInfo(nErrorCode, ErrInfo);
	m_strErrorCode = ErrInfo.m_strErrorCode;
	m_nErrorLevel = ErrInfo.m_nErrorLevel;
	m_strErrorText = ErrInfo.m_strErrorText;
	m_strDomain = strDomain;
	if (!strClass.IsEmpty()) m_strClass = strClass;
	m_nErrorLine = nLine;
	m_strErrorFile = strFile;
}

CErrorInfo::CErrorInfo(int nErrorLevel, CString strErrorText, 
					   CString strDomain, CString strClass, int nLine, CString strFile)
{
	m_nErrorLevel = nErrorLevel;
	m_strErrorText = strErrorText;
	m_strDomain = strDomain;
	m_strClass = strClass;
	m_nErrorLine = nLine;
	m_strErrorFile = strFile;
}

CString CErrorInfo::GetErrorText()
{
	CString strRet;
	strRet.Format(_T("[%s] - [%s: %s] - [%s: %s] - [%d: %s]"), m_strErrorCode, 
		CErrorManager::Instance()->ErrorLevelToText(m_nErrorLevel), 
		m_strErrorText, m_strDomain, m_strClass, m_nErrorLine, m_strErrorFile);
	return strRet;
}	

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CErrorManager::CErrorManager()
{
	InitConfig();
}

CErrorManager::~CErrorManager()
{
}

int CErrorManager::InitConfig()
{
	CString strErrorXml=_T("ErrorCodes.xml");
	CString strXmlPath=CPublicFunc::GetFullPathOfFile(strErrorXml);
	CXmlManager XmlMngr;
	int nErr=XmlMngr.Load(strXmlPath);
	if (NOERROROCCUR(nErr))
		nErr=XmlMngr.GetItem(_T("ErrorCodes"), m_itemErrors);
	return nErr;
}

CString CErrorManager::ErrorLevelToText(int nErrorLevel)
{
	CString strRet;
	switch(nErrorLevel) 
	{
	case ERRORLEVEL_INFO:
		strRet = _T("信息");
		break;
	case ERRORLEVEL_WARNING:
		strRet = _T("警告");
		break;
	case ERRORLEVEL_ERROR:
		strRet = _T("错误");
		break;
	default:
		strRet = _T("未知类型");
		break;
	}
	return strRet;
}

CErrorManager* CErrorManager::Instance()
{
	static CErrorManager ErrorMngr;
	return &ErrorMngr;
}

int CErrorManager::GetErrorInfo(int nErrorCode, CErrorInfo& ErrInfo)
{
	int nErr=ERROR_NONE;
	CString strErrorCode;
	strErrorCode.Format("E%d", nErrorCode);
	CItemBase* pItemBase=NULL;
	BOOL bFindAttr=m_itemErrors.FindAttr(strErrorCode, &pItemBase);
	if (bFindAttr && pItemBase!=NULL)
	{
		ErrInfo.m_strErrorCode = strErrorCode;
		pItemBase->GetAttr(strErrorCode, ErrInfo.m_strErrorText);
		pItemBase->GetAttr(XML_ERRORMANAGER_ERRORLEVEL, ErrInfo.m_nErrorLevel);		
		pItemBase->GetAttr(XML_ERRORMANAGER_ERRORCLASS, ErrInfo.m_strClass);
	}
	else
	{
		nErr = ERROR_ERRORMANAGER_CODE_NOT_FOUND;
	}
	return nErr;
}

CString CErrorManager::ErrorCodeToText(int nErrorCode)
{
	CErrorInfo ErrInfo;
	GetErrorInfo(nErrorCode, ErrInfo);
	CString strRet = ErrInfo.m_strErrorText;
	return strRet;
}

CString CErrorManager::WSAErrorCodeToText(DWORD dwWSAErrorCode)
{
	CString strRet;
	// Put your own common error text here (give more explaination etc..) 
	switch(dwWSAErrorCode)
	{
	case WSAEFAULT:
		strRet = _T("WSAEFAULT	The buf parameter is not completely contained in a valid part of the user address space.");
		break; 
	case WSAENOTCONN:
		strRet = _T("WSAENOTCONN	The socket is not connected."); 
		break;
	case WSAEINTR:
		strRet = _T("WSAEINTR	The (blocking) call was canceled through WSACancelBlockingCall.	"); 
		break;
	case WSAENOTSOCK:
		strRet = _T("WSAENOTSOCK	The descriptor s is not a socket."); 
		break; 
	case WSANOTINITIALISED:
		strRet = _T("WSANOTINITIALISED: A successful WSAStartup call must occur before using this function.");
		break; 
	case WSAENETDOWN:
		strRet = _T("WSAENETDOWN	The network subsystem has failed."); 
		break;
	case WSAEINPROGRESS:
		strRet = _T("WSAEINPROGRESS	A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function."); 
		break;
	case WSAENETRESET:
		strRet = _T(" WSAENETRESET	The connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress."); 
		break; 
	case WSAEOPNOTSUPP:
		strRet = _T("WSAEOPNOTSUPP	MSG_OOB was specified, but the socket is not stream-style such as type SOCK_STREAM, OOB data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations.	"); 
		break; 
	case WSAESHUTDOWN:
		strRet = _T("WSAESHUTDOWN	The socket has been shut down; it is not possible to receive on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH."); 
		break;
	case WSAEWOULDBLOCK:
		strRet = _T("WSAEWOULDBLOCK	The socket is marked as nonblocking and the receive operation would block.	"); 
		break; 
	case WSAEMSGSIZE:
		strRet = _T("WSAENOTSOCK		The message was too large to fit into the specified buffer and was truncated."); 
		break;
	case WSAEINVAL:
		strRet = _T("WSAEINVAL	The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled or (for byte stream sockets only) len was zero or negative.	"); 
		break;
	case WSAECONNABORTED:
		strRet = _T("WSAECONNABORTED	The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable."); 
		break; 
	case WSAETIMEDOUT:
		strRet = _T("WSAETIMEDOUT	The connection has been dropped because of a network failure or because the peer system failed to respond.	"); 
		break; 
	case WSAECONNRESET:
		strRet = _T("WSAECONNRESET Connection dropped..");
		break;		
	default:
		strRet = _T("");  
		break;
	}
	
	// Use system format..  
	if(strRet.IsEmpty())
	{
		LPVOID lpMsgBuf=NULL;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
			NULL, dwWSAErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPTSTR) &lpMsgBuf,	0, NULL );
		strRet.Format(_T("%s"), lpMsgBuf);
		LocalFree(lpMsgBuf);
	}
	return strRet;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -