From c2c1670ab2154205bdbb4d08951bc4aa830a630a Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Wed, 10 Jul 2024 13:33:19 -0400 Subject: [PATCH] write debug log to seperate file --- SPTInstaller/App.axaml.cs | 21 ++++++++++++------- SPTInstaller/Models/InternalData.cs | 1 + SPTInstaller/SPTInstaller.csproj | 4 ++-- .../InstallPathSelectionViewModel.cs | 6 ++---- .../ViewModels/InstallerUpdateViewModel.cs | 8 +++---- .../ViewModels/MainWindowViewModel.cs | 10 ++++++--- SPTInstaller/ViewModels/MessageViewModel.cs | 4 +++- SPTInstaller/ViewModels/OverviewViewModel.cs | 6 ++---- SPTInstaller/ViewModels/PreChecksViewModel.cs | 5 +++-- 9 files changed, 37 insertions(+), 28 deletions(-) diff --git a/SPTInstaller/App.axaml.cs b/SPTInstaller/App.axaml.cs index f8cd840..4b6ae50 100644 --- a/SPTInstaller/App.axaml.cs +++ b/SPTInstaller/App.axaml.cs @@ -9,13 +9,16 @@ using SPTInstaller.ViewModels; using SPTInstaller.Views; using System.Reactive; using System.Text; -using DynamicData; +using SPTInstaller.Helpers; +using SPTInstaller.Models; namespace SPTInstaller; public partial class App : Application { public static string LogPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "spt-installer", "spt-installer.log"); + public static string LogDebugPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "spt-installer", "spt-isntaller-debug.log"); public static void ReLaunch(bool debug, string installPath = "") { @@ -56,34 +59,38 @@ public partial class App : Application { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - var debug = false; + var data = ServiceHelper.Get() ?? throw new Exception("failed to get internal data"); + + data.DebugMode = false; var providedPath = ""; if (desktop.Args != null) { - debug = desktop.Args.Any(x => x.ToLower() == "debug"); + data.DebugMode = desktop.Args.Any(x => x.ToLower() == "debug"); var installPath = desktop.Args.FirstOrDefault(x => x.StartsWith("installPath=", StringComparison.CurrentCultureIgnoreCase)); providedPath = installPath != null && installPath.Contains('=') ? installPath?.Split('=')[1] ?? "" : ""; } - if (debug) + if (data.DebugMode) { + Log.CloseAndFlush(); + Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo - .File(path: LogPath, + .File(path: LogDebugPath, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug) .CreateLogger(); - System.Diagnostics.Trace.Listeners.Add(new SerilogTraceListener.SerilogTraceListener()); + Trace.Listeners.Add(new SerilogTraceListener.SerilogTraceListener()); Log.Debug("TraceListener is registered"); } desktop.MainWindow = new MainWindow { - DataContext = new MainWindowViewModel(providedPath, debug), + DataContext = new MainWindowViewModel(providedPath), }; } diff --git a/SPTInstaller/Models/InternalData.cs b/SPTInstaller/Models/InternalData.cs index 7f47166..96d77a3 100644 --- a/SPTInstaller/Models/InternalData.cs +++ b/SPTInstaller/Models/InternalData.cs @@ -5,6 +5,7 @@ namespace SPTInstaller.Models; public class InternalData { + public bool DebugMode { get; set; } = false; /// /// The folder to install SPT into /// diff --git a/SPTInstaller/SPTInstaller.csproj b/SPTInstaller/SPTInstaller.csproj index 706711b..552f7d4 100644 --- a/SPTInstaller/SPTInstaller.csproj +++ b/SPTInstaller/SPTInstaller.csproj @@ -10,8 +10,8 @@ icon.ico Assets\spt_installer.ico Debug;Release;TEST - 2.87 - 2.87 + 2.88 + 2.88 SPT diff --git a/SPTInstaller/ViewModels/InstallPathSelectionViewModel.cs b/SPTInstaller/ViewModels/InstallPathSelectionViewModel.cs index 332d64c..1c08794 100644 --- a/SPTInstaller/ViewModels/InstallPathSelectionViewModel.cs +++ b/SPTInstaller/ViewModels/InstallPathSelectionViewModel.cs @@ -13,7 +13,6 @@ namespace SPTInstaller.ViewModels; public class InstallPathSelectionViewModel : ViewModelBase { - private bool _debugging = false; private InternalData _data; private string _selectedPath; @@ -39,9 +38,8 @@ public class InstallPathSelectionViewModel : ViewModelBase set => this.RaiseAndSetIfChanged(ref _errorMessage, value); } - public InstallPathSelectionViewModel(IScreen host, string installPath, bool debugging) : base(host) + public InstallPathSelectionViewModel(IScreen host, string installPath) : base(host) { - _debugging = debugging; _data = ServiceHelper.Get() ?? throw new Exception("Failed to get internal data"); SelectedPath = Environment.CurrentDirectory; ValidPath = false; @@ -154,6 +152,6 @@ public class InstallPathSelectionViewModel : ViewModelBase _data.TargetInstallPath = SelectedPath; - NavigateTo(new PreChecksViewModel(HostScreen, _debugging)); + NavigateTo(new PreChecksViewModel(HostScreen)); } } \ No newline at end of file diff --git a/SPTInstaller/ViewModels/InstallerUpdateViewModel.cs b/SPTInstaller/ViewModels/InstallerUpdateViewModel.cs index de4ad17..045c2ae 100644 --- a/SPTInstaller/ViewModels/InstallerUpdateViewModel.cs +++ b/SPTInstaller/ViewModels/InstallerUpdateViewModel.cs @@ -12,11 +12,9 @@ public class InstallerUpdateViewModel : ViewModelBase public InstallerUpdateInfo UpdateInfo { get; set; } = new(); private InternalData _data; - private bool _debugging; private string _installPath; - public InstallerUpdateViewModel(IScreen Host, string installPath, bool debugging) : base(Host) + public InstallerUpdateViewModel(IScreen Host, string installPath) : base(Host) { - _debugging = debugging; _installPath = installPath; _data = ServiceHelper.Get() ?? throw new Exception("Failed to get internal data"); @@ -27,14 +25,14 @@ public class InstallerUpdateViewModel : ViewModelBase if (!UpdateInfo.UpdateAvailable) { - NavigateTo(new OverviewViewModel(HostScreen, _installPath, _debugging)); + NavigateTo(new OverviewViewModel(HostScreen, _installPath)); } }); } public void NotNowCommand() { - NavigateTo(new OverviewViewModel(HostScreen, _installPath, _debugging)); + NavigateTo(new OverviewViewModel(HostScreen, _installPath)); } public async Task UpdateInstallCommand() diff --git a/SPTInstaller/ViewModels/MainWindowViewModel.cs b/SPTInstaller/ViewModels/MainWindowViewModel.cs index 811fd9f..116944a 100644 --- a/SPTInstaller/ViewModels/MainWindowViewModel.cs +++ b/SPTInstaller/ViewModels/MainWindowViewModel.cs @@ -3,6 +3,8 @@ using ReactiveUI; using Serilog; using System.Globalization; using System.Reflection; +using SPTInstaller.Helpers; +using SPTInstaller.Models; namespace SPTInstaller.ViewModels; @@ -19,10 +21,12 @@ public class MainWindowViewModel : ReactiveObject, IActivatableViewModel, IScree set => this.RaiseAndSetIfChanged(ref _title, value); } - public MainWindowViewModel(string installPath, bool debugging) + public MainWindowViewModel(string installPath) { + var data = ServiceHelper.Get() ?? throw new Exception("failed to get interanl data"); + Title = - $"{(debugging ? "-debug-" : "")} SPT Installer {"v" + Assembly.GetExecutingAssembly().GetName()?.Version?.ToString() ?? "--unknown version--"}"; + $"{(data.DebugMode ? "-debug-" : "")} SPT Installer {"v" + Assembly.GetExecutingAssembly().GetName()?.Version?.ToString() ?? "--unknown version--"}"; Log.Information($"========= {Title} Started ========="); Log.Information(Environment.OSVersion.VersionString); @@ -31,7 +35,7 @@ public class MainWindowViewModel : ReactiveObject, IActivatableViewModel, IScree Log.Information("System Language: {iso} - {name}", uiCulture.TwoLetterISOLanguageName, uiCulture.DisplayName); - Router.Navigate.Execute(new InstallerUpdateViewModel(this, installPath, debugging)); + Router.Navigate.Execute(new InstallerUpdateViewModel(this, installPath)); } public void CloseCommand() diff --git a/SPTInstaller/ViewModels/MessageViewModel.cs b/SPTInstaller/ViewModels/MessageViewModel.cs index 1755f75..3f348a9 100644 --- a/SPTInstaller/ViewModels/MessageViewModel.cs +++ b/SPTInstaller/ViewModels/MessageViewModel.cs @@ -80,6 +80,8 @@ public class MessageViewModel : ViewModelBase public ICommand CopyLogFileToClipboard => ReactiveCommand.CreateFromTask(async () => { + var data = ServiceHelper.Get(); + if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { try @@ -91,7 +93,7 @@ public class MessageViewModel : ViewModelBase } var dataObject = new DataObject(); - var logFile = await desktop.MainWindow.StorageProvider.TryGetFileFromPathAsync(App.LogPath); + var logFile = await desktop.MainWindow.StorageProvider.TryGetFileFromPathAsync(data.DebugMode ? App.LogDebugPath : App.LogPath); if (logFile == null) { diff --git a/SPTInstaller/ViewModels/OverviewViewModel.cs b/SPTInstaller/ViewModels/OverviewViewModel.cs index f9d9eb9..1a8dd5f 100644 --- a/SPTInstaller/ViewModels/OverviewViewModel.cs +++ b/SPTInstaller/ViewModels/OverviewViewModel.cs @@ -6,11 +6,9 @@ namespace SPTInstaller.ViewModels; public class OverviewViewModel : ViewModelBase { private string _providedPath; - private bool _debugging; - public OverviewViewModel(IScreen Host, string providedPath, bool debugging) : base(Host) + public OverviewViewModel(IScreen Host, string providedPath) : base(Host) { _providedPath = providedPath; - _debugging = debugging; if (!string.IsNullOrEmpty(_providedPath)) { @@ -20,6 +18,6 @@ public class OverviewViewModel : ViewModelBase public void NextCommand() { - NavigateTo(new InstallPathSelectionViewModel(HostScreen, _providedPath, _debugging)); + NavigateTo(new InstallPathSelectionViewModel(HostScreen, _providedPath)); } } \ No newline at end of file diff --git a/SPTInstaller/ViewModels/PreChecksViewModel.cs b/SPTInstaller/ViewModels/PreChecksViewModel.cs index 9b32d52..ec84d0a 100644 --- a/SPTInstaller/ViewModels/PreChecksViewModel.cs +++ b/SPTInstaller/ViewModels/PreChecksViewModel.cs @@ -109,12 +109,13 @@ public class PreChecksViewModel : ViewModelBase }); } - public PreChecksViewModel(IScreen host, bool debugging) : base(host) + public PreChecksViewModel(IScreen host) : base(host) { - Debugging = debugging; var data = ServiceHelper.Get(); var installer = ServiceHelper.Get(); + Debugging = data.DebugMode; + installer.RecheckRequested += ReCheckRequested; InstallButtonText = "Please wait ...";