Leaked source code of windows server 2003
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.

212 lines
6.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1999
  5. //
  6. // File: mmctrace.h
  7. //
  8. // Contents: Declaration of the debug trace code
  9. //
  10. // History: 15-Jul-99 VivekJ Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #ifndef MMCDEBUG_H
  14. #define MMCDEBUG_H
  15. #pragma once
  16. #include "baseapi.h" // for MMCBASE_API
  17. //--------------------------------------------------------------------------
  18. #ifdef DBG
  19. //--------------------------------------------------------------------------
  20. /*
  21. * Define a macro to break into the debugger.
  22. *
  23. * On Intel, do an inline break. That'll keep us from breaking
  24. * inside NTDLL and switching from source mode to disassembly mode.
  25. */
  26. #ifdef _M_IX86
  27. #define MMCDebugBreak() _asm { int 3 }
  28. #else
  29. #define MMCDebugBreak() DebugBreak()
  30. #endif
  31. // forward class declarations
  32. class MMCBASE_API CTraceTag;
  33. typedef CTraceTag * PTRACETAG;
  34. typedef std::vector<PTRACETAG> CTraceTags;
  35. MMCBASE_API CTraceTags * GetTraceTags(); // singleton.
  36. class CStr;
  37. CStr & GetFilename();
  38. extern LPCTSTR const szTraceIniFile;
  39. enum
  40. {
  41. TRACE_COM2 = 0x0001,
  42. TRACE_OUTPUTDEBUGSTRING = 0x0002,
  43. TRACE_FILE = 0x0004,
  44. TRACE_DEBUG_BREAK = 0x0008,
  45. TRACE_DUMP_STACK = 0x0010,
  46. TRACE_ALL = ( TRACE_COM2 | TRACE_OUTPUTDEBUGSTRING | TRACE_FILE | TRACE_DEBUG_BREAK | TRACE_DUMP_STACK )
  47. };
  48. /*+-------------------------------------------------------------------------*
  49. * class CTraceTag
  50. *
  51. * PURPOSE: Encapsulates a particular trace type.
  52. *
  53. * USAGE: Instantiate it with
  54. *
  55. * #ifdef DBG
  56. * CTraceTag tagTest( TEXT("TestCategory"), TEXT("TestName"))
  57. * #endif
  58. *
  59. * Make sure to use STRING LITERALS for the category and name; the tag
  60. * stores the pointer to the string only.
  61. *
  62. * You can also specify which outputs to enable by default. Or, from the
  63. * traces dialog, each output can be individually enabled/disabled.
  64. *
  65. * Add code to use the trace just like a printf statement as follows:
  66. *
  67. * example: Trace(tagTest, "Error: %d", hr);
  68. *
  69. * The complete Trace statement must be on a single line. If not, use continuation
  70. * characters (\).
  71. *+-------------------------------------------------------------------------*/
  72. class MMCBASE_API CTraceTag
  73. {
  74. public:
  75. CTraceTag(LPCTSTR szCategory, LPCTSTR szName, DWORD dwDefaultFlags = 0);
  76. ~CTraceTag();
  77. const LPCTSTR GetCategory() const {return m_szCategory;}
  78. const LPCTSTR GetName() const {return m_szName;}
  79. void SetTempState() {m_dwFlagsTemp = m_dwFlags;}
  80. void Commit();
  81. void SetFlag(DWORD dwMask) {m_dwFlagsTemp |= dwMask;}
  82. void ClearFlag(DWORD dwMask) {m_dwFlagsTemp &= ~dwMask;}
  83. void RestoreDefaults() {m_dwFlags = m_dwDefaultFlags; m_dwFlagsTemp = m_dwDefaultFlags;}
  84. DWORD GetFlag(DWORD dwMask) const {return m_dwFlagsTemp & dwMask;}
  85. void TraceFn( LPCTSTR szFormat, va_list ) const;
  86. BOOL FIsDefault() const {return (m_dwFlags == m_dwDefaultFlags);}
  87. BOOL FAny() const {return (m_dwFlags != 0);}
  88. BOOL FCom2() const {return (m_dwFlags & TRACE_COM2);}
  89. BOOL FDebug() const {return (m_dwFlags & TRACE_OUTPUTDEBUGSTRING);}
  90. BOOL FFile() const {return (m_dwFlags & TRACE_FILE);}
  91. BOOL FBreak() const {return (m_dwFlags & TRACE_DEBUG_BREAK);}
  92. BOOL FDumpStack() const {return (m_dwFlags & TRACE_DUMP_STACK);}
  93. // temp flag functions
  94. BOOL FAnyTemp() const {return (m_dwFlagsTemp != 0);}
  95. DWORD GetAll() {return m_dwFlags;}
  96. static CStr& GetFilename();
  97. static unsigned int& GetStackLevels();
  98. protected:
  99. // these are designed to be overloaded by a derived class to instrument certain
  100. // pieces of code as appropriate.
  101. virtual void OnEnable() {}
  102. virtual void OnDisable() {}
  103. private:
  104. void OutputString(const CStr &str) const; // sends the specified string to all appropriate outputs.
  105. void DumpStack() const; // sends the stack trace to all appropriate outputs.
  106. private:
  107. LPCTSTR m_szCategory;
  108. LPCTSTR m_szName;
  109. DWORD m_dwDefaultFlags;
  110. DWORD m_dwFlags;
  111. DWORD m_dwFlagsTemp; // thrown away if Cancel is hit in the dialog.
  112. static HANDLE s_hfileCom2;
  113. static HANDLE s_hfile;
  114. };
  115. MMCBASE_API void Trace(const CTraceTag &, LPCTSTR szFormat, ... );
  116. MMCBASE_API void TraceDirtyFlag (LPCTSTR szComponent, bool bDirty ); // trace for the dirty flag for persistent objects.
  117. MMCBASE_API void TraceSnapinPersistenceError(LPCTSTR szError);
  118. MMCBASE_API void TraceBaseLegacy (LPCTSTR szFormat, ... );
  119. MMCBASE_API void TraceConuiLegacy (LPCTSTR szFormat, ... );
  120. MMCBASE_API void TraceNodeMgrLegacy(LPCTSTR szFormat, ... );
  121. MMCBASE_API void DoDebugTraceDialog();
  122. template<class TYPE>
  123. inline void SAFE_RELEASE(TYPE*& pObj)
  124. {
  125. if (pObj != NULL)
  126. {
  127. pObj->Release();
  128. pObj = NULL;
  129. }
  130. else
  131. {
  132. TraceBaseLegacy(_T("Release called on NULL interface ptr\n"));
  133. }
  134. }
  135. #define BEGIN_TRACETAG(_class) \
  136. class _class : public CTraceTag \
  137. { \
  138. public: \
  139. _class(LPCTSTR szCategory, LPCTSTR szName, DWORD dwDefaultFlags = 0) \
  140. : CTraceTag(szCategory, szName, dwDefaultFlags) {}
  141. #define END_TRACETAG(_class, _Category, _Name) \
  142. } _tag##_class(_Category, _Name);
  143. //--------------------------------------------------------------------------
  144. #else // DBG
  145. //--------------------------------------------------------------------------
  146. // these macros evaluate to blanks.
  147. #define CTraceTag()
  148. #define MMCDebugBreak()
  149. // Expand to ";", <tab>, one "/" followed by another "/"
  150. // (which is //).
  151. // NOTE: This means the Trace statements have to be on ONE line.
  152. // If you need multiple line Trace statements, enclose them in
  153. // a #ifdef DBG block.
  154. #define Trace ;/##/
  155. #define TraceDirtyFlag ;/##/
  156. #define TraceCore ;/##/
  157. #define TraceConuiLegacy ;/##/
  158. #define TraceNodeMgrLegacy ;/##/
  159. template<class TYPE>
  160. inline void SAFE_RELEASE(TYPE*& pObj)
  161. {
  162. if (pObj != NULL)
  163. {
  164. pObj->Release();
  165. pObj = NULL;
  166. }
  167. }
  168. //--------------------------------------------------------------------------
  169. #endif // DBG
  170. //--------------------------------------------------------------------------
  171. #endif // MMCDEBUG_H