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.

343 lines
8.6 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 __ATLTRACE_H__
  11. #define __ATLTRACE_H__
  12. #pragma once
  13. #include <atldef.h>
  14. #include <atlconv.h>
  15. #ifdef _DEBUG
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #endif
  19. #ifndef _ATL_NO_DEBUG_CRT
  20. // Warning: if you define the above symbol, you will have
  21. // to provide your own definition of the ATLASSERT(x) macro
  22. // in order to compile ATL
  23. #include <crtdbg.h>
  24. #endif
  25. #ifdef _DEBUG
  26. #include <atldebugapi.h>
  27. extern "C" IMAGE_DOS_HEADER __ImageBase;
  28. #endif // _DEBUG
  29. namespace ATL
  30. {
  31. // Declare a global instance of this class to automatically register a custom trace category at startup
  32. class CTraceCategory
  33. {
  34. public:
  35. explicit CTraceCategory( LPCTSTR pszCategoryName, UINT nStartingLevel = 0 ) throw();
  36. operator DWORD_PTR() const throw();
  37. public:
  38. #ifdef _DEBUG
  39. DWORD_PTR m_dwCategory;
  40. #endif
  41. };
  42. #ifdef _DEBUG
  43. class CTrace
  44. {
  45. public:
  46. typedef int (__cdecl *fnCrtDbgReport_t)(int,const char *,int,const char *,const char *,...);
  47. CTrace(
  48. #ifdef _ATL_NO_DEBUG_CRT
  49. fnCrtDbgReport_t pfnCrtDbgReport = NULL)
  50. #else
  51. fnCrtDbgReport_t pfnCrtDbgReport = _CrtDbgReport)
  52. #endif
  53. : m_hInst(reinterpret_cast<HINSTANCE>(&__ImageBase)),
  54. m_dwModule( 0 )
  55. {
  56. m_dwModule = AtlTraceRegister(m_hInst, pfnCrtDbgReport);
  57. // LoadSettings();
  58. }
  59. ~CTrace()
  60. {
  61. AtlTraceUnregister(m_dwModule);
  62. }
  63. bool ChangeCategory(DWORD_PTR dwCategory, UINT nLevel, ATLTRACESTATUS eStatus)
  64. {
  65. return 0 !=
  66. AtlTraceModifyCategory(0, dwCategory, nLevel, eStatus);
  67. }
  68. bool GetCategory(DWORD_PTR dwCategory, UINT *pnLevel, ATLTRACESTATUS *peStatus)
  69. {
  70. ATLASSERT(pnLevel && peStatus);
  71. return 0 != AtlTraceGetCategory(0, dwCategory, pnLevel, peStatus);
  72. }
  73. void __cdecl TraceV(const char *pszFileName, int nLine,
  74. DWORD_PTR dwCategory, UINT nLevel, LPCSTR pszFmt, va_list args) const
  75. {
  76. AtlTraceVA(m_dwModule, pszFileName, nLine, dwCategory, nLevel, pszFmt, args);
  77. }
  78. void __cdecl TraceV(const char *pszFileName, int nLine,
  79. DWORD_PTR dwCategory, UINT nLevel, LPCWSTR pszFmt, va_list args) const
  80. {
  81. AtlTraceVU(m_dwModule, pszFileName, nLine, dwCategory, nLevel, pszFmt, args);
  82. }
  83. DWORD_PTR RegisterCategory(LPCSTR pszCategory)
  84. {return(AtlTraceRegisterCategoryA(m_dwModule, pszCategory));}
  85. #ifdef _UNICODE
  86. DWORD_PTR RegisterCategory(LPCWSTR pszCategory)
  87. {return(AtlTraceRegisterCategoryU(m_dwModule, pszCategory));}
  88. #endif
  89. bool LoadSettings(LPCTSTR pszFileName = NULL) const
  90. {return 0 != AtlTraceLoadSettings(pszFileName, FALSE);}
  91. void SaveSettings(LPCTSTR pszFileName = NULL) const
  92. {AtlTraceSaveSettings(pszFileName);}
  93. protected:
  94. HINSTANCE m_hInst;
  95. DWORD_PTR m_dwModule;
  96. };
  97. extern CTrace g_AtlTrace;
  98. extern CTraceCategory atlTraceGeneral;
  99. class CTraceFileAndLineInfo
  100. {
  101. public:
  102. CTraceFileAndLineInfo(const char *pszFileName, int nLineNo)
  103. : m_pszFileName(pszFileName), m_nLineNo(nLineNo)
  104. {}
  105. void __cdecl operator()(DWORD_PTR dwCategory, UINT nLevel, const char *pszFmt, ...) const
  106. {
  107. va_list ptr; va_start(ptr, pszFmt);
  108. g_AtlTrace.TraceV(m_pszFileName, m_nLineNo, dwCategory, nLevel, pszFmt, ptr);
  109. va_end(ptr);
  110. }
  111. void __cdecl operator()(DWORD_PTR dwCategory, UINT nLevel, const wchar_t *pszFmt, ...) const
  112. {
  113. va_list ptr; va_start(ptr, pszFmt);
  114. g_AtlTrace.TraceV(m_pszFileName, m_nLineNo, dwCategory, nLevel, pszFmt, ptr);
  115. va_end(ptr);
  116. }
  117. void __cdecl operator()(const char *pszFmt, ...) const
  118. {
  119. va_list ptr; va_start(ptr, pszFmt);
  120. g_AtlTrace.TraceV(m_pszFileName, m_nLineNo, atlTraceGeneral, 0, pszFmt, ptr);
  121. va_end(ptr);
  122. }
  123. void __cdecl operator()(const wchar_t *pszFmt, ...) const
  124. {
  125. va_list ptr; va_start(ptr, pszFmt);
  126. g_AtlTrace.TraceV(m_pszFileName, m_nLineNo, atlTraceGeneral, 0, pszFmt, ptr);
  127. va_end(ptr);
  128. }
  129. private:
  130. const char *const m_pszFileName;
  131. const int m_nLineNo;
  132. };
  133. #endif // _DEBUG
  134. #ifdef _DEBUG
  135. inline CTraceCategory::CTraceCategory( LPCTSTR pszCategoryName, UINT nStartingLevel ) throw() :
  136. m_dwCategory( 0 )
  137. {
  138. m_dwCategory = g_AtlTrace.RegisterCategory( pszCategoryName );
  139. g_AtlTrace.ChangeCategory( m_dwCategory, nStartingLevel, ATLTRACESTATUS_INHERIT);
  140. }
  141. inline CTraceCategory::operator DWORD_PTR() const throw()
  142. {
  143. return( m_dwCategory );
  144. }
  145. #else // !_DEBUG
  146. inline CTraceCategory::CTraceCategory( LPCTSTR pszCategoryName, UINT nStartingLevel ) throw()
  147. {
  148. (void)pszCategoryName;
  149. (void)nStartingLevel;
  150. }
  151. inline CTraceCategory::operator DWORD_PTR() const throw()
  152. {
  153. return( 0 );
  154. }
  155. #endif // _DEBUG
  156. } // namespace ATL
  157. namespace ATL
  158. {
  159. #ifdef _DEBUG
  160. #define DECLARE_TRACE_CATEGORY( name ) extern ATL::CTraceCategory name;
  161. #else
  162. #define DECLARE_TRACE_CATEGORY( name ) const DWORD_PTR name = 0;
  163. #endif
  164. DECLARE_TRACE_CATEGORY( atlTraceGeneral )
  165. DECLARE_TRACE_CATEGORY( atlTraceCOM )
  166. DECLARE_TRACE_CATEGORY( atlTraceQI )
  167. DECLARE_TRACE_CATEGORY( atlTraceRegistrar )
  168. DECLARE_TRACE_CATEGORY( atlTraceRefcount )
  169. DECLARE_TRACE_CATEGORY( atlTraceWindowing )
  170. DECLARE_TRACE_CATEGORY( atlTraceControls )
  171. DECLARE_TRACE_CATEGORY( atlTraceHosting )
  172. DECLARE_TRACE_CATEGORY( atlTraceDBClient )
  173. DECLARE_TRACE_CATEGORY( atlTraceDBProvider )
  174. DECLARE_TRACE_CATEGORY( atlTraceSnapin )
  175. DECLARE_TRACE_CATEGORY( atlTraceNotImpl )
  176. DECLARE_TRACE_CATEGORY( atlTraceAllocation )
  177. DECLARE_TRACE_CATEGORY( atlTraceException )
  178. DECLARE_TRACE_CATEGORY( atlTraceTime )
  179. DECLARE_TRACE_CATEGORY( atlTraceCache )
  180. DECLARE_TRACE_CATEGORY( atlTraceStencil )
  181. DECLARE_TRACE_CATEGORY( atlTraceString )
  182. DECLARE_TRACE_CATEGORY( atlTraceMap )
  183. DECLARE_TRACE_CATEGORY( atlTraceUtil )
  184. DECLARE_TRACE_CATEGORY( atlTraceSecurity )
  185. DECLARE_TRACE_CATEGORY( atlTraceSync )
  186. DECLARE_TRACE_CATEGORY( atlTraceISAPI )
  187. // atlTraceUser categories are no longer needed. Just declare your own trace category using CTraceCategory.
  188. DECLARE_TRACE_CATEGORY( atlTraceUser )
  189. DECLARE_TRACE_CATEGORY( atlTraceUser2 )
  190. DECLARE_TRACE_CATEGORY( atlTraceUser3 )
  191. DECLARE_TRACE_CATEGORY( atlTraceUser4 )
  192. #pragma deprecated( atlTraceUser )
  193. #pragma deprecated( atlTraceUser2 )
  194. #pragma deprecated( atlTraceUser3 )
  195. #pragma deprecated( atlTraceUser4 )
  196. #ifdef _DEBUG
  197. #ifndef _ATL_NO_DEBUG_CRT
  198. class CNoUIAssertHook
  199. {
  200. public:
  201. CNoUIAssertHook()
  202. {
  203. ATLASSERT( s_pfnPrevHook == NULL );
  204. s_pfnPrevHook = _CrtSetReportHook(CrtHookProc);
  205. }
  206. ~CNoUIAssertHook()
  207. {
  208. _CrtSetReportHook(s_pfnPrevHook);
  209. s_pfnPrevHook = NULL;
  210. }
  211. private:
  212. static int __cdecl CrtHookProc(int eReportType, char* pszMessage, int* pnRetVal)
  213. {
  214. if (eReportType == _CRT_ASSERT)
  215. {
  216. ::OutputDebugStringA( "ASSERTION FAILED\n" );
  217. ::OutputDebugStringA( pszMessage );
  218. *pnRetVal = 1;
  219. return TRUE;
  220. }
  221. if (s_pfnPrevHook != NULL)
  222. {
  223. return s_pfnPrevHook(eReportType, pszMessage, pnRetVal);
  224. }
  225. else
  226. {
  227. return FALSE;
  228. }
  229. }
  230. private:
  231. static _CRT_REPORT_HOOK s_pfnPrevHook;
  232. };
  233. __declspec( selectany ) _CRT_REPORT_HOOK CNoUIAssertHook::s_pfnPrevHook = NULL;
  234. #define DECLARE_NOUIASSERT() ATL::CNoUIAssertHook _g_NoUIAssertHook;
  235. #endif // _ATL_NO_DEBUG_CRT
  236. #ifndef ATLTRACE
  237. #define ATLTRACE ATL::CTraceFileAndLineInfo(__FILE__, __LINE__)
  238. #define ATLTRACE2 ATLTRACE
  239. #endif
  240. inline void _cdecl AtlTrace(LPCSTR pszFormat, ...)
  241. {
  242. va_list ptr;
  243. va_start(ptr, pszFormat);
  244. g_AtlTrace.TraceV(NULL, -1, atlTraceGeneral, 0, pszFormat, ptr);
  245. va_end(ptr);
  246. }
  247. inline void _cdecl AtlTrace(LPCWSTR pszFormat, ...)
  248. {
  249. va_list ptr;
  250. va_start(ptr, pszFormat);
  251. g_AtlTrace.TraceV(NULL, -1, atlTraceGeneral, 0, pszFormat, ptr);
  252. va_end(ptr);
  253. }
  254. inline void _cdecl AtlTrace2(DWORD_PTR dwCategory, UINT nLevel, LPCSTR pszFormat, ...)
  255. {
  256. va_list ptr;
  257. va_start(ptr, pszFormat);
  258. g_AtlTrace.TraceV(NULL, -1, dwCategory, nLevel, pszFormat, ptr);
  259. va_end(ptr);
  260. }
  261. inline void _cdecl AtlTrace2(DWORD_PTR dwCategory, UINT nLevel, LPCWSTR pszFormat, ...)
  262. {
  263. va_list ptr;
  264. va_start(ptr, pszFormat);
  265. g_AtlTrace.TraceV(NULL, -1, dwCategory, nLevel, pszFormat, ptr);
  266. va_end(ptr);
  267. }
  268. #define ATLTRACENOTIMPL(funcname) ATLTRACE(ATL::atlTraceNotImpl, 0, _T("ATL: %s not implemented.\n"), funcname); return E_NOTIMPL
  269. #else // !DEBUG
  270. inline void _cdecl AtlTraceNull(...){}
  271. inline void _cdecl AtlTrace(LPCSTR , ...){}
  272. inline void _cdecl AtlTrace2(DWORD_PTR, UINT, LPCSTR , ...){}
  273. inline void _cdecl AtlTrace(LPCWSTR , ...){}
  274. inline void _cdecl AtlTrace2(DWORD_PTR, UINT, LPCWSTR , ...){}
  275. #ifndef ATLTRACE
  276. // BUG BUG BUG ATLTRACE != AtlTrace2!!!
  277. #define ATLTRACE __noop
  278. #define ATLTRACE2 __noop
  279. #endif //ATLTRACE
  280. #define ATLTRACENOTIMPL(funcname) return E_NOTIMPL
  281. #define DECLARE_NOUIASSERT()
  282. #endif //!_DEBUG
  283. }; // namespace ATL
  284. #endif // __ATLTRACE_H__