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.

179 lines
4.6 KiB

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include "utils.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #ifdef TRACELOG
  8. DWORD sg_dwTraceID = INVALID_TRACEID;
  9. char sg_szTraceName[100]; // saves name of dll
  10. DWORD sg_dwTracingToDebugger = 0;
  11. DWORD sg_dwTracingToConsole = 0;
  12. DWORD sg_dwTracingToFile = 0;
  13. DWORD sg_dwDebuggerMask = 0;
  14. BOOL TRACELogRegister(LPCTSTR szName)
  15. {
  16. HKEY hTracingKey;
  17. char szTracingKey[100];
  18. const char szDebuggerTracingEnableValue[] = "EnableDebuggerTracing";
  19. const char szConsoleTracingEnableValue[] = "EnableConsoleTracing";
  20. const char szFileTracingEnableValue[] = "EnableFileTracing";
  21. const char szTracingMaskValue[] = "ConsoleTracingMask";
  22. sg_dwTracingToDebugger = 0;
  23. sg_dwTracingToConsole = 0;
  24. sg_dwTracingToFile = 0;
  25. #ifdef UNICODE
  26. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%ls", szName);
  27. #else
  28. wsprintfA(szTracingKey, "Software\\Microsoft\\Tracing\\%s", szName);
  29. #endif
  30. if ( ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  31. szTracingKey,
  32. 0,
  33. KEY_READ,
  34. &hTracingKey) )
  35. {
  36. DWORD dwDataSize = sizeof (DWORD);
  37. DWORD dwDataType;
  38. RegQueryValueExA(hTracingKey,
  39. szDebuggerTracingEnableValue,
  40. 0,
  41. &dwDataType,
  42. (LPBYTE) &sg_dwTracingToDebugger,
  43. &dwDataSize);
  44. RegQueryValueExA(hTracingKey,
  45. szConsoleTracingEnableValue,
  46. 0,
  47. &dwDataType,
  48. (LPBYTE) &sg_dwTracingToConsole,
  49. &dwDataSize);
  50. RegQueryValueExA(hTracingKey,
  51. szFileTracingEnableValue,
  52. 0,
  53. &dwDataType,
  54. (LPBYTE) &sg_dwTracingToFile,
  55. &dwDataSize);
  56. RegQueryValueExA(hTracingKey,
  57. szTracingMaskValue,
  58. 0,
  59. &dwDataType,
  60. (LPBYTE) &sg_dwDebuggerMask,
  61. &dwDataSize);
  62. RegCloseKey (hTracingKey);
  63. }
  64. #ifdef UNICODE
  65. wsprintfA(sg_szTraceName, "%ls", szName);
  66. #else
  67. wsprintfA(sg_szTraceName, "%s", szName);
  68. #endif
  69. sg_dwTraceID = TraceRegister(szName);
  70. return (sg_dwTraceID != INVALID_TRACEID);
  71. }
  72. void TRACELogDeRegister()
  73. {
  74. sg_dwTracingToDebugger = 0;
  75. sg_dwTracingToConsole = 0;
  76. sg_dwTracingToFile = 0;
  77. if (sg_dwTraceID != INVALID_TRACEID)
  78. {
  79. TraceDeregister(sg_dwTraceID);
  80. sg_dwTraceID = INVALID_TRACEID;
  81. }
  82. }
  83. void TRACELogPrint(IN DWORD dwDbgLevel, IN LPCSTR lpszFormat, IN ...)
  84. {
  85. char szTraceBuf[MAXDEBUGSTRINGLENGTH + 1];
  86. va_list arglist;
  87. if ( ( sg_dwTracingToDebugger > 0 ) &&
  88. ( 0 != ( dwDbgLevel & sg_dwDebuggerMask ) ) )
  89. {
  90. // retrieve local time
  91. SYSTEMTIME SystemTime;
  92. GetLocalTime(&SystemTime);
  93. wsprintfA(szTraceBuf,
  94. "%s:[%02u:%02u:%02u.%03u,tid=%x:] [%s] ",
  95. sg_szTraceName,
  96. SystemTime.wHour,
  97. SystemTime.wMinute,
  98. SystemTime.wSecond,
  99. SystemTime.wMilliseconds,
  100. GetCurrentThreadId(),
  101. TraceLevel(dwDbgLevel));
  102. va_list ap;
  103. va_start(ap, lpszFormat);
  104. _vsnprintf(&szTraceBuf[lstrlenA(szTraceBuf)],
  105. MAXDEBUGSTRINGLENGTH - lstrlenA(szTraceBuf),
  106. lpszFormat,
  107. ap
  108. );
  109. lstrcatA (szTraceBuf, "\n");
  110. OutputDebugStringA (szTraceBuf);
  111. va_end(ap);
  112. }
  113. if (sg_dwTraceID != INVALID_TRACEID)
  114. {
  115. wsprintfA(szTraceBuf, "[%s] %s", TraceLevel(dwDbgLevel), lpszFormat);
  116. va_start(arglist, lpszFormat);
  117. TraceVprintfExA(sg_dwTraceID, dwDbgLevel | TRACE_USE_MSEC, szTraceBuf, arglist);
  118. va_end(arglist);
  119. }
  120. }
  121. char *TraceLevel(DWORD dwDbgLevel)
  122. {
  123. switch(dwDbgLevel)
  124. {
  125. case TL_ERROR: return "ERROR";
  126. case TL_WARN: return "WARN ";
  127. case TL_INFO: return "INFO ";
  128. case TL_TRACE: return "TRACE";
  129. case TL_EVENT: return "EVENT";
  130. default: return " ??? ";
  131. }
  132. }
  133. #endif // TRACELOG
  134. #ifdef __cplusplus
  135. }
  136. #endif