From ccf84fc34b83c3d3e6b2b378136b55dda5f9bdc8 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Wed, 31 Oct 2018 14:32:19 -0700 Subject: Making Debug and Trace behavior consistent with eachother and Desktop (#20581) * Making Debug and Trace behavior more consistent with Desktop and eachother * Syncs IndentLevel and IndentSize for Debug and Trace * Completing DebugProvider APIs. Fixing Indentation bugs * Make WriteCore public * Make s_provider and s_indentSize volatile * Applied PR feedbacks --- .../shared/System/Diagnostics/Debug.cs | 42 +++++++------- .../System/Diagnostics/DebugProvider.Unix.cs | 8 ++- .../shared/System/Diagnostics/DebugProvider.cs | 64 ++++++++-------------- .../System/Diagnostics/DebugProvider.Windows.cs | 8 ++- 4 files changed, 60 insertions(+), 62 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs index 433e00b2a4..3dce03af92 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs @@ -11,9 +11,9 @@ namespace System.Diagnostics /// /// Provides a set of properties and methods for debugging code. /// - public static partial class Debug + public static class Debug { - private static DebugProvider s_provider = new DebugProvider(); + private static volatile DebugProvider s_provider = new DebugProvider(); public static DebugProvider SetProvider(DebugProvider provider) { @@ -25,27 +25,32 @@ namespace System.Diagnostics public static bool AutoFlush { get { return true; } set { } } + [ThreadStatic] + private static int t_indentLevel; public static int IndentLevel { get { - return DebugProvider.IndentLevel; + return t_indentLevel; } set { - DebugProvider.IndentLevel = value; + t_indentLevel = value < 0 ? 0 : value; + s_provider.OnIndentLevelChanged(t_indentLevel); } } + private static volatile int s_indentSize = 4; public static int IndentSize { get { - return DebugProvider.IndentSize; + return s_indentSize; } set { - DebugProvider.IndentSize = value; + s_indentSize = value < 0 ? 0 : value; + s_provider.OnIndentSizeChanged(s_indentSize); } } @@ -105,7 +110,7 @@ namespace System.Diagnostics { stackTrace = ""; } - WriteLine(FormatAssert(stackTrace, message, detailMessage)); + WriteAssert(stackTrace, message, detailMessage); s_provider.ShowDialog(stackTrace, message, detailMessage, "Assertion Failed"); } } @@ -123,7 +128,7 @@ namespace System.Diagnostics { stackTrace = ""; } - WriteLine(FormatAssert(stackTrace, message, detailMessage)); + WriteAssert(stackTrace, message, detailMessage); s_provider.ShowDialog(stackTrace, message, detailMessage, SR.GetResourceString(failureKindMessage)); } } @@ -140,15 +145,14 @@ namespace System.Diagnostics Assert(false, message, detailMessage); } - private static string FormatAssert(string stackTrace, string message, string detailMessage) + private static void WriteAssert(string stackTrace, string message, string detailMessage) { - string newLine = DebugProvider.GetIndentString() + Environment.NewLine; - return SR.DebugAssertBanner + newLine - + SR.DebugAssertShortMessage + newLine - + message + newLine - + SR.DebugAssertLongMessage + newLine - + detailMessage + newLine - + stackTrace; + WriteLine(SR.DebugAssertBanner + Environment.NewLine + + SR.DebugAssertShortMessage + Environment.NewLine + + message + Environment.NewLine + + SR.DebugAssertLongMessage + Environment.NewLine + + detailMessage + Environment.NewLine + + stackTrace); } [System.Diagnostics.Conditional("DEBUG")] @@ -160,7 +164,7 @@ namespace System.Diagnostics [System.Diagnostics.Conditional("DEBUG")] public static void WriteLine(string message) { - Write(message + Environment.NewLine); + s_provider.WriteLine(message); } [System.Diagnostics.Conditional("DEBUG")] @@ -196,7 +200,7 @@ namespace System.Diagnostics } else { - WriteLine(category + ":" + message); + WriteLine(category + ": " + message); } } @@ -215,7 +219,7 @@ namespace System.Diagnostics } else { - Write(category + ":" + message); + Write(category + ": " + message); } } 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 b0f2f95ee4..481f309e28 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs @@ -38,8 +38,14 @@ namespace System.Diagnostics } } - private static void WriteCore(string message) + public static void WriteCore(string message) { + if (s_WriteCore != null) + { + s_WriteCore(message); + return; + } + WriteToDebugger(message); if (s_shouldWriteToStdErr) diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs index 5ddb6d58ba..416423e289 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.cs @@ -18,21 +18,30 @@ namespace System.Diagnostics { if (message == null) { - s_WriteCore(string.Empty); + WriteCore(string.Empty); return; } - if (s_needIndent) + if (_needIndent) { message = GetIndentString() + message; - s_needIndent = false; + _needIndent = false; } - s_WriteCore(message); + WriteCore(message); if (message.EndsWith(Environment.NewLine)) { - s_needIndent = true; + _needIndent = true; } } } + + public virtual void WriteLine(string message) + { + Write(message + Environment.NewLine); + } + + public virtual void OnIndentLevelChanged(int indentLevel) { } + + public virtual void OnIndentSizeChanged(int indentSize) { } private static readonly object s_lock = new object(); @@ -54,48 +63,21 @@ namespace System.Diagnostics } } - [ThreadStatic] - private static int s_indentLevel; - internal static int IndentLevel - { - get - { - return s_indentLevel; - } - set - { - s_indentLevel = value < 0 ? 0 : value; - } - } - - private static int s_indentSize = 4; - internal static int IndentSize - { - get - { - return s_indentSize; - } - set - { - s_indentSize = value < 0 ? 0 : value; - } - } - - private static bool s_needIndent; + private bool _needIndent = true; - private static string s_indentString; + private string _indentString; - internal static string GetIndentString() + private string GetIndentString() { - int indentCount = IndentSize * IndentLevel; - if (s_indentString?.Length == indentCount) + int indentCount = Debug.IndentSize * Debug.IndentLevel; + if (_indentString?.Length == indentCount) { - return s_indentString; + return _indentString; } - return s_indentString = new string(' ', indentCount); + return _indentString = new string(' ', indentCount); } // internal and not readonly so that the tests can swap this out. - internal static Action s_WriteCore = WriteCore; + internal static Action s_WriteCore; } -} +} \ 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 b16d30b6d5..88f6116f15 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Windows.cs @@ -36,8 +36,14 @@ namespace System.Diagnostics private static readonly object s_ForLock = new object(); - private static void WriteCore(string message) + public static void WriteCore(string message) { + if (s_WriteCore != null) + { + s_WriteCore(message); + return; + } + // really huge messages mess up both VS and dbmon, so we chop it up into // reasonable chunks if it's too big. This is the number of characters // that OutputDebugstring chunks at. -- cgit v1.2.3