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.

347 lines
7.4 KiB

  1. //==========================================================================;
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. // PURPOSE.
  7. //
  8. // Copyright (c) 1992-1994 Microsoft Corporation
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. // debug.c
  13. //
  14. // Description:
  15. // This file contains code to support easy-to-use debugging support.
  16. // All code compiles to nothing if DEBUG is not defined.
  17. //
  18. //
  19. //==========================================================================;
  20. #ifdef DEBUG
  21. #include <windows.h>
  22. #include <windowsx.h>
  23. #include <stdarg.h>
  24. #include "debug.h"
  25. //
  26. // since we don't UNICODE our debugging messages, use the ASCII entry
  27. // points regardless of how we are compiled.
  28. //
  29. #ifndef WIN32
  30. #define lstrcatA lstrcat
  31. #define lstrlenA lstrlen
  32. #define GetProfileIntA GetProfileInt
  33. #define OutputDebugStringA OutputDebugString
  34. #endif
  35. //
  36. //
  37. //
  38. BOOL __gfDbgEnabled = TRUE; // master enable
  39. UINT __guDbgLevel = 0; // current debug level
  40. //--------------------------------------------------------------------------;
  41. //
  42. // void DbgVPrintF
  43. //
  44. // Description:
  45. //
  46. //
  47. // Arguments:
  48. // LPSTR szFormat:
  49. //
  50. // va_list va:
  51. //
  52. // Return (void):
  53. // No value is returned.
  54. //
  55. //--------------------------------------------------------------------------;
  56. void FAR CDECL DbgVPrintF
  57. (
  58. LPSTR szFormat,
  59. va_list va
  60. )
  61. {
  62. char ach[DEBUG_MAX_LINE_LEN];
  63. BOOL fDebugBreak = FALSE;
  64. BOOL fPrefix = TRUE;
  65. BOOL fCRLF = TRUE;
  66. ach[0] = '\0';
  67. for (;;)
  68. {
  69. switch (*szFormat)
  70. {
  71. case '!':
  72. fDebugBreak = TRUE;
  73. szFormat++;
  74. continue;
  75. case '`':
  76. fPrefix = FALSE;
  77. szFormat++;
  78. continue;
  79. case '~':
  80. fCRLF = FALSE;
  81. szFormat++;
  82. continue;
  83. }
  84. break;
  85. }
  86. if (fDebugBreak)
  87. {
  88. ach[0] = '\007';
  89. ach[1] = '\0';
  90. }
  91. if (fPrefix)
  92. {
  93. lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  94. }
  95. #ifdef WIN32
  96. wvsprintfA(ach + lstrlenA(ach), szFormat, va);
  97. #else
  98. wvsprintf(ach + lstrlenA(ach), szFormat, (LPSTR)va);
  99. #endif
  100. if (fCRLF)
  101. {
  102. lstrcatA(ach, "\r\n");
  103. }
  104. OutputDebugStringA(ach);
  105. if (fDebugBreak)
  106. {
  107. #if DBG
  108. DebugBreak();
  109. #endif
  110. }
  111. } // DbgVPrintF()
  112. //--------------------------------------------------------------------------;
  113. //
  114. // void dprintf
  115. //
  116. // Description:
  117. // dprintf() is called by the DPF() macro if DEBUG is defined at compile
  118. // time. It is recommended that you only use the DPF() macro to call
  119. // this function--so you don't have to put #ifdef DEBUG around all
  120. // of your code.
  121. //
  122. // Arguments:
  123. // UINT uDbgLevel:
  124. //
  125. // LPSTR szFormat:
  126. //
  127. // Return (void):
  128. // No value is returned.
  129. //
  130. //--------------------------------------------------------------------------;
  131. void FAR CDECL dprintf
  132. (
  133. UINT uDbgLevel,
  134. LPSTR szFormat,
  135. ...
  136. )
  137. {
  138. va_list va;
  139. if (!__gfDbgEnabled || (__guDbgLevel < uDbgLevel))
  140. return;
  141. va_start(va, szFormat);
  142. DbgVPrintF(szFormat, va);
  143. va_end(va);
  144. } // dprintf()
  145. //--------------------------------------------------------------------------;
  146. //
  147. // BOOL DbgEnable
  148. //
  149. // Description:
  150. //
  151. //
  152. // Arguments:
  153. // BOOL fEnable:
  154. //
  155. // Return (BOOL):
  156. // Returns the previous debugging state.
  157. //
  158. //--------------------------------------------------------------------------;
  159. BOOL WINAPI DbgEnable
  160. (
  161. BOOL fEnable
  162. )
  163. {
  164. BOOL fOldState;
  165. fOldState = __gfDbgEnabled;
  166. __gfDbgEnabled = fEnable;
  167. return (fOldState);
  168. } // DbgEnable()
  169. //--------------------------------------------------------------------------;
  170. //
  171. // UINT DbgSetLevel
  172. //
  173. // Description:
  174. //
  175. //
  176. // Arguments:
  177. // UINT uLevel:
  178. //
  179. // Return (UINT):
  180. // Returns the previous debugging level.
  181. //
  182. //--------------------------------------------------------------------------;
  183. UINT WINAPI DbgSetLevel
  184. (
  185. UINT uLevel
  186. )
  187. {
  188. UINT uOldLevel;
  189. uOldLevel = __guDbgLevel;
  190. __guDbgLevel = uLevel;
  191. return (uOldLevel);
  192. } // DbgSetLevel()
  193. //--------------------------------------------------------------------------;
  194. //
  195. // UINT DbgGetLevel
  196. //
  197. // Description:
  198. //
  199. //
  200. // Arguments:
  201. // None.
  202. //
  203. // Return (UINT):
  204. // Returns the current debugging level.
  205. //
  206. //--------------------------------------------------------------------------;
  207. UINT WINAPI DbgGetLevel
  208. (
  209. void
  210. )
  211. {
  212. return (__guDbgLevel);
  213. } // DbgGetLevel()
  214. //--------------------------------------------------------------------------;
  215. //
  216. // UINT DbgInitialize
  217. //
  218. // Description:
  219. //
  220. //
  221. // Arguments:
  222. // BOOL fEnable:
  223. //
  224. // Return (UINT):
  225. // Returns the debugging level that was set.
  226. //
  227. //--------------------------------------------------------------------------;
  228. UINT WINAPI DbgInitialize
  229. (
  230. BOOL fEnable
  231. )
  232. {
  233. UINT uLevel;
  234. uLevel = GetProfileIntA(DEBUG_SECTION, DEBUG_MODULE_NAME, (UINT)-1);
  235. if ((UINT)-1 == uLevel)
  236. {
  237. //
  238. // if the debug key is not present, then force debug output to
  239. // be disabled. this way running a debug version of a component
  240. // on a non-debugging machine will not generate output unless
  241. // the debug key exists.
  242. //
  243. uLevel = 0;
  244. fEnable = FALSE;
  245. }
  246. DbgSetLevel(uLevel);
  247. DbgEnable(fEnable);
  248. return (__guDbgLevel);
  249. } // DbgInitialize()
  250. //--------------------------------------------------------------------------;
  251. //
  252. // void _Assert
  253. //
  254. // Description:
  255. // This routine is called if the ASSERT macro (defined in debug.h)
  256. // tests and expression that evaluates to FALSE. This routine
  257. // displays an "assertion failed" message box allowing the user to
  258. // abort the program, enter the debugger (the "retry" button), or
  259. // ignore the assertion and continue executing. The message box
  260. // displays the file name and line number of the _Assert() call.
  261. //
  262. // Arguments:
  263. // char * szFile: Filename where assertion occurred.
  264. // int iLine: Line number of assertion.
  265. //
  266. //--------------------------------------------------------------------------;
  267. void WINAPI _Assert
  268. (
  269. char * szFile,
  270. int iLine
  271. )
  272. {
  273. static CHAR ach[300]; // debug output (avoid stack overflow)
  274. int id;
  275. wsprintfA(ach, "Assertion failed in file %s, line %d. [Press RETRY to debug.]", (LPSTR)szFile, iLine);
  276. id = MessageBoxA(NULL, ach, "Assertion Failed",
  277. MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE );
  278. switch (id)
  279. {
  280. case IDABORT: // Kill the application.
  281. FatalAppExit(0, TEXT("Good Bye"));
  282. break;
  283. case IDRETRY: // Break into the debugger.
  284. #if DBG
  285. DebugBreak();
  286. #endif
  287. break;
  288. case IDIGNORE: // Ignore assertion, continue executing.
  289. break;
  290. }
  291. } // _Assert
  292. #endif // #ifdef DEBUG