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.

138 lines
3.5 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 DEBUG_OUTPUT_NONE 0
  15. #define DEBUG_OUTPUT_ERROR 1
  16. #define DEBUG_OUTPUT_WARNING 2
  17. #define DEBUG_OUTPUT_TRACE 3
  18. #define DEBUGLEVEL L"debugOutput"
  19. static bool g_fDebugOutputLevelInit = false;
  20. static unsigned long g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  21. void __cdecl _TRACE (int level, const wchar_t *format, ... )
  22. {
  23. if ( g_ulDebugOutput > DEBUG_OUTPUT_NONE )
  24. {
  25. va_list arglist;
  26. const size_t DEBUG_BUF_LEN = 512;
  27. WCHAR Buffer[DEBUG_BUF_LEN];
  28. int cb;
  29. if ( level < 0 )
  30. indentLevel += level;
  31. //
  32. // Format the output into a buffer and then print it.
  33. //
  34. wstring strTabs;
  35. for (int nLevel = 0; nLevel < indentLevel; nLevel++)
  36. strTabs += L" ";
  37. OutputDebugStringW (strTabs.c_str ());
  38. va_start(arglist, format);
  39. cb = _vsnwprintf (Buffer, DEBUG_BUF_LEN, format, arglist);
  40. if ( cb )
  41. {
  42. OutputDebugStringW (Buffer);
  43. }
  44. va_end(arglist);
  45. if ( level > 0 )
  46. indentLevel += level;
  47. }
  48. }
  49. PCSTR
  50. StripDirPrefixA(
  51. PCSTR pszPathName
  52. )
  53. /*++
  54. Routine Description:
  55. Strip the directory prefix off a filename (ANSI version)
  56. Arguments:
  57. pstrFilename - Pointer to filename string
  58. Return Value:
  59. Pointer to the last component of a filename (without directory prefix)
  60. --*/
  61. {
  62. DWORD dwLen = lstrlenA(pszPathName);
  63. pszPathName += dwLen - 1; // go to the end
  64. while (*pszPathName != '\\' && dwLen--)
  65. {
  66. pszPathName--;
  67. }
  68. return pszPathName + 1;
  69. }
  70. //+----------------------------------------------------------------------------
  71. // Function: CheckInit
  72. //
  73. // Synopsis: Performs debugging library initialization
  74. // including reading the registry for the desired infolevel
  75. //
  76. //-----------------------------------------------------------------------------
  77. void CheckDebugOutputLevel ()
  78. {
  79. if ( g_fDebugOutputLevelInit )
  80. return;
  81. g_fDebugOutputLevelInit = true;
  82. HKEY hKey = 0;
  83. DWORD dwDisposition = 0;
  84. LONG lResult = ::RegCreateKeyEx (HKEY_LOCAL_MACHINE, // handle of an open key
  85. DEBUGKEY, // address of subkey name
  86. 0, // reserved
  87. L"", // address of class string
  88. REG_OPTION_NON_VOLATILE, // special options flag
  89. KEY_ALL_ACCESS, // desired security access
  90. NULL, // address of key security structure
  91. &hKey, // address of buffer for opened handle
  92. &dwDisposition); // address of disposition value buffer
  93. if (lResult == ERROR_SUCCESS)
  94. {
  95. DWORD dwSize = sizeof(unsigned long);
  96. lResult = RegQueryValueExW (hKey, DEBUGLEVEL, NULL, NULL,
  97. (LPBYTE)&g_ulDebugOutput, &dwSize);
  98. if (lResult != ERROR_SUCCESS)
  99. {
  100. g_ulDebugOutput = DEBUG_OUTPUT_NONE;
  101. if ( ERROR_FILE_NOT_FOUND == lResult )
  102. {
  103. RegSetValueExW (hKey, DEBUGLEVEL, 0, REG_DWORD,
  104. (LPBYTE)&g_ulDebugOutput, sizeof (g_ulDebugOutput));
  105. }
  106. }
  107. RegCloseKey(hKey);
  108. }
  109. }
  110. #endif // if DBG