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.

379 lines
7.7 KiB

  1. //
  2. // Microsoft Corporation - Copyright 1997
  3. //
  4. //
  5. // DEBUG.CPP - Debugging code
  6. //
  7. #include "pch.h"
  8. // local globals
  9. static const char s_szNewline[] = "\n";
  10. // globals
  11. const char g_szTrue[] = "True";
  12. const char g_szFalse[] = "False";
  13. // local structures
  14. CODETOSTR HRtoStr = {
  15. 2,
  16. {
  17. { S_OK, "S_OK or NOERROR" },
  18. { S_FALSE, "S_FALSE" }
  19. }
  20. };
  21. CODETOSTR HSEtoStr = {
  22. 5,
  23. {
  24. { HSE_STATUS_SUCCESS, "HSE_STATUS_SUCCESS" },
  25. { ERROR_INVALID_INDEX, "ERROR_INVALID_INDEX" },
  26. { ERROR_INSUFFICIENT_BUFFER, "ERROR_INSUFFICIENT_BUFFER" },
  27. { ERROR_MORE_DATA, "ERROR_MORE_DATA" },
  28. { ERROR_NO_DATA, "ERROR_NO_DATA" }
  29. }
  30. };
  31. CODETOSTR ErrtoStr = {
  32. 4,
  33. {
  34. { ERROR_INVALID_PARAMETER, "ERROR_INVALID_PARAMETER" },
  35. { HSE_STATUS_SUCCESS_AND_KEEP_CONN, "HSE_STATUS_SUCCESS_AND_KEEP_CONN" },
  36. { HSE_STATUS_PENDING, "HSE_STATUS_PENDING" },
  37. { HSE_STATUS_ERROR, "HSE_STATUS_ERROR" }
  38. }
  39. };
  40. // local functions
  41. const char *FindResult(
  42. LPCODETOSTR lpCodeToStr,
  43. DWORD dwResult );
  44. #ifdef DEBUG /***********************************************************/
  45. // globals
  46. DWORD g_dwTraceFlag = // flag used to turn on/off certain debug msgs.
  47. // 0x00000000;
  48. TF_PARSE | TF_FUNC;
  49. // TF_ALWAYS;
  50. //
  51. // What: IsTraceFlagSet
  52. //
  53. // Desc: Tests to see is any of the flags in dwFlag
  54. // are set in g_dwTraceFlag.
  55. //
  56. // In: dwFlag are the flags to be tested.
  57. //
  58. // Return: TRUE if flag is set or if dwFlags == TF_ALWAYS.
  59. // Otherwise, we return FALSE.
  60. //
  61. BOOL CDECL IsTraceFlagSet( DWORD dwFlag )
  62. {
  63. return (
  64. ( (dwFlag & g_dwTraceFlag) == dwFlag ) ?
  65. TRUE :
  66. ( ( dwFlag == TF_ALWAYS ) ?
  67. TRUE :
  68. FALSE )
  69. );
  70. }
  71. //
  72. // What: AssertMsg
  73. //
  74. // Desc: Debugging output with stop condition.
  75. //
  76. BOOL CDECL AssertMsg(
  77. BOOL fShouldBeTrue,
  78. LPCSTR pszFile,
  79. DWORD dwLine,
  80. LPCSTR pszStatement )
  81. {
  82. if ( !fShouldBeTrue )
  83. {
  84. CHAR ach[ 1024 + 40 ]; // Largest path plus extra
  85. wsprintf( ach, "Assert: %s (%u): %s",
  86. pszFile, dwLine, pszStatement );
  87. OutputDebugString( ach );
  88. OutputDebugString( s_szNewline );
  89. }
  90. return !fShouldBeTrue;
  91. } // AssertMsg( )
  92. //
  93. // What: TraceMsg
  94. //
  95. // Desc: Debugging output with conditional flags (see
  96. // DEBUG.H for flags).
  97. //
  98. void CDECL TraceMsg(
  99. DWORD dwFlag,
  100. LPCSTR pszMsg,
  101. ... )
  102. {
  103. CHAR ach[ 1024 + 40 ]; // Largest path plus extra
  104. va_list vArgs;
  105. if ( IsTraceFlagSet( dwFlag ) )
  106. {
  107. int cch;
  108. StrCpy( ach, "Trace: " );
  109. cch = lstrlen( ach );
  110. va_start( vArgs, pszMsg );
  111. wvsprintf( &ach[ cch ], pszMsg, vArgs );
  112. va_end( vArgs );
  113. OutputDebugString( ach );
  114. OutputDebugString( s_szNewline );
  115. }
  116. } // TraceMsg( )
  117. //
  118. // What: TraceMsgResult
  119. //
  120. // Desc: Debugging output with conditional flags and
  121. // it displays the HRESULT in "English".
  122. //
  123. void CDECL TraceMsgResult(
  124. DWORD dwFlag,
  125. LPCODETOSTR lpCodeToStr,
  126. DWORD dwResult,
  127. LPCSTR pszMsg,
  128. ... )
  129. {
  130. CHAR ach[ 1024 + 40 ]; // Largest path plus extra
  131. va_list vArgs;
  132. if ( IsTraceFlagSet( dwFlag ) )
  133. {
  134. int cch;
  135. StrCpy( ach, "Trace: " );
  136. cch = lstrlen( ach );
  137. va_start( vArgs, pszMsg );
  138. wvsprintf( &ach[ cch ], pszMsg, vArgs );
  139. va_end( vArgs );
  140. cch = lstrlen( ach );
  141. wsprintf( &ach[ cch ], " = %s", FindResult( lpCodeToStr, dwResult ) );
  142. OutputDebugString( ach );
  143. OutputDebugString( s_szNewline );
  144. }
  145. } // TraceMsgResult( )
  146. #endif // DEBUG /********************************************************/
  147. /*********************************************************
  148. These are for debugging the GET/POST as well as
  149. debugging the parser.
  150. *********************************************************/
  151. //
  152. // What: DebugMsg
  153. //
  154. // Desc: Debugging output
  155. //
  156. void CDECL DebugMsg(
  157. LPSTR lpszOut,
  158. LPCSTR pszMsg,
  159. ... )
  160. {
  161. CHAR ach[ 1024 + 40 ]; // Largest path plus extra
  162. va_list vArgs;
  163. int cch;
  164. va_start( vArgs, pszMsg );
  165. wvsprintf( ach, pszMsg, vArgs );
  166. va_end( vArgs );
  167. #ifdef DEBUG
  168. OutputDebugString( ach );
  169. OutputDebugString( s_szNewline );
  170. #endif // DEBUG
  171. if ( lpszOut )
  172. {
  173. StrCat( lpszOut, ach );
  174. }
  175. } // DebugMsg( )
  176. //
  177. // What: DebugMsgResult
  178. //
  179. // Desc: Debugging output displaying the dwResult in "English".
  180. //
  181. void CDECL DebugMsgResult(
  182. LPSTR lpszOut,
  183. LPCODETOSTR lpCodeToStr,
  184. DWORD dwResult,
  185. LPCSTR pszMsg,
  186. ... )
  187. {
  188. CHAR ach[ 1024 + 40 ]; // Largest path plus extra
  189. va_list vArgs;
  190. int cch;
  191. va_start( vArgs, pszMsg );
  192. wvsprintf( ach, pszMsg, vArgs );
  193. va_end( vArgs );
  194. cch = lstrlen( ach );
  195. wsprintf( &ach[ cch ], " = %s", FindResult( lpCodeToStr, dwResult ) );
  196. #ifdef DEBUG
  197. OutputDebugString( ach );
  198. OutputDebugString( s_szNewline );
  199. #endif // DEBUG
  200. if ( lpszOut )
  201. {
  202. StrCat( lpszOut, ach );
  203. }
  204. } // DebugMsgResult( )
  205. //
  206. // What: LogMsg
  207. //
  208. // Desc: Append text to be written to server log.
  209. //
  210. void CDECL LogMsg(
  211. LPSTR lpszLog,
  212. LPSTR lpszOut,
  213. LPCSTR pszMsg,
  214. ... )
  215. {
  216. va_list vArgs;
  217. char szBuffer[ 1024 + 40 ];
  218. va_start( vArgs, pszMsg );
  219. wvsprintf( szBuffer, pszMsg, vArgs );
  220. va_end( vArgs );
  221. #ifdef DEBUG
  222. if ( lstrlen( szBuffer ) >= HSE_LOG_BUFFER_LEN )
  223. {
  224. OutputDebugString( "Log overflow=" );
  225. OutputDebugString( szBuffer );
  226. OutputDebugString( s_szNewline );
  227. }
  228. else
  229. {
  230. StrCpy( lpszLog, szBuffer );
  231. OutputDebugString( "Log Message= " );
  232. OutputDebugString( lpszLog );
  233. OutputDebugString( s_szNewline );
  234. }
  235. #endif // DEBUG
  236. if ( lpszOut )
  237. {
  238. StrCat( lpszOut, szBuffer );
  239. StrCat( lpszOut, s_szNewline );
  240. }
  241. } // LogMsg( )
  242. //
  243. // What: LogMsgResult
  244. //
  245. // Desc: Append text to be written to server log and appends an english
  246. // translation to the dwResult code.
  247. //
  248. void CDECL LogMsgResult(
  249. LPSTR lpszLog,
  250. LPSTR lpszOut,
  251. LPCODETOSTR lpCodeToStr,
  252. DWORD dwResult,
  253. LPCSTR pszMsg,
  254. ... )
  255. {
  256. va_list vArgs;
  257. int cch;
  258. char szBuffer[ 1024 + 40 ];
  259. va_start( vArgs, pszMsg );
  260. wvsprintf( szBuffer, pszMsg, vArgs );
  261. va_end( vArgs );
  262. cch = lstrlen( szBuffer );
  263. wsprintf( &szBuffer[ cch ], " = %s", FindResult( lpCodeToStr, dwResult ) );
  264. #ifdef DEBUG
  265. if ( lstrlen( szBuffer ) >= HSE_LOG_BUFFER_LEN )
  266. {
  267. OutputDebugString( "Log overflow=" );
  268. OutputDebugString( szBuffer );
  269. OutputDebugString( s_szNewline );
  270. }
  271. else
  272. {
  273. StrCpy( lpszLog, szBuffer );
  274. OutputDebugString( "Log Message= " );
  275. OutputDebugString( lpszLog );
  276. OutputDebugString( s_szNewline );
  277. }
  278. #endif // DEBUG
  279. if ( lpszOut )
  280. {
  281. StrCat( lpszOut, szBuffer );
  282. StrCat( lpszOut, s_szNewline );
  283. }
  284. } // LogMsgResult( )
  285. //
  286. // What: FindHResult
  287. //
  288. // Desc: Searches for a string for a HRESULT. If not found
  289. // it will display the HEX value.
  290. //
  291. const char *FindResult(
  292. LPCODETOSTR lpCodeToStr,
  293. DWORD dwResult )
  294. {
  295. static char szResult[ 11 ]; // just enough for "0x00000000" + NULL
  296. for( DWORD i = 0 ; i < lpCodeToStr->dwCount ; i++ )
  297. {
  298. if ( lpCodeToStr->ids[ i ].dwCode == dwResult )
  299. {
  300. return lpCodeToStr->ids[ i ].lpDesc;
  301. }
  302. } // for i
  303. wsprintf( szResult, "%x", dwResult );
  304. return szResult;
  305. } // FindHResult( )