mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-05 05:24:26 +01:00
Move the log and crash dump lookup out of the start use case
This commit is contained in:
@@ -62,6 +62,7 @@ public sealed class AppServices
|
||||
files, clock, log, settingsPaths.UpdateCheckPath());
|
||||
StartSimulation = new StartSimulation(State, SettingsStore, Library.Vehicles, Executables, files, paths,
|
||||
Processes, Random, log);
|
||||
Aftermath = new SimulatorAftermath(files, paths, log);
|
||||
}
|
||||
|
||||
public static AppServices Current =>
|
||||
@@ -139,5 +140,7 @@ public sealed class AppServices
|
||||
|
||||
public StartSimulation StartSimulation { get; }
|
||||
|
||||
public SimulatorAftermath Aftermath { get; }
|
||||
|
||||
public LocalizationService Localization { get; }
|
||||
}
|
||||
|
||||
81
StarterNG/Application/SimulatorAftermath.cs
Normal file
81
StarterNG/Application/SimulatorAftermath.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using StarterNG.Application.Abstractions;
|
||||
using StarterNG.Infrastructure.Adapters;
|
||||
|
||||
namespace StarterNG.Application;
|
||||
|
||||
/// <summary>What a simulator that went down left behind: its log and its crash dumps.</summary>
|
||||
public sealed class SimulatorAftermath
|
||||
{
|
||||
private readonly IFileSystem _files;
|
||||
private readonly IGamePaths _paths;
|
||||
private readonly IDiagnosticsLog _log;
|
||||
|
||||
public SimulatorAftermath(IFileSystem files, IGamePaths paths, IDiagnosticsLog log)
|
||||
{
|
||||
_files = files;
|
||||
_paths = paths;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minidumps written since the simulator was started, newest first. The
|
||||
/// simulator names them crash_*.dmp relative to its working directory - the
|
||||
/// installation - and crashpad, where it is set up, keeps its own under
|
||||
/// crashdumps; the folder of the binary is looked at too, for a simulator that
|
||||
/// was once run from there.
|
||||
/// </summary>
|
||||
public IReadOnlyList<string> CrashDumpsSince(DateTime startedUtc, string executable)
|
||||
{
|
||||
var dumps = new List<string>();
|
||||
try
|
||||
{
|
||||
foreach (string directory in new[] { _paths.Root, Path.GetDirectoryName(executable) ?? _paths.Root }
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.Where(_files.DirectoryExists))
|
||||
dumps.AddRange(_files.GetFiles(directory, "*.dmp"));
|
||||
|
||||
string crashpad = _paths.FromRoot("crashdumps");
|
||||
if (_files.DirectoryExists(crashpad))
|
||||
dumps.AddRange(_files.GetFilesRecursive(crashpad, "*.dmp"));
|
||||
|
||||
// A little slack: the start is taken just after the launch, not before it.
|
||||
var since = startedUtc - TimeSpan.FromSeconds(5);
|
||||
return dumps.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.Where(dump => _files.GetLastWriteTimeUtc(dump) >= since)
|
||||
.OrderByDescending(_files.GetLastWriteTimeUtc)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Log("looking for crash dumps", ex);
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The end of the simulator's own log, for when it died without a word on its error stream.</summary>
|
||||
public IReadOnlyList<string> SimulatorLogTail(int count)
|
||||
{
|
||||
string path = _paths.FromRoot("log.txt");
|
||||
try
|
||||
{
|
||||
if (!_files.FileExists(path))
|
||||
return Array.Empty<string>();
|
||||
|
||||
var lines = LegacyText.Decode(_files.ReadAllBytes(path))
|
||||
.Split('\n')
|
||||
.Select(line => line.TrimEnd('\r'))
|
||||
.Where(line => line.Trim().Length > 0)
|
||||
.ToList();
|
||||
return lines.Skip(Math.Max(0, lines.Count - count)).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Log($"reading {path}", ex);
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,64 +63,6 @@ public sealed class StartSimulation
|
||||
_log = log;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minidumps written since the simulator was started, newest first. The
|
||||
/// simulator names them crash_*.dmp relative to its working directory - the
|
||||
/// installation - and crashpad, where it is set up, keeps its own under
|
||||
/// crashdumps; the folder of the binary is looked at too, for a simulator that
|
||||
/// was once run from there.
|
||||
/// </summary>
|
||||
public IReadOnlyList<string> CrashDumpsSince(DateTime startedUtc, string executable)
|
||||
{
|
||||
var dumps = new List<string>();
|
||||
try
|
||||
{
|
||||
foreach (string directory in new[] { _paths.Root, Path.GetDirectoryName(executable) ?? _paths.Root }
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.Where(_files.DirectoryExists))
|
||||
dumps.AddRange(_files.GetFiles(directory, "*.dmp"));
|
||||
|
||||
string crashpad = _paths.FromRoot("crashdumps");
|
||||
if (_files.DirectoryExists(crashpad))
|
||||
dumps.AddRange(_files.GetFilesRecursive(crashpad, "*.dmp"));
|
||||
|
||||
// A little slack: the start is taken just after the launch, not before it.
|
||||
var since = startedUtc - TimeSpan.FromSeconds(5);
|
||||
return dumps.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.Where(dump => _files.GetLastWriteTimeUtc(dump) >= since)
|
||||
.OrderByDescending(_files.GetLastWriteTimeUtc)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Log("looking for crash dumps", ex);
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The end of the simulator's own log, for when it died without a word on its error stream.</summary>
|
||||
public IReadOnlyList<string> SimulatorLogTail(int count)
|
||||
{
|
||||
string path = _paths.FromRoot("log.txt");
|
||||
try
|
||||
{
|
||||
if (!_files.FileExists(path))
|
||||
return Array.Empty<string>();
|
||||
|
||||
var lines = LegacyText.Decode(_files.ReadAllBytes(path))
|
||||
.Split('\n')
|
||||
.Select(line => line.TrimEnd('\r'))
|
||||
.Where(line => line.Trim().Length > 0)
|
||||
.ToList();
|
||||
return lines.Skip(Math.Max(0, lines.Count - count)).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Log($"reading {path}", ex);
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
public static string? StartableVehicle(Trainset? trainset, string? preferred)
|
||||
{
|
||||
if (trainset is null || trainset.Vehicles.Count == 0)
|
||||
|
||||
@@ -597,7 +597,7 @@ public partial class MainWindow : Window
|
||||
|
||||
var words = exit.CleanErrors.Count > 0
|
||||
? exit.CleanErrors
|
||||
: AppServices.Current.StartSimulation.SimulatorLogTail(12);
|
||||
: AppServices.Current.Aftermath.SimulatorLogTail(12);
|
||||
|
||||
var message = new StringBuilder();
|
||||
message.AppendLine(string.Format(App.Loc["SimExitCode"], exit.DescribeCode()));
|
||||
@@ -611,7 +611,7 @@ public partial class MainWindow : Window
|
||||
}
|
||||
message.AppendLine().Append(App.Loc["SimExitSeeLog"]);
|
||||
|
||||
string? dump = AppServices.Current.StartSimulation.CrashDumpsSince(startedUtc, executable).FirstOrDefault();
|
||||
string? dump = AppServices.Current.Aftermath.CrashDumpsSince(startedUtc, executable).FirstOrDefault();
|
||||
if (dump is not null)
|
||||
message.AppendLine().AppendLine().AppendLine(string.Format(App.Loc["SimExitDump"], dump))
|
||||
.Append(App.Loc["SimExitOpenDump"]);
|
||||
|
||||
Reference in New Issue
Block a user