extended logging

This commit is contained in:
WeeXnes 2025-04-22 03:24:56 +02:00
parent eb4e4201c7
commit a7b21d9ca5
11 changed files with 257 additions and 16 deletions

View file

@ -1,15 +1,38 @@
using System; using System;
using System.IO;
using System.Runtime.InteropServices.JavaScript;
using System.Text.Json; using System.Text.Json;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using PS2_Manager.Core; using PS2_Manager.Core;
using Tmds.DBus.Protocol;
namespace PS2_Manager; namespace PS2_Manager;
public partial class App : Application public partial class App : Application
{ {
private void SetExceptionHandler()
{
AppDomain currentDomain = default(AppDomain);
currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
}
private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;
Console.Error("Exception: " + ex.Message);
DateTime currentDateTime = DateTime.Now;
string formattedDateTime = currentDateTime.ToString("dddd, dd MMMM yyyy HH:mm:ss");
using (StreamWriter writer = new StreamWriter("error_log.txt", append: true))
{
writer.WriteLine(formattedDateTime);
writer.WriteLine(ex.ToString());
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
}
public override void Initialize() public override void Initialize()
{ {
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
@ -17,6 +40,8 @@ public partial class App : Application
public override void OnFrameworkInitializationCompleted() public override void OnFrameworkInitializationCompleted()
{ {
SetExceptionHandler();
Console.Info("App started at " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Globals.LoadSettings(); Globals.LoadSettings();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {

146
PS2_Manager/Core/Console.cs Normal file
View file

@ -0,0 +1,146 @@
global using Console = PS2_Manager.Core.Console;
global using VanillaConsole = System.Console;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace PS2_Manager.Core
{
public static class Console
{
public static class Data
{
public static class Colors
{
public static bool colored_output = true;
public static ConsoleColor int_color = ConsoleColor.Blue;
public static ConsoleColor double_color = ConsoleColor.Cyan;
public static ConsoleColor float_color = ConsoleColor.DarkCyan;
}
public static class Formatting
{
public static bool timestamp_prefix = false;
public static string success_char = "✓";
public static string warning_char = "⌬";
public static string info_char = "◈";
public static string error_char = "☓";
public static string writeline_char = "•";
}
}
private static void ConfiguredWriteline(
string text,
ConsoleColor color,
ConsoleColor foregroundColor = ConsoleColor.White)
{
try
{
VanillaConsole.OutputEncoding = Encoding.UTF8;
}
catch (Exception ex)
{
}
ConsoleColor prevColor = VanillaConsole.BackgroundColor;
ConsoleColor prevForeColor = VanillaConsole.ForegroundColor;
if (Data.Colors.colored_output)
{
VanillaConsole.BackgroundColor = color;
VanillaConsole.ForegroundColor = foregroundColor;
}
DateTime currentTime = DateTime.Now;
if (Data.Formatting.timestamp_prefix)
text = currentTime.ToString("[HH:mm:ss]") + text;
VanillaConsole.Write(text + " ");
if (Data.Colors.colored_output)
{
VanillaConsole.BackgroundColor = prevColor;
VanillaConsole.ForegroundColor = prevForeColor;
}
VanillaConsole.WriteLine("");
}
public static void WriteLine(string text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,VanillaConsole.BackgroundColor, ConsoleColor.White);
}
public static void WriteLine(float text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.float_color, ConsoleColor.White);
}
public static void WriteLine(double text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.double_color, ConsoleColor.White);
}
public static void WriteLine(int text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.int_color, ConsoleColor.White);
}
public static void Success(string text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.success_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Green,
ConsoleColor.Black);
}
public static void Info(string text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.info_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Blue,
ConsoleColor.Black);
}
public static void Error(string text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.error_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkRed,
ConsoleColor.Black);
}
public static void Warning(string text,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
ConfiguredWriteline(" " + Data.Formatting.warning_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkYellow,
ConsoleColor.Black);
}
public static void WriteLine<T>(List<T> List, bool verbose = true)
{
ConfiguredWriteline("List contains " + typeof(T) + "(" + List.Count + ")", ConsoleColor.DarkMagenta,
ConsoleColor.Black);
if (!verbose)
return;
for (int i = 0; i < List.Count; i++)
{
if (i % 2 == 0)
{
ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.DarkGray);
}
else
{
ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.Black);
}
}
}
}
}

View file

@ -229,6 +229,11 @@ public class Game
break; break;
} }
} }
public override string ToString()
{
return this.Name + " -> " + this.GameID;
}
} }

View file

@ -9,7 +9,7 @@
<ScrollViewer Grid.Row="0"> <ScrollViewer Grid.Row="0">
<StackPanel Orientation="Vertical" Margin="10"> <StackPanel Orientation="Vertical" Margin="10">
<TextBlock Text="Display Name:" HorizontalAlignment="Center" Padding="0,10"/> <TextBlock Text="Display Name:" HorizontalAlignment="Center" Padding="0,10"/>
<TextBox Name="DisplayNameBox" TextChanged="DisplayNameBox_OnTextChanged"/> <TextBox Name="DisplayNameBox"/>
<Separator/> <Separator/>
<Button Content="Calculate Checksums" Click="ChecksumButtonOnClick" HorizontalAlignment="Stretch"></Button> <Button Content="Calculate Checksums" Click="ChecksumButtonOnClick" HorizontalAlignment="Stretch"></Button>
<Border Background="#35313d" CornerRadius="5" Padding="10" Margin="0,5" IsVisible="False" Name="ChecksumArea"> <Border Background="#35313d" CornerRadius="5" Padding="10" Margin="0,5" IsVisible="False" Name="ChecksumArea">

View file

@ -23,7 +23,7 @@ public partial class EditGame : UserControl
{ {
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
Console.WriteLine("MD5Hash.ValueChanged"); Console.Info("MD5 Hash calculated");
MD5TextBlock.Text = "MD5: " + MD5Hash.Value; MD5TextBlock.Text = "MD5: " + MD5Hash.Value;
}); });
}; };
@ -31,17 +31,12 @@ public partial class EditGame : UserControl
{ {
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
Console.WriteLine("SHA1Hash.ValueChanged"); Console.Info("SHA1 Hash calculated");
SHATextBlock.Text = "SHA1: " + SHA1Hash.Value; SHATextBlock.Text = "SHA1: " + SHA1Hash.Value;
}); });
}; };
} }
private void DisplayNameBox_OnTextChanged(object? sender, TextChangedEventArgs e)
{
}
private void ChecksumButtonOnClick(object? sender, RoutedEventArgs e) private void ChecksumButtonOnClick(object? sender, RoutedEventArgs e)
{ {
this.GetChecksum(); this.GetChecksum();
@ -69,9 +64,9 @@ public partial class EditGame : UserControl
}; };
md5Worker.RunWorkerAsync(); md5Worker.RunWorkerAsync();
Console.Info("MD5 Hashing started...");
sha1Worker.RunWorkerAsync(); sha1Worker.RunWorkerAsync();
Console.Info("SHA1 Hashing started...");
Console.WriteLine("Hashing started...");
} }
private void Save_OnClick(object? sender, RoutedEventArgs e) private void Save_OnClick(object? sender, RoutedEventArgs e)

View file

@ -37,6 +37,8 @@ public partial class GameInfo : UserControl
private void UpdateArtworks() private void UpdateArtworks()
{ {
Console.Info("Showing: " + ArtworkType.Value.ToString());
Bitmap? tempBitmap = null; Bitmap? tempBitmap = null;
switch (ArtworkType.Value) switch (ArtworkType.Value)
{ {
@ -66,6 +68,7 @@ public partial class GameInfo : UserControl
} }
CoverTextHint.Text = ArtworkType.Value.ToString(); CoverTextHint.Text = ArtworkType.Value.ToString();
CoverImage.Source = tempBitmap; CoverImage.Source = tempBitmap;
@ -99,11 +102,15 @@ 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);
this.game.SaveCover(ArtworkType.Value); this.game.SaveCover(ArtworkType.Value);
UpdateArtworks(); UpdateArtworks();
MainWindow.RefreshGamesListTrigger?.Invoke(null, EventArgs.Empty); MainWindow.RefreshGamesListTrigger?.Invoke(null, EventArgs.Empty);
} }
else
{
Console.Error("OpenFilePicker was called but Canceled or no Files selected");
}
} }
private void NextButton(object? sender, RoutedEventArgs e) private void NextButton(object? sender, RoutedEventArgs e)

View file

@ -26,6 +26,7 @@ public partial class MainWindow : Window
GameShowcaseGrid.IsVisible = true; GameShowcaseGrid.IsVisible = true;
InfoWindow.Child = new GameInfo(game); InfoWindow.Child = new GameInfo(game);
GameEdit.Child = new EditGame(game); GameEdit.Child = new EditGame(game);
Console.Info(game + " set as Showcase");
} }
private void WindowDrag(object? sender, PointerPressedEventArgs e) private void WindowDrag(object? sender, PointerPressedEventArgs e)
@ -38,6 +39,7 @@ public partial class MainWindow : Window
private void WindowClose(object? sender, PointerPressedEventArgs e) private void WindowClose(object? sender, PointerPressedEventArgs e)
{ {
Console.Success("Window closing");
this.Close(); this.Close();
} }
@ -57,9 +59,13 @@ public partial class MainWindow : Window
if (files.Count >= 1) if (files.Count >= 1)
{ {
//Console.WriteLine(ISO.GetSerial()); Console.Info("AddGameWindow was called for " + files[0].Path.LocalPath);
new AddGameWindow(files[0].Path.LocalPath).Show(); new AddGameWindow(files[0].Path.LocalPath).Show();
} }
else
{
Console.Error("OpenFilePicker was called but Canceled or no Files selected");
}
} }
private async void Control_OnLoaded(object? sender, RoutedEventArgs e) private async void Control_OnLoaded(object? sender, RoutedEventArgs e)
@ -71,16 +77,16 @@ public partial class MainWindow : Window
public void FetchGamesFromLibrary(object? sender = null, EventArgs? e = null) public void FetchGamesFromLibrary(object? sender = null, EventArgs? e = null)
{ {
Console.Info("Fetching games from library...");
List<Game> Games = new List<Game>(); List<Game> Games = new List<Game>();
Console.WriteLine("Loading library...");
Util.CheckDir(Path.Combine(settings.library_path.GetValue<string>(), "DVD")); Util.CheckDir(Path.Combine(settings.library_path.GetValue<string>(), "DVD"));
string[] files = Directory.GetFiles(Path.Combine(settings.library_path.GetValue<string>(), "DVD")); string[] files = Directory.GetFiles(Path.Combine(settings.library_path.GetValue<string>(), "DVD"));
foreach (var file in files) foreach (var file in files)
{ {
Console.WriteLine(file);
Game newGame = Game newGame =
new Game(file, true); new Game(file, true);
Games.Add(newGame); Games.Add(newGame);
Console.Success("Successfully fetched " + newGame);
} }
Games = Games.OrderBy(game => game.Name).ToList(); Games = Games.OrderBy(game => game.Name).ToList();
GamesList.ItemsSource = Games; GamesList.ItemsSource = Games;
@ -90,6 +96,7 @@ public partial class MainWindow : Window
{ {
if (GamesList.SelectedItem is Game selectedGame) if (GamesList.SelectedItem is Game selectedGame)
{ {
Console.Info("Selected " + selectedGame + " from ListView");
this.ShowcaseGame(selectedGame); this.ShowcaseGame(selectedGame);
} }
} }
@ -97,6 +104,7 @@ public partial class MainWindow : Window
{ {
if (GamesList.SelectedItem is Game selectedGame) if (GamesList.SelectedItem is Game selectedGame)
{ {
Console.Info("Uninstalling " + selectedGame);
selectedGame.Uninstall(); selectedGame.Uninstall();
RefreshGamesListTrigger?.Invoke(sender, e); RefreshGamesListTrigger?.Invoke(sender, e);
} }

View file

@ -0,0 +1,31 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="PS2_Manager.MessageBox"
Title="MessageBox"
SystemDecorations="None">
<Grid RowDefinitions="20, *">
<Grid Grid.Row="0">
<Border Background="#35313d"
PointerPressed="WindowDrag">
<Grid ColumnDefinitions="*, 20">
<TextBlock Name="WindowTitle" FontSize="12" Text="Install Game" Padding="8,0,0,0" VerticalAlignment="Center"/>
<Border Grid.Column="1" Background="#4b4753">
<TextBlock FontSize="12" Text="×" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
</Border>
</Grid>
<Grid Grid.Row="1" ColumnDefinitions="Auto,Auto,*" Margin="20">
<Border Margin="0,0,0,0">
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Label Content="Oh no! an error has occurred" Foreground="White" HorizontalAlignment="Center" FontSize="25"/>
<Label Content="full exception log has been dumped into error__log.txt" Foreground="White" HorizontalAlignment="Center" FontSize="12"/>
<Label Content="" Name="ErrorDump" Foreground="Gray" HorizontalAlignment="Center" FontSize="12" Padding="0,15,0,0"/>
</StackPanel>
</Border>
</Grid>
</Grid>
</Window>

View file

@ -0,0 +1,22 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
namespace PS2_Manager;
public partial class MessageBox : Window
{
public MessageBox(string messageContent)
{
InitializeComponent();
ErrorDump.Content = "Exception: " + messageContent;
}
private void WindowDrag(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
BeginMoveDrag(e);
}
}
}

View file

@ -27,6 +27,7 @@ public partial class Setup : Window
private void FinishSetup() private void FinishSetup()
{ {
Console.Success("Setup Complete");
InfoText1.Text = "Setup Finished"; InfoText1.Text = "Setup Finished";
InfoText2.Text = "You can now exit to the Main Application"; InfoText2.Text = "You can now exit to the Main Application";
OpenLibraryButton.Content = "Exit"; OpenLibraryButton.Content = "Exit";
@ -40,6 +41,7 @@ public partial class Setup : Window
private async void OpenLibraryButton_OnClick(object? sender, RoutedEventArgs e) private async void OpenLibraryButton_OnClick(object? sender, RoutedEventArgs e)
{ {
Console.Info("Open Library Button Clicked");
settings.library_path.SetValue(""); settings.library_path.SetValue("");
while (String.IsNullOrEmpty(settings.library_path.GetValue<string>())) while (String.IsNullOrEmpty(settings.library_path.GetValue<string>()))
{ {

View file

@ -14,7 +14,7 @@ public partial class Welcome : UserControl
} }
private void OnRepositoryLinkClicked(object sender, RoutedEventArgs e) private void OnRepositoryLinkClicked(object sender, RoutedEventArgs e)
{ {
// Open the link in the default web browser Console.Info("Repository Link Clicked");
Process.Start(new ProcessStartInfo Process.Start(new ProcessStartInfo
{ {
FileName = "https://git.weexnes.dev/WeeXnes/ps2_manager", FileName = "https://git.weexnes.dev/WeeXnes/ps2_manager",