diff --git a/.gitignore b/.gitignore index 9491a2f..af1fbf9 100644 --- a/.gitignore +++ b/.gitignore @@ -360,4 +360,7 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd + +JetBrains IDE +/**/.idea/* \ No newline at end of file diff --git a/Universal x86 Tuning Utility/App.xaml.cs b/Universal x86 Tuning Utility/App.xaml.cs index 2989155..57460f6 100644 --- a/Universal x86 Tuning Utility/App.xaml.cs +++ b/Universal x86 Tuning Utility/App.xaml.cs @@ -20,10 +20,10 @@ using Universal_x86_Tuning_Utility.Scripts; using System.Threading.Tasks; using System.Threading; -using System.Diagnostics.Metrics; -using System.Windows.Interop; -using Universal_x86_Tuning_Utility.Views.Windows; +using Microsoft.Extensions.Logging; using RyzenSmu; +using Serilog; +using Serilog.Sinks.SystemConsole.Themes; using Universal_x86_Tuning_Utility.Scripts.ASUS; namespace Universal_x86_Tuning_Utility @@ -42,6 +42,7 @@ public partial class App public static ASUSWmi wmi; public static XgMobileConnectionService xgMobileConnectionService; + private static ILogger? _logger; /// /// Gets registered service. @@ -75,6 +76,14 @@ public static bool IsAdministrator() /// private async void OnStartup(object sender, StartupEventArgs e) { + Log.Logger = new LoggerConfiguration() + .WriteTo.Console(theme: AnsiConsoleTheme.Code, applyThemeToRedirectedOutput: true) + .WriteTo.File(LOGS_FOLDER + "uxtu_log.txt", + fileSizeLimitBytes: 8*1024*1024, // 8MB + rollingInterval: RollingInterval.Day + ) + .CreateLogger(); + try { if (!App.IsAdministrator()) @@ -95,11 +104,19 @@ private async void OnStartup(object sender, StartupEventArgs e) await Task.Run(() => product = GetSystemInfo.Product); Display.setUpLists(); } - catch { } + catch (Exception ex) + { + Log.Logger.Error(ex, "Failed to setup product and display refresh rates"); + } _host = Host .CreateDefaultBuilder() .ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)); }) + .ConfigureLogging(logging => + { + logging.ClearProviders(); + logging.AddSerilog(); + }) .ConfigureServices((context, services) => { // App Host @@ -123,7 +140,11 @@ private async void OnStartup(object sender, StartupEventArgs e) Settings.Default.isASUS = true; Settings.Default.Save(); } - } catch { } + } + catch (Exception ex) + { + Log.Logger.Error(ex, "Failed to setup ASUS WMI services"); + } // Theme manipulation services.AddSingleton(); @@ -159,6 +180,7 @@ private async void OnStartup(object sender, StartupEventArgs e) services.Configure(context.Configuration.GetSection(nameof(AppConfig))); }).Build(); + _logger = _host.Services.GetRequiredService>(); _ = Tablet.TabletDevices; bool firstBoot = false; @@ -172,7 +194,10 @@ private async void OnStartup(object sender, StartupEventArgs e) Settings.Default.SettingsUpgradeRequired = false; Settings.Default.Save(); } - catch { } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to update settings on startup"); + } } firstBoot = Settings.Default.FirstBoot; @@ -189,6 +214,7 @@ private async void OnStartup(object sender, StartupEventArgs e) if (!createdNew) { + _logger.LogWarning("Failed to start app, as there is already running uxtu instance. Shutting down"); MessageBox.Show("An instance of Universal x86 Tuning Utility is already open!", "Error starting Universal x86 Tuning Utility"); // Close the new instance Shutdown(); @@ -220,9 +246,9 @@ private async void OnStartup(object sender, StartupEventArgs e) { await Task.Run(() => UnblockFilesInDirectory(path)); } - catch + catch (Exception ex) { - + _logger.LogError(ex, "Failed to unblock files in {dir} directory", path); } } @@ -232,7 +258,12 @@ private async void OnStartup(object sender, StartupEventArgs e) await Task.Run(() => Game_Manager.installedGames = Game_Manager.syncGame_Library()); } - } catch (Exception ex) { MessageBox.Show(ex.Message); } + } + catch (Exception ex) + { + Log.Logger.Fatal(ex, "Failed to build and start a host"); + MessageBox.Show(ex.Message); + } } public static async void CheckForUpdate() @@ -281,6 +312,7 @@ private async void OnExit(object sender, ExitEventArgs e) private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { // For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0 + _logger?.LogCritical(e.Exception, "Unhandled dispatcher exception"); } static void UnblockFilesInDirectory(string directoryPath) diff --git a/Universal x86 Tuning Utility/Scripts/ASUS/XgMobileConnectionService.cs b/Universal x86 Tuning Utility/Scripts/ASUS/XgMobileConnectionService.cs index 75545f0..cf7507d 100644 --- a/Universal x86 Tuning Utility/Scripts/ASUS/XgMobileConnectionService.cs +++ b/Universal x86 Tuning Utility/Scripts/ASUS/XgMobileConnectionService.cs @@ -4,11 +4,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace Universal_x86_Tuning_Utility.Scripts.ASUS { public class XgMobileConnectionService { + private readonly ILogger _logger; private readonly ASUSWmi wmi; private static readonly byte[] XG_MOBILE_CURVE_FUNC_NAME = { 0x5e, 0xd1, 0x01 }; private static readonly byte[] XG_MOBILE_DISABLE_FAN_CONTROL_FUNC_NAME = { 0x5e, 0xd1, 0x02 }; @@ -25,15 +27,19 @@ public class XgMobileStatusEvent } public event EventHandler? XgMobileStatus; - public XgMobileConnectionService(ASUSWmi wmi) + public XgMobileConnectionService(ASUSWmi wmi, ILogger logger) { + this.wmi = wmi; + _logger = logger; try { - this.wmi = wmi; UpdateXgMobileStatus(); wmi.SubscribeToEvents((a, b) => UpdateXgMobileStatus()); } - catch { return; } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to initialize XgMobileConnectionService"); + } } private void UpdateXgMobileStatus() @@ -62,7 +68,10 @@ private void UpdateXgMobileStatus() }); } } - catch { return; } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to update UpdateXgMobileStatus"); + } } private bool IsEGPUDetected() @@ -133,8 +142,9 @@ private bool SendXgMobileUsbCommand(byte[] command) Array.Copy(command, paramsArr, command.Length); return device.WriteFeatureData(paramsArr); } - catch (Exception) + catch (Exception ex) { + _logger.LogError(ex, "Failed to send xg mobile usb command"); throw; } finally diff --git a/Universal x86 Tuning Utility/Universal x86 Tuning Utility.csproj b/Universal x86 Tuning Utility/Universal x86 Tuning Utility.csproj index 716b7e0..81b1d93 100644 --- a/Universal x86 Tuning Utility/Universal x86 Tuning Utility.csproj +++ b/Universal x86 Tuning Utility/Universal x86 Tuning Utility.csproj @@ -43,6 +43,9 @@ + + + diff --git a/Universal x86 Tuning Utility/Views/Pages/Games.xaml.cs b/Universal x86 Tuning Utility/Views/Pages/Games.xaml.cs index dd99592..f240060 100644 --- a/Universal x86 Tuning Utility/Views/Pages/Games.xaml.cs +++ b/Universal x86 Tuning Utility/Views/Pages/Games.xaml.cs @@ -26,6 +26,7 @@ using Universal_x86_Tuning_Utility.Services; using Universal_x86_Tuning_Utility.Views.Windows; using Windows.Gaming.Preview.GamesEnumeration; +using Microsoft.Extensions.Logging; using Wpf.Ui.Common.Interfaces; using YamlDotNet.Core; using static Universal_x86_Tuning_Utility.Scripts.Game_Manager; @@ -92,11 +93,13 @@ protected virtual void OnPropertyChanged(string propertyName) } } + private readonly ILogger _logger; public static List GameList = null; DispatcherTimer updateFPS = new DispatcherTimer(); - public Games(ViewModels.GamesViewModel viewModel) + public Games(ViewModels.GamesViewModel viewModel, ILogger logger) { + _logger = logger; InitializeComponent(); _ = Tablet.TabletDevices; ViewModel = viewModel; @@ -132,7 +135,10 @@ private void UpdateFPS_Tick(object? sender, EventArgs e) } } } - catch { } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to update fps"); + } } private static GameDataManager gameDataManager = new GameDataManager(Settings.Default.Path + "gameData.json"); diff --git a/Universal x86 Tuning Utility/Views/Pages/Premade.xaml.cs b/Universal x86 Tuning Utility/Views/Pages/Premade.xaml.cs index e919e26..6992a94 100644 --- a/Universal x86 Tuning Utility/Views/Pages/Premade.xaml.cs +++ b/Universal x86 Tuning Utility/Views/Pages/Premade.xaml.cs @@ -15,9 +15,11 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using Microsoft.Extensions.Logging; using Universal_x86_Tuning_Utility.Properties; using Universal_x86_Tuning_Utility.Scripts; using Universal_x86_Tuning_Utility.Scripts.Misc; +using Exception = System.Exception; using Uri = System.Uri; namespace Universal_x86_Tuning_Utility.Views.Pages @@ -27,6 +29,7 @@ namespace Universal_x86_Tuning_Utility.Views.Pages /// public partial class Premade : Page { + private readonly ILogger _logger; private string ExtremePreset = "", PerformancePreset = "", BalPreset = "", EcoPreset = ""; private void tbPerf_Click(object sender, RoutedEventArgs e) @@ -54,8 +57,10 @@ private void tbEco_Click(object sender, RoutedEventArgs e) } private string cpuName = ""; - public Premade() + public Premade(ILogger logger) { + _logger = logger; + try { InitializeComponent(); @@ -63,7 +68,10 @@ public Premade() PremadePresets.SetPremadePresets(); update(); } - catch { } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to initialize Premade page"); + } } private void update() @@ -104,7 +112,10 @@ private void update() if (selectedPreset == 3) exPreset(); } } - catch { } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to update Premade page"); + } } private async void perfPreset() @@ -128,9 +139,10 @@ private async void perfPreset() Settings.Default.CommandString = PerformancePreset; Settings.Default.premadePreset = 2; Settings.Default.Save(); - } catch + } + catch (Exception ex) { - + _logger.LogError(ex, "Failed to setup performance preset"); } } @@ -160,7 +172,11 @@ private async void exPreset() Settings.Default.premadePreset = 3; Settings.Default.Save(); - } catch { } + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to setup extreme preset"); + } } private async void ecoPreset() @@ -184,7 +200,11 @@ private async void ecoPreset() Settings.Default.CommandString = EcoPreset; Settings.Default.premadePreset = 0; Settings.Default.Save(); - } catch { } + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to setup eco preset"); + } } private async void balPreset() @@ -208,9 +228,10 @@ private async void balPreset() Settings.Default.CommandString = BalPreset; Settings.Default.premadePreset = 1; Settings.Default.Save(); - } catch + } + catch (Exception ex) { - + _logger.LogError(ex, "Failed to setup balanced preset"); } } } diff --git a/Universal x86 Tuning Utility/Views/Pages/SettingsPage.xaml.cs b/Universal x86 Tuning Utility/Views/Pages/SettingsPage.xaml.cs index 9ac2f2d..5d4519e 100644 --- a/Universal x86 Tuning Utility/Views/Pages/SettingsPage.xaml.cs +++ b/Universal x86 Tuning Utility/Views/Pages/SettingsPage.xaml.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Net.NetworkInformation; using System.Windows; +using Microsoft.Extensions.Logging; using Universal_x86_Tuning_Utility.Properties; using Universal_x86_Tuning_Utility.Scripts.Misc; using Wpf.Ui.Common.Interfaces; @@ -16,14 +17,17 @@ namespace Universal_x86_Tuning_Utility.Views.Pages /// public partial class SettingsPage : INavigableView { + private readonly ILogger _logger; + public ViewModels.SettingsViewModel ViewModel { get; } - public SettingsPage(ViewModels.SettingsViewModel viewModel) + public SettingsPage(ViewModels.SettingsViewModel viewModel, ILogger logger) { ViewModel = viewModel; + _logger = logger; InitializeComponent(); @@ -157,6 +161,7 @@ private async void btnDownload_Click(object sender, System.Windows.RoutedEventAr catch (Exception ex) { // log error or display error message to user + _logger.LogError(ex, "Failed to launch MSI"); MessageBox.Show("Failed to launch MSI: " + ex.Message); } }