Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.9 KiB

  1. #ifndef __WIADBG_H_INCLUDED
  2. #define __WIADBG_H_INCLUDED
  3. #if defined(DBG) || defined(_DEBUG) || defined(DEBUG)
  4. #define WIA_DEBUG
  5. #endif
  6. #if defined(WIA_DEBUG)
  7. // This will eliminate the warning "conditional expression is constant"
  8. // that we get when compiling the do { ... } while (false) stuff in the
  9. // debug macros when /W4 is set
  10. #pragma warning(disable:4127)
  11. #define WIA_DEBUG_CREATE( hInstance, pszModuleName, bDisplayUi, bLogFile)\
  12. do\
  13. {\
  14. _global_pWiaDebugger = CWiaDebugger::Create( hInstance, pszModuleName, bDisplayUi, bLogFile );\
  15. } while (false)
  16. #define WIA_DEBUG_EXISTS() (_global_pWiaDebugger != NULL)
  17. #define WIA_DEBUG_DESTROY()\
  18. do\
  19. {\
  20. if (NULL != _global_pWiaDebugger) {\
  21. _global_pWiaDebugger->Destroy();\
  22. _global_pWiaDebugger = NULL;\
  23. }\
  24. } while (false)
  25. #define WIA_TRACE(args)\
  26. do\
  27. {\
  28. if (NULL != _global_pWiaDebugger)\
  29. _global_pWiaDebugger->WiaTrace args;\
  30. } while (false)
  31. #define WIA_ERROR(args)\
  32. do\
  33. {\
  34. if (NULL != _global_pWiaDebugger)\
  35. _global_pWiaDebugger->WiaError args;\
  36. } while (false)
  37. #define WIA_PRINT_COLOR(args)\
  38. do\
  39. {\
  40. if (NULL != _global_pWiaDebugger)\
  41. _global_pWiaDebugger->PrintColor args;\
  42. } while (false)
  43. #define WIA_SETFLAGS(flags)\
  44. do\
  45. {\
  46. if (NULL != _global_pWiaDebugger)\
  47. _global_pWiaDebugger->SetDebugFlags(flags);\
  48. } while (false)
  49. #define WIA_GETFLAGS(flags)\
  50. do\
  51. {\
  52. if (NULL != _global_pWiaDebugger)\
  53. flags = _global_pWiaDebugger->GetDebugFlags();\
  54. } while (false)
  55. #define WIA_SETFILEHANDLE(hndl)\
  56. do\
  57. {\
  58. if (NULL != _global_pWiaDebugger)\
  59. _global_pWiaDebugger->SetLogFileHandle;\
  60. } while (false)
  61. #ifdef WINNT
  62. #define WIA_ASSERT(x)\
  63. do\
  64. {\
  65. if (!(x))\
  66. {\
  67. WIA_ERROR((TEXT("ASSERTION FAILED: %hs(%d): %hs"),__FILE__,__LINE__,#x));\
  68. DebugBreak();\
  69. }\
  70. }\
  71. while (false)
  72. #else
  73. #define WIA_ASSERT(x)\
  74. do\
  75. {\
  76. if (!(x))\
  77. {\
  78. WIA_ERROR((TEXT("ASSERTION FAILED: %hs(%d): %hs"),__FILE__,__LINE__,#x));\
  79. _asm { int 3 };\
  80. }\
  81. }\
  82. while (false)
  83. #endif
  84. #define WIA_PUSHFUNCTION(n)\
  85. CDebugFunctionPushPop _debugFunctionPushPop( &_global_pWiaDebugger, n )
  86. #define WIA_DECLARE_DEBUGGER()
  87. #define WIA_CHECK_HR(hr,fnc)\
  88. if (FAILED(hr))\
  89. {\
  90. WIA_ERROR((TEXT("%s failed, hr=0x%08X" ), fnc, hr));\
  91. }
  92. #define WIA_RETURN_HR(hr)\
  93. if (FAILED(hr))\
  94. {\
  95. WIA_ERROR((TEXT("Returning WiaError (hr=0x%08X)"),hr));\
  96. }\
  97. return hr;
  98. #else
  99. #define WIA_DEBUG_CREATE(hInstance, pszModuleName, bDisplayUi, bLogFile)
  100. #define WIA_DEBUG_EXISTS() (0)
  101. #define WIA_DEBUG_DESTROY()
  102. #define WIA_TRACE(args)
  103. #define WIA_ERROR(args)
  104. #define WIA_PRINT_COLOR(args)
  105. #define WIA_SETFLAGS(flags)
  106. #define WIA_GETFLAGS(flags)
  107. #define WIA_SETFILEHANDLE(hndl)
  108. #define WIA_ASSERT(x)
  109. #define WIA_PUSHFUNCTION(n)
  110. #define WIA_DECLARE_DEBUGGER()
  111. #define WIA_CHECK_HR(hr,fnc)
  112. #define WIA_RETURN_HR(hr) return hr;
  113. #endif
  114. class CWiaDebugger
  115. {
  116. private:
  117. HWND m_hWnd;
  118. HANDLE m_hLogFile;
  119. int m_nStackLevel;
  120. TCHAR m_szModuleName[MAX_PATH];
  121. BOOL m_bDisplayUi;
  122. BOOL m_bLogFile;
  123. HANDLE m_hThread;
  124. HANDLE m_hStartedEvent;
  125. DWORD m_dwThreadId;
  126. int m_nFlags;
  127. HINSTANCE m_hInstance;
  128. enum { m_nBufferMax = 2048 };
  129. private:
  130. CWiaDebugger( HINSTANCE hInstance, LPTSTR pszModuleName, BOOL bDisplayUi, BOOL bLogFile, HANDLE hStartedEvent );
  131. DWORD DebugLoop(void);
  132. static DWORD ThreadProc( LPVOID pParam );
  133. public:
  134. ~CWiaDebugger(void);
  135. static CWiaDebugger * __stdcall Create( HINSTANCE hInstance, LPTSTR pszModuleName, BOOL bDisplayUi=TRUE, BOOL bLogFile=TRUE );
  136. void Destroy(void)
  137. {
  138. PostMessage( WM_CLOSE );
  139. }
  140. void PostMessage( UINT uMsg, WPARAM wParam=0, LPARAM lParam=0 )
  141. {
  142. if (m_hWnd)
  143. ::PostMessage( m_hWnd, uMsg, wParam, lParam );
  144. else PostThreadMessage( m_dwThreadId, uMsg, wParam, lParam );
  145. }
  146. const HANDLE ThreadHandle(void) const
  147. {
  148. return m_hThread;
  149. }
  150. HANDLE SetLogFileHandle(HANDLE hFile);
  151. HANDLE GetLogFileHandle(void);
  152. // Various forms of the WiaTrace commands
  153. void WiaTrace( LPCWSTR lpszFormat, ... );
  154. void WiaTrace( LPCSTR lpszFormat, ... );
  155. void WiaTrace( HRESULT hr );
  156. // Various forms of the WiaError commands
  157. void WiaError( LPCWSTR lpszFormat, ... );
  158. void WiaError( LPCSTR lpszFormat, ... );
  159. void WiaError( HRESULT hr );
  160. // Print in color
  161. void PrintColor( COLORREF crColor, LPCWSTR lpszMsg );
  162. void PrintColor( COLORREF crColor, LPCSTR lpszMsg );
  163. // Set the default debug level
  164. int SetDebugFlags( int nDebugLevel );
  165. int GetDebugFlags(void);
  166. // Call stack indenting
  167. int PushLevel( LPCTSTR lpszFunctionName );
  168. int PopLevel( LPCTSTR lpszFunctionName );
  169. int GetStackLevel(void);
  170. enum
  171. {
  172. DebugNone = 0x00000000,
  173. DebugToWindow = 0x00000001,
  174. DebugToFile = 0x00000002,
  175. DebugToDebugger = 0x00000004,
  176. DebugPrintThreadId = 0x00010000,
  177. DebugPrintModuleName = 0x00020000,
  178. DebugMaximum = 0xFFFFFFFF
  179. };
  180. protected:
  181. void RouteString( LPWSTR lpszMsg, COLORREF nColor );
  182. void RouteString( LPSTR lpszMsg, COLORREF nColor );
  183. void WriteMessageToFile( LPTSTR lpszMsg );
  184. LPTSTR RemoveTrailingCrLf( LPTSTR lpszStr );
  185. LPSTR UnicodeToAnsi( LPSTR lpszAnsi, LPTSTR lpszUnicode );
  186. LPTSTR AnsiToTChar( LPCSTR pszAnsi, LPTSTR pszTChar );
  187. LPTSTR WideToTChar( LPWSTR pszWide, LPTSTR pszTChar );
  188. int AddString( const LPCTSTR sz, COLORREF cr );
  189. void PrependString( LPTSTR lpszTgt, LPCTSTR lpszStr );
  190. void PrependThreadId( LPTSTR lpszMsg );
  191. void PrependModuleName( LPTSTR lpszMsg );
  192. void InsertStackLevelIndent( LPTSTR lpszMsg, int nStackLevel );
  193. };
  194. class CDebugFunctionPushPop
  195. {
  196. typedef (*CPushFunction)( LPCTSTR );
  197. typedef (*CPopFunction)( LPCTSTR );
  198. CWiaDebugger **m_ppDebugger;
  199. CPushFunction m_pfnPush;
  200. CPushFunction m_pfnPop;
  201. LPCTSTR m_lpszFunctionName;
  202. public:
  203. CDebugFunctionPushPop( CWiaDebugger **ppDebugger, LPCTSTR lpszFunctionName=NULL )
  204. : m_ppDebugger(ppDebugger),
  205. m_lpszFunctionName(lpszFunctionName)
  206. {
  207. if (m_ppDebugger && *m_ppDebugger)
  208. (*m_ppDebugger)->PushLevel(m_lpszFunctionName);
  209. }
  210. ~CDebugFunctionPushPop(void)
  211. {
  212. if (m_ppDebugger && *m_ppDebugger)
  213. (*m_ppDebugger)->PopLevel(m_lpszFunctionName);
  214. }
  215. };
  216. #ifdef WIA_DEBUG
  217. extern CWiaDebugger *_global_pWiaDebugger;
  218. #endif
  219. #endif