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.

148 lines
3.8 KiB

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