From f31ff2ed3465b05cce71d31e752acad401c68416 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Mon, 5 Jul 2021 22:39:22 -0700 Subject: [PATCH] Clean up error code used by dotnet-counters and dotnet-trace (#2412) --- src/Tools/Common/Commands/Utils.cs | 9 ++++++++ src/Tools/dotnet-counters/CounterMonitor.cs | 22 +++++++++---------- .../CommandLine/Commands/CollectCommand.cs | 22 +++++++++---------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/Tools/Common/Commands/Utils.cs b/src/Tools/Common/Commands/Utils.cs index 2205e9875..cb501fb38 100644 --- a/src/Tools/Common/Commands/Utils.cs +++ b/src/Tools/Common/Commands/Utils.cs @@ -161,4 +161,13 @@ namespace Microsoft.Internal.Common.Utils private void SystemConsoleLineRewriter() => Console.SetCursorPosition(0, LineToClear); } + + internal class ReturnCode + { + public static int Ok = 0; + public static int SessionCreationError = 1; + public static int TracingError = 2; + public static int ArgumentError = 3; + public static int UnknownError = 4; + } } diff --git a/src/Tools/dotnet-counters/CounterMonitor.cs b/src/Tools/dotnet-counters/CounterMonitor.cs index c8eee8df7..437a57a40 100644 --- a/src/Tools/dotnet-counters/CounterMonitor.cs +++ b/src/Tools/dotnet-counters/CounterMonitor.cs @@ -95,7 +95,7 @@ namespace Microsoft.Diagnostics.Tools.Counters { if (!ProcessLauncher.Launcher.HasChildProc && !CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out _processId)) { - return 0; + return ReturnCode.ArgumentError; } shouldExit = new ManualResetEvent(false); _ct.Register(() => shouldExit.Set()); @@ -105,7 +105,7 @@ namespace Microsoft.Diagnostics.Tools.Counters { if (holder == null) { - return 1; + return ReturnCode.Ok; } try { @@ -129,7 +129,7 @@ namespace Microsoft.Diagnostics.Tools.Counters catch (Exception) { } // Swallow all exceptions for now. console.Out.WriteLine($"Complete"); - return 1; + return ReturnCode.Ok; } } } @@ -139,7 +139,7 @@ namespace Microsoft.Diagnostics.Tools.Counters { if (!ProcessLauncher.Launcher.HasChildProc && !CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out _processId)) { - return 0; + return ReturnCode.ArgumentError; } shouldExit = new ManualResetEvent(false); @@ -150,7 +150,7 @@ namespace Microsoft.Diagnostics.Tools.Counters { if (holder == null) { - return 1; + return ReturnCode.Ok; } try @@ -164,7 +164,7 @@ namespace Microsoft.Diagnostics.Tools.Counters if (_output.Length == 0) { _console.Error.WriteLine("Output cannot be an empty string"); - return 0; + return ReturnCode.ArgumentError; } if (format == CountersExportFormat.csv) { @@ -188,7 +188,7 @@ namespace Microsoft.Diagnostics.Tools.Counters else { _console.Error.WriteLine($"The output format {format} is not a valid output format."); - return 0; + return ReturnCode.ArgumentError; } shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort) || resumeRuntime; int ret = await Start(); @@ -201,7 +201,7 @@ namespace Microsoft.Diagnostics.Tools.Counters _session.Stop(); } catch (Exception) { } // session.Stop() can throw if target application already stopped before we send the stop command. - return 1; + return ReturnCode.Ok; } } } @@ -332,7 +332,7 @@ namespace Microsoft.Diagnostics.Tools.Counters string providerString = BuildProviderString(); if (providerString.Length == 0) { - return 1; + return ReturnCode.ArgumentError; } _renderer.Initialize(); @@ -373,7 +373,7 @@ namespace Microsoft.Diagnostics.Tools.Counters if (shouldExit.WaitOne(250)) { StopMonitor(); - return 0; + return ReturnCode.Ok; } if (Console.KeyAvailable) { @@ -395,7 +395,7 @@ namespace Microsoft.Diagnostics.Tools.Counters pauseCmdSet = false; } } - return await Task.FromResult(0); + return await Task.FromResult(ReturnCode.Ok); } } } diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index 65856947a..710119cd0 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -45,11 +45,11 @@ namespace Microsoft.Diagnostics.Tools.Trace /// private static async Task Collect(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string diagnosticPort, bool showchildio, bool resumeRuntime) { - int ret = 0; bool collectionStopped = false; bool cancelOnEnter = true; bool cancelOnCtrlC = true; bool printStatusOverTime = true; + int ret = ReturnCode.Ok; try { @@ -80,7 +80,7 @@ namespace Microsoft.Diagnostics.Tools.Trace if (showchildio) { Console.WriteLine("--show-child-io must not be specified when attaching to a process"); - return ErrorCodes.ArgumentError; + return ReturnCode.ArgumentError; } if (CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out int resolvedProcessId)) { @@ -88,12 +88,12 @@ namespace Microsoft.Diagnostics.Tools.Trace } else { - return ErrorCodes.ArgumentError; + return ReturnCode.ArgumentError; } } else if (!CommandUtils.ValidateArgumentsForChildProcess(processId, name, diagnosticPort)) { - return ErrorCodes.ArgumentError; + return ReturnCode.ArgumentError; } if (profile.Length == 0 && providers.Length == 0 && clrevents.Length == 0) @@ -117,7 +117,7 @@ namespace Microsoft.Diagnostics.Tools.Trace if (selectedProfile == null) { Console.Error.WriteLine($"Invalid profile name: {profile}"); - return ErrorCodes.ArgumentError; + return ReturnCode.ArgumentError; } Profile.MergeProfileAndProviders(selectedProfile, providerCollection, enabledBy); @@ -143,7 +143,7 @@ namespace Microsoft.Diagnostics.Tools.Trace if (providerCollection.Count <= 0) { Console.Error.WriteLine("No providers were specified to start a trace."); - return ErrorCodes.ArgumentError; + return ReturnCode.ArgumentError; } PrintProviders(providerCollection, enabledBy); @@ -160,7 +160,7 @@ namespace Microsoft.Diagnostics.Tools.Trace // if builder returned null, it means we received ctrl+C while waiting for clients to connect. Exit gracefully. if (holder == null) { - return await Task.FromResult(ret); + return await Task.FromResult(ReturnCode.Ok); } diagnosticsClient = holder.Client; if (ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort)) @@ -187,7 +187,7 @@ namespace Microsoft.Diagnostics.Tools.Trace if (attempts > 10) { Console.Error.WriteLine("Unable to examine process."); - return ErrorCodes.SessionCreationError; + return ReturnCode.SessionCreationError; } Thread.Sleep(200); } @@ -224,7 +224,7 @@ namespace Microsoft.Diagnostics.Tools.Trace if (session == null) { Console.Error.WriteLine("Unable to create session."); - return ErrorCodes.SessionCreationError; + return ReturnCode.SessionCreationError; } if (shouldStopAfterDuration) @@ -325,8 +325,8 @@ namespace Microsoft.Diagnostics.Tools.Trace catch (Exception ex) { Console.Error.WriteLine($"[ERROR] {ex.ToString()}"); - ret = ErrorCodes.TracingError; collectionStopped = true; + ret = ReturnCode.TracingError; } finally { @@ -340,7 +340,7 @@ namespace Microsoft.Diagnostics.Tools.Trace { if (!collectionStopped || ct.IsCancellationRequested) { - ret = ErrorCodes.TracingError; + ret = ReturnCode.TracingError; } // If we launched a child proc that hasn't exited yet, terminate it before we exit. -- 2.34.1