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.

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