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.

233 lines
5.0 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include <rtutils.h>
  4. #include "tchar.h"
  5. #include "debug.h"
  6. //#include "ipnat.h"
  7. //****************************************************************************
  8. // Global Parameters
  9. //****************************************************************************
  10. TCHAR g_szDebugKey[] = _T("SOFTWARE\\Microsoft\\Tracing\\Beacon\\Debug");
  11. DEBUG_MODULE_INFO g_DebugInfo[] = {
  12. {TM_DEFAULT, TL_CRIT, _T("<default> "), _T("DebugLevel")}, //
  13. {TM_STATIC, TL_TRACE, _T("STATIC "), _T("StaticDebugLevel")}, //
  14. {TM_INFO, TL_TRACE, _T("INFO "), _T("InfoDebugLevel")}, //
  15. {TM_DYNAMIC, TL_TRACE, _T("DYN "), _T("DynamicDebugLevel")}, //
  16. {TB_FILE, TL_NONE, _T("FILE "), _T("LogToFile")}, //
  17. };
  18. WCHAR g_szModule[] = SZ_MODULE;
  19. ULONG g_uTraceId = INVALID_TRACEID;
  20. BOOLEAN bEnabled = FALSE;
  21. void
  22. DestroyDebugger(VOID)
  23. {
  24. if ( g_uTraceId != INVALID_TRACEID )
  25. {
  26. TraceDeregister(g_uTraceId);
  27. g_uTraceId = INVALID_TRACEID;
  28. }
  29. }
  30. void
  31. InitDebugger()
  32. {
  33. HKEY hkey;
  34. DWORD dwType, cb;
  35. DWORD dwLevel;
  36. int iModule;
  37. int nModules;
  38. //
  39. // Open the registry key that contains the debug configuration info
  40. //
  41. if (RegOpenKeyEx((HKEY) HKEY_LOCAL_MACHINE,
  42. g_szDebugKey,
  43. 0,
  44. KEY_READ,
  45. &hkey) == ERROR_SUCCESS)
  46. {
  47. cb = sizeof(dwLevel);
  48. //
  49. // Enable Debugging
  50. bEnabled = TRUE;
  51. //
  52. // Initialize all the modules to the base value or their custom value
  53. //
  54. nModules = (sizeof(g_DebugInfo)/sizeof(DEBUG_MODULE_INFO));
  55. for (iModule=0; iModule < nModules; iModule++)
  56. {
  57. //
  58. // Open each custom debug level if present
  59. //
  60. if ((RegQueryValueEx(hkey,
  61. g_DebugInfo[iModule].szDebugKey,
  62. NULL,
  63. &dwType,
  64. (PUCHAR)
  65. &dwLevel,
  66. &cb) == ERROR_SUCCESS) && (dwType == REG_DWORD))
  67. {
  68. g_DebugInfo[iModule].dwLevel = dwLevel;
  69. }
  70. else
  71. {
  72. g_DebugInfo[iModule].dwLevel = g_DebugInfo[TM_DEFAULT].dwLevel;
  73. }
  74. if( (TB_FILE == iModule) &&
  75. (1 == dwLevel))
  76. {
  77. // Init the Trace Manager
  78. g_uTraceId = TraceRegister(g_szModule);
  79. }
  80. }
  81. RegCloseKey(hkey);
  82. }
  83. else
  84. {
  85. // Debug Key Doesn't exist
  86. }
  87. return;
  88. }
  89. //
  90. // use _vsnwprintf instead
  91. //
  92. void
  93. DbgPrintEx(ULONG Module, ULONG ErrorLevel, LPOLESTR pszMsg, ...)
  94. {
  95. va_list VaList;
  96. WCHAR msg[BUF_SIZE];
  97. int len = 0;
  98. if ( (bEnabled is TRUE) &&
  99. (ErrorLevel <= g_DebugInfo[Module].dwLevel ))
  100. {
  101. len = swprintf(msg, L"%s-", g_szModule);
  102. wcscat(msg, g_DebugInfo[Module].szModuleName);
  103. len += wcslen(g_DebugInfo[Module].szModuleName);
  104. _vsnwprintf(&msg[len],
  105. BUF_SIZE,
  106. pszMsg,
  107. (va_list)(&pszMsg + 1));
  108. wcscat(msg, L"\n");
  109. if ( g_uTraceId is INVALID_TRACEID )
  110. {
  111. OutputDebugString(msg);
  112. }
  113. else
  114. {
  115. TracePrintfExW(g_uTraceId,
  116. TRACE_FLAG_NEUTR,
  117. L"%s",
  118. msg);
  119. }
  120. }
  121. }
  122. void
  123. DEBUG_DO_NOTHING(ULONG Module, ULONG ErrorLevel, LPOLESTR pszMsg, ...)
  124. {
  125. }
  126. //
  127. // UTILITY Functions
  128. //
  129. LPOLESTR
  130. AppendAndAllocateWString(
  131. LPOLESTR oldString,
  132. LPOLESTR newString
  133. )
  134. {
  135. LPOLESTR retString = NULL;
  136. ULONG retStringSize = 0, oldStringSize = 0, newStringSize = 0;
  137. if ( oldString != NULL )
  138. {
  139. oldStringSize = wcslen( oldString );
  140. }
  141. if ( newString != NULL )
  142. {
  143. newStringSize = wcslen(newString);
  144. }
  145. retStringSize = oldStringSize + newStringSize + 1;
  146. retString = (LPOLESTR) CoTaskMemAlloc( retStringSize * sizeof(OLECHAR) );
  147. if(retString != NULL)
  148. {
  149. ZeroMemory(retString, retStringSize * sizeof(OLECHAR));
  150. if(oldString)
  151. {
  152. wcscat(retString, oldString);
  153. CoTaskMemFree(oldString);
  154. oldString = NULL;
  155. }
  156. if(newString)
  157. {
  158. wcscat(retString, oldString);
  159. }
  160. return retString;
  161. }
  162. return oldString;
  163. }