Skip to content

Commit

Permalink
🔧 Logging cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherFoxGuy committed Jun 21, 2021
1 parent 40dd6b3 commit 310362c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 45 deletions.
53 changes: 28 additions & 25 deletions Client/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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...";
Expand All @@ -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();
Expand All @@ -173,18 +175,18 @@ 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
Process.Start($"{currdir}/ror-updater-selfupdate.exe");
}
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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Client/Pages/ChoicePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion Client/Pages/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public MainPage()
}
catch (Exception ex)
{
Utils.LOG(ex.ToString());
Utils.LOG(Utils.LogVerb.ERROR, ex.ToString());
}
}

Expand Down
27 changes: 14 additions & 13 deletions Client/Pages/UpdatePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private async void RunFileUpdate()

private async Task InstallGame(IProgress<int> progress)
{
Utils.LOG("Info| Installing Game...");
Utils.LOG(Utils.LogVerb.INFO, "Installing Game...");

var i = 0;
foreach (var file in App.Instance.ReleaseInfoData.Filelist)
Expand All @@ -93,12 +93,12 @@ private async Task InstallGame(IProgress<int> progress)
progress?.Report(i++);
}

Utils.LOG("Info| Done.");
Utils.LOG(Utils.LogVerb.INFO, "Done.");
}

private async Task UpdateGame(IProgress<int> progress)
{
Utils.LOG("Info| Updating Game...");
Utils.LOG(Utils.LogVerb.INFO, "Updating Game...");

var filesStatus = new List<FileStatus>();

Expand All @@ -122,16 +122,16 @@ private async Task UpdateGame(IProgress<int> 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);
Expand All @@ -141,7 +141,7 @@ private async Task UpdateGame(IProgress<int> progress)
}
}

Utils.LOG("Info| Done.");
Utils.LOG(Utils.LogVerb.INFO, "Done.");
}

private void button_back_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
20 changes: 18 additions & 2 deletions Shared/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 310362c

Please sign in to comment.