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.

187 lines
4.1 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef ATLDEBUG_TRACE_MANAGER
  11. #define ATLDEBUG_TRACE_MANAGER
  12. #include "Allocate.h"
  13. // Names
  14. class CAtlTraceModuleInfo
  15. {
  16. public:
  17. explicit CAtlTraceModuleInfo();
  18. void Reset(HINSTANCE hInst);
  19. HINSTANCE GetInstance() const {return m_hInst;}
  20. const WCHAR *Path() const {return m_szPath;}
  21. const WCHAR *Name() const {return m_szName;}
  22. int m_iFirstCategory;
  23. LONG m_nCategories;
  24. private:
  25. WCHAR m_szPath[MAX_PATH], m_szName[_MAX_FNAME];
  26. HINSTANCE m_hInst;
  27. };
  28. class CAtlTraceSettings
  29. {
  30. public:
  31. CAtlTraceSettings() :
  32. m_nLevel(0),
  33. m_eStatus(Inherit),
  34. m_nRefCount(0),
  35. m_nCookie(0)
  36. {
  37. }
  38. UINT m_nLevel;
  39. enum Status
  40. {
  41. Inherit = 0,
  42. Enabled,
  43. Disabled
  44. };
  45. Status m_eStatus;
  46. // Only valid if (m_nRefCount > 0) && (m_nCookie != 0)
  47. LONG m_nRefCount;
  48. LONG m_nCookie;
  49. public:
  50. // Tries to mark the object as allocated. If the object is not available for allocation, returns false.
  51. // Call this, then initialize the data, then call MarkValid. A successful TryAllocate gets a reference
  52. // to the object
  53. bool TryAllocate()
  54. {
  55. if( m_nCookie != 0 )
  56. {
  57. return( false );
  58. }
  59. LONG nNewRefCount = ::InterlockedIncrement( &m_nRefCount );
  60. if( nNewRefCount == 1 )
  61. {
  62. // We are the first ones here
  63. return( true );
  64. }
  65. return( false );
  66. }
  67. // Marks the object as valid.
  68. void MarkValid( LONG nCookie )
  69. {
  70. ATLASSERT( nCookie != 0 );
  71. m_nCookie = nCookie;
  72. }
  73. // Tries to get a reference to the object. If the object is invalid, returns false. Must call Release to
  74. // release the reference after a successful TryAddRef
  75. bool TryAddRef()
  76. {
  77. LONG nNewRefCount = ::InterlockedIncrement( &m_nRefCount );
  78. if( (nNewRefCount > 1) && (m_nCookie != 0) )
  79. {
  80. // The object is valid, and we now own a reference to it
  81. return( true );
  82. }
  83. else
  84. {
  85. Release();
  86. return( false );
  87. }
  88. }
  89. // Releases a reference to the object. If the objects refcount hits zero, the object is invalidated
  90. void Release()
  91. {
  92. LONG nNewRefCount = ::InterlockedDecrement( &m_nRefCount );
  93. if( nNewRefCount == 0 )
  94. {
  95. // We just released the last reference, so mark as invalid
  96. m_nCookie = 0;
  97. }
  98. }
  99. };
  100. // Categories
  101. class CAtlTraceCategory : public CAtlTraceSettings
  102. {
  103. public:
  104. CAtlTraceCategory();
  105. const WCHAR *Name() const {return m_szName;}
  106. void Reset(const WCHAR *pszName, LONG nModuleCookie);
  107. LONG m_nModuleCookie;
  108. int m_iNextCategory;
  109. private:
  110. WCHAR m_szName[ATL_TRACE_MAX_NAME_SIZE];
  111. };
  112. // Modules (DLLs)
  113. class CAtlTraceModule : public CAtlTraceModuleInfo, public CAtlTraceSettings
  114. {
  115. public:
  116. typedef int (__cdecl *fnCrtDbgReport_t)(int,const CHAR *,int,const CHAR *,const CHAR *,...);
  117. explicit CAtlTraceModule();
  118. void CrtDbgReport(fnCrtDbgReport_t pfnCrtDbgReport);
  119. fnCrtDbgReport_t CrtDbgReport() const {return m_pfnCrtDbgReport;}
  120. private:
  121. fnCrtDbgReport_t m_pfnCrtDbgReport;
  122. };
  123. // Process Info
  124. class CAtlTraceProcess : public CAtlTraceModuleInfo
  125. {
  126. public:
  127. explicit CAtlTraceProcess(DWORD_PTR dwMaxSize);
  128. void Save(FILE *file, UINT nTabs) const;
  129. bool Load(FILE *file);
  130. UINT IncRef() {return ++m_nRef;}
  131. UINT DecRef() {return --m_nRef;}
  132. DWORD Id() const {return m_dwId;}
  133. DWORD_PTR MaxSize() const {return m_dwMaxSize;}
  134. void *Base() const {return m_pvBase;}
  135. int ModuleCount() const {return m_nModuleCount;}
  136. int CategoryCount() const {return m_nCategoryCount;}
  137. void IncModuleCount( UINT nModules ) {m_nModuleCount += nModules;}
  138. void IncCategoryCount( UINT nCategories ) {m_nCategoryCount += nCategories;}
  139. LONG GetNextCookie();
  140. DWORD_PTR m_dwFrontAlloc, m_dwBackAlloc, m_dwCurrFront, m_dwCurrBack;
  141. UINT m_nLevel;
  142. bool m_bLoaded, m_bEnabled, m_bFuncAndCategoryNames, m_bFileNameAndLineNo;
  143. private:
  144. DWORD m_dwId;
  145. DWORD_PTR m_dwMaxSize;
  146. UINT m_nRef;
  147. void *m_pvBase;
  148. UINT m_nModuleCount, m_nCategoryCount;
  149. LONG m_nNextCookie;
  150. };
  151. #endif // ATLDEBUG_TRACE_MANAGER