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.

161 lines
4.7 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 DEBUGLEVEL L"debugOutput"
  20. static bool g_fDebugOutputLevelInit = false;
  21. static unsigned long g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  22. void __cdecl _TRACE (int level, const wchar_t *format, ... )
  23. {
  24. if ( g_ulDebugOutput > DEBUG_OUTPUT_NONE )
  25. {
  26. va_list arglist;
  27. const size_t DEBUG_BUF_LEN = 512;
  28. WCHAR Buffer[DEBUG_BUF_LEN];
  29. Buffer[0] = 0;
  30. if ( level < 0 )
  31. indentLevel += level;
  32. //
  33. // Format the output into a buffer and then print it.
  34. //
  35. wstring strTabs;
  36. for (int nLevel = 0; nLevel < indentLevel; nLevel++)
  37. strTabs += L" ";
  38. OutputDebugStringW (strTabs.c_str ());
  39. va_start(arglist, format);
  40. // security review 2/27/2002 BryanWal
  41. // NTRAID Bug9 538774 Security: certmgr.dll : convert to strsafe string functions
  42. // NTRAID Bug9 611409 prefast: certmgr: debug.cpp(52) : warning 53:
  43. // Call to '_vsnwprintf' may not zero-terminate string 'Buffer'.
  44. // Don't check return value - we don't care if this gets truncated as
  45. // it's just debugging output
  46. if ( SUCCEEDED (::StringCchVPrintf (Buffer,
  47. DEBUG_BUF_LEN,
  48. format,
  49. arglist)) )
  50. {
  51. // check return value to quiet prefast
  52. }
  53. if ( Buffer[0] )
  54. OutputDebugStringW (Buffer);
  55. va_end(arglist);
  56. if ( level > 0 )
  57. indentLevel += level;
  58. }
  59. }
  60. PCSTR StripDirPrefixA (PCSTR pszPathName)
  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. ASSERT (pszPathName);
  71. if ( !pszPathName )
  72. return 0;
  73. // security review 2/27/2002 BryanWal ok
  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. 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_NON_VOLATILE, // special options flag
  101. // security review 2/27/2002 BryanWal ok
  102. KEY_ALL_ACCESS, // desired security access - required to create new key
  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/27/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. // security review 2/27/2002 BryanWal ok
  119. ::RegSetValueExW (hKey, DEBUGLEVEL, 0, REG_DWORD,
  120. (LPBYTE)&g_ulDebugOutput, sizeof (g_ulDebugOutput));
  121. }
  122. }
  123. else
  124. {
  125. ASSERT (REG_DWORD == dwType);
  126. }
  127. ::RegCloseKey(hKey);
  128. }
  129. }
  130. #endif // if DBG