Skip to content

Commit

Permalink
Merge pull request #187 from 1MrEnot/feature/logging
Browse files Browse the repository at this point in the history
Add logging
  • Loading branch information
JamesCJ60 authored Apr 7, 2024
2 parents a596548 + 6471e48 commit 865d25c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,7 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

JetBrains IDE
/**/.idea/*
50 changes: 41 additions & 9 deletions Universal x86 Tuning Utility/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +42,7 @@ public partial class App

public static ASUSWmi wmi;
public static XgMobileConnectionService xgMobileConnectionService;
private static ILogger<App>? _logger;

/// <summary>
/// Gets registered service.
Expand Down Expand Up @@ -75,6 +76,14 @@ public static bool IsAdministrator()
/// </summary>
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())
Expand All @@ -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
Expand All @@ -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<IThemeService, ThemeService>();
Expand Down Expand Up @@ -159,6 +180,7 @@ private async void OnStartup(object sender, StartupEventArgs e)
services.Configure<AppConfig>(context.Configuration.GetSection(nameof(AppConfig)));
}).Build();

_logger = _host.Services.GetRequiredService<ILogger<App>>();

_ = Tablet.TabletDevices;
bool firstBoot = false;
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<XgMobileConnectionService> _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 };
Expand All @@ -25,15 +27,19 @@ public class XgMobileStatusEvent
}
public event EventHandler<XgMobileStatusEvent>? XgMobileStatus;

public XgMobileConnectionService(ASUSWmi wmi)
public XgMobileConnectionService(ASUSWmi wmi, ILogger<XgMobileConnectionService> 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()
Expand Down Expand Up @@ -62,7 +68,10 @@ private void UpdateXgMobileStatus()
});
}
}
catch { return; }
catch (Exception ex)
{
_logger.LogError(ex, "Failed to update UpdateXgMobileStatus");
}
}

private bool IsEGPUDetected()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<PackageReference Include="NvAPIWrapper.Net" Version="0.8.1.101" />
<PackageReference Include="Octokit" Version="9.1.2" />
<PackageReference Include="SecretNest.SequentialScheduler" Version="1.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="WPF-UI" Version="2.1.0" />
Expand Down
10 changes: 8 additions & 2 deletions Universal x86 Tuning Utility/Views/Pages/Games.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,11 +93,13 @@ protected virtual void OnPropertyChanged(string propertyName)
}
}

private readonly ILogger<Games> _logger;

public static List<GameLauncherItem> GameList = null;
DispatcherTimer updateFPS = new DispatcherTimer();
public Games(ViewModels.GamesViewModel viewModel)
public Games(ViewModels.GamesViewModel viewModel, ILogger<Games> logger)
{
_logger = logger;
InitializeComponent();
_ = Tablet.TabletDevices;
ViewModel = viewModel;
Expand Down Expand Up @@ -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");
Expand Down
39 changes: 30 additions & 9 deletions Universal x86 Tuning Utility/Views/Pages/Premade.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +29,7 @@ namespace Universal_x86_Tuning_Utility.Views.Pages
/// </summary>
public partial class Premade : Page
{
private readonly ILogger<Premade> _logger;
private string ExtremePreset = "", PerformancePreset = "", BalPreset = "", EcoPreset = "";

private void tbPerf_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -54,16 +57,21 @@ private void tbEco_Click(object sender, RoutedEventArgs e)
}

private string cpuName = "";
public Premade()
public Premade(ILogger<Premade> logger)
{
_logger = logger;

try
{
InitializeComponent();
_ = Tablet.TabletDevices;
PremadePresets.SetPremadePresets();
update();
}
catch { }
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize Premade page");
}
}

private void update()
Expand Down Expand Up @@ -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()
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,14 +17,17 @@ namespace Universal_x86_Tuning_Utility.Views.Pages
/// </summary>
public partial class SettingsPage : INavigableView<ViewModels.SettingsViewModel>
{
private readonly ILogger<SettingsPage> _logger;

public ViewModels.SettingsViewModel ViewModel
{
get;
}

public SettingsPage(ViewModels.SettingsViewModel viewModel)
public SettingsPage(ViewModels.SettingsViewModel viewModel, ILogger<SettingsPage> logger)
{
ViewModel = viewModel;
_logger = logger;

InitializeComponent();

Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 865d25c

Please sign in to comment.