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.

151 lines
3.9 KiB

  1. #include "precomp.h"
  2. #include "debug.h"
  3. static DWORD sg_dwTraceID = INVALID_TRACEID;
  4. static char sg_szTraceName[100]; // Used for OutputDebugString
  5. #ifdef DBG
  6. DWORD sg_dwTracingToDebugger = 1; // Enable OutputDebugString for debug version by default
  7. #else
  8. DWORD sg_dwTracingToDebugger = 0; // call OutputDebugString
  9. #endif
  10. DWORD sg_dwDebuggerMask = 0;
  11. inline const char *TraceLevel(DWORD dwDbgLevel)
  12. {
  13. switch(dwDbgLevel)
  14. {
  15. case TL_ERROR: return "ERROR";
  16. case TL_WARN: return "WARN ";
  17. case TL_INFO: return "INFO ";
  18. // case TL_TRACE: return "TRACE";
  19. // case TL_EVENT: return "EVENT";
  20. default: return " ??? ";
  21. }
  22. }
  23. BOOL TRACELogRegister(LPCTSTR szName)
  24. {
  25. HKEY hTracingKey;
  26. char szTracingKey[100];
  27. const char szDebuggerTracingEnableValue[] = "EnableDebuggerTracing";
  28. const char szTracingMaskValue[] = "FileTracingMask";
  29. //
  30. // Register Tracing, this creates the registry entries
  31. // (HKEY_LOCAL_MACHINE\Software\Microsoft\Tracing\"szName")
  32. // (if they did not exist previously)
  33. //
  34. if ((sg_dwTraceID = TraceRegister(szName)) == INVALID_TRACEID)
  35. return FALSE;
  36. #ifdef UNICODE
  37. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  38. #else
  39. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  40. #endif
  41. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  42. szTracingKey,
  43. 0,
  44. KEY_READ,
  45. &hTracingKey) )
  46. {
  47. DWORD dwDataSize = sizeof (DWORD);
  48. DWORD dwDataType;
  49. RegQueryValueExA(hTracingKey,
  50. szDebuggerTracingEnableValue,
  51. 0,
  52. &dwDataType,
  53. (LPBYTE) &sg_dwTracingToDebugger,
  54. &dwDataSize);
  55. RegQueryValueExA(hTracingKey,
  56. szTracingMaskValue,
  57. 0,
  58. &dwDataType,
  59. (LPBYTE) &sg_dwDebuggerMask,
  60. &dwDataSize);
  61. RegCloseKey (hTracingKey);
  62. }
  63. #ifdef UNICODE
  64. wsprintfA(sg_szTraceName, "%ls", szName);
  65. #else
  66. wsprintfA(sg_szTraceName, "%s", szName);
  67. #endif
  68. return TRUE;
  69. }
  70. void TRACELogDeRegister()
  71. {
  72. sg_dwTracingToDebugger = 0;
  73. if (sg_dwTraceID != INVALID_TRACEID)
  74. {
  75. TraceDeregister(sg_dwTraceID);
  76. sg_dwTraceID = INVALID_TRACEID;
  77. }
  78. }
  79. void TRACELogPrint(IN DWORD dwDbgLevel, IN LPCSTR lpszFormat, IN ...)
  80. {
  81. #define MAXDEBUGSTRINGLENGTH 1024
  82. char szTraceBuf[MAXDEBUGSTRINGLENGTH + 1];
  83. va_list arglist;
  84. if ( ( sg_dwTracingToDebugger > 0 ) &&
  85. ( dwDbgLevel & sg_dwDebuggerMask ) )
  86. {
  87. // retrieve local time
  88. SYSTEMTIME SystemTime;
  89. GetLocalTime(&SystemTime);
  90. wsprintfA(szTraceBuf,
  91. "%s:[%02u:%02u:%02u.%03u:] [%s] ",
  92. sg_szTraceName,
  93. SystemTime.wHour,
  94. SystemTime.wMinute,
  95. SystemTime.wSecond,
  96. SystemTime.wMilliseconds,
  97. TraceLevel(dwDbgLevel));
  98. va_list ap;
  99. va_start(ap, lpszFormat);
  100. _vsnprintf(&szTraceBuf[lstrlenA(szTraceBuf)],
  101. MAXDEBUGSTRINGLENGTH - lstrlenA(szTraceBuf),
  102. lpszFormat,
  103. ap
  104. );
  105. lstrcatA (szTraceBuf, "\n");
  106. OutputDebugStringA (szTraceBuf);
  107. va_end(ap);
  108. }
  109. if (sg_dwTraceID != INVALID_TRACEID && ( dwDbgLevel & sg_dwDebuggerMask ))
  110. {
  111. wsprintfA(szTraceBuf, "[%s] %s", TraceLevel(dwDbgLevel), lpszFormat);
  112. va_start(arglist, lpszFormat);
  113. TraceVprintfExA(sg_dwTraceID, dwDbgLevel | TRACE_USE_MSEC, szTraceBuf, arglist);
  114. va_end(arglist);
  115. }
  116. }