From ab58a6a64cdc775e5d6581cadca6af429c67ef85 Mon Sep 17 00:00:00 2001 From: stevealexandre Date: Mon, 12 Sep 2022 18:22:54 +0200 Subject: [PATCH] fix: log admin permission issue --- UWPHook/App.config | 12 +++ UWPHook/AppManager.cs | 16 +-- UWPHook/GamesWindow.xaml.cs | 71 ++++++++++--- UWPHook/Properties/Settings.Designer.cs | 37 ++++++- UWPHook/Properties/Settings.settings | 11 ++ UWPHook/SettingsWindow.xaml | 129 ++++++++++++++---------- UWPHook/SettingsWindow.xaml.cs | 8 +- UWPHook/SteamGridDb/SteamGridDbApi.cs | 4 +- UWPHook/UWPHook.csproj | 3 + 9 files changed, 202 insertions(+), 89 deletions(-) diff --git a/UWPHook/App.config b/UWPHook/App.config index 3d6ee9f..d2e29b0 100644 --- a/UWPHook/App.config +++ b/UWPHook/App.config @@ -82,6 +82,18 @@ + + + + ERROR + DEBUG + TRACE + + + + + 0 + diff --git a/UWPHook/AppManager.cs b/UWPHook/AppManager.cs index 21b4ad2..23afe60 100644 --- a/UWPHook/AppManager.cs +++ b/UWPHook/AppManager.cs @@ -1,4 +1,5 @@ using Serilog; +using Serilog.Core; using System; using System.Collections.Generic; using System.Diagnostics; @@ -28,16 +29,13 @@ namespace UWPHook /// The AUMID of the app to launch public static void LaunchUWPApp(string[] args) { - - // We receive the args from Steam, // 0 is application location, // 1 is the aumid, // 2 is the executable, the rest are extras string aumid = args[1]; executablePath = args[2].Contains("/") ? args[2].Replace('/', '\\') : args[2]; - FileStream debug = File.OpenWrite("debug.log"); - Log.Debug("Arguments => " + String.Join("/", args)); + Log.Verbose("Arguments => " + String.Join("/", args)); var mgr = new ApplicationActivationManager(); uint processId; @@ -50,13 +48,14 @@ namespace UWPHook { mgr.ActivateApplication(aumid, extra_args, ActivateOptions.None, out processId); runningProcessId = (int) processId; - Log.Debug("Process ID => " + runningProcessId.ToString()); + Log.Verbose("Process ID => " + runningProcessId.ToString()); //Bring the launched app to the foreground, this fixes in-home streaming BringProcess(); } catch (Exception e) { + Log.Error("Error while trying to launch your app." + Environment.NewLine + e.Message); throw new Exception("Error while trying to launch your app." + Environment.NewLine + e.Message); } } @@ -96,12 +95,12 @@ namespace UWPHook foreach (var process in processes) { string executableFile = executablePath.Contains('\\') ? executablePath.Substring(executablePath.LastIndexOf('\\') + 1) : executablePath; - Log.Debug("Process " + process.Value.Path + " contains " + executablePath + " ? : " + process.Value.Path.Contains(executablePath).ToString()); - Log.Debug("Process " + process.Key + " contains " + executableFile + " ? : " + process.Key.Contains(executableFile).ToString()); + Log.Verbose("Process " + process.Value.Path + " contains " + executablePath + " ? : " + process.Value.Path.Contains(executablePath).ToString()); + Log.Verbose("Process " + process.Key + " contains " + executableFile + " ? : " + process.Key.Contains(executableFile).ToString()); if (process.Value.Path.Contains(executablePath) || process.Key.Contains(executableFile)) { int pid = process.Value.Pid; - Log.Debug($"Launcher opened child process ({runningProcessId}->{pid}), using new process as target"); + Log.Verbose($"Launcher opened child process ({runningProcessId}->{pid}), using new process as target"); runningProcessId = pid; isLauncherProcess = true; @@ -173,6 +172,7 @@ namespace UWPHook } catch (Exception e) { + Log.Error("Error trying to get installed apps on your PC " + Environment.NewLine + e.Message, e.InnerException); throw new Exception("Error trying to get installed apps on your PC " + Environment.NewLine + e.Message, e.InnerException); } diff --git a/UWPHook/GamesWindow.xaml.cs b/UWPHook/GamesWindow.xaml.cs index ba1501a..b9197bf 100644 --- a/UWPHook/GamesWindow.xaml.cs +++ b/UWPHook/GamesWindow.xaml.cs @@ -1,5 +1,6 @@ using Force.Crc32; using Serilog; +using Serilog.Core; using SharpSteam; using System; using System.Collections.Generic; @@ -29,18 +30,30 @@ namespace UWPHook { AppEntryModel Apps; BackgroundWorker bwrLoad; + static LoggingLevelSwitch levelSwitch = new LoggingLevelSwitch(); public GamesWindow() { InitializeComponent(); - Debug.WriteLine("Init GamesWindow"); + Log.Debug("Init GamesWindow"); Apps = new AppEntryModel(); var args = Environment.GetCommandLineArgs(); + + // Init log file to AppData\Roaming\Briano\UWPHook directory with size rotation on 10Mb with max 5 files + System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); + FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location); + string loggerFilePath = String.Join("\\", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fvi.CompanyName, fvi.ProductName, "application.log"); + Log.Logger = new LoggerConfiguration() - .MinimumLevel.Error() - .WriteTo.File("debug.log", rollingInterval: RollingInterval.Day) + .MinimumLevel.ControlledBy(levelSwitch) + .WriteTo.File(path: loggerFilePath, rollOnFileSizeLimit: true, fileSizeLimitBytes: 10485760, retainedFileCountLimit: 5) + .WriteTo.Console() .CreateLogger(); + // Switch to Info by default to inform logger level in log file and switch to the correct log level + levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Information; + SetLogLevel(); + // If null or 1, the app was launched normally if (args?.Length > 1) { @@ -176,6 +189,7 @@ namespace UWPHook } catch (TaskCanceledException exception) { + Log.Error(exception.Message); msg = exception.Message; } @@ -202,8 +216,9 @@ namespace UWPHook { stream = client.OpenRead(imageUrl); } - catch (Exception e) + catch (Exception exception) { + Log.Error(exception.Message); //Image with error? //Skip for now } @@ -277,13 +292,14 @@ namespace UWPHook } catch (TaskCanceledException exception) { + Log.Error(exception.Message); throw; } if (games != null) { var game = games[0]; - Debug.WriteLine("Detected Game: " + game.ToString()); + Log.Verbose("Detected Game: " + game.ToString()); UInt64 gameId = GenerateSteamGridAppId(appName, appTarget); if (!Directory.Exists(tmpGridDirectory)) @@ -296,7 +312,7 @@ namespace UWPHook var gameHeroes = api.GetGameHeroes(game.Id); var gameLogos = api.GetGameLogos(game.Id); - Debug.WriteLine("Game ID: " + game.Id); + Log.Verbose("Game ID: " + game.Id); await Task.WhenAll( gameGridsVertical, @@ -360,7 +376,7 @@ namespace UWPHook List gridImagesDownloadTasks = new List(); bool downloadGridImages = !String.IsNullOrEmpty(Properties.Settings.Default.SteamGridDbApiKey); //To make things faster, decide icons and download grid images before looping users - Debug.WriteLine("downloadGridImages: " + (downloadGridImages)); + Log.Verbose("downloadGridImages: " + (downloadGridImages)); foreach (var app in selected_apps) { @@ -368,7 +384,7 @@ namespace UWPHook if (downloadGridImages) { - Debug.WriteLine("Downloading grid images for app " + app.Name); + Log.Verbose("Downloading grid images for app " + app.Name); gridImagesDownloadTasks.Add(DownloadTempGridImages(app.Name, exePath)); } @@ -390,6 +406,7 @@ namespace UWPHook //If it's a short VDF, let's just overwrite it if (ex.GetType() != typeof(VDFTooShortException)) { + Log.Error("Error: Program failed to load existing Steam shortcuts." + Environment.NewLine + ex.Message); throw new Exception("Error: Program failed to load existing Steam shortcuts." + Environment.NewLine + ex.Message); } } @@ -421,13 +438,13 @@ namespace UWPHook Boolean isFound = false; for (int i = 0; i < shortcuts.Length; i++) { - Debug.WriteLine(shortcuts[i].ToString()); + Log.Verbose(shortcuts[i].ToString()); if (shortcuts[i].AppName == app.Name) { isFound = true; - Debug.WriteLine(app.Name + " already added to Steam. Updating existing shortcut."); + Log.Verbose(app.Name + " already added to Steam. Updating existing shortcut."); shortcuts[i] = newApp; } } @@ -451,12 +468,14 @@ namespace UWPHook } catch (Exception ex) { + Log.Error("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); throw new Exception("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); } } } catch (Exception ex) { + Log.Error("Error: Program failed exporting your games:" + Environment.NewLine + ex.Message + ex.StackTrace); MessageBox.Show("Error: Program failed exporting your games:" + Environment.NewLine + ex.Message + ex.StackTrace); } } @@ -517,7 +536,7 @@ namespace UWPHook string steamExe = steam.MainModule.FileName; //we always ask politely - Debug.WriteLine("Requesting Steam shutdown"); + Log.Debug("Requesting Steam shutdown"); Process.Start(steamExe, "-exitsteam"); bool restarted = false; @@ -531,7 +550,7 @@ namespace UWPHook await Task.Delay(TimeSpan.FromSeconds(0.5f)); if (getSteam() == null) { - Debug.WriteLine("Restarting Steam"); + Log.Debug("Restarting Steam"); Process.Start(steamExe); restarted = true; break; @@ -540,14 +559,14 @@ namespace UWPHook if (!restarted) { - Debug.WriteLine("Steam instance not restarted"); + Log.Debug("Steam instance not restarted"); MessageBox.Show("Failed to restart Steam, please launch it manually", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); return false; } } else { - Debug.WriteLine("Steam instance not found to be restarted"); + Log.Debug("Steam instance not found to be restarted"); } return true; @@ -555,7 +574,7 @@ namespace UWPHook public static void ClearAllShortcuts() { - Debug.WriteLine("DBG: Clearing all elements in shortcuts.vdf"); + Log.Debug("Clearing all elements in shortcuts.vdf"); string[] tags = Settings.Default.Tags.Split(','); string steam_folder = SteamManager.GetSteamFolder(); @@ -582,11 +601,13 @@ namespace UWPHook } catch (Exception ex) { + Log.Error("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); throw new Exception("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); } } catch (Exception ex) { + Log.Error("Error: Program failed while trying to clear your Steam shortcuts:" + Environment.NewLine + ex.Message + ex.StackTrace); MessageBox.Show("Error: Program failed while trying to clear your Steam shortcuts:" + Environment.NewLine + ex.Message + ex.StackTrace); } } @@ -679,6 +700,7 @@ namespace UWPHook } catch (Exception ex) { + Log.Error(ex.Message); MessageBox.Show(ex.Message, "UWPHook", MessageBoxButton.OK, MessageBoxImage.Error); } } @@ -732,5 +754,24 @@ namespace UWPHook } } } + + public static void SetLogLevel() + { + switch (Settings.Default.SelectedLogLevel) + { + case 1: + Log.Information("Init log with DEBUG level."); + levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Debug; + break; + case 2: + Log.Information("Init log with TRACE level."); + levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Verbose; + break; + default: + Log.Information("Init log with ERROR level."); + levelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Error; + break; + } + } } } diff --git a/UWPHook/Properties/Settings.Designer.cs b/UWPHook/Properties/Settings.Designer.cs index c28f761..bb6c64b 100644 --- a/UWPHook/Properties/Settings.Designer.cs +++ b/UWPHook/Properties/Settings.Designer.cs @@ -1,10 +1,10 @@ //------------------------------------------------------------------------------ // -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 +// Ce code a été généré par un outil. +// Version du runtime :4.0.30319.42000 // -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. +// Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si +// le code est régénéré. // //------------------------------------------------------------------------------ @@ -12,7 +12,7 @@ namespace UWPHook.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.2.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.3.0.0")] public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -219,5 +219,32 @@ namespace UWPHook.Properties { this["SteamGridDB_Humor"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("\r\n\r\n ERROR\r\n DEBUG\r\n TRACE\r\n")] + public global::System.Collections.Specialized.StringCollection LogLevel { + get { + return ((global::System.Collections.Specialized.StringCollection)(this["LogLevel"])); + } + set { + this["LogLevel"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0")] + public int SelectedLogLevel { + get { + return ((int)(this["SelectedLogLevel"])); + } + set { + this["SelectedLogLevel"] = value; + } + } } } diff --git a/UWPHook/Properties/Settings.settings b/UWPHook/Properties/Settings.settings index c86b7f9..75865b4 100644 --- a/UWPHook/Properties/Settings.settings +++ b/UWPHook/Properties/Settings.settings @@ -70,5 +70,16 @@ <string>true</string> </ArrayOfString> + + <?xml version="1.0" encoding="utf-16"?> +<ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <string>ERROR</string> + <string>DEBUG</string> + <string>TRACE</string> +</ArrayOfString> + + + 0 + \ No newline at end of file diff --git a/UWPHook/SettingsWindow.xaml b/UWPHook/SettingsWindow.xaml index 9f5baf2..88f9c53 100644 --- a/UWPHook/SettingsWindow.xaml +++ b/UWPHook/SettingsWindow.xaml @@ -18,22 +18,29 @@ - + - - + - - + + + + + + + + + + - - - - + + + + @@ -41,27 +48,37 @@ - + + - -