From: Hyungju Lee Date: Wed, 8 Mar 2023 10:11:48 +0000 (+0900) Subject: Handle exception in System.Console.SetCursorPosition in some environments for dotnet... X-Git-Tag: accepted/tizen/7.0/unified/20230503.111430~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=40054d638bec7bc6d2e547e0d827c9ce599bccc5;p=platform%2Fcore%2Fdotnet%2Fdiagnostics.git Handle exception in System.Console.SetCursorPosition in some environments for dotnet-* tools (#3678) --- diff --git a/src/Tools/Common/Commands/Utils.cs b/src/Tools/Common/Commands/Utils.cs index cb501fb38..758968633 100644 --- a/src/Tools/Common/Commands/Utils.cs +++ b/src/Tools/Common/Commands/Utils.cs @@ -160,6 +160,29 @@ namespace Microsoft.Internal.Common.Utils } private void SystemConsoleLineRewriter() => Console.SetCursorPosition(0, LineToClear); + + private static bool? _isSetCursorPositionSupported; + public bool IsRewriteConsoleLineSupported + { + get { + bool isSupported = _isSetCursorPositionSupported ?? EnsureInitialized(); + return isSupported; + + bool EnsureInitialized() + { + try { + var left = Console.CursorLeft; + var top = Console.CursorTop; + Console.SetCursorPosition(0, LineToClear); + Console.SetCursorPosition(left, top); + _isSetCursorPositionSupported = true; + } catch { + _isSetCursorPositionSupported = false; + } + return (bool)_isSetCursorPositionSupported; + } + } + } } internal class ReturnCode diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index c980e6d18..e227b26a2 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -285,16 +285,23 @@ namespace Microsoft.Diagnostics.Tools.Trace { rewriter = new LineRewriter { LineToClear = Console.CursorTop - 1 }; Console.CursorVisible = false; + if (!rewriter.IsRewriteConsoleLineSupported) + { + ConsoleWriteLine("Recording trace in progress. Press or to exit..."); + } } Action printStatus = () => { if (printStatusOverTime) { - rewriter?.RewriteConsoleLine(); - fileInfo.Refresh(); - ConsoleWriteLine($"[{stopwatch.Elapsed.ToString(@"dd\:hh\:mm\:ss")}]\tRecording trace {GetSize(fileInfo.Length)}"); - ConsoleWriteLine("Press or to exit..."); + if (rewriter.IsRewriteConsoleLineSupported) + { + rewriter?.RewriteConsoleLine(); + fileInfo.Refresh(); + ConsoleWriteLine($"[{stopwatch.Elapsed.ToString(@"dd\:hh\:mm\:ss")}]\tRecording trace {GetSize(fileInfo.Length)}"); + ConsoleWriteLine("Press or to exit..."); + } } if (rundownRequested)