Skip to content

Commit

Permalink
trace2: write exit event
Browse files Browse the repository at this point in the history
Add TRACE2 exit event, following the pattern established with the version
and start events.
  • Loading branch information
Lessley Dennington committed Feb 10, 2023
1 parent ddb7591 commit 028ad46
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/shared/Core/Trace2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum Trace2Event
Version = 0,
[EnumMember(Value = "start")]
Start = 1,
[EnumMember(Value = "exit")]
Exit = 2
}

public class Trace2Settings
Expand Down Expand Up @@ -49,6 +51,16 @@ public interface ITrace2 : IDisposable
string appPath,
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0);

/// <summary>
/// Shut down TRACE2 tracing by writing Exit event and disposing of writers.
/// </summary>
/// <param name="exitCode">The exit code of the GCM application.</param>
/// <param name="filePath">Path of the file this method is called from.</param>
/// <param name="lineNumber">Line number of file this method is called from.</param>
void Stop(int exitCode,
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0);
}

public class Trace2 : DisposableObject, ITrace2
Expand Down Expand Up @@ -93,6 +105,12 @@ public Trace2(IEnvironment environment, Trace2Settings settings, string[] argv,
WriteStart(appPath, filePath, lineNumber);
}

public void Stop(int exitCode, string filePath, int lineNumber)
{
WriteExit(exitCode, filePath, lineNumber);
ReleaseManagedResources();
}

protected override void ReleaseManagedResources()
{
lock (_writersLock)
Expand Down Expand Up @@ -233,6 +251,22 @@ private void TryParseSettings(TextWriter error, IFileSystem fileSystem)
});
}

private void WriteExit(int code, string filePath = "", int lineNumber = 0)
{
EnsureArgument.NotNull(code, nameof(code));

WriteMessage(new ExitMessage()
{
Event = Trace2Event.Exit,
Sid = _sid,
Time = DateTimeOffset.Now,
File = Path.GetFileName(filePath).ToLower(),
Line = lineNumber,
Code = code,
ElapsedTime = (DateTimeOffset.UtcNow - _applicationStartTime).TotalSeconds
});
}

private void AddWriter(ITrace2Writer writer)
{
ThrowIfDisposed();
Expand Down Expand Up @@ -362,3 +396,27 @@ public override string ToNormalString()
return BuildNormalString(string.Join(" ", Argv));
}
}

public class ExitMessage : Trace2Message
{
[JsonProperty("t_abs", Order = 7)]
public double ElapsedTime { get; set; }

[JsonProperty("code", Order = 8)]
public int Code { get; set; }

public override string ToJson()
{
return JsonConvert.SerializeObject(this,
new StringEnumConverter(),
new IsoDateTimeConverter()
{
DateTimeFormat = TimeFormat
});
}

public override string ToNormalString()
{
return BuildNormalString($"elapsed:{ElapsedTime} code:{Code}");
}
}
1 change: 1 addition & 0 deletions src/shared/Git-Credential-Manager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static void Main(string[] args)
.GetAwaiter()
.GetResult();

context.Trace2.Stop(exitCode);
Environment.Exit(exitCode);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/shared/TestInfrastructure/Objects/NullTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class NullTrace2 : ITrace2
string filePath = "",
int lineNumber = 0) { }

public void Stop(int exitCode, string fileName, int lineNumber) { }

#endregion

#region IDisposable
Expand Down

0 comments on commit 028ad46

Please sign in to comment.