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.

229 lines
4.9 KiB

  1. //
  2. // Microsoft Corporation 1998
  3. //
  4. // DEBUG.CPP - Debug library
  5. //
  6. #include "main.h"
  7. #if DBG
  8. //
  9. // Global Variable containing the debugging level.
  10. //
  11. DWORD dwDebugLevel;
  12. //
  13. // Debug strings
  14. //
  15. const TCHAR c_szGPTDemo[] = TEXT("RIGPSNAP(%x): ");
  16. const TCHAR c_szCRLF[] = TEXT("\r\n");
  17. //
  18. // Registry debug information
  19. //
  20. #define DEBUG_REG_LOCATION TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon")
  21. #define DEBUG_KEY_NAME TEXT("RIGPSNAP")
  22. //*************************************************************
  23. //
  24. // InitDebugSupport()
  25. //
  26. // Purpose: Sets the debugging level.
  27. // Also checks the registry for a debugging level.
  28. //
  29. // Parameters: None
  30. //
  31. // Return: void
  32. //
  33. // Comments:
  34. //
  35. //
  36. // History: Date Author Comment
  37. // 5/25/95 ericflo Created
  38. //
  39. //*************************************************************
  40. void InitDebugSupport(void)
  41. {
  42. LONG lResult;
  43. HKEY hKey;
  44. DWORD dwType, dwSize;
  45. //
  46. // Initialize the debug level to normal
  47. //
  48. dwDebugLevel = DL_NORMAL;
  49. //
  50. // Check the registry
  51. //
  52. lResult = RegOpenKey (HKEY_LOCAL_MACHINE, DEBUG_REG_LOCATION,
  53. &hKey);
  54. if (lResult == ERROR_SUCCESS) {
  55. dwSize = sizeof(dwDebugLevel);
  56. RegQueryValueEx(hKey, DEBUG_KEY_NAME, NULL, &dwType,
  57. (LPBYTE)&dwDebugLevel, &dwSize);
  58. RegCloseKey(hKey);
  59. }
  60. }
  61. //*************************************************************
  62. //
  63. // DebugMsg()
  64. //
  65. // Purpose: Displays debug messages based on the debug level
  66. // and type of debug message.
  67. //
  68. // Parameters: mask - debug message type
  69. // pszMsg - debug message
  70. // ... - variable number of parameters
  71. //
  72. // Return: void
  73. //
  74. //
  75. // Comments:
  76. //
  77. //
  78. // History: Date Author Comment
  79. // 5/25/95 ericflo Created
  80. //
  81. //*************************************************************
  82. void _DebugMsg(UINT mask, LPCTSTR pszMsg, ...)
  83. {
  84. BOOL bOutput;
  85. TCHAR szDebugTitle[30];
  86. TCHAR szDebugBuffer[2*MAX_PATH+40];
  87. va_list marker;
  88. DWORD dwErrCode;
  89. //
  90. // Save the last error code (so the debug output doesn't change it).
  91. //
  92. dwErrCode = GetLastError();
  93. //
  94. // Detemerine the correct amount of debug output
  95. //
  96. switch (LOWORD(dwDebugLevel)) {
  97. case DL_VERBOSE:
  98. bOutput = TRUE;
  99. break;
  100. case DL_NORMAL:
  101. //
  102. // Normal debug output. Don't
  103. // display verbose stuff, but
  104. // do display warnings/asserts.
  105. //
  106. if (mask != DM_VERBOSE) {
  107. bOutput = TRUE;
  108. } else {
  109. bOutput = FALSE;
  110. }
  111. break;
  112. case DL_NONE:
  113. default:
  114. //
  115. // Only display asserts
  116. //
  117. if (mask == DM_ASSERT) {
  118. bOutput = TRUE;
  119. } else {
  120. bOutput = FALSE;
  121. }
  122. break;
  123. }
  124. //
  125. // Display the error message if appropriate
  126. //
  127. if (bOutput) {
  128. wsprintf (szDebugTitle, c_szGPTDemo, GetCurrentProcessId());
  129. OutputDebugString(szDebugTitle);
  130. va_start(marker, pszMsg);
  131. wvsprintf(szDebugBuffer, pszMsg, marker);
  132. OutputDebugString(szDebugBuffer);
  133. OutputDebugString(c_szCRLF);
  134. va_end(marker);
  135. if (dwDebugLevel & DL_LOGFILE) {
  136. HANDLE hFile;
  137. DWORD dwBytesWritten;
  138. hFile = CreateFile(TEXT("c:\\rigpsnap.log"),
  139. GENERIC_WRITE,
  140. FILE_SHARE_READ,
  141. NULL,
  142. OPEN_ALWAYS,
  143. FILE_ATTRIBUTE_NORMAL,
  144. NULL);
  145. if (hFile != INVALID_HANDLE_VALUE) {
  146. if (SetFilePointer (hFile, 0, NULL, FILE_END) != 0xFFFFFFFF) {
  147. WriteFile (hFile, (LPCVOID) szDebugTitle,
  148. lstrlen (szDebugTitle) * sizeof(TCHAR),
  149. &dwBytesWritten,
  150. NULL);
  151. WriteFile (hFile, (LPCVOID) szDebugBuffer,
  152. lstrlen (szDebugBuffer) * sizeof(TCHAR),
  153. &dwBytesWritten,
  154. NULL);
  155. WriteFile (hFile, (LPCVOID) c_szCRLF,
  156. lstrlen (c_szCRLF) * sizeof(TCHAR),
  157. &dwBytesWritten,
  158. NULL);
  159. }
  160. CloseHandle (hFile);
  161. }
  162. }
  163. }
  164. //
  165. // Restore the last error code
  166. //
  167. SetLastError(dwErrCode);
  168. //
  169. // Break to the debugger if appropriate
  170. //
  171. if (mask == DM_ASSERT) {
  172. DebugBreak();
  173. }
  174. }
  175. #endif // DBG