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.

234 lines
5.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. #if DBG
  12. //
  13. // Global Variable containing the debugging level.
  14. //
  15. DWORD dwDebugLevel;
  16. //
  17. // Debug strings
  18. //
  19. const TCHAR c_szGPTDemo[] = TEXT("GPTDEMO(%x): ");
  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("GPTDemoDebugLevel")
  26. //*************************************************************
  27. //
  28. // InitDebugSupport()
  29. //
  30. // Purpose: Sets the debugging level.
  31. // Also checks the registry for a debugging level.
  32. //
  33. // Parameters: None
  34. //
  35. // Return: void
  36. //
  37. // Comments:
  38. //
  39. //
  40. // History: Date Author Comment
  41. // 5/25/95 ericflo Created
  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[2*MAX_PATH+40];
  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. wsprintf (szDebugTitle, c_szGPTDemo, GetCurrentProcessId());
  133. OutputDebugString(szDebugTitle);
  134. va_start(marker, pszMsg);
  135. wvsprintf(szDebugBuffer, pszMsg, marker);
  136. OutputDebugString(szDebugBuffer);
  137. OutputDebugString(c_szCRLF);
  138. va_end(marker);
  139. if (dwDebugLevel & DL_LOGFILE) {
  140. HANDLE hFile;
  141. DWORD dwBytesWritten;
  142. hFile = CreateFile(TEXT("c:\\GPTDemo.log"),
  143. GENERIC_WRITE,
  144. FILE_SHARE_READ,
  145. NULL,
  146. OPEN_ALWAYS,
  147. FILE_ATTRIBUTE_NORMAL,
  148. NULL);
  149. if (hFile != INVALID_HANDLE_VALUE) {
  150. if (SetFilePointer (hFile, 0, NULL, FILE_END) != 0xFFFFFFFF) {
  151. WriteFile (hFile, (LPCVOID) szDebugTitle,
  152. lstrlen (szDebugTitle) * sizeof(TCHAR),
  153. &dwBytesWritten,
  154. NULL);
  155. WriteFile (hFile, (LPCVOID) szDebugBuffer,
  156. lstrlen (szDebugBuffer) * sizeof(TCHAR),
  157. &dwBytesWritten,
  158. NULL);
  159. WriteFile (hFile, (LPCVOID) c_szCRLF,
  160. lstrlen (c_szCRLF) * sizeof(TCHAR),
  161. &dwBytesWritten,
  162. NULL);
  163. }
  164. CloseHandle (hFile);
  165. }
  166. }
  167. }
  168. //
  169. // Restore the last error code
  170. //
  171. SetLastError(dwErrCode);
  172. //
  173. // Break to the debugger if appropriate
  174. //
  175. if (mask == DM_ASSERT) {
  176. DebugBreak();
  177. }
  178. }
  179. #endif // DBG