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.

235 lines
5.1 KiB

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