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.

313 lines
10 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. Debug.h
  6. Abstract:
  7. New debug services for spooler.
  8. Author:
  9. Albert Ting (AlbertT) 15-Jan-1995
  10. Revision History:
  11. --*/
  12. #ifndef _DBGLOG_H
  13. #define _DBGLOG_H
  14. /********************************************************************
  15. Setting up the debug support:
  16. =============================
  17. Define a MODULE prefix string. Since this will be printed as
  18. a prefix to all debug output, it should be concise and unique.
  19. In your global header file:
  20. #define MODULE "prtlib:"
  21. Define a MODULE_DEBUG variable. This is the actual symbol
  22. that the library will use to indicate debugging level.
  23. This DWORD is broken into two bitfield WORDs: the low word
  24. indicates which levels to print to the debugger; the high word
  25. breaks into the debugger. The library takes the DebugLevel from
  26. a debug message, then ANDs it with the debug level. If the bit
  27. is on, the corresponding action (print or break) is taken.
  28. In your global header file:
  29. #define MODULE_DEBUG PrtlibDebug
  30. Finally, the actual debug variable must be defined and initialized
  31. to a default debug level. This must be done in exactly one
  32. *.c translation unit:
  33. In one of your source files:
  34. MODULE_DEBUG_INIT ( {LevelsToPrint}, {LevelsToBreak} );
  35. Adding logging to source code:
  36. ==============================
  37. The general format for debug message is:
  38. DBGMSG( {DebugLevel}, ( {args to printf} ));
  39. The DebugLevel dictates whether the level should print, break
  40. into the debugger, or just log to memory (logging always done).
  41. The args to printf must be placed in an extra set of parens,
  42. and should assume everything is ANSI. To print LPTSTRs, use
  43. the TSTR macro:
  44. DBGMSG( DBG_WARN,
  45. ( "LPTSTR "TSTR", LPSTR %s, LPWSTR %ws\n",
  46. TEXT("hello"), "hello", L"hello" ));
  47. Viewing DBGMSGs:
  48. ================
  49. Messages will print to the debugger (usermode, or kernel debugger
  50. if no usermode debugger is available) for all printable levels.
  51. To change the level, you can edit the MODULE_DEBUG variable
  52. (PrtlibDebug in the above example).
  53. By default, DBG_ERROR and DBG_WARNING messages a logged to the
  54. error log (stored at gpbterrlog). All others are stored in the
  55. trace log (gpbttracelog). These currently log to memory in
  56. a circular buffer. Use the splx.dll extension to dump these
  57. logs.
  58. At compile time, you can switch these logs to go to file rather
  59. than memory. They will be stored as the PID + index number in
  60. the default directory of the process.
  61. ********************************************************************/
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. //
  66. // These values are strictly debug, but must be defined in the free
  67. // build because the TStatus error checking uses them as the first ctr
  68. // parameter. (During inlining they are discarded.)
  69. //
  70. #define DBG_NONE 0x0000
  71. #define DBG_INFO 0x0001
  72. #define DBG_WARN 0x0002
  73. #define DBG_WARNING 0x0002
  74. #define DBG_ERROR 0x0004
  75. #define DBG_TRACE 0x0008
  76. #define DBG_SECURITY 0x0010
  77. #define DBG_EXEC 0x0020
  78. #define DBG_PORT 0x0040
  79. #define DBG_NOTIFY 0x0080
  80. #define DBG_PAUSE 0x0100
  81. #define DBG_THREADM 0x0400
  82. #define DBG_MIN 0x0800
  83. #define DBG_TIME 0x1000
  84. #define DBG_FOLDER 0x2000
  85. #define DBG_NOHEAD 0x8000
  86. #if DBG
  87. extern DWORD MODULE_DEBUG;
  88. //
  89. // This should be used exactly once in a C file. It defines
  90. // the Debug variable, and also the DbgMsg function.
  91. //
  92. // If we are statically linking with SplLib (SplLib is a library, not
  93. // a Dll), then we will get the definition from SplLib, so don't define
  94. // it here.
  95. //
  96. #ifdef LINK_SPLLIB
  97. #define MODULE_DEBUG_INIT( print, break ) \
  98. DWORD MODULE_DEBUG = (DBG_PRINT( print ) | DBG_BREAK( break ))
  99. #else // !LINK_SPLLIB
  100. #ifdef _STRSAFE_H_INCLUDED_
  101. #define MODULE_DEBUG_INIT( print, break ) \
  102. VOID \
  103. DbgMsg( \
  104. LPCSTR pszMsgFormat, \
  105. ... \
  106. ) \
  107. { \
  108. CHAR szMsgText[1024]; \
  109. va_list vargs; \
  110. \
  111. va_start( vargs, pszMsgFormat ); \
  112. StringCchVPrintfA(szMsgText, COUNTOF(szMsgText), pszMsgFormat, vargs ); \
  113. va_end( vargs ); \
  114. \
  115. if( szMsgText[0] && szMsgText[0] != ' ' ){ \
  116. OutputDebugStringA( MODULE ); \
  117. } \
  118. OutputDebugStringA( szMsgText ); \
  119. } \
  120. DWORD MODULE_DEBUG = (DBG_PRINT( print ) | DBG_BREAK( break ))
  121. #else // !_STRSAFE_H_INCLUDED_
  122. #define MODULE_DEBUG_INIT( print, break ) \
  123. VOID \
  124. DbgMsg( \
  125. LPCSTR pszMsgFormat, \
  126. ... \
  127. ) \
  128. { \
  129. CHAR szMsgText[1024]; \
  130. va_list vargs; \
  131. \
  132. va_start( vargs, pszMsgFormat ); \
  133. wvsprintfA(szMsgText, pszMsgFormat, vargs); \
  134. va_end( vargs ); \
  135. \
  136. if( szMsgText[0] && szMsgText[0] != ' ' ){ \
  137. OutputDebugStringA( MODULE ); \
  138. } \
  139. OutputDebugStringA( szMsgText ); \
  140. } \
  141. DWORD MODULE_DEBUG = (DBG_PRINT( print ) | DBG_BREAK( break ))
  142. #endif // _STRSAFE_H_INCLUDED_
  143. #endif // LINK_SPLLIB
  144. #define DBGSTR( str ) \
  145. ((str) ? (str) : TEXT("(NULL)"))
  146. #ifdef UNICODE
  147. #define TSTR "%ws"
  148. #else
  149. #define TSTR "%s"
  150. #endif
  151. #define DBG_PRINT_MASK 0xffff
  152. #define DBG_BREAK_SHIFT 16
  153. #define DBG_PRINT(x) (x)
  154. #define DBG_BREAK(x) (((x) << DBG_BREAK_SHIFT)|(x))
  155. #define SPLASSERT(expr) \
  156. if (!(expr)) { \
  157. DbgMsg( "Failed: %s\nLine %d, %s\n", \
  158. #expr, \
  159. __LINE__, \
  160. __FILE__ ); \
  161. DebugBreak(); \
  162. }
  163. VOID
  164. DbgMsg(
  165. LPCSTR pszMsgFormat,
  166. ...
  167. );
  168. #ifdef DBGLOG
  169. #define DBGMSG( uDbgLevel, argsPrint ) \
  170. vDbgLogError( MODULE_DEBUG, \
  171. uDbgLevel, \
  172. __LINE__, \
  173. __FILE__, \
  174. MODULE, \
  175. pszDbgAllocMsgA argsPrint )
  176. LPSTR
  177. pszDbgAllocMsgA(
  178. LPCSTR pszMsgFormatA,
  179. ...
  180. );
  181. VOID
  182. vDbgLogError(
  183. UINT uDbg,
  184. UINT uDbgLevel,
  185. UINT uLine,
  186. LPCSTR pszFileA,
  187. LPCSTR pszModuleA,
  188. LPCSTR pszMsgA
  189. );
  190. #else
  191. VOID
  192. DbgBreakPoint(
  193. VOID
  194. );
  195. #define DBGMSG( Level, MsgAndArgs ) \
  196. { \
  197. if( ( (Level) & 0xFFFF ) & MODULE_DEBUG ){ \
  198. DbgMsg MsgAndArgs; \
  199. } \
  200. if( ( (Level) << 16 ) & MODULE_DEBUG ) \
  201. DbgBreakPoint(); \
  202. }
  203. #endif
  204. #else
  205. #define MODULE_DEBUG_INIT( print, break )
  206. #define DBGMSG( uDbgLevel, argsPrint )
  207. #define SPLASSERT(exp)
  208. #endif
  209. //
  210. // Automatic checking if an object is valid.
  211. //
  212. #if DBG
  213. VOID
  214. vWarnInvalid(
  215. PVOID pvObject,
  216. UINT uDbg,
  217. UINT uLine,
  218. LPCSTR pszFileA,
  219. LPCSTR pszModuleA
  220. );
  221. #define VALID_PTR(x) \
  222. ((( x ) && (( x )->bValid( ))) ? \
  223. TRUE : \
  224. ( vWarnInvalid( (PVOID)(x), MODULE_DEBUG, __LINE__, __FILE__, MODULE ), FALSE ))
  225. #define VALID_OBJ(x) \
  226. ((( x ).bValid( )) ? \
  227. TRUE : \
  228. ( vWarnInvalid( (PVOID)&(x), MODULE_DEBUG, __LINE__, __FILE__, MODULE ), FALSE ))
  229. #define VALID_BASE(x) \
  230. (( x::bValid( )) ? \
  231. TRUE : \
  232. ( vWarnInvalid( (PVOID)this, MODULE_DEBUG, __LINE__, __FILE__, MODULE ), FALSE ))
  233. #else
  234. #define VALID_PTR(x) \
  235. (( x ) && (( x )->bValid()))
  236. #define VALID_OBJ(x) \
  237. (( x ).bValid())
  238. #define VALID_BASE(x) \
  239. ( x::bValid( ))
  240. #endif
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. #endif // _DBGLOG_H