From: Andrew Au Date: Mon, 3 Jun 2019 16:27:58 +0000 (-0700) Subject: Make sure we have the right exit code for dotnet-trace collect X-Git-Tag: submit/tizen/20190813.035844~6^2^2~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=049d377673644e073b560da4a856532c58a09054;p=platform%2Fcore%2Fdotnet%2Fdiagnostics.git Make sure we have the right exit code for dotnet-trace collect --- diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index 00bd26c27..c1ad9c132 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -32,17 +32,21 @@ namespace Microsoft.Diagnostics.Tools.Trace { try { - if (output == null) - throw new ArgumentNullException(nameof(output)); + Debug.Assert(output != null); + Debug.Assert(profile != null); if (processId <= 0) - throw new ArgumentException(nameof(processId)); - if (profile == null) - throw new ArgumentNullException(nameof(profile)); + { + Console.Error.WriteLine("Process ID should not be negative."); + return ErrorCodes.ArgumentError; + } var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles .FirstOrDefault(p => p.Name.Equals(profile, StringComparison.OrdinalIgnoreCase)); if (selectedProfile == null) - throw new ArgumentException($"Invalid profile name: {profile}"); + { + Console.Error.WriteLine($"Invalid profile name: {profile}"); + return ErrorCodes.ArgumentError; + } var providerCollection = Extensions.ToProviders(providers); var profileProviders = new List(); @@ -75,7 +79,10 @@ namespace Microsoft.Diagnostics.Tools.Trace providerCollection.AddRange(profileProviders); if (providerCollection.Count <= 0) - throw new ArgumentException("No providers were specified to start a trace."); + { + Console.Error.WriteLine("No providers were specified to start a trace."); + return ErrorCodes.ArgumentError; + } PrintProviders(providerCollection); @@ -86,6 +93,7 @@ namespace Microsoft.Diagnostics.Tools.Trace providers: providerCollection); var shouldExit = new ManualResetEvent(false); + var failed = false; var terminated = false; ulong sessionId = 0; @@ -95,9 +103,13 @@ namespace Microsoft.Diagnostics.Tools.Trace if (sessionId == 0) { Console.Error.WriteLine("Unable to create session."); - return -1; + return ErrorCodes.SessionCreationError; + } + if (File.Exists(output.FullName)) + { + Console.Error.WriteLine("Unable to create file."); + return ErrorCodes.FileCreationError; } - var collectingTask = new Task(() => { try { @@ -125,6 +137,7 @@ namespace Microsoft.Diagnostics.Tools.Trace } catch (Exception ex) { + failed = true; Console.Error.WriteLine($"[ERROR] {ex.ToString()}"); } finally @@ -149,7 +162,7 @@ namespace Microsoft.Diagnostics.Tools.Trace { EventPipeClient.StopTracing(processId, sessionId); } - collectingTask.Wait(); + await collectingTask; } Console.Out.WriteLine(); @@ -158,13 +171,12 @@ namespace Microsoft.Diagnostics.Tools.Trace if (format != TraceFileFormat.Netperf) TraceFileFormatConverter.ConvertToFormat(format, output.FullName); - await Task.FromResult(0); - return sessionId != 0 ? 0 : 1; + return failed ? ErrorCodes.TracingError : 0; } catch (Exception ex) { Console.Error.WriteLine($"[ERROR] {ex.ToString()}"); - return 1; + return ErrorCodes.UnknownError; } } diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/ErrorCode.cs b/src/Tools/dotnet-trace/CommandLine/Commands/ErrorCode.cs new file mode 100644 index 000000000..9987ca884 --- /dev/null +++ b/src/Tools/dotnet-trace/CommandLine/Commands/ErrorCode.cs @@ -0,0 +1,11 @@ +namespace Microsoft.Diagnostics.Tools.Trace +{ + internal static class ErrorCodes + { + public static int SessionCreationError = 1; + public static int FileCreationError = 2; + public static int TracingError = 3; + public static int ArgumentError = 4; + public static int UnknownError = 5; + } +} \ No newline at end of file