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.

165 lines
4.9 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 DEBUGKEY L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AdminDebug\\LogHours"
  16. #define DEBUG_OUTPUT_NONE 0
  17. #define DEBUG_OUTPUT_ERROR 1
  18. #define DEBUG_OUTPUT_WARNING 2
  19. #define DEBUG_OUTPUT_TRACE 3
  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,
  44. DEBUG_BUF_LEN,
  45. format,
  46. arglist)) )
  47. {
  48. // ignore
  49. }
  50. if ( Buffer[0] )
  51. OutputDebugStringW (Buffer);
  52. va_end(arglist);
  53. if ( level > 0 )
  54. indentLevel += level;
  55. }
  56. }
  57. PCSTR
  58. StripDirPrefixA(
  59. PCSTR pszPathName
  60. )
  61. /*++
  62. Routine Description:
  63. Strip the directory prefix off a filename (ANSI version)
  64. Arguments:
  65. pstrFilename - Pointer to filename string
  66. Return Value:
  67. Pointer to the last component of a filename (without directory prefix)
  68. --*/
  69. {
  70. // NOTICE-2002/02/18-artm Unchecked pointer acceptable only b/c this is debug build.
  71. // Paramenter pszPathName can be unchecked here since this code is only included
  72. // in a debug build. Otherwise, it would need to be checked for NULL, and the
  73. // empty string case would need to be addressed.
  74. DWORD dwLen = lstrlenA(pszPathName);
  75. pszPathName += dwLen - 1; // go to the end
  76. while (*pszPathName != '\\' && dwLen--)
  77. {
  78. pszPathName--;
  79. }
  80. return pszPathName + 1;
  81. }
  82. //+----------------------------------------------------------------------------
  83. // Function: CheckInit
  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. // NOTICE-2002/02/18-artm This code only included in debug build.
  97. //
  98. // The NULL security structure is intentional so that the ACL's will be inherited
  99. // from HKEY_LOCAL_MACHINE. This restricts access to local administrators. If
  100. // this code is later included in a release build, reconsider the requirement of
  101. // running as local administrator.
  102. LONG lResult = ::RegCreateKeyEx (HKEY_LOCAL_MACHINE, // handle of an open key
  103. DEBUGKEY, // address of subkey name
  104. 0, // reserved
  105. L"", // address of class string
  106. REG_OPTION_NON_VOLATILE, // special options flag
  107. KEY_ALL_ACCESS, // desired security access
  108. NULL, // address of key security structure
  109. &hKey, // address of buffer for opened handle
  110. &dwDisposition); // address of disposition value buffer
  111. // If key opened/created successfully, then read the existing debug level.
  112. if (lResult == ERROR_SUCCESS)
  113. {
  114. DWORD dwSize = sizeof(unsigned long);
  115. lResult = RegQueryValueExW (hKey, DEBUGLEVEL, NULL, NULL,
  116. (LPBYTE)&g_ulDebugOutput, &dwSize);
  117. if (lResult != ERROR_SUCCESS)
  118. {
  119. g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  120. // If debug not set yet in registry (key created by this function),
  121. // initialize key value.
  122. if ( ERROR_FILE_NOT_FOUND == lResult )
  123. {
  124. RegSetValueExW (hKey, DEBUGLEVEL, 0, REG_DWORD,
  125. (LPBYTE)&g_ulDebugOutput, sizeof (g_ulDebugOutput));
  126. }
  127. }
  128. RegCloseKey(hKey);
  129. }
  130. }
  131. #endif // if DBG