From 310362cf0aed466aae7bcc6fc4a09a339f80179a Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 21 Jun 2021 13:09:18 +0200 Subject: [PATCH] :wrench: Logging cleaned up --- Client/App.xaml.cs | 53 +++++++++++++++++---------------- Client/Pages/ChoicePage.xaml.cs | 8 ++--- Client/Pages/MainPage.xaml.cs | 2 +- Client/Pages/UpdatePage.xaml.cs | 27 +++++++++-------- Shared/Utils.cs | 20 +++++++++++-- 5 files changed, 65 insertions(+), 45 deletions(-) diff --git a/Client/App.xaml.cs b/Client/App.xaml.cs index beec599..4b35133 100644 --- a/Client/App.xaml.cs +++ b/Client/App.xaml.cs @@ -61,26 +61,27 @@ public void InitApp(object sender, StartupEventArgs e) // Render the form _sForm.Update(); - File.WriteAllText(Utils.LogPath, "Updater Started"); + File.WriteAllText(Utils.LogPath, "Updater Started\n"); SentrySdk.ConfigureScope(scope => { scope.AddAttachment(Utils.LogPath); }); var assembly = Assembly.GetExecutingAssembly(); var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); _localUpdaterVersion = fileVersionInfo.ProductVersion; - Utils.LOG($"Info| Updater version: {_localUpdaterVersion}"); + Utils.LOG(Utils.LogVerb.DEBUG, $"Updater version: {_localUpdaterVersion}"); - if (File.Exists("RoR.exe") && Utils.FileIsInUse("RoR.exe")) + /* TODO: Not working? + if (File.Exists("RoR.exe") && Utils.FileIsInUse("RoR.exe")) { - Utils.LOG($"Error| game in use"); + Utils.LOG($"game in use"); MessageBox.Show("Please close the game before starting the updater", "Error", MessageBoxButton.OK, MessageBoxImage.Error); Quit(); - } + }*/ - Utils.LOG("Info| Creating Web Handler"); + Utils.LOG(Utils.LogVerb.INFO, "Creating Web Handler"); _webClient = new WebClient(); - Utils.LOG("Info| Done."); + Utils.LOG(Utils.LogVerb.INFO, "Done."); var currentDirectory = Directory.GetCurrentDirectory(); @@ -93,8 +94,8 @@ public void InitApp(object sender, StartupEventArgs e) } catch (Exception ex) { - Utils.LOG("Error| Failed to read settings file"); - Utils.LOG(ex.ToString()); + Utils.LOG(Utils.LogVerb.ERROR, "Failed to read settings file"); + Utils.LOG(Utils.LogVerb.ERROR, ex.ToString()); SentrySdk.CaptureException(ex); Settings = new Settings(); Settings.SetDefaults(); @@ -108,11 +109,11 @@ public void InitApp(object sender, StartupEventArgs e) CDNUrl = Settings.ServerUrl; - Utils.LOG("Info| Done."); - Utils.LOG($"Info| Skip_updates: {Settings.SkipUpdates}"); + Utils.LOG(Utils.LogVerb.INFO, "Done."); + Utils.LOG(Utils.LogVerb.INFO, $"Skip_updates: {Settings.SkipUpdates}"); //Download list - Utils.LOG($"Info| Downloading main list from server: {Settings.ServerUrl}/branches.json"); + Utils.LOG(Utils.LogVerb.INFO, $"Downloading main list from server: {Settings.ServerUrl}/branches.json"); try { var brjson = _webClient.DownloadString($"{Settings.ServerUrl}/branches.json"); @@ -121,17 +122,18 @@ public void InitApp(object sender, StartupEventArgs e) } catch (Exception ex) { - Utils.LOG(ex.ToString()); + Utils.LOG(Utils.LogVerb.ERROR, ex.ToString()); SentrySdk.CaptureException(ex); var result = MessageBox.Show("Could not connect to the main server.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); if (result == MessageBoxResult.OK) { - Utils.LOG("Error| Failed to connect to server."); + Utils.LOG(Utils.LogVerb.ERROR, "Failed to connect to server."); Quit(); } } + Utils.LOG(Utils.LogVerb.DEBUG, $"Online updater version: {BranchInfo?.UpdaterVersion}"); if (_localUpdaterVersion != BranchInfo?.UpdaterVersion && !Settings.SkipUpdates) { _sForm.label1.Text = @"Updating..."; @@ -144,17 +146,17 @@ public void InitApp(object sender, StartupEventArgs e) var versionInfo = FileVersionInfo.GetVersionInfo("RoR.exe"); LocalVersion = versionInfo.ProductVersion; - Utils.LOG("Info| local RoR ver: " + LocalVersion); + Utils.LOG(Utils.LogVerb.INFO, "Local RoR version: " + LocalVersion); } catch { LocalVersion = "unknown"; - Utils.LOG("Info| Game Not found!"); + Utils.LOG(Utils.LogVerb.INFO, "Game Not found!"); } - Utils.LOG("Info| Done."); - Utils.LOG("Success| Initialization done!"); + Utils.LOG(Utils.LogVerb.INFO, "Done."); + Utils.LOG(Utils.LogVerb.INFO, "Initialization done!"); _pageSwitcher = new PageSwitcher(); _pageSwitcher.Show(); @@ -173,10 +175,10 @@ void ProcessSelfUpdate() try { var currdir = Directory.GetCurrentDirectory(); - Utils.LOG($"Downloading {Settings.ServerUrl}/selfupdate.exe"); + Utils.LOG(Utils.LogVerb.INFO, $"Downloading {Settings.ServerUrl}/selfupdate.exe"); _webClient.DownloadFile($"{Settings.ServerUrl}/selfupdate.exe", $"{currdir}/ror-updater-selfupdate.exe"); - Utils.LOG($"Downloading {Settings.ServerUrl}/patch.zip"); + Utils.LOG(Utils.LogVerb.INFO, $"Downloading {Settings.ServerUrl}/patch.zip"); _webClient.DownloadFile($"{Settings.ServerUrl}/patch.zip", $"{Path.GetTempPath()}/patch.zip"); Thread.Sleep(100); //Wait a bit @@ -184,7 +186,7 @@ void ProcessSelfUpdate() } catch (Exception ex) { - Utils.LOG(ex.ToString()); + Utils.LOG(Utils.LogVerb.ERROR, ex.ToString()); MessageBox.Show("SelfUpdate error", "Error", MessageBoxButton.OK, MessageBoxImage.Error); SentrySdk.CaptureException(ex); @@ -212,8 +214,8 @@ public void UpdateBranch(string branchname) } catch (Exception ex) { - Utils.LOG($"Error| Failed to switch to branch {branchname}"); - Utils.LOG(ex.ToString()); + Utils.LOG(Utils.LogVerb.ERROR, $"Failed to switch to branch {branchname}"); + Utils.LOG(Utils.LogVerb.ERROR, ex.ToString()); SelectedBranch = BranchInfo.Branches.First().Value; } @@ -230,13 +232,14 @@ public void UpdateBranch(string branchname) } catch (Exception ex) { - Utils.LOG(ex.ToString()); + Utils.LOG(Utils.LogVerb.ERROR, ex.ToString()); MessageBox.Show("Failed to download branch info", "Error", MessageBoxButton.OK, MessageBoxImage.Error); SentrySdk.CaptureException(ex); } - Utils.LOG($"Info| Switched to branch: {SelectedBranch.Name} Version: {ReleaseInfoData.Version}"); + Utils.LOG(Utils.LogVerb.INFO, + $"Switched to branch: {SelectedBranch.Name} Version: {ReleaseInfoData.Version}"); } void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) diff --git a/Client/Pages/ChoicePage.xaml.cs b/Client/Pages/ChoicePage.xaml.cs index f5d5d8b..1706060 100644 --- a/Client/Pages/ChoicePage.xaml.cs +++ b/Client/Pages/ChoicePage.xaml.cs @@ -29,7 +29,7 @@ public partial class ChoicePage : UserControl, ISwitchable public ChoicePage() { InitializeComponent(); - Utils.LOG("Info| Choice menu opened."); + Utils.LOG(Utils.LogVerb.INFO, "Choice menu opened."); //Repair game is also update game, both do the same, both do their work. @@ -76,21 +76,21 @@ private void button_back_Click(object sender, RoutedEventArgs e) private void Install_button_Click(object sender, RoutedEventArgs e) { - Utils.LOG("Info| Selected Install."); + Utils.LOG(Utils.LogVerb.INFO, "Selected Install."); App.Choice = UpdateChoice.INSTALL; PageManager.Switch(new UpdatePage()); } private void Repair_button_Click(object sender, RoutedEventArgs e) { - Utils.LOG("Info| Selected Repair."); + Utils.LOG(Utils.LogVerb.INFO, "Selected Repair."); App.Choice = UpdateChoice.REPAIR; PageManager.Switch(new UpdatePage()); } private void Update_button_Click(object sender, RoutedEventArgs e) { - Utils.LOG("Info| Selected Update."); + Utils.LOG(Utils.LogVerb.INFO, "Selected Update."); App.Choice = UpdateChoice.UPDATE; PageManager.Switch(new UpdatePage()); } diff --git a/Client/Pages/MainPage.xaml.cs b/Client/Pages/MainPage.xaml.cs index 4abec29..20d1f8e 100644 --- a/Client/Pages/MainPage.xaml.cs +++ b/Client/Pages/MainPage.xaml.cs @@ -44,7 +44,7 @@ public MainPage() } catch (Exception ex) { - Utils.LOG(ex.ToString()); + Utils.LOG(Utils.LogVerb.ERROR, ex.ToString()); } } diff --git a/Client/Pages/UpdatePage.xaml.cs b/Client/Pages/UpdatePage.xaml.cs index 0126180..a9855f0 100644 --- a/Client/Pages/UpdatePage.xaml.cs +++ b/Client/Pages/UpdatePage.xaml.cs @@ -83,7 +83,7 @@ private async void RunFileUpdate() private async Task InstallGame(IProgress progress) { - Utils.LOG("Info| Installing Game..."); + Utils.LOG(Utils.LogVerb.INFO, "Installing Game..."); var i = 0; foreach (var file in App.Instance.ReleaseInfoData.Filelist) @@ -93,12 +93,12 @@ private async Task InstallGame(IProgress progress) progress?.Report(i++); } - Utils.LOG("Info| Done."); + Utils.LOG(Utils.LogVerb.INFO, "Done."); } private async Task UpdateGame(IProgress progress) { - Utils.LOG("Info| Updating Game..."); + Utils.LOG(Utils.LogVerb.INFO, "Updating Game..."); var filesStatus = new List(); @@ -122,16 +122,16 @@ private async Task UpdateGame(IProgress progress) switch (item.Status) { case HashResult.UPTODATE: - Utils.LOG($"Info| file up to date: {item.File.Name}"); + Utils.LOG(Utils.LogVerb.INFO, $"file up to date: {item.File.Name}"); AddToLogFile($"File up to date: {item.File.Directory.TrimStart('.')}/{item.File.Name}"); break; case HashResult.OUTOFDATE: AddToLogFile($"File out of date: {item.File.Directory.TrimStart('.')}/{item.File.Name}"); - Utils.LOG($"Info| File out of date: {item.File.Name}"); + Utils.LOG(Utils.LogVerb.INFO, $"File out of date: {item.File.Name}"); await DownloadFile(item.File.Directory, item.File.Name); break; case HashResult.NOT_FOUND: - Utils.LOG($"Info| File doesnt exits: {item.File.Name}"); + Utils.LOG(Utils.LogVerb.INFO, $"File doesnt exits: {item.File.Name}"); AddToLogFile( $"Downloading new file: {item.File.Directory.TrimStart('.')}/{item.File.Name}"); await DownloadFile(item.File.Directory, item.File.Name); @@ -141,7 +141,7 @@ private async Task UpdateGame(IProgress progress) } } - Utils.LOG("Info| Done."); + Utils.LOG(Utils.LogVerb.INFO, "Done."); } private void button_back_Click(object sender, RoutedEventArgs e) @@ -181,11 +181,12 @@ HashResult HashFile(PFileInfo item) string sFileHash = null; var filePath = $"{item.Directory}/{item.Name}"; - Utils.LOG($"Info| Checking file: {item.Name}"); + Utils.LOG(Utils.LogVerb.INFO, $"Checking file: {item.Name}"); if (!File.Exists(filePath)) return HashResult.NOT_FOUND; sFileHash = Utils.GetFileHash(filePath); - Utils.LOG($"Info| {item.Name} Hash: Local: {sFileHash.ToLower()} Online: {item.Hash.ToLower()}"); + Utils.LOG(Utils.LogVerb.INFO, + $"{item.Name} Hash: Local: {sFileHash.ToLower()} Online: {item.Hash.ToLower()}"); return sFileHash.ToLower().Equals(item.Hash.ToLower()) ? HashResult.UPTODATE : HashResult.OUTOFDATE; @@ -203,19 +204,19 @@ private async Task DownloadFile(string dir, string file) try { - Utils.LOG($"Info| ULR: {dlLink}"); - Utils.LOG($"Info| File: {dest}"); + Utils.LOG(Utils.LogVerb.INFO, $"ULR: {dlLink}"); + Utils.LOG(Utils.LogVerb.INFO, $"File: {dest}"); await _webClient.DownloadFileTaskAsync(new Uri(dlLink), dest); } catch (Exception ex) { - Utils.LOG(ex.ToString()); + Utils.LOG(Utils.LogVerb.ERROR, ex.ToString()); MessageBox.Show($"Failed to download file: {dest}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); SentrySdk.CaptureException(ex); } } - + private class FileStatus { public PFileInfo File; diff --git a/Shared/Utils.cs b/Shared/Utils.cs index 2fed12a..1bb80a2 100644 --- a/Shared/Utils.cs +++ b/Shared/Utils.cs @@ -29,10 +29,26 @@ internal class Utils { internal static readonly string LogPath = $"{Path.GetTempPath()}/RoR_Updater_Log.txt"; - public static void LOG(string str) + internal enum LogVerb + { + DEBUG, + INFO, + ERROR + } + + public static void LOG(LogVerb verb, string str) { var file = new StreamWriter(LogPath, true); - file.WriteLine(str); + + var prefix = verb switch + { + LogVerb.DEBUG => "Debug", + LogVerb.INFO => "Info", + LogVerb.ERROR => "Error", + _ => "" + }; + + file.WriteLine($"{prefix} | {str}"); file.Close(); }