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.

270 lines
6.8 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1999
  5. //
  6. // File: StdDbg.h
  7. //
  8. // Contents: Common debug definitions.
  9. //
  10. // History: 5/20/1996 RaviR Created
  11. //
  12. //____________________________________________________________________________
  13. #include "admindbg.h"
  14. #include <tchar.h> // for _T
  15. #include <string>
  16. //
  17. // EXAMPLE: A debug file for component SAMPLE, with the debugging tag
  18. // name "Samp" is defined as shown below:
  19. //
  20. //
  21. // //
  22. // // File: SampDbg.h
  23. // //
  24. //
  25. // #ifndef _SAMPDBG_H_
  26. // #define _SAMPDBG_H_
  27. //
  28. // #include "stddbg.h"
  29. //
  30. // #ifdef DBG
  31. // DECLARE_DEBUG(Samp)
  32. // #define DBG_COMP SampInfoLevel
  33. // #endif // DBG
  34. //
  35. // #endif // _SAMPDBG_H_
  36. //
  37. //
  38. // A corresponding DECLARE_INFOLEVEL(Samp) should be implemented in a .cpp
  39. // file. This creates a global instance of an CDbg -> SampInfoLevel.
  40. // SampInfoLevel can be initialized by setting the "Samp" value under reg key
  41. //
  42. // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AdminDebug
  43. //
  44. // By defalut it is set to (DEB_ERROR | DEB_WARN)
  45. //
  46. //
  47. // ------------------------------------------------------------------------
  48. // Method: CDbg::DebugOut(debug_level, lpstrfmt, ...);
  49. //
  50. // Where debug_level is a combination of one or more of the DEB_XXX
  51. // values defined in admindbg.h. If ((SampInfoLevel & debug_level) != 0)
  52. // The string lpstrfmt will be printed out to the debugger.
  53. //
  54. // ------------------------------------------------------------------------
  55. // Method: DebugMsg(file, line, message)
  56. //
  57. // Force output the <file, line, message>.
  58. //
  59. // ------------------------------------------------------------------------
  60. //
  61. #ifndef __STDDBG_HXX__
  62. #define __STDDBG_HXX__
  63. //
  64. // C++ files redefine THIS_FILE by adding the following two lines:
  65. //
  66. // #undef THIS_FILE
  67. // static char THIS_FILE[] = __FILE__;
  68. //
  69. #define THIS_FILE __FILE__
  70. #define DEB_RESOURCE DEB_USER10 // Constructor/Destructor
  71. #define DEB_METHOD DEB_USER11
  72. #define DEB_FUNCTION DEB_USER12
  73. #undef ASSERT
  74. #undef VERIFY
  75. #ifdef DBG
  76. #define Dbg DBG_COMP.DebugOut
  77. // Heap checking
  78. extern DWORD dwHeapChecking;
  79. #define DECLARE_HEAPCHECKING DWORD dwHeapChecking = 0
  80. #define DEBUGCHECK \
  81. if ( (dwHeapChecking & 0x1) == 0x1 ) \
  82. { \
  83. HeapValidate(GetProcessHeap(),0,NULL); \
  84. } else 1
  85. // Debug messages
  86. #define TRACE_CONSTRUCTOR(cls) \
  87. Dbg(DEB_RESOURCE, _T(#cls) _T("::") _T(#cls) _T("<%x>\n"), this);
  88. #define TRACE_DESTRUCTOR(cls) \
  89. Dbg(DEB_RESOURCE, _T(#cls) _T("::~") _T(#cls) _T("<%x>\n"), this);
  90. #define TRACE_METHOD(Class, Method) \
  91. DEBUGCHECK; \
  92. Dbg(DEB_METHOD, _T(#Class) _T("::") _T(#Method) _T("(%x)\n"), this);
  93. #define TRACE_FUNCTION(Function) \
  94. DEBUGCHECK; \
  95. Dbg(DEB_FUNCTION, _T(#Function) _T("\n"));
  96. #define CHECK_HRESULT(hr) \
  97. if ( FAILED(hr) ) \
  98. { \
  99. DBG_COMP.DebugErrorX(THIS_FILE, __LINE__, hr); \
  100. } else 1
  101. #define CHECK_LASTERROR(lr) \
  102. if ( lr != ERROR_SUCCESS ) \
  103. { \
  104. DBG_COMP.DebugErrorL(THIS_FILE, __LINE__, lr); \
  105. } else 1
  106. #define DBG_OUT_LASTERROR \
  107. DBG_COMP.DebugErrorL(THIS_FILE, __LINE__, GetLastError());
  108. #define ASSERTMSG(x) \
  109. (void)((x) || (DBG_COMP.DebugMsg(THIS_FILE, __LINE__, _T(#x)),0))
  110. #define VERIFYMSG(e) ASSERTMSG(e)
  111. #define ASSERT(x) Win4Assert(x)
  112. #define VERIFY(x) Win4Assert(x)
  113. /*
  114. * COMPILETIME_ASSERT(f)
  115. *
  116. * Generates a build break at compile time if the constant expression
  117. * is not true. Unlike the "#if" compile-time directive, the expression
  118. * in COMPILETIME_ASSERT() is allowed to use "sizeof".
  119. *
  120. * Compiler magic! If the expression "f" is FALSE, then you get
  121. *
  122. * error C2196: case value '0' already used
  123. */
  124. #define COMPILETIME_ASSERT(f) switch (0) case 0: case f: break;
  125. #else
  126. inline void __DummyDbg(ULONG, LPCWSTR, ...) { }
  127. inline void __DummyDbg(ULONG, LPCSTR, ...) { }
  128. #define Dbg 1 ? (void)0 : ::__DummyDbg
  129. inline void __DummyTrace(LPCWSTR, ...) { }
  130. inline void __DummyTrace(LPCSTR, ...) { }
  131. #define TRACE_SCOPE(x)
  132. #define DECLARE_HEAPCHECKING
  133. #define DEBUGCHECK
  134. #define TRACE_CONSTRUCTOR(cls)
  135. #define TRACE_DESTRUCTOR(cls)
  136. #define TRACE_METHOD(ClassName,MethodName)
  137. #define TRACE_FUNCTION(FunctionName)
  138. #define CHECK_HRESULT(hr)
  139. #define CHECK_LASTERROR(lr)
  140. #define DBG_OUT_LASTERROR
  141. #define ASSERTMSG(e)
  142. #define VERIFYMSG(e) (e)
  143. #define ASSERT(e)
  144. #define VERIFY(e) (e)
  145. #define COMPILETIME_ASSERT(f)
  146. #endif // DBG
  147. #ifdef DBG
  148. /*
  149. * this is a roundabout way of getting this accomplished (real impl is
  150. * in stddbg.cpp), but it gets around a compiler bug that won't allow
  151. * us to ignore C4786.
  152. */
  153. struct CDebugLeakDetectorBase
  154. {
  155. virtual ~CDebugLeakDetectorBase() = 0 {};
  156. virtual void DumpLeaks() = 0;
  157. virtual int AddRef(const std::string& strClass) = 0;
  158. virtual int Release(const std::string& strClass) = 0;
  159. };
  160. extern CDebugLeakDetectorBase& GetLeakDetector();
  161. #define DEBUG_DECLARE_INSTANCE_COUNTER(cls)
  162. #define DEBUG_INCREMENT_INSTANCE_COUNTER(cls) GetLeakDetector().AddRef(#cls)
  163. #define DEBUG_DECREMENT_INSTANCE_COUNTER(cls) GetLeakDetector().Release(#cls)
  164. #define DEBUG_VERIFY_INSTANCE_COUNT(cls)
  165. #else
  166. #define DEBUG_DECLARE_INSTANCE_COUNTER(cls)
  167. #define DEBUG_INCREMENT_INSTANCE_COUNTER(cls)
  168. #define DEBUG_DECREMENT_INSTANCE_COUNTER(cls)
  169. #define DEBUG_VERIFY_INSTANCE_COUNT(cls)
  170. #endif
  171. #ifdef UNICODE
  172. #define DBGSTRING %ls
  173. #else
  174. #define DBGSTRING %s
  175. #endif
  176. #define SAFEDBGBSTR(x) ((x==NULL)?L"<NULL>":x)
  177. #define SAFEDBGTCHAR(x) ((x==NULL)?_T("<NULL>"):x)
  178. #define ASSERT_OBJECTPTR(x) ASSERT( NULL == (x) || !::IsBadWritePtr(x,sizeof(x)) );
  179. #define ASSERT_STRINGPTR(x) ASSERT( NULL == (x) || AfxIsValidStringPtr(x) );
  180. #define FREE_OBJECTPTR(x) { ASSERT_OBJECTPTR(x); delete x; x = NULL; }
  181. #ifdef DBG
  182. class CTraceTag;
  183. class tstring;
  184. struct DBG_PersistTraceData
  185. {
  186. DBG_PersistTraceData();
  187. void TraceErr(LPCTSTR strInterface, LPCTSTR msg);
  188. typedef void (*PTraceErrorFn)(LPCTSTR szError);
  189. void SetTraceInfo(PTraceErrorFn pFN, bool bComponent, const tstring& owner);
  190. PTraceErrorFn pTraceFN;
  191. bool bIComponent;
  192. bool bIComponentData;
  193. // cannot use tstring - it cannot be defined here
  194. // since this file is included at the top of tstring.h
  195. #ifdef UNICODE
  196. std::wstring strSnapin;
  197. #else
  198. std::string strSnapin;
  199. #endif
  200. };
  201. #endif // DBG
  202. #endif // __STDDBG_HXX__