Clean up error code used by dotnet-counters and dotnet-trace (#2412)
authorSung Yoon Whang <suwhang@microsoft.com>
Tue, 6 Jul 2021 05:39:22 +0000 (22:39 -0700)
committerGitHub <noreply@github.com>
Tue, 6 Jul 2021 05:39:22 +0000 (22:39 -0700)
src/Tools/Common/Commands/Utils.cs
src/Tools/dotnet-counters/CounterMonitor.cs
src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

index 2205e987588be58a81a59ea91a09044f31023ae5..cb501fb3871c7b8826aa6640bdcff348df3d8390 100644 (file)
@@ -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;
+    }
 }
index c8eee8df70ec7b7759a650a64a639678b10061d6..437a57a4085ac0974997c23114d896be4b2bb000 100644 (file)
@@ -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);
         }
     }
 }
index 65856947ae695f00b06bde0661d428fe2dfc65bd..710119cd0f44ba85393dd9fc17bec1267c8c3ccf 100644 (file)
@@ -45,11 +45,11 @@ namespace Microsoft.Diagnostics.Tools.Trace
         /// <returns></returns>
         private static async Task<int> 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.