Source code of Windows XP (NT5)
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.

140 lines
3.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2001.
  5. //
  6. // File: debug.cpp
  7. //
  8. // Contents: Debugging support
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #if DBG == 1
  13. static int indentLevel = 0;
  14. #define DEBUGKEY L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AdminDebug\\LogHours"
  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. int cb;
  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. cb = _vsnwprintf (Buffer, DEBUG_BUF_LEN, format, arglist);
  41. if ( cb )
  42. {
  43. OutputDebugStringW (Buffer);
  44. }
  45. va_end(arglist);
  46. if ( level > 0 )
  47. indentLevel += level;
  48. }
  49. }
  50. PCSTR
  51. StripDirPrefixA(
  52. PCSTR pszPathName
  53. )
  54. /*++
  55. Routine Description:
  56. Strip the directory prefix off a filename (ANSI version)
  57. Arguments:
  58. pstrFilename - Pointer to filename string
  59. Return Value:
  60. Pointer to the last component of a filename (without directory prefix)
  61. --*/
  62. {
  63. DWORD dwLen = lstrlenA(pszPathName);
  64. pszPathName += dwLen - 1; // go to the end
  65. while (*pszPathName != '\\' && dwLen--)
  66. {
  67. pszPathName--;
  68. }
  69. return pszPathName + 1;
  70. }
  71. //+----------------------------------------------------------------------------
  72. // Function: CheckInit
  73. //
  74. // Synopsis: Performs debugging library initialization
  75. // including reading the registry for the desired infolevel
  76. //
  77. //-----------------------------------------------------------------------------
  78. void CheckDebugOutputLevel ()
  79. {
  80. if ( g_fDebugOutputLevelInit )
  81. return;
  82. g_fDebugOutputLevelInit = true;
  83. HKEY hKey = 0;
  84. DWORD dwDisposition = 0;
  85. LONG lResult = ::RegCreateKeyEx (HKEY_LOCAL_MACHINE, // handle of an open key
  86. DEBUGKEY, // address of subkey name
  87. 0, // reserved
  88. L"", // address of class string
  89. REG_OPTION_NON_VOLATILE, // special options flag
  90. KEY_ALL_ACCESS, // desired security access
  91. NULL, // address of key security structure
  92. &hKey, // address of buffer for opened handle
  93. &dwDisposition); // address of disposition value buffer
  94. if (lResult == ERROR_SUCCESS)
  95. {
  96. DWORD dwSize = sizeof(unsigned long);
  97. lResult = RegQueryValueExW (hKey, DEBUGLEVEL, NULL, NULL,
  98. (LPBYTE)&g_ulDebugOutput, &dwSize);
  99. if (lResult != ERROR_SUCCESS)
  100. {
  101. g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  102. if ( ERROR_FILE_NOT_FOUND == lResult )
  103. {
  104. RegSetValueExW (hKey, DEBUGLEVEL, 0, REG_DWORD,
  105. (LPBYTE)&g_ulDebugOutput, sizeof (g_ulDebugOutput));
  106. }
  107. }
  108. RegCloseKey(hKey);
  109. }
  110. }
  111. #endif // if DBG