Source code of Windows XP (NT5)
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.

337 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. mspdebug.cpp
  5. Abstract:
  6. This module contains the debugging support for the MSPs.
  7. --*/
  8. #include "precomp.h"
  9. #pragma hdrstop
  10. #include <stdio.h>
  11. //
  12. // no need to build this code if MSPLOG is not defined
  13. //
  14. #ifdef MSPLOG
  15. #define MAXDEBUGSTRINGLENGTH 512
  16. static DWORD sg_dwTraceID = INVALID_TRACEID;
  17. static char sg_szTraceName[100]; // saves name of dll
  18. static DWORD sg_dwTracingToDebugger = 0;
  19. static DWORD sg_dwTracingToConsole = 0;
  20. static DWORD sg_dwTracingToFile = 0;
  21. static DWORD sg_dwDebuggerMask = 0;
  22. //
  23. // this flag indicates whether any tracing needs to be done at all
  24. //
  25. BOOL g_bMSPBaseTracingOn = FALSE;
  26. BOOL NTAPI MSPLogRegister(LPCTSTR szName)
  27. {
  28. HKEY hTracingKey;
  29. char szTracingKey[100];
  30. const char szDebuggerTracingEnableValue[] = "EnableDebuggerTracing";
  31. const char szConsoleTracingEnableValue[] = "EnableConsoleTracing";
  32. const char szFileTracingEnableValue[] = "EnableFileTracing";
  33. const char szTracingMaskValue[] = "ConsoleTracingMask";
  34. sg_dwTracingToDebugger = 0;
  35. sg_dwTracingToConsole = 0;
  36. sg_dwTracingToFile = 0;
  37. #ifdef UNICODE
  38. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  39. #else
  40. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  41. #endif
  42. _ASSERTE(sg_dwTraceID == INVALID_TRACEID);
  43. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  44. szTracingKey,
  45. 0,
  46. KEY_READ,
  47. &hTracingKey) )
  48. {
  49. DWORD dwDataSize = sizeof (DWORD);
  50. DWORD dwDataType;
  51. RegQueryValueExA(hTracingKey,
  52. szDebuggerTracingEnableValue,
  53. 0,
  54. &dwDataType,
  55. (LPBYTE) &sg_dwTracingToDebugger,
  56. &dwDataSize);
  57. RegQueryValueExA(hTracingKey,
  58. szConsoleTracingEnableValue,
  59. 0,
  60. &dwDataType,
  61. (LPBYTE) &sg_dwTracingToConsole,
  62. &dwDataSize);
  63. RegQueryValueExA(hTracingKey,
  64. szFileTracingEnableValue,
  65. 0,
  66. &dwDataType,
  67. (LPBYTE) &sg_dwTracingToFile,
  68. &dwDataSize);
  69. RegQueryValueExA(hTracingKey,
  70. szTracingMaskValue,
  71. 0,
  72. &dwDataType,
  73. (LPBYTE) &sg_dwDebuggerMask,
  74. &dwDataSize);
  75. RegCloseKey (hTracingKey);
  76. }
  77. else
  78. {
  79. //
  80. // the key could not be opened. in case the key does not exist,
  81. // register with rtutils so that the reg keys get created
  82. //
  83. #ifdef UNICODE
  84. wsprintfA(sg_szTraceName, "%ls", szName);
  85. #else
  86. wsprintfA(sg_szTraceName, "%s", szName);
  87. #endif
  88. //
  89. // tracing should not have been initialized
  90. //
  91. sg_dwTraceID = TraceRegister(szName);
  92. if (sg_dwTraceID != INVALID_TRACEID)
  93. {
  94. g_bMSPBaseTracingOn = TRUE;
  95. }
  96. return TRUE;
  97. }
  98. //
  99. // are we asked to do any tracing at all?
  100. //
  101. if ( (0 != sg_dwTracingToDebugger) ||
  102. (0 != sg_dwTracingToConsole ) ||
  103. (0 != sg_dwTracingToFile ) )
  104. {
  105. //
  106. // see if we need to register with rtutils
  107. //
  108. if ( (0 != sg_dwTracingToConsole ) || (0 != sg_dwTracingToFile) )
  109. {
  110. //
  111. // rtutils tracing is enabled. register with rtutils
  112. //
  113. #ifdef UNICODE
  114. wsprintfA(sg_szTraceName, "%ls", szName);
  115. #else
  116. wsprintfA(sg_szTraceName, "%s", szName);
  117. #endif
  118. //
  119. // tracing should not have been initialized
  120. //
  121. _ASSERTE(sg_dwTraceID == INVALID_TRACEID);
  122. //
  123. // register
  124. //
  125. sg_dwTraceID = TraceRegister(szName);
  126. }
  127. //
  128. // if debugger tracing, or succeeded registering with rtutils, we are all set
  129. //
  130. if ( (0 != sg_dwTracingToDebugger) || (sg_dwTraceID != INVALID_TRACEID) )
  131. {
  132. //
  133. // some tracing is enabled. set the global flag
  134. //
  135. g_bMSPBaseTracingOn = TRUE;
  136. return TRUE;
  137. }
  138. else
  139. {
  140. //
  141. // registration did not go through and debugger logging is off
  142. //
  143. return FALSE;
  144. }
  145. }
  146. //
  147. // logging is not enabled. nothing to do
  148. //
  149. return TRUE;
  150. }
  151. void NTAPI MSPLogDeRegister()
  152. {
  153. if (g_bMSPBaseTracingOn)
  154. {
  155. sg_dwTracingToDebugger = 0;
  156. sg_dwTracingToConsole = 0;
  157. sg_dwTracingToFile = 0;
  158. //
  159. // if we registered tracing, unregister now
  160. //
  161. if ( sg_dwTraceID != INVALID_TRACEID )
  162. {
  163. TraceDeregister(sg_dwTraceID);
  164. sg_dwTraceID = INVALID_TRACEID;
  165. }
  166. }
  167. }
  168. void NTAPI LogPrint(IN DWORD dwDbgLevel, IN LPCSTR lpszFormat, IN ...)
  169. /*++
  170. Routine Description:
  171. Formats the incoming debug message & calls TraceVprintfEx to print it.
  172. Arguments:
  173. dwDbgLevel - The type of the message.
  174. lpszFormat - printf-style format string, followed by appropriate
  175. list of arguments
  176. Return Value:
  177. --*/
  178. {
  179. static char * message[] =
  180. {
  181. "ERROR",
  182. "WARNING",
  183. "INFO",
  184. "TRACE",
  185. "EVENT",
  186. "INVALID TRACE LEVEL"
  187. };
  188. char szTraceBuf[MAXDEBUGSTRINGLENGTH + 1];
  189. DWORD dwIndex;
  190. if ( ( sg_dwTracingToDebugger > 0 ) &&
  191. ( 0 != ( dwDbgLevel & sg_dwDebuggerMask ) ) )
  192. {
  193. switch(dwDbgLevel)
  194. {
  195. case MSP_ERROR: dwIndex = 0; break;
  196. case MSP_WARN: dwIndex = 1; break;
  197. case MSP_INFO: dwIndex = 2; break;
  198. case MSP_TRACE: dwIndex = 3; break;
  199. case MSP_EVENT: dwIndex = 4; break;
  200. default: dwIndex = 5; break;
  201. }
  202. // retrieve local time
  203. SYSTEMTIME SystemTime;
  204. GetLocalTime(&SystemTime);
  205. wsprintfA(szTraceBuf,
  206. "%s:[%02u:%02u:%02u.%03u,tid=%x:]%s: ",
  207. sg_szTraceName,
  208. SystemTime.wHour,
  209. SystemTime.wMinute,
  210. SystemTime.wSecond,
  211. SystemTime.wMilliseconds,
  212. GetCurrentThreadId(),
  213. message[dwIndex]);
  214. va_list ap;
  215. va_start(ap, lpszFormat);
  216. _vsnprintf(&szTraceBuf[lstrlenA(szTraceBuf)],
  217. MAXDEBUGSTRINGLENGTH - lstrlenA(szTraceBuf),
  218. lpszFormat,
  219. ap
  220. );
  221. lstrcatA (szTraceBuf, "\n");
  222. OutputDebugStringA (szTraceBuf);
  223. va_end(ap);
  224. }
  225. if (sg_dwTraceID != INVALID_TRACEID)
  226. {
  227. if ( ( sg_dwTracingToConsole > 0 ) || ( sg_dwTracingToFile > 0 ) )
  228. {
  229. switch(dwDbgLevel)
  230. {
  231. case MSP_ERROR: dwIndex = 0; break;
  232. case MSP_WARN: dwIndex = 1; break;
  233. case MSP_INFO: dwIndex = 2; break;
  234. case MSP_TRACE: dwIndex = 3; break;
  235. case MSP_EVENT: dwIndex = 4; break;
  236. default: dwIndex = 5; break;
  237. }
  238. wsprintfA(szTraceBuf, "[%s] %s", message[dwIndex], lpszFormat);
  239. va_list arglist;
  240. va_start(arglist, lpszFormat);
  241. TraceVprintfExA(sg_dwTraceID, dwDbgLevel | TRACE_USE_MSEC, szTraceBuf, arglist);
  242. va_end(arglist);
  243. }
  244. }
  245. }
  246. #endif // MSPLOG
  247. // eof