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.

243 lines
5.5 KiB

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