DebugProvider.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 stackTrace) :
  67. base(Environment.NewLine + stackTrace)
  68. {
  69. }
  70. internal DebugAssertException(string message, string stackTrace) :
  71. base(message + Environment.NewLine + Environment.NewLine + stackTrace)
  72. {
  73. }
  74. internal DebugAssertException(string message, string detailMessage, string stackTrace) :
  75. base(message + Environment.NewLine + detailMessage + Environment.NewLine + Environment.NewLine + stackTrace)
  76. {
  77. }
  78. }
  79. private bool _needIndent = true;
  80. private string _indentString;
  81. private string GetIndentString()
  82. {
  83. int indentCount = Debug.IndentSize * Debug.IndentLevel;
  84. if (_indentString?.Length == indentCount)
  85. {
  86. return _indentString;
  87. }
  88. return _indentString = new string(' ', indentCount);
  89. }
  90. // internal and not readonly so that the tests can swap this out.
  91. internal static Action<string, string, string, string> s_FailCore = null;
  92. internal static Action<string> s_WriteCore = null;
  93. }
  94. }