Debug模式應用程式輸出Debug除錯資訊

Eleven發表於2012-01-19
// Debug模式,主要輸出一些除錯的資訊。
#ifdef UNICODE
	#define _FILE_			_STR2WSTR(__FILE__)
	#define _FUNCTION_			_STR2WSTR(__FUNCTION__)
#else
	#define _FILE_			__FILE__
	#define _FUNCTION_			__FUNCTION__
#endif

#define MAX_BUF_SIZE   (1024)
#define MAX_BIN_COUNT   (16)
#define MAX_BIN_SIZE   (64)

#ifdef _DEBUG
	#define new DEBUG_NEW

	#define DBGPRINT(lpszFmt, ...) \
			TCHAR szText[1024] = {0}; \
			StringCchPrintf(szText, _countof(szText), lpszFmt, __VA_ARGS__);	\
			OutputDebugString(szText);

	#define DBGFAILED(dwError)	\
			LPTSTR lpszBuf = NULL;	\
			TCHAR szError[1024] = {0};	\
			FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, LANG_NEUTRAL, (LPTSTR)&lpszBuf, 0, NULL);	\
			StringCchPrintf(szError, _countof(szError), _T("\nError: File: %s \n\tLine: %d Function: %s()\n\tReason: %s\n"), _FILE_, __LINE__, _FUNCTION_, lpszBuf);	\
			OutputDebugString(szError);	\
			LocalFree(lpszBuf);

	
	#define DUMPBIN(pData, dwSize) \
     			TCHAR szData[MAX_BIN_SIZE] = {0}; \
     			TRACELOGINFO(_T("\n-----------00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F")); \
     			for(DWORD i = 0; i < dwSize; i++) \
     			{ \
      				if(0 == (i % MAX_BIN_COUNT)) \
      				{ \
       					TRACELOGINFO(szData); \
       					StringCchPrintf(szData, _COUNTOF_(szData), _T("\n%08Xh: %02X"), i, pData[i]); \
       				} \
       				else \
       				{ \
       					StringCchPrintf(szData + _tcslen(szData), _COUNTOF_(szData) - _tcslen(szData), _T(" %02X"), pData[i]); \
       				} \
     			} \
     			OutputDebugString(szData); \
     			OutputDebugString(_T("\n----------------------------------------------------------\n"));

#else
	#define DBGPRINT(lpszFmt, ...)
	#define DBGFAILED()
	#define DUMPBIN(pData, dwSize)	
#endif

 

相關文章