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.

189 lines
4.6 KiB

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