Leaked source code of windows server 2003
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.

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