DebugProvider.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. // Do not remove this, it is needed to retain calls to these conditional methods in release builds
  5. #define DEBUG
  6. namespace System.Diagnostics
  7. {
  8. /// <summary>
  9. /// Provides default implementation for Write and Fail methods in Debug class.
  10. /// </summary>
  11. public partial class DebugProvider
  12. {
  13. public virtual void Fail(string? message, string? detailMessage)
  14. {
  15. string stackTrace;
  16. try
  17. {
  18. stackTrace = new StackTrace(0, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal);
  19. }
  20. catch
  21. {
  22. stackTrace = "";
  23. }
  24. WriteAssert(stackTrace, message, detailMessage);
  25. FailCore(stackTrace, message, detailMessage, "Assertion failed.");
  26. }
  27. internal void WriteAssert(string stackTrace, string? message, string? detailMessage)
  28. {
  29. WriteLine(SR.DebugAssertBanner + Environment.NewLine
  30. + SR.DebugAssertShortMessage + Environment.NewLine
  31. + message + Environment.NewLine
  32. + SR.DebugAssertLongMessage + Environment.NewLine
  33. + detailMessage + Environment.NewLine
  34. + stackTrace);
  35. }
  36. public virtual void Write(string? message)
  37. {
  38. lock (s_lock)
  39. {
  40. if (message == null)
  41. {
  42. WriteCore(string.Empty);
  43. return;
  44. }
  45. if (_needIndent)
  46. {
  47. message = GetIndentString() + message;
  48. _needIndent = false;
  49. }
  50. WriteCore(message);
  51. if (message.EndsWith(Environment.NewLine))
  52. {
  53. _needIndent = true;
  54. }
  55. }
  56. }
  57. public virtual void WriteLine(string? message)
  58. {
  59. Write(message + Environment.NewLine);
  60. }
  61. public virtual void OnIndentLevelChanged(int indentLevel) { }
  62. public virtual void OnIndentSizeChanged(int indentSize) { }
  63. private static readonly object s_lock = new object();
  64. private sealed class DebugAssertException : Exception
  65. {
  66. internal DebugAssertException(string? message, string? detailMessage, string? stackTrace) :
  67. base(Terminate(message) + Terminate(detailMessage) + stackTrace)
  68. {
  69. }
  70. private static string? Terminate(string? s)
  71. {
  72. if (s == null)
  73. return s;
  74. s = s.Trim();
  75. if (s.Length > 0)
  76. s += Environment.NewLine;
  77. return s;
  78. }
  79. }
  80. private bool _needIndent = true;
  81. private string? _indentString;
  82. private string GetIndentString()
  83. {
  84. int indentCount = Debug.IndentSize * Debug.IndentLevel;
  85. if (_indentString?.Length == indentCount)
  86. {
  87. return _indentString;
  88. }
  89. return _indentString = new string(' ', indentCount);
  90. }
  91. // internal and not readonly so that the tests can swap this out.
  92. internal static Action<string, string?, string?, string>? s_FailCore = null;
  93. internal static Action<string>? s_WriteCore = null;
  94. }
  95. }