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.

264 lines
6.3 KiB

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