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.

191 lines
4.5 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. trace.cpp
  5. Abstract:
  6. This module contains the debugging support for the MSPs.
  7. --*/
  8. #if defined(DXMRTPTRACE)
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include "trace.h"
  12. #define MAXDEBUGSTRINGLENGTH 512
  13. static DWORD sg_dwTraceID = INVALID_TRACEID;
  14. static char sg_szTraceName[100]; // saves name of dll
  15. static DWORD sg_dwEnableDebuggerTracing = 0;
  16. static DWORD sg_dwConsoleTracingMask = 0;
  17. BOOL NTAPI DxmTraceRegister(LPCTSTR szName)
  18. {
  19. HKEY hTracingKey;
  20. char szTracingKey[100];
  21. const char szTracingEnableValue[] = "EnableDebuggerTracing";
  22. const char szTracingMaskValue[] = "ConsoleTracingMask";
  23. sg_dwEnableDebuggerTracing = 0;
  24. #ifdef UNICODE
  25. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  26. #else
  27. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  28. #endif
  29. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  30. szTracingKey,
  31. 0,
  32. KEY_READ,
  33. &hTracingKey) )
  34. {
  35. DWORD dwDataSize = sizeof (DWORD);
  36. DWORD dwDataType;
  37. RegQueryValueExA(hTracingKey,
  38. szTracingEnableValue,
  39. 0,
  40. &dwDataType,
  41. (LPBYTE) &sg_dwEnableDebuggerTracing,
  42. &dwDataSize);
  43. RegQueryValueExA(hTracingKey,
  44. szTracingMaskValue,
  45. 0,
  46. &dwDataType,
  47. (LPBYTE) &sg_dwConsoleTracingMask,
  48. &dwDataSize);
  49. RegCloseKey (hTracingKey);
  50. }
  51. #ifdef UNICODE
  52. wsprintfA(sg_szTraceName, "%ls", szName);
  53. #else
  54. wsprintfA(sg_szTraceName, "%s", szName);
  55. #endif
  56. sg_dwTraceID = TraceRegister(szName);
  57. return (sg_dwTraceID != INVALID_TRACEID);
  58. }
  59. void NTAPI DxmTraceDeRegister()
  60. {
  61. sg_dwEnableDebuggerTracing = 0;
  62. if (sg_dwTraceID != INVALID_TRACEID)
  63. {
  64. TraceDeregister(sg_dwTraceID);
  65. sg_dwTraceID = INVALID_TRACEID;
  66. }
  67. }
  68. char *trace_message[] = {
  69. "TRACE",
  70. "ERROR",
  71. "WARN",
  72. "INFO",
  73. "INVALID TRACE LEVEL"
  74. };
  75. void NTAPI DxmTracePrint(IN DWORD dwDbgLevel,
  76. IN DWORD dwDbgType,
  77. IN LPCSTR lpszFormat,
  78. IN ...)
  79. /*++
  80. Routine Description:
  81. Formats the incoming debug message & calls TraceVprintfEx to print it.
  82. Arguments:
  83. dwDbgLevel - The type of the message.
  84. lpszFormat - printf-style format string, followed by appropriate
  85. list of arguments
  86. Return Value:
  87. --*/
  88. {
  89. char szTraceBuf[MAXDEBUGSTRINGLENGTH + 1];
  90. DWORD dwIndex, dwLevel;
  91. if (dwDbgType > 4)
  92. dwDbgType = 4;
  93. dwLevel = dwDbgLevel & 0x000f0000;
  94. switch(dwDbgLevel) {
  95. case TRACE_TRACE: dwIndex = 0; break;
  96. case TRACE_ERROR: dwIndex = 1; break;
  97. case TRACE_WARN: dwIndex = 2; break;
  98. case TRACE_INFO: dwIndex = 3; break;
  99. default: dwIndex = 4;
  100. }
  101. // based on the 4 basic levels, use Type to build up to 4 levels
  102. // including the basic one
  103. dwDbgLevel = (dwLevel << (dwDbgType-1)) | (dwDbgLevel & 0xffff);
  104. if ( ( sg_dwEnableDebuggerTracing > 0 ) &&
  105. ( 0 != ( dwDbgLevel & sg_dwConsoleTracingMask ) ) )
  106. {
  107. // retrieve local time
  108. SYSTEMTIME SystemTime;
  109. GetLocalTime(&SystemTime);
  110. wsprintfA(szTraceBuf,
  111. "%s:[%02u:%02u:%02u.%03u,tid=%x:]%s: ",
  112. sg_szTraceName,
  113. SystemTime.wHour,
  114. SystemTime.wMinute,
  115. SystemTime.wSecond,
  116. SystemTime.wMilliseconds,
  117. GetCurrentThreadId(),
  118. trace_message[dwIndex]);
  119. va_list ap;
  120. va_start(ap, lpszFormat);
  121. _vsnprintf(&szTraceBuf[lstrlenA(szTraceBuf)],
  122. MAXDEBUGSTRINGLENGTH - lstrlenA(szTraceBuf),
  123. lpszFormat,
  124. ap
  125. );
  126. lstrcatA (szTraceBuf, "\n");
  127. OutputDebugStringA (szTraceBuf);
  128. va_end(ap);
  129. }
  130. if (sg_dwTraceID != INVALID_TRACEID)
  131. {
  132. wsprintfA(szTraceBuf, "[%s] %s", trace_message[dwIndex], lpszFormat);
  133. va_list arglist;
  134. va_start(arglist, lpszFormat);
  135. TraceVprintfExA(sg_dwTraceID, dwDbgLevel, szTraceBuf, arglist);
  136. va_end(arglist);
  137. }
  138. }
  139. #endif // DXMRTPTRACE
  140. // eof