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.2 KiB

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