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.

193 lines
4.4 KiB

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