写代码就要调试:)
有时候你在使用你的代码的Releasebanben时,出错。但是,你将代码转换到Debug环境下,调试代码时却发现不了哪那里出错了。或者,你的工程很大,代码很多,你要发现出错的地方可能需要在Debug下调试很长时间。
下面这段实用的代码段你可以加入你的代码中,很方便且很快速地定位出错的地方:
#ifdef _DEBUG
#define TRACE_ENTRY DebugMessage( L"%s (ENTRY)/n", _T(__FUNCTION__) );
#define TRACE_EXIT DebugMessage( L"%s (EXIT)/n", _T(__FUNCTION__) );
#define TRACE_RETURN(hr) {DebugMessage( L"%s (EXIT) [0x%x]/n", _T(__FUNCTION__), hr ); return hr;}
#define TRACE_EXCEPTION DebugMessage( L"%s (EXCEPTION) [%s,%d]/n", _T(__FUNCTION__), _T(__FILE__), __LINE__ );
#define TRACE(l,m) DebugMessage( L"%s (%s) %s/n", _T(__FUNCTION__), l, m );
#define TRACE_DEBUG(m) TRACE( L"DEBUG", m );
#define TRACE_INFO(m) TRACE( L"INFO", m );
#define TRACE_NOTICE(m) TRACE( L"NOTICE", m );
#define TRACE_WARNING(m) TRACE( L"WARNING", m );
#define TRACE_ERROR(m) TRACE( L"ERROR", m );
#else
#define TRACE_ENTRY __noop;
#define TRACE_EXIT __noop;
#define TRACE_RETURN(hr) return hr;
#define TRACE_EXCEPTION DebugMessage( L"%s (EXCEPTION) [%s,%d]/n", _T(__FUNCTION__), _T(__FILE__), __LINE__ );
#define TRACE(l,m) __noop;
#define TRACE_DEBUG(m) __noop;
#define TRACE_INFO(m) __noop;
#define TRACE_NOTICE(m) __noop;
#define TRACE_WARNING(m) __noop;
#define TRACE_ERROR(m) __noop;
#endif
BOOL DebugMessage(LPCWSTR lpszMessage, ...)
{
BOOL bResult;
va_list VAList;
// Pass the variable parameters to DebugMessageV to be processed.
va_start(VAList, lpszMessage);
bResult = DebugMessageV(MAX_PATH, lpszMessage, VAList);
va_end(VAList);
return bResult;
}
static BOOL DebugMessageV(DWORD dwSize, LPCWSTR lpszMessage, va_list arglist)
{
LPWSTR lpszMsgBuf;
HRESULT hResult;
// Parameter checking.
if( (NULL == lpszMessage)
||
(0 == dwSize)
)
{
return FALSE;
}
// Allocate memory for message buffer.
lpszMsgBuf = new WCHAR[dwSize + 1];
if(NULL == lpszMsgBuf)
return FALSE;
// Pass the variable parameters to wvsprintf to be formated.
hResult = StringCbVPrintfW(lpszMsgBuf, (dwSize + 1) * sizeof(WCHAR), lpszMessage, arglist);
// Dump string to debug output.
OutputDebugStringW(lpszMsgBuf);
// Clean up.
delete[] lpszMsgBuf;
return SUCCEEDED(hResult);
}
然后,就可以按照下面的方式来使用他们:
void
DWFPrinterDDI::SetColorFromBrush( DWFPrintDocument::tW2DPage* pW2DPage,
BRUSHOBJ* pBrush,
DWFPrintJob::teColorOption eColorOption )
{
TRACE_ENTRY;
//
// 这里写你自己的代码
//
TRACE_ERROR( L"Internal PrintDocument missing!" );
TRACE_ERROR( L"Internal page missing!" );
TRACE_ERROR(L"Internal Class Factory object missing!");
TRACE_WARNING( L"Cannot handle ROP - recording region to copy from mirror" );
TRACE_RETURN( rc );
TRACE_DEBUG( L"Testing for overlapped bitmaps..." );
TRACE_NOTICE( L"Stroke path contains Bezier curve path" );
TRACE_NOTICE( L"Stroke path contains ellipse" );
TRACE_DEBUG( L"Clip is TRIVIAL, enumerate" );
TRACE_EXIT;
}