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.

356 lines
11 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. Debug.h
  5. Abstract:
  6. Interface for the CDebug class.
  7. Author:
  8. Eran Yariv (EranY) Jul, 1999
  9. Revision History:
  10. --*/
  11. #if !defined(AFX_DEBUG_H__DDEC9CAD_CF2D_4F3F_9538_2F6041A022B6__INCLUDED_)
  12. #define AFX_DEBUG_H__DDEC9CAD_CF2D_4F3F_9538_2F6041A022B6__INCLUDED_
  13. #if _MSC_VER > 1000
  14. #pragma once
  15. #endif // _MSC_VER > 1000
  16. #if !defined(DEBUG)
  17. #if defined(_DEBUG)
  18. #define DEBUG
  19. #elif defined(DBG)
  20. #define DEBUG
  21. #endif
  22. #endif
  23. //
  24. // Remove previous declarations:
  25. //
  26. #if defined(ASSERTION)
  27. #undef ASSERTION
  28. #endif
  29. #ifdef VERBOSE
  30. #undef VERBOSE
  31. #endif
  32. #ifdef ASSERTION_FAILURE
  33. #undef ASSERTION_FAILURE
  34. #endif
  35. #include <FaxDebug.h>
  36. //
  37. // Bit-mask of errors / messages :
  38. //
  39. typedef enum
  40. {
  41. DBG_MSG = 0x00000001, // General messages (not warnings or errors)
  42. DBG_WARNING = 0x00000002, // Warnings
  43. GENERAL_ERR = 0x00000004,
  44. TAPI_MSG = 0x00000008,
  45. ASSERTION_FAILED = 0x00000010, // Debug messages are displayed when an assertion fails
  46. FUNC_TRACE = 0x00000020, // Function entry / exit traces
  47. MEM_ERR = 0x00000040, // Errors start from here...
  48. COM_ERR = 0x00000080,
  49. RESOURCE_ERR = 0x00000100,
  50. EXCEPTION_ERR = 0x00000200,
  51. RPC_ERR = 0x00000400,
  52. WINDOW_ERR = 0x00000800,
  53. FILE_ERR = 0x00001000,
  54. SECURITY_ERR = 0x00002000,
  55. REGISTRY_ERR = 0x00004000,
  56. PRINT_ERR = 0x00008000,
  57. SETUP_ERR = 0x00010000,
  58. NET_ERR = 0x00020000,
  59. SCM_ERR = 0x00040000,
  60. STARTUP_ERR = 0x00080000,
  61. DBG_ERRORS_ONLY = 0xFFFFFFDC, // everything but DBG_MSG,FUNC_TRACE,DBG_WARNING
  62. DBG_ERRORS_WARNINGS = 0xFFFFFFDE, // everything but DBG_MSG,FUNC_TRACE
  63. DBG_ALL = 0xFFFFFFFF
  64. } DbgMsgType;
  65. #ifdef ENABLE_FRE_LOGGING
  66. #define ENABLE_LOGGING
  67. #endif
  68. #ifdef DEBUG
  69. #define ENABLE_LOGGING
  70. #endif
  71. #ifdef ENABLE_LOGGING
  72. #define DEFAULT_DEBUG_MASK ASSERTION_FAILED
  73. #define DEFAULT_FORMAT_MASK DBG_PRNT_ALL_TO_STD
  74. // use these in your debugging sessions
  75. #define DBG_ENTER CDebug debugObject
  76. #define VERBOSE debugObject.Trace
  77. #ifndef DEBUG
  78. #define DebugBreak() ;
  79. #endif
  80. #define ASSERTION_FAILURE { \
  81. debugObject.DbgPrint (ASSERTION_FAILED, \
  82. TEXT(__FILE__), \
  83. __LINE__, \
  84. TEXT("ASSERTION FAILURE!!!")); \
  85. DebugBreak(); \
  86. }
  87. #define ASSERTION(x) if (!(x)) ASSERTION_FAILURE
  88. #define CALL_FAIL(t,szFunc,hr) \
  89. debugObject.DbgPrint(t, \
  90. TEXT(__FILE__), \
  91. __LINE__, \
  92. TEXT("Call to function %s failed with 0x%08X"), \
  93. szFunc, \
  94. hr);
  95. //////////////////////////////////////////
  96. // use these to cofigure the debug output
  97. #define SET_DEBUG_MASK(m) CDebug::SetDebugMask(m)
  98. #define SET_FORMAT_MASK(m) CDebug::SetFormatMask(m)
  99. #define SET_DEBUG_FLUSH(m) CDebug::SetDebugFlush(m)
  100. #define GET_DEBUG_MASK CDebug::GetDebugMask()
  101. #define GET_FORMAT_MASK CDebug::GetFormatMask()
  102. #define MODIFY_DEBUG_MASK(a,b) CDebug::ModifyDebugMask(a,b)
  103. #define MODIFY_FORMAT_MASK(a,b) CDebug::ModifyFormatMask(a,b)
  104. #define IS_DEBUG_SESSION_FROM_REG CDebug::DebugFromRegistry()
  105. #define OPEN_DEBUG_LOG_FILE(f) CDebug::OpenLogFile(f)
  106. #define CLOSE_DEBUG_LOG_FILE CDebug::CloseLogFile()
  107. #define SET_DEBUG_FILE_HANDLE(h) CDebug::SetLogFile(h)
  108. //////////////////////////////////////////
  109. #ifndef _DEBUG_INDENT_SIZE
  110. #define _DEBUG_INDENT_SIZE 3
  111. #endif // #ifndef _DEBUG_INDENT_SIZE
  112. class CDebug
  113. {
  114. public:
  115. CDebug (LPCTSTR lpctstrModule) :
  116. m_ReturnType (DBG_FUNC_RET_UNKNOWN)
  117. {
  118. EnterModule (lpctstrModule);
  119. }
  120. CDebug (LPCTSTR lpctstrModule,
  121. LPCTSTR lpctstrFormat,
  122. ...) :
  123. m_ReturnType (DBG_FUNC_RET_UNKNOWN)
  124. {
  125. va_list arg_ptr;
  126. va_start(arg_ptr, lpctstrFormat);
  127. EnterModuleWithParams (lpctstrModule, lpctstrFormat, arg_ptr);
  128. }
  129. CDebug (LPCTSTR lpctstrModule, HRESULT &hr)
  130. {
  131. EnterModule (lpctstrModule);
  132. SetHR (hr);
  133. }
  134. CDebug (LPCTSTR lpctstrModule,
  135. HRESULT &hr,
  136. LPCTSTR lpctstrFormat,
  137. ...)
  138. {
  139. va_list arg_ptr;
  140. va_start(arg_ptr, lpctstrFormat);
  141. EnterModuleWithParams (lpctstrModule, lpctstrFormat, arg_ptr);
  142. SetHR (hr);
  143. }
  144. CDebug (LPCTSTR lpctstrModule, DWORD &dw)
  145. {
  146. EnterModule (lpctstrModule);
  147. SetDWRes (dw);
  148. }
  149. CDebug (LPCTSTR lpctstrModule, UINT &dw)
  150. {
  151. EnterModule (lpctstrModule);
  152. SetDWRes ((DWORD &)dw);
  153. }
  154. CDebug (LPCTSTR lpctstrModule,
  155. DWORD &dw,
  156. LPCTSTR lpctstrFormat,
  157. ...)
  158. {
  159. va_list arg_ptr;
  160. va_start(arg_ptr, lpctstrFormat);
  161. EnterModuleWithParams (lpctstrModule, lpctstrFormat, arg_ptr);
  162. SetDWRes (dw);
  163. }
  164. CDebug (LPCTSTR lpctstrModule,
  165. UINT &dw,
  166. LPCTSTR lpctstrFormat,
  167. ...)
  168. {
  169. va_list arg_ptr;
  170. va_start(arg_ptr, lpctstrFormat);
  171. EnterModuleWithParams (lpctstrModule, lpctstrFormat, arg_ptr);
  172. SetDWRes ((DWORD &)dw);
  173. }
  174. CDebug (LPCTSTR lpctstrModule, BOOL &b)
  175. {
  176. EnterModule (lpctstrModule);
  177. SetBOOL (b);
  178. }
  179. CDebug (LPCTSTR lpctstrModule,
  180. BOOL &b,
  181. LPCTSTR lpctstrFormat,
  182. ...)
  183. {
  184. va_list arg_ptr;
  185. va_start(arg_ptr, lpctstrFormat);
  186. EnterModuleWithParams (lpctstrModule, lpctstrFormat, arg_ptr);
  187. SetBOOL (b);
  188. }
  189. virtual ~CDebug();
  190. static void DbgPrint (
  191. DbgMsgType type,
  192. LPCTSTR lpctstrFileName,
  193. DWORD dwLine,
  194. LPCTSTR lpctstrFormat,
  195. ...
  196. );
  197. void Trace (
  198. DbgMsgType type,
  199. LPCTSTR lpctstrFormat,
  200. ...
  201. );
  202. static void ResetIndent() { InterlockedExchange(&m_sdwIndent,0); }
  203. static void Indent() { InterlockedIncrement(&m_sdwIndent); }
  204. static void Unindent();
  205. // calling any of those functions overrides the registry entries
  206. // SetDebugMask - overrides the DebugLevelEx entry
  207. // SetFormatMask - overrides the DebugFormatEx entry
  208. static void SetDebugMask(DWORD dwMask);
  209. static void SetFormatMask(DWORD dwMask);
  210. static DWORD GetDebugMask() { return m_sDbgMask; }
  211. static DWORD GetFormatMask() { return m_sFmtMask; }
  212. static DWORD ModifyDebugMask(DWORD dwAdd,DWORD dwRemove);
  213. static DWORD ModifyFormatMask(DWORD dwAdd,DWORD dwRemove);
  214. static void SetDebugFlush(BOOL fFlush);
  215. static HANDLE SetLogFile(HANDLE hFile);
  216. static BOOL OpenLogFile(LPCTSTR lpctstrFilename);
  217. static void CloseLogFile();
  218. // returns whether we find debug setting in the registry
  219. // call this before using SetDebugMask or SetFormatMask to verify
  220. // if the registry is being used, so registry will decide on
  221. // debug level
  222. static BOOL DebugFromRegistry();
  223. private:
  224. static void Print (
  225. DbgMsgType type,
  226. LPCTSTR lpctstrFileName,
  227. DWORD dwLine,
  228. LPCTSTR lpctstrFormat,
  229. va_list arg_ptr
  230. );
  231. void EnterModuleWithParams (LPCTSTR lpctstrModule,
  232. LPCTSTR lpctstrFormat,
  233. va_list arg_ptr);
  234. void EnterModule (LPCTSTR lpctstrModule);
  235. void SetHR (HRESULT &hr) { m_ReturnType = DBG_FUNC_RET_HR; m_phr = &hr; }
  236. void SetDWRes (DWORD &dw) { m_ReturnType = DBG_FUNC_RET_DWORD; m_pDword = &dw; }
  237. void SetBOOL (BOOL &b) { m_ReturnType = DBG_FUNC_RET_BOOL; m_pBool = &b; }
  238. static LONG m_sdwIndent;
  239. static DWORD m_sDbgMask; // a combination of DbgMsgType values
  240. static DWORD m_sFmtMask; // a combination of DbgMsgFormat values
  241. static HANDLE m_shLogFile;
  242. static BOOL m_sbMaskReadFromReg; // Did we already read the debug & format mask from the registry?
  243. static BOOL m_sbRegistryExist; // Do we use debug mask of registry?
  244. static BOOL m_sbFlush; // Do we run FlushFileBuffer after writing to log file
  245. static BOOL ReadMaskFromReg(); // Attempt to read debug & format mask from registry.
  246. static LPCTSTR GetMsgTypeString(DWORD dwMask);
  247. static BOOL OutputFileString(LPCTSTR szMsg);
  248. TCHAR m_tszModuleName[MAX_PATH];
  249. typedef enum
  250. {
  251. DBG_FUNC_RET_UNKNOWN,
  252. DBG_FUNC_RET_HR,
  253. DBG_FUNC_RET_DWORD,
  254. DBG_FUNC_RET_BOOL
  255. } DbgFuncRetType;
  256. DbgFuncRetType m_ReturnType;
  257. HRESULT *m_phr;
  258. DWORD *m_pDword;
  259. BOOL *m_pBool;
  260. };
  261. #define START_RPC_TIME(f) DWORD dwRPCTimeCheck=GetTickCount();
  262. #define END_RPC_TIME(f) VERBOSE (DBG_MSG, TEXT("%s took %ld millisecs"), \
  263. f, GetTickCount()-dwRPCTimeCheck);
  264. #else // ENABLE_LOGGING
  265. #define DBG_ENTER void(0);
  266. #define VERBOSE void(0);
  267. #define ASSERTION_FAILURE void(0);
  268. #define ASSERTION(x) void(0);
  269. #define CALL_FAIL(t,szFunc,hr) void(0);
  270. #define START_RPC_TIME(f) void(0);
  271. #define END_RPC_TIME(f) void(0);
  272. #define SET_DEBUG_MASK(m) void(0);
  273. #define SET_FORMAT_MASK(m) void(0);
  274. #define GET_DEBUG_MASK 0;
  275. #define GET_FORMAT_MASK 0;
  276. #define MODIFY_DEBUG_MASK(a,b) 0;
  277. #define MODIFY_FORMAT_MASK(a,b) 0;
  278. #define IS_DEBUG_SESSION_FROM_REG FALSE;
  279. #define OPEN_DEBUG_LOG_FILE(f) FALSE;
  280. #define CLOSE_DEBUG_LOG_FILE void(0);
  281. #define SET_DEBUG_FILE_HANDLE(h) void(0);
  282. #endif // ENABLE_LOGGING
  283. #endif // !defined(AFX_DEBUG_H__DDEC9CAD_CF2D_4F3F_9538_2F6041A022B6__INCLUDED_)