From ec028d6bd5a6d471cfda7946d233c0f7add35a5b Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Fri, 2 Nov 2018 05:04:48 -0700 Subject: [PATCH 1/2] Allows Debug.Fail to go through Trace Listeners --- .../shared/System/Diagnostics/Debug.cs | 30 ++++--------------- .../System/Diagnostics/DebugProvider.Unix.cs | 8 ++++- .../System/Diagnostics/DebugProvider.cs | 30 +++++++++++++++++-- .../Diagnostics/DebugProvider.Windows.cs | 8 ++++- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs index cd22c8174627..d700cd53f79d 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs @@ -101,17 +101,7 @@ public static void Assert(bool condition, string message, string detailMessage) { if (!condition) { - string stackTrace; - try - { - stackTrace = new StackTrace(0, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal); - } - catch - { - stackTrace = ""; - } - WriteAssert(stackTrace, message, detailMessage); - s_provider.ShowDialog(stackTrace, message, detailMessage, "Assertion Failed"); + Fail(message, detailMessage); } } @@ -128,31 +118,21 @@ internal static void ContractFailure(bool condition, string message, string deta { stackTrace = ""; } - WriteAssert(stackTrace, message, detailMessage); - s_provider.ShowDialog(stackTrace, message, detailMessage, SR.GetResourceString(failureKindMessage)); + s_provider.WriteAssert(stackTrace, message, detailMessage); + DebugProvider.ShowDialog(stackTrace, message, detailMessage, SR.GetResourceString(failureKindMessage)); } } [System.Diagnostics.Conditional("DEBUG")] public static void Fail(string message) { - Assert(false, message, string.Empty); + Fail(message, string.Empty); } [System.Diagnostics.Conditional("DEBUG")] public static void Fail(string message, string detailMessage) { - Assert(false, message, detailMessage); - } - - private static void WriteAssert(string stackTrace, string message, string detailMessage) - { - WriteLine(SR.DebugAssertBanner + Environment.NewLine - + SR.DebugAssertShortMessage + Environment.NewLine - + message + Environment.NewLine - + SR.DebugAssertLongMessage + Environment.NewLine - + detailMessage + Environment.NewLine - + stackTrace); + s_provider.Fail(message, detailMessage); } [System.Diagnostics.Conditional("DEBUG")] diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs index 481f309e2884..53b95991585e 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs @@ -10,8 +10,14 @@ public partial class DebugProvider { private static readonly bool s_shouldWriteToStdErr = Environment.GetEnvironmentVariable("COMPlus_DebugWriteToStdErr") == "1"; - public virtual void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource) + public static void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource) { + if (s_ShowDialog != null) + { + s_ShowDialog(stackTrace, message, detailMessage, errorSource); + return; + } + if (Debugger.IsAttached) { Debugger.Break(); diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs index 3150e68ed53a..1208c6105516 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs @@ -8,10 +8,35 @@ namespace System.Diagnostics { /// - /// Provides default implementation for Write and ShowDialog methods in Debug class. + /// Provides default implementation for Write and Fail methods in Debug class. /// public partial class DebugProvider { + public virtual void Fail(string message, string detailMessage) + { + string stackTrace; + try + { + stackTrace = new StackTrace(0, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal); + } + catch + { + stackTrace = ""; + } + WriteAssert(stackTrace, message, detailMessage); + ShowDialog(stackTrace, message, detailMessage, "Assertion Failed"); + } + + internal void WriteAssert(string stackTrace, string message, string detailMessage) + { + WriteLine(SR.DebugAssertBanner + Environment.NewLine + + SR.DebugAssertShortMessage + Environment.NewLine + + message + Environment.NewLine + + SR.DebugAssertLongMessage + Environment.NewLine + + detailMessage + Environment.NewLine + + stackTrace); + } + public virtual void Write(string message) { lock (s_lock) @@ -78,6 +103,7 @@ private string GetIndentString() } // internal and not readonly so that the tests can swap this out. + internal static Action s_ShowDialog = null; internal static Action s_WriteCore = null; } -} \ No newline at end of file +} diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs index 88f6116f15b1..38dcdafd18dd 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs @@ -6,8 +6,14 @@ namespace System.Diagnostics { public partial class DebugProvider { - public virtual void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource) + public static void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource) { + if (s_ShowDialog != null) + { + s_ShowDialog(stackTrace, message, detailMessage, errorSource); + return; + } + if (Debugger.IsAttached) { Debugger.Break(); From 577ff2fff44cb51c6902bb8415414c441f82914c Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Fri, 2 Nov 2018 08:44:46 -0700 Subject: [PATCH 2/2] Rename --- .../shared/System/Diagnostics/Debug.cs | 2 +- .../shared/System/Diagnostics/DebugProvider.Unix.cs | 6 +++--- .../shared/System/Diagnostics/DebugProvider.cs | 4 ++-- .../src/System/Diagnostics/DebugProvider.Windows.cs | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs index d700cd53f79d..6eef1c8aeeb9 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs @@ -119,7 +119,7 @@ internal static void ContractFailure(bool condition, string message, string deta stackTrace = ""; } s_provider.WriteAssert(stackTrace, message, detailMessage); - DebugProvider.ShowDialog(stackTrace, message, detailMessage, SR.GetResourceString(failureKindMessage)); + DebugProvider.FailCore(stackTrace, message, detailMessage, SR.GetResourceString(failureKindMessage)); } } diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs index 53b95991585e..e1517179ec17 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs @@ -10,11 +10,11 @@ public partial class DebugProvider { private static readonly bool s_shouldWriteToStdErr = Environment.GetEnvironmentVariable("COMPlus_DebugWriteToStdErr") == "1"; - public static void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource) + public static void FailCore(string stackTrace, string message, string detailMessage, string errorSource) { - if (s_ShowDialog != null) + if (s_FailCore != null) { - s_ShowDialog(stackTrace, message, detailMessage, errorSource); + s_FailCore(stackTrace, message, detailMessage, errorSource); return; } diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs index 1208c6105516..686fc18db3e2 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs @@ -24,7 +24,7 @@ public virtual void Fail(string message, string detailMessage) stackTrace = ""; } WriteAssert(stackTrace, message, detailMessage); - ShowDialog(stackTrace, message, detailMessage, "Assertion Failed"); + FailCore(stackTrace, message, detailMessage, "Assertion Failed"); } internal void WriteAssert(string stackTrace, string message, string detailMessage) @@ -103,7 +103,7 @@ private string GetIndentString() } // internal and not readonly so that the tests can swap this out. - internal static Action s_ShowDialog = null; + internal static Action s_FailCore = null; internal static Action s_WriteCore = null; } } diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs b/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs index 38dcdafd18dd..1eb445df1781 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs @@ -6,11 +6,11 @@ namespace System.Diagnostics { public partial class DebugProvider { - public static void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource) + public static void FailCore(string stackTrace, string message, string detailMessage, string errorSource) { - if (s_ShowDialog != null) + if (s_FailCore != null) { - s_ShowDialog(stackTrace, message, detailMessage, errorSource); + s_FailCore(stackTrace, message, detailMessage, errorSource); return; }