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.

118 lines
2.7 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. //
  3. // Debug.CPP
  4. //
  5. //
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <stdarg.h>
  9. #include "debug.h"
  10. #ifdef DBG
  11. // @@BEGIN_DDKSPLIT -- This section will be removed in the DDK sample. See ddkreadme.txt for more info.
  12. #define MODULE "DMSYNTH"
  13. #if 0 // The following section will only take affect in the DDK sample.
  14. // @@END_DDKSPLIT
  15. #define MODULE "DDKSYNTH"
  16. // @@BEGIN_DDKSPLIT -- This section will be removed in the DDK sample.
  17. #endif
  18. // @@END_DDKSPLIT
  19. // Section in WIN.INI for all debug settings
  20. const char szDebugSection[] = "debug";
  21. // Key in WIN.INI for our debug level. All messages with
  22. // a level of this number or lower will be displayed.
  23. const char szDebugKey[] = MODULE;
  24. // Key in WIN.INI [debug] section which determines if assert calls
  25. // DebugBreak or not
  26. //
  27. const char szAssertBreak[] = "AssertBreak";
  28. // Prefix for all debug outputs
  29. //
  30. const char szDebugPrefix[] = MODULE ": ";
  31. // The current debug level.
  32. static int giDebugLevel;
  33. // Do asserts break?
  34. static BOOL gfAssertBreak;
  35. // Sets the debug level from WIN.INI
  36. //
  37. void DebugInit(
  38. void)
  39. {
  40. giDebugLevel = GetProfileInt(szDebugSection, szDebugKey, 0);
  41. gfAssertBreak = GetProfileInt(szDebugSection, szAssertBreak, 0);
  42. // Nepotism at its finest
  43. DebugTrace(-1, "Debug level is %d\n", giDebugLevel);
  44. }
  45. // Send a debug trace out.
  46. //
  47. // Any message with a level less than or equal to the current debug
  48. // level will be displayed using the OutputDebugString API. This means either
  49. // the IDE Debug window if the app is running in that context or WDEB if
  50. // it's running.
  51. //
  52. static BOOL fNeedPrefix = TRUE;
  53. void DebugTrace(
  54. int iDebugLevel, // The debug level of this message
  55. LPSTR pstrFormat, // A printf style format string
  56. ...) // | ... | Variable paramters based on <p pstrFormat>
  57. {
  58. char sz[512];
  59. if (iDebugLevel != -1 && iDebugLevel > giDebugLevel)
  60. {
  61. return;
  62. }
  63. va_list va;
  64. va_start(va, pstrFormat);
  65. vsprintf(sz, pstrFormat, va);
  66. va_end(va);
  67. if (fNeedPrefix)
  68. {
  69. OutputDebugString(szDebugPrefix);
  70. }
  71. OutputDebugString(sz);
  72. // Let them construct multiple piece trace outs w/o
  73. // prefixing each one
  74. //
  75. fNeedPrefix = FALSE;
  76. for (;*pstrFormat && !fNeedPrefix; ++pstrFormat)
  77. {
  78. if (*pstrFormat == '\n')
  79. {
  80. fNeedPrefix = TRUE;
  81. }
  82. }
  83. }
  84. void DebugAssert(
  85. LPSTR szExp,
  86. LPSTR szFile,
  87. ULONG ulLine)
  88. {
  89. DebugTrace(0, "ASSERT: \"%s\" %s@%lu\n", szExp, szFile, ulLine);
  90. if (gfAssertBreak)
  91. {
  92. DebugBreak();
  93. }
  94. }
  95. #endif