using System.Diagnostics;
using System.Text.Json;
namespace XFEWarframe.Client;
public sealed record GameExitRecord(int ProcessId, DateTimeOffset StartedAt, DateTimeOffset ExitedAt, int ExitCode, string ExitCodeHex, string BuildLabel);
internal static class GameExitDiagnostics
{
internal static void Observe(int processId, string game, string build)
{
try
{
var process = Process.GetProcessById(processId);
_ = Task.Run(async () =>
{
using (process)
{
try
{
var started = new DateTimeOffset(process.StartTime.ToUniversalTime());
await process.WaitForExitAsync().ConfigureAwait(false);
var record = new GameExitRecord(processId, started, DateTimeOffset.UtcNow, process.ExitCode,
$"0x{unchecked((uint)process.ExitCode):X8}", build);
var path = ClientInstaller.Managed(game, $"logs/exit-{DateTime.UtcNow:yyyyMMdd-HHmmss}-{processId}.json");
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await File.WriteAllTextAsync(path, JsonSerializer.Serialize(record, ClientJson.Default.GameExitRecord)).ConfigureAwait(false);
}
catch { /* Best effort: diagnostics never terminate the launcher or the game. */ }
}
});
}
catch (ArgumentException) { /* Process already exited before observation began. */ }
}
}
using System.Diagnostics;
using System.Text.Json;
namespace XFEWarframe.Client;
public sealed record GameExitRecord(int ProcessId, DateTimeOffset StartedAt, DateTimeOffset ExitedAt, int ExitCode, string ExitCodeHex, string BuildLabel);
internal static class GameExitDiagnostics
{
internal static void Observe(int processId, string game, string build)
{
try
{
var process = Process.GetProcessById(processId);
_ = Task.Run(async () =>
{
using (process)
{
try
{
var started = new DateTimeOffset(process.StartTime.ToUniversalTime());
await process.WaitForExitAsync().ConfigureAwait(false);
var record = new GameExitRecord(processId, started, DateTimeOffset.UtcNow, process.ExitCode,
$"0x{unchecked((uint)process.ExitCode):X8}", build);
var path = ClientInstaller.Managed(game, $"logs/exit-{DateTime.UtcNow:yyyyMMdd-HHmmss}-{processId}.json");
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await File.WriteAllTextAsync(path, JsonSerializer.Serialize(record, ClientJson.Default.GameExitRecord)).ConfigureAwait(false);
}
catch { /* Best effort: diagnostics never terminate the launcher or the game. */ }
}
});
}
catch (ArgumentException) { /* Process already exited before observation began. */ }
}
}