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.

183 lines
5.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1999-2000.
  5. //
  6. // File: debug.cpp
  7. //
  8. // Contents: Debugging support
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include "strsafe.h"
  13. #if DBG == 1
  14. #define DEBUG_OUTPUT_NONE 0
  15. #define DEBUG_OUTPUT_ERROR 1
  16. #define DEBUG_OUTPUT_WARNING 2
  17. #define DEBUG_OUTPUT_TRACE 3
  18. #define DEBUGKEY L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AdminDebug\\ACLDiag"
  19. #define DEBUGLEVEL L"debugOutput"
  20. static bool g_fDebugOutputLevelInit = false;
  21. static unsigned long g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  22. static int indentLevel = 0;
  23. //NTRAID#NTBUG9-530206-2002/06/18-ronmart-Use countof for buffers on the stack
  24. #ifndef countof
  25. #define countof(x) (sizeof(x) / sizeof((x)[0]))
  26. #endif // countof
  27. void TRACE (const wchar_t *format, ... )
  28. {
  29. if ( g_ulDebugOutput > DEBUG_OUTPUT_NONE )
  30. {
  31. va_list arglist;
  32. const size_t BUF_LEN = 512;
  33. WCHAR szBuffer[BUF_LEN];
  34. //
  35. // Format the output into a buffer and then print it.
  36. //
  37. wstring strTabs;
  38. for (int nLevel = 0; nLevel < indentLevel; nLevel++)
  39. strTabs += L" ";
  40. OutputDebugStringW (strTabs.c_str ());
  41. va_start(arglist, format);
  42. //NTRAID#NTBUG9-530206-2002/06/18-ronmart-Use countof for buffers on the stack
  43. HRESULT hr = StringCchVPrintf (szBuffer,
  44. countof(szBuffer),
  45. format,
  46. arglist);
  47. if ( SUCCEEDED (hr) )
  48. {
  49. OutputDebugStringW (szBuffer);
  50. }
  51. va_end(arglist);
  52. }
  53. }
  54. void _TRACE (int level, const wchar_t *format, ... )
  55. {
  56. if ( g_ulDebugOutput > DEBUG_OUTPUT_NONE )
  57. {
  58. va_list arglist;
  59. const size_t BUF_LEN = 512;
  60. WCHAR szBuffer[BUF_LEN];
  61. if ( level < 0 )
  62. indentLevel += level;
  63. //
  64. // Format the output into a buffer and then print it.
  65. //
  66. wstring strTabs;
  67. for (int nLevel = 0; nLevel < indentLevel; nLevel++)
  68. strTabs += L" ";
  69. OutputDebugStringW (strTabs.c_str ());
  70. va_start(arglist, format);
  71. //NTRAID#NTBUG9-530206-2002/06/18-ronmart-Use countof for buffers on the stack
  72. HRESULT hr = StringCchVPrintf (szBuffer,
  73. countof(szBuffer),
  74. format,
  75. arglist);
  76. if ( SUCCEEDED (hr) )
  77. {
  78. OutputDebugStringW (szBuffer);
  79. }
  80. va_end(arglist);
  81. if ( level > 0 )
  82. indentLevel += level;
  83. }
  84. }
  85. PCSTR StripDirPrefixA (PCSTR pszPathName)
  86. /*++
  87. Routine Description:
  88. Strip the directory prefix off a filename (ANSI version)
  89. Arguments:
  90. pstrFilename - Pointer to filename string
  91. Return Value:
  92. Pointer to the last component of a filename (without directory prefix)
  93. --*/
  94. {
  95. ASSERT (pszPathName);
  96. if ( !pszPathName )
  97. return 0;
  98. if ( !pszPathName[0] )
  99. return 0;
  100. DWORD dwLen = lstrlenA(pszPathName);
  101. pszPathName += dwLen - 1; // go to the end
  102. while (*pszPathName != '\\' && dwLen--)
  103. {
  104. pszPathName--;
  105. }
  106. return pszPathName + 1;
  107. }
  108. //+----------------------------------------------------------------------------
  109. // Function: CheckDebugOutputLevel
  110. //
  111. // Synopsis: Performs debugging library initialization
  112. // including reading the registry for the desired infolevel
  113. //
  114. //-----------------------------------------------------------------------------
  115. void CheckDebugOutputLevel ()
  116. {
  117. if ( g_fDebugOutputLevelInit )
  118. return;
  119. g_fDebugOutputLevelInit = true;
  120. HKEY hKey = 0;
  121. DWORD dwDisposition = 0;
  122. LONG lResult = ::RegCreateKeyEx (HKEY_LOCAL_MACHINE, // handle of an open key
  123. DEBUGKEY, // address of subkey name
  124. 0, // reserved
  125. L"", // address of class string
  126. REG_OPTION_VOLATILE, // special options flag
  127. KEY_ALL_ACCESS, // desired security access - required to create new key
  128. NULL, // address of key security structure
  129. &hKey, // address of buffer for opened handle
  130. &dwDisposition); // address of disposition value buffer
  131. if (lResult == ERROR_SUCCESS)
  132. {
  133. DWORD dwSize = sizeof(unsigned long);
  134. lResult = RegQueryValueExW (hKey, DEBUGLEVEL, NULL, NULL,
  135. (LPBYTE)&g_ulDebugOutput, &dwSize);
  136. if (lResult != ERROR_SUCCESS)
  137. {
  138. g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  139. if ( ERROR_FILE_NOT_FOUND == lResult )
  140. {
  141. RegSetValueExW (hKey, DEBUGLEVEL, 0, REG_DWORD,
  142. (LPBYTE)&g_ulDebugOutput, sizeof (g_ulDebugOutput));
  143. }
  144. }
  145. RegCloseKey(hKey);
  146. }
  147. }
  148. #endif // if DBG