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.

159 lines
4.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2002.
  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. static int indentLevel = 0;
  15. #define DEBUG_OUTPUT_NONE 0
  16. #define DEBUG_OUTPUT_ERROR 1
  17. #define DEBUG_OUTPUT_WARNING 2
  18. #define DEBUG_OUTPUT_TRACE 3
  19. #define DEBUGKEY L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AdminDebug\\CertTmpl"
  20. #define DEBUGLEVEL L"debugOutput"
  21. static bool g_fDebugOutputLevelInit = false;
  22. static unsigned long g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  23. void __cdecl _TRACE (int level, const wchar_t *format, ... )
  24. {
  25. if ( g_ulDebugOutput > DEBUG_OUTPUT_NONE )
  26. {
  27. va_list arglist;
  28. const size_t DEBUG_BUF_LEN = 512;
  29. WCHAR Buffer[DEBUG_BUF_LEN];
  30. Buffer[0] = 0;
  31. if ( level < 0 )
  32. indentLevel += level;
  33. //
  34. // Format the output into a buffer and then print it.
  35. //
  36. wstring strTabs;
  37. for (int nLevel = 0; nLevel < indentLevel; nLevel++)
  38. strTabs += L" ";
  39. OutputDebugStringW (strTabs.c_str ());
  40. va_start(arglist, format);
  41. // Don't check return value - we don't care if this gets truncated as
  42. // it's just debugging output
  43. if ( SUCCEEDED (::StringCchVPrintf (Buffer, // check value only to shut up prefast
  44. DEBUG_BUF_LEN,
  45. format,
  46. arglist)) )
  47. {
  48. }
  49. if ( Buffer[0] )
  50. OutputDebugStringW (Buffer);
  51. va_end(arglist);
  52. if ( level > 0 )
  53. indentLevel += level;
  54. }
  55. }
  56. PCSTR
  57. StripDirPrefixA (
  58. PCSTR pszPathName
  59. )
  60. /*++
  61. Routine Description:
  62. Strip the directory prefix off a filename (ANSI version)
  63. Arguments:
  64. pstrFilename - Pointer to filename string
  65. Return Value:
  66. Pointer to the last component of a filename (without directory prefix)
  67. --*/
  68. {
  69. if ( pszPathName )
  70. {
  71. DWORD dwLen = lstrlenA(pszPathName);
  72. pszPathName += dwLen - 1; // go to the end
  73. while (*pszPathName != '\\' && dwLen--)
  74. {
  75. pszPathName--;
  76. }
  77. return pszPathName + 1;
  78. }
  79. else
  80. return "";
  81. }
  82. //+----------------------------------------------------------------------------
  83. // Function: CheckDebugOutputLevel
  84. //
  85. // Synopsis: Performs debugging library initialization
  86. // including reading the registry for the desired infolevel
  87. //
  88. //-----------------------------------------------------------------------------
  89. void CheckDebugOutputLevel ()
  90. {
  91. if ( g_fDebugOutputLevelInit )
  92. return;
  93. g_fDebugOutputLevelInit = true;
  94. HKEY hKey = 0;
  95. DWORD dwDisposition = 0;
  96. LONG lResult = ::RegCreateKeyEx (HKEY_LOCAL_MACHINE, // handle of an open key
  97. DEBUGKEY, // address of subkey name
  98. 0, // reserved
  99. L"", // address of class string
  100. REG_OPTION_VOLATILE, // special options flag
  101. // security review 2/21/2002 BryanWal ok
  102. KEY_SET_VALUE | KEY_QUERY_VALUE, // desired security access
  103. NULL, // address of key security structure
  104. &hKey, // address of buffer for opened handle
  105. &dwDisposition); // address of disposition value buffer
  106. if (lResult == ERROR_SUCCESS)
  107. {
  108. DWORD dwSize = sizeof(unsigned long);
  109. DWORD dwType = REG_DWORD;
  110. // security review 2/21/2002 BryanWal ok
  111. lResult = RegQueryValueExW (hKey, DEBUGLEVEL, NULL, &dwType,
  112. (LPBYTE)&g_ulDebugOutput, &dwSize);
  113. if (lResult != ERROR_SUCCESS)
  114. {
  115. g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  116. if ( ERROR_FILE_NOT_FOUND == lResult )
  117. {
  118. lResult = ::RegSetValueExW (hKey, DEBUGLEVEL, 0, REG_DWORD,
  119. (LPBYTE)&g_ulDebugOutput, sizeof (g_ulDebugOutput));
  120. ASSERT (lResult == ERROR_SUCCESS);
  121. }
  122. }
  123. else
  124. {
  125. ASSERT (dwType == REG_DWORD);
  126. }
  127. RegCloseKey(hKey);
  128. }
  129. }
  130. #endif // if DBG