more logging

This commit is contained in:
WeeXnes 2025-04-22 04:14:30 +02:00
parent a7b21d9ca5
commit 9cf82557ba
7 changed files with 110 additions and 79 deletions

View file

@ -10,7 +10,6 @@
Title="Install Game" Title="Install Game"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
Loaded="Control_OnLoaded" Loaded="Control_OnLoaded"
SizeChanged="Control_OnSizeChanged"
Background="#201c29" Background="#201c29"
SystemDecorations="None"> SystemDecorations="None">
<Grid RowDefinitions="20, *"> <Grid RowDefinitions="20, *">

View file

@ -26,6 +26,7 @@ public partial class AddGameWindow : Window
private void RefreshPreview() private void RefreshPreview()
{ {
Console.Info("Refreshing AddGameWindow Preview");
IsoPathBox.Text = this.newGame.GamePath; IsoPathBox.Text = this.newGame.GamePath;
SerialBox.Text = this.newGame.GameID; SerialBox.Text = this.newGame.GameID;
GameNameBox.Text = this.newGame.Name; GameNameBox.Text = this.newGame.Name;
@ -37,32 +38,6 @@ public partial class AddGameWindow : Window
RefreshPreview(); RefreshPreview();
} }
private void Control_OnSizeChanged(object? sender, SizeChangedEventArgs e)
{
var newSize = e.NewSize;
Console.WriteLine($"[SizeChanged] New size: {newSize.Width} x {newSize.Height}");
}
public void LoadPs2Cover(string gameId, Image imageControl)
{
//gameId = "SCPS-15110";
string url = $"https://github.com/xlenore/ps2-covers/blob/main/covers/default/{gameId}.jpg?raw=true";
try
{
using HttpClient client = new();
byte[] data = client.GetByteArrayAsync(url).GetAwaiter().GetResult();
using MemoryStream stream = new(data);
var bitmap = new Bitmap(stream);
imageControl.Source = bitmap;
}
catch (Exception ex)
{
Console.WriteLine($"[Error] Could not load cover for {gameId}: {ex.Message}");
}
}
private void WindowDrag(object? sender, PointerPressedEventArgs e) private void WindowDrag(object? sender, PointerPressedEventArgs e)
@ -90,7 +65,7 @@ public partial class AddGameWindow : Window
}; };
newGame.InstallationFinished += (o, args) => newGame.InstallationFinished += (o, args) =>
{ {
Console.WriteLine("Installation finished"); Console.WriteLine("Installation for " + newGame + " finished");
this.Close(); this.Close();
}; };
} }
@ -116,8 +91,13 @@ public partial class AddGameWindow : Window
if (files.Count >= 1) if (files.Count >= 1)
{ {
Console.Info("Updating " + Artwork.Type.Front + " Artwork for " + this.newGame.GameID);
this.newGame.ArtworkFront = new Bitmap(files[0].Path.LocalPath); this.newGame.ArtworkFront = new Bitmap(files[0].Path.LocalPath);
RefreshPreview(); RefreshPreview();
} }
else
{
Console.Error("OpenFilePicker was called but Canceled or no Files selected");
}
} }
} }

View file

@ -41,7 +41,7 @@ public partial class App : Application
public override void OnFrameworkInitializationCompleted() public override void OnFrameworkInitializationCompleted()
{ {
SetExceptionHandler(); SetExceptionHandler();
Console.Info("App started at " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")); Console.SetLogFile("output.log");
Globals.LoadSettings(); Globals.LoadSettings();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {

View file

@ -6,14 +6,26 @@ using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
namespace PS2_Manager.Core namespace PS2_Manager.Core
{ {
public static class Console public static class Console
{ {
private static StreamWriter logWriter = null;
private static readonly object logLock = new();
public static void SetLogFile(string path)
{
if (logWriter != null)
{
logWriter.Dispose();
}
logWriter = new StreamWriter(path, append: true)
{
AutoFlush = true
};
}
public static class Data public static class Data
{ {
public static class Colors public static class Colors
@ -34,6 +46,7 @@ namespace PS2_Manager.Core
public static string writeline_char = "•"; public static string writeline_char = "•";
} }
} }
private static void ConfiguredWriteline( private static void ConfiguredWriteline(
string text, string text,
ConsoleColor color, ConsoleColor color,
@ -43,103 +56,128 @@ namespace PS2_Manager.Core
{ {
VanillaConsole.OutputEncoding = Encoding.UTF8; VanillaConsole.OutputEncoding = Encoding.UTF8;
} }
catch (Exception ex) catch {}
{
} // Save current colors
ConsoleColor prevColor = VanillaConsole.BackgroundColor; ConsoleColor prevColor = VanillaConsole.BackgroundColor;
ConsoleColor prevForeColor = VanillaConsole.ForegroundColor; ConsoleColor prevForeColor = VanillaConsole.ForegroundColor;
// Timestamp
DateTime currentTime = DateTime.Now;
string timestamp = currentTime.ToString("[HH:mm:ss] ");
if (Data.Formatting.timestamp_prefix)
text = timestamp + text;
// Apply console colors
if (Data.Colors.colored_output) if (Data.Colors.colored_output)
{ {
VanillaConsole.BackgroundColor = color; VanillaConsole.BackgroundColor = color;
VanillaConsole.ForegroundColor = foregroundColor; VanillaConsole.ForegroundColor = foregroundColor;
} }
DateTime currentTime = DateTime.Now;
if (Data.Formatting.timestamp_prefix)
text = currentTime.ToString("[HH:mm:ss]") + text;
VanillaConsole.Write(text + " "); VanillaConsole.Write(text + " ");
if (Data.Colors.colored_output) if (Data.Colors.colored_output)
{ {
VanillaConsole.BackgroundColor = prevColor; VanillaConsole.BackgroundColor = prevColor;
VanillaConsole.ForegroundColor = prevForeColor; VanillaConsole.ForegroundColor = prevForeColor;
} }
VanillaConsole.WriteLine(""); VanillaConsole.WriteLine("");
// Log to file (if enabled)
if (logWriter != null)
{
lock (logLock)
{
try
{
logWriter.WriteLine(timestamp + text);
}
catch (Exception ex)
{
VanillaConsole.WriteLine("Log file write failed: " + ex.Message);
}
}
}
} }
public static void WriteLine(string text, public static void WriteLine(string text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,VanillaConsole.BackgroundColor, ConsoleColor.White); ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,
VanillaConsole.BackgroundColor, ConsoleColor.White);
} }
public static void WriteLine(float text, public static void WriteLine(float text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.float_color, ConsoleColor.White); ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,
Data.Colors.float_color, ConsoleColor.White);
} }
public static void WriteLine(double text, public static void WriteLine(double text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.double_color, ConsoleColor.White); ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,
Data.Colors.double_color, ConsoleColor.White);
} }
public static void WriteLine(int text, public static void WriteLine(int text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.int_color, ConsoleColor.White); ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,
Data.Colors.int_color, ConsoleColor.White);
} }
public static void Success(string text, public static void Success(string text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.success_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Green, ConfiguredWriteline(" " + Data.Formatting.success_char + " (" + lineNumber + "|" + caller + ") " + text,
ConsoleColor.Black); ConsoleColor.Green, ConsoleColor.Black);
} }
public static void Info(string text, public static void Info(string text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.info_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Blue, ConfiguredWriteline(" " + Data.Formatting.info_char + " (" + lineNumber + "|" + caller + ") " + text,
ConsoleColor.Black); ConsoleColor.Blue, ConsoleColor.Black);
} }
public static void Error(string text, public static void Error(string text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.error_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkRed, ConfiguredWriteline(" " + Data.Formatting.error_char + " (" + lineNumber + "|" + caller + ") " + text,
ConsoleColor.Black); ConsoleColor.DarkRed, ConsoleColor.Black);
} }
public static void Warning(string text, public static void Warning(string text,
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null) [CallerMemberName] string caller = null)
{ {
ConfiguredWriteline(" " + Data.Formatting.warning_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkYellow, ConfiguredWriteline(" " + Data.Formatting.warning_char + " (" + lineNumber + "|" + caller + ") " + text,
ConsoleColor.Black); ConsoleColor.DarkYellow, ConsoleColor.Black);
} }
public static void WriteLine<T>(List<T> List, bool verbose = true) public static void WriteLine<T>(List<T> List, bool verbose = true)
{ {
ConfiguredWriteline("List contains " + typeof(T) + "(" + List.Count + ")", ConsoleColor.DarkMagenta, ConfiguredWriteline("List contains " + typeof(T) + "(" + List.Count + ")",
ConsoleColor.Black); ConsoleColor.DarkMagenta, ConsoleColor.Black);
if (!verbose) if (!verbose)
return; return;
for (int i = 0; i < List.Count; i++) for (int i = 0; i < List.Count; i++)
{ {
if (i % 2 == 0) var color = (i % 2 == 0) ? ConsoleColor.DarkGray : ConsoleColor.Black;
{ ConfiguredWriteline("(" + i + "): " + List[i], color);
ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.DarkGray);
}
else
{
ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.Black);
}
} }
} }
} }

View file

@ -45,12 +45,21 @@ public class Game
} }
public void ChangeName(string newName) public void ChangeName(string newName)
{
Console.Info("Changing name of " + this.Name + " to " + newName);
try
{ {
this.Name = newName; this.Name = newName;
string targetDirectory = settings.library_path.GetValue<string>(); string targetDirectory = settings.library_path.GetValue<string>();
string newFileName = $"{this.GameID}.{this.Name}.iso"; string newFileName = $"{this.GameID}.{this.Name}.iso";
string destPath = Path.Combine(Path.Combine(targetDirectory, "DVD"), newFileName); string destPath = Path.Combine(Path.Combine(targetDirectory, "DVD"), newFileName);
File.Move(this.GamePath, destPath); File.Move(this.GamePath, destPath);
Console.Success(this.Name + " renamed successfully");
}
catch (Exception e)
{
Console.Error(e.Message);
}
} }
public string GetGameTitle() public string GetGameTitle()
@ -75,7 +84,7 @@ public class Game
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error: {ex.Message}"); Console.Error($"{ex.Message}");
return ""; return "";
} }
} }
@ -85,7 +94,9 @@ public class Game
private string ParseFormattedFilename(string filename) private string ParseFormattedFilename(string filename)
{ {
string[] parts = filename.Split('.'); string[] parts = filename.Split('.');
return parts[^2]; string parsedName = parts[^2];
Console.Info(parsedName + " parsed from " + filename);
return parsedName;
} }
public async void Install() public async void Install()
@ -96,7 +107,7 @@ public class Game
private Bitmap DownloadCover(Artwork.Type type) private Bitmap DownloadCover(Artwork.Type type)
{ {
Console.Info("Downloading " + type + " Artwork for " + this.GameID);
Bitmap cover = null; Bitmap cover = null;
string url = ""; string url = "";
switch (type) switch (type)
@ -124,9 +135,11 @@ public class Game
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"[Error] Could not load cover for {this.GameID}: {ex.Message}"); Console.Error($"Could not load cover for {this.GameID}: {ex.Message}");
} }
Console.Success(type + " Artwork for " + this.GameID + " downloaded successfully");
return cover; return cover;
} }
@ -137,6 +150,7 @@ public class Game
public async Task CopyIsoWithProgressAsync() public async Task CopyIsoWithProgressAsync()
{ {
Console.Info("Copying ISO file for " + this + "...");
string targetDirectory = settings.library_path.GetValue<string>(); string targetDirectory = settings.library_path.GetValue<string>();
Util.CheckDir(Path.Combine(targetDirectory, "DVD")); Util.CheckDir(Path.Combine(targetDirectory, "DVD"));
@ -161,15 +175,13 @@ public class Game
this.InstallProgress.Value = (double)bytesCopied / totalBytes * 100; this.InstallProgress.Value = (double)bytesCopied / totalBytes * 100;
} }
} }
Console.Success("ISO File copied successfully for " + this);
//this.InstallCover();
this.SaveCover(Artwork.Type.Front); this.SaveCover(Artwork.Type.Front);
this.SaveCover(Artwork.Type.Back); this.SaveCover(Artwork.Type.Back);
this.SaveCover(Artwork.Type.Disc); this.SaveCover(Artwork.Type.Disc);
this.InstallationFinished?.Invoke(this, EventArgs.Empty); this.InstallationFinished?.Invoke(this, EventArgs.Empty);
MainWindow.RefreshGamesListTrigger?.Invoke(this, EventArgs.Empty); MainWindow.RefreshGamesListTrigger?.Invoke(this, EventArgs.Empty);
Console.WriteLine($"Copied ISO to: {destPath}");
} }
@ -178,6 +190,7 @@ public class Game
public Bitmap? LoadCover(Artwork.Type artworkType) public Bitmap? LoadCover(Artwork.Type artworkType)
{ {
Console.Info("Loading " + artworkType + " Artwork for " + this.GameID);
string targetDirectory = settings.library_path.GetValue<string>(); string targetDirectory = settings.library_path.GetValue<string>();
Util.CheckDir(Path.Combine(targetDirectory, "ART")); Util.CheckDir(Path.Combine(targetDirectory, "ART"));
try try
@ -228,6 +241,7 @@ public class Game
).Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_ICO.png")); ).Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_ICO.png"));
break; break;
} }
Console.Success("Saved " + artworkType + " Artwork for " + this.GameID);
} }
public override string ToString() public override string ToString()

View file

@ -102,7 +102,7 @@ public partial class GameInfo : UserControl
this.game.ArtworkDVD = new Bitmap(files[0].Path.LocalPath); this.game.ArtworkDVD = new Bitmap(files[0].Path.LocalPath);
break; break;
} }
Console.Error("Updating " + ArtworkType.Value + " Artwork for " + this.game.GameID); Console.Info("Updating " + ArtworkType.Value + " Artwork for " + this.game.GameID);
this.game.SaveCover(ArtworkType.Value); this.game.SaveCover(ArtworkType.Value);
UpdateArtworks(); UpdateArtworks();
MainWindow.RefreshGamesListTrigger?.Invoke(null, EventArgs.Empty); MainWindow.RefreshGamesListTrigger?.Invoke(null, EventArgs.Empty);

View file

@ -104,7 +104,7 @@ public partial class MainWindow : Window
{ {
if (GamesList.SelectedItem is Game selectedGame) if (GamesList.SelectedItem is Game selectedGame)
{ {
Console.Info("Uninstalling " + selectedGame); Console.Warning("Uninstalling " + selectedGame);
selectedGame.Uninstall(); selectedGame.Uninstall();
RefreshGamesListTrigger?.Invoke(sender, e); RefreshGamesListTrigger?.Invoke(sender, e);
} }