Leaked source code of windows server 2003
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.

162 lines
4.3 KiB

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include "tracelog.h"
  4. static DWORD sg_dwTraceID = INVALID_TRACEID;
  5. static char sg_szTraceName[100]; // Used for OutputDebugString
  6. #ifdef DBG
  7. DWORD sg_dwTracingToDebugger = 1; // Enable OutputDebugString for debug version by default
  8. #else
  9. DWORD sg_dwTracingToDebugger = 0;
  10. #endif
  11. DWORD sg_dwDebuggerMask = 0;
  12. inline const char *TraceLevel(DWORD dwDbgLevel)
  13. {
  14. switch(dwDbgLevel)
  15. {
  16. case TL_ERROR: return "ERROR";
  17. case TL_WARN: return "WARN ";
  18. case TL_INFO: return "INFO ";
  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. sg_dwTracingToDebugger = 0;
  29. sg_dwTraceID = TraceRegister(szName);
  30. TraceDeregister(sg_dwTraceID);
  31. #ifdef UNICODE
  32. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  33. #else
  34. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  35. #endif
  36. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  37. szTracingKey,
  38. 0,
  39. KEY_READ|KEY_SET_VALUE,
  40. &hTracingKey) )
  41. {
  42. DWORD dwDataSize = sizeof (DWORD);
  43. DWORD dwDataType;
  44. #ifdef DBG
  45. //
  46. // Always Enable file tracing for debug version
  47. //
  48. DWORD dwEnableFileTracing = 1;
  49. RegSetValueExA(hTracingKey,
  50. "EnableFileTracing",
  51. 0,
  52. REG_DWORD,
  53. (LPBYTE) &dwEnableFileTracing,
  54. sizeof (DWORD));
  55. #endif
  56. dwDataSize = sizeof (DWORD);
  57. RegQueryValueExA(hTracingKey,
  58. szDebuggerTracingEnableValue,
  59. 0,
  60. &dwDataType,
  61. (LPBYTE) &sg_dwTracingToDebugger,
  62. &dwDataSize);
  63. dwDataSize = sizeof (DWORD);
  64. RegQueryValueExA(hTracingKey,
  65. szTracingMaskValue,
  66. 0,
  67. &dwDataType,
  68. (LPBYTE) &sg_dwDebuggerMask,
  69. &dwDataSize);
  70. RegCloseKey (hTracingKey);
  71. }
  72. #ifdef UNICODE
  73. wsprintfA(sg_szTraceName, "%ls", szName);
  74. #else
  75. wsprintfA(sg_szTraceName, "%s", szName);
  76. #endif
  77. sg_dwTraceID = TraceRegister(szName);
  78. return (sg_dwTraceID != INVALID_TRACEID);
  79. }
  80. void TRACELogDeRegister()
  81. {
  82. sg_dwTracingToDebugger = 0;
  83. if (sg_dwTraceID != INVALID_TRACEID)
  84. {
  85. TraceDeregister(sg_dwTraceID);
  86. sg_dwTraceID = INVALID_TRACEID;
  87. }
  88. }
  89. void TRACELogPrint(IN DWORD dwDbgLevel, IN LPCSTR lpszFormat, IN ...)
  90. {
  91. #define MAXDEBUGSTRINGLENGTH 1024
  92. char szTraceBuf[MAXDEBUGSTRINGLENGTH + 1];
  93. va_list arglist;
  94. if ( ( sg_dwTracingToDebugger > 0 ) &&
  95. ( dwDbgLevel & sg_dwDebuggerMask ) )
  96. {
  97. // retrieve local time
  98. SYSTEMTIME SystemTime;
  99. GetLocalTime(&SystemTime);
  100. wsprintfA(szTraceBuf,
  101. "%s:[%02u:%02u:%02u.%03u:] [%s] ",
  102. sg_szTraceName,
  103. SystemTime.wHour,
  104. SystemTime.wMinute,
  105. SystemTime.wSecond,
  106. SystemTime.wMilliseconds,
  107. TraceLevel(dwDbgLevel));
  108. va_list ap;
  109. va_start(ap, lpszFormat);
  110. _vsnprintf(&szTraceBuf[lstrlenA(szTraceBuf)],
  111. MAXDEBUGSTRINGLENGTH - lstrlenA(szTraceBuf),
  112. lpszFormat,
  113. ap
  114. );
  115. lstrcatA (szTraceBuf, "\n");
  116. OutputDebugStringA (szTraceBuf);
  117. va_end(ap);
  118. }
  119. if (sg_dwTraceID != INVALID_TRACEID && ( dwDbgLevel & sg_dwDebuggerMask ))
  120. {
  121. wsprintfA(szTraceBuf, "[%s] %s", TraceLevel(dwDbgLevel), lpszFormat);
  122. va_start(arglist, lpszFormat);
  123. TraceVprintfExA(sg_dwTraceID, dwDbgLevel | TRACE_USE_MSEC, szTraceBuf, arglist);
  124. va_end(arglist);
  125. }
  126. }