Why choose or not use macros when determining errors / HRESULT processing / logging?
I went up to the error handling class called through the interface, so I could use the general Boost pointer to call the class when and where I need it. (Honestly, I don't know if this was the best approach, but I basically wanted to see if I could do it and how it would look.) i.e:
typedef std::shared_ptr<iErrorHandling> Error_Handler;
Error_Handler Err_Handler(new ErrHandling);
if (error)
{
Err_Handler->vDX_ERR(ERR_D3D_INIT_SWAP);
}
I started using the class with DirectX, and since DirectX requires a lot of HRESULT processing, I pointed out the use of a macro to avoid all if / else statements. I came across this:
#define lengthof(rg) (sizeof(rg)/sizeof(*rg))
inline const char* StringFromError(char* szErr, long nSize, long nErr)
{
_ASSERTE(szErr);
*szErr = 0;
DWORD cb = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, nErr, 0, szErr, nSize, 0);
char szUnk[] = "<unknown>";
if( !cb && nSize >= lengthof(szUnk) ) lstrcpyA(szErr, szUnk);
return szErr;
}
inline HRESULT TraceHR(const char* pszFile, long nLine, HRESULT hr)
{
if( FAILED(hr) )
{
char szErr[128];
char sz[_MAX_PATH + lengthof(szErr) + 64];
wsprintfA(sz, "%s(%d) : error 0x%x: %s\n", pszFile, nLine, hr,
StringFromError(szErr, lengthof(szErr), hr));
OutputDebugStringA(sz);
}
return hr;
}
#ifdef _DEBUG
#define TRACEHR(_hr) TraceHR(__FILE__, __LINE__, _hr)
#else
#define TRACEHR(_hr) _hr
#endif
#define HR(ex) { HRESULT _hr = ex; if(FAILED(_hr)) return TRACEHR(_hr), _hr; }
(From: https://www.sellsbrothers.com/writing/a_young_person.htm )
, , "", .
?
, ?
; __FILE__ __FUNC__ __LINE__ ?
:
static LPTSTR ERR_D3D_INIT_HW = __T("cD3D::Initialize: Failed to establish hardware.");
static LPTSTR ERR_D3D_INIT_SWAP = __T("cD3D::Initialize: Failed to create the swap device.");
, , - ?