Skip to content

Commit 82a130b

Browse files
committed
GUI: Add logging
1 parent 7615af7 commit 82a130b

File tree

9 files changed

+356
-175
lines changed

9 files changed

+356
-175
lines changed

VP.NET.GUI/Models/Log.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using Avalonia.Threading;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace VP.NET.GUI.Models
11+
{
12+
/// <summary>
13+
/// VP.NET logging system class
14+
/// </summary>
15+
public static class Log
16+
{
17+
public enum LogSeverity
18+
{
19+
Information,
20+
Warning,
21+
Error
22+
}
23+
24+
public static string LogFilePath = Path.Combine(Utils.GetDataFolderPath(), "log.log");
25+
26+
/// <summary>
27+
/// Write a log entry to console and file
28+
/// </summary>
29+
/// <param name="logSeverity"></param>
30+
/// <param name="from"></param>
31+
/// <param name="data"></param>
32+
public static void Add(LogSeverity logSeverity, string from, string data)
33+
{
34+
var logString = DateTime.Now.ToString() + " - *" + logSeverity.ToString() + "* : (" + from + ") " + data;
35+
36+
Task.Run(async () => {
37+
try
38+
{
39+
await WaitForFileAccess(LogFilePath);
40+
using (var writer = new StreamWriter(LogFilePath, true))
41+
{
42+
writer.WriteLine(logString, Encoding.UTF8);
43+
}
44+
}
45+
catch (Exception ex)
46+
{
47+
WriteToConsole(ex.Message);
48+
}
49+
WriteToConsole(logString);
50+
});
51+
}
52+
53+
/// <summary>
54+
/// Write a log entry to console and file
55+
/// </summary>
56+
/// <param name="logSeverity"></param>
57+
/// <param name="from"></param>
58+
/// <param name="exception"></param>
59+
public static void Add(LogSeverity logSeverity, string from, Exception exception)
60+
{
61+
Add(logSeverity, from, exception.Message);
62+
if (exception.InnerException != null)
63+
{
64+
Add(logSeverity, from, exception.InnerException.Message);
65+
}
66+
}
67+
68+
/// <summary>
69+
/// Write a string to VS console
70+
/// </summary>
71+
/// <param name="data"></param>
72+
public async static void WriteToConsole(string data)
73+
{
74+
try
75+
{
76+
if (Debugger.IsAttached)
77+
{
78+
System.Diagnostics.Debug.WriteLine(data);
79+
}
80+
}
81+
catch { }
82+
}
83+
84+
/// <summary>
85+
/// Wait for the log file being available for write
86+
/// Returns when the file is ready
87+
/// </summary>
88+
/// <param name="filename"></param>
89+
private static async Task WaitForFileAccess(string filename)
90+
{
91+
try
92+
{
93+
if (File.Exists(filename))
94+
{
95+
using (FileStream inputStream = File.Open(filename, FileMode.Open, FileAccess.Read))
96+
{
97+
inputStream.Close();
98+
return;
99+
}
100+
}
101+
}
102+
catch (IOException)
103+
{
104+
await Task.Delay(500);
105+
await WaitForFileAccess(filename);
106+
}
107+
}
108+
}
109+
}

VP.NET.GUI/Models/Settings.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class Settings
2424
public string? ToolLastVPCompressionOpenPath { get; set; } = null;
2525
public string? ToolLastVPCompressionDestinationPath { get; set; } = null;
2626

27-
2827
public void Load()
2928
{
3029
try
@@ -48,15 +47,11 @@ public void Load()
4847
ToolLastVPCompressionDestinationPath = tempSettings.ToolLastVPCompressionDestinationPath;
4948
}
5049

51-
}
52-
else
53-
{
54-
5550
}
5651
}
5752
catch (Exception ex)
5853
{
59-
54+
Log.Add(Log.LogSeverity.Error, "Settings.Load()", ex);
6055
}
6156
}
6257

@@ -75,6 +70,7 @@ public void Save(bool writeIni = true)
7570
}
7671
catch (Exception ex)
7772
{
73+
Log.Add(Log.LogSeverity.Error, "Settings.Save()", ex);
7874
}
7975
}
8076
}

VP.NET.GUI/Models/Utils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static string FormatBytes(long bytes)
4646
}
4747
catch (Exception ex)
4848
{
49+
Log.Add(Log.LogSeverity.Error, "Utils.FormatBytes()", ex);
4950
return string.Empty;
5051
}
5152
}

VP.NET.GUI/ViewModels/ExtractViewModel.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Avalonia.Threading;
22
using CommunityToolkit.Mvvm.ComponentModel;
3+
using System;
34
using System.Collections.Generic;
45
using System.Threading.Tasks;
6+
using VP.NET.GUI.Models;
57
using VP.NET.GUI.Views;
68

79
namespace VP.NET.GUI.ViewModels
@@ -24,20 +26,32 @@ public partial class ExtractViewModel : ViewModelBase
2426

2527
public ExtractViewModel() { }
2628

29+
/// <summary>
30+
/// Extract files from the vp with a Progress bar and cancel button
31+
/// </summary>
32+
/// <param name="extractVpFiles"></param>
33+
/// <param name="destination"></param>
34+
/// <param name="dialog"></param>
2735
public ExtractViewModel(List<VPFile> extractVpFiles, string destination, ExtractView dialog)
2836
{
2937
this.extractVpFiles = extractVpFiles;
3038
this.extractView = dialog;
3139
_ = Task.Factory.StartNew(async () => {
3240
//Get number of files to extract
33-
foreach(var file in extractVpFiles)
41+
try
3442
{
35-
MaxFiles += file.GetNumberOfFiles();
36-
}
37-
foreach (var file in extractVpFiles)
43+
foreach (var file in extractVpFiles)
44+
{
45+
MaxFiles += file.GetNumberOfFiles();
46+
}
47+
foreach (var file in extractVpFiles)
48+
{
49+
if (!cancelExtraction)
50+
await file.ExtractRecursiveAsync(destination, progressCallback);
51+
}
52+
} catch (Exception ex)
3853
{
39-
if(!cancelExtraction)
40-
await file.ExtractRecursiveAsync(destination, progressCallback);
54+
Log.Add(Log.LogSeverity.Error, "ExtractViewModel", ex);
4155
}
4256
Dispatcher.UIThread.Invoke(() => { extractView?.Close(); });
4357
});

VP.NET.GUI/ViewModels/MainWindowViewModel.cs

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public MainWindowViewModel()
3434
Settings.Load();
3535
}catch (Exception ex)
3636
{
37-
37+
Log.Add(Log.LogSeverity.Error, "MainWindowViewModel", ex);
3838
}
3939
}
4040

@@ -54,41 +54,47 @@ private void AddWorkingFile(string path)
5454
/// </summary>
5555
internal async void OpenFile()
5656
{
57-
FilePickerOpenOptions options = new FilePickerOpenOptions();
58-
options.AllowMultiple = true;
59-
options.Title = "Open VP files";
60-
if(Settings.LastVPLoadPath != null )
57+
try
6158
{
62-
options.SuggestedStartLocation = await MainWindow.Instance!.StorageProvider.TryGetFolderFromPathAsync(Settings.LastVPLoadPath);
63-
}
64-
options.FileTypeFilter = new List<FilePickerFileType> {
59+
FilePickerOpenOptions options = new FilePickerOpenOptions();
60+
options.AllowMultiple = true;
61+
options.Title = "Open VP files";
62+
if (Settings.LastVPLoadPath != null)
63+
{
64+
options.SuggestedStartLocation = await MainWindow.Instance!.StorageProvider.TryGetFolderFromPathAsync(Settings.LastVPLoadPath);
65+
}
66+
options.FileTypeFilter = new List<FilePickerFileType> {
6567
new("VP files (*.vp, *.vpc)") { Patterns = new[] { "*.vp", "*.vpc" } },
6668
new("All files (*.*)") { Patterns = new[] { "*" } }
6769
};
6870

6971

70-
var result = await MainWindow.Instance!.StorageProvider.OpenFilePickerAsync(options);
72+
var result = await MainWindow.Instance!.StorageProvider.OpenFilePickerAsync(options);
7173

72-
if (result != null && result.Count > 0)
73-
{
74-
var newPath = (await result[0].GetParentAsync())?.Path.LocalPath;
75-
if (Settings.LastVPLoadPath != newPath)
74+
if (result != null && result.Count > 0)
7675
{
77-
Settings.LastVPLoadPath = newPath;
78-
Settings.Save();
79-
}
80-
81-
foreach (var file in result)
82-
{
83-
try
76+
var newPath = (await result[0].GetParentAsync())?.Path.LocalPath;
77+
if (Settings.LastVPLoadPath != newPath)
8478
{
85-
AddWorkingFile(file.Path.LocalPath);
79+
Settings.LastVPLoadPath = newPath;
80+
Settings.Save();
8681
}
87-
catch (Exception ex)
82+
83+
foreach (var file in result)
8884
{
89-
System.Diagnostics.Debug.WriteLine(ex.ToString());
85+
try
86+
{
87+
AddWorkingFile(file.Path.LocalPath);
88+
}
89+
catch (Exception ex)
90+
{
91+
Log.Add(Log.LogSeverity.Error, "MainWindowViewModel.OpenFile(1)", ex);
92+
}
9093
}
9194
}
95+
}catch (Exception ex)
96+
{
97+
Log.Add(Log.LogSeverity.Error, "MainWindowViewModel.OpenFile(2)", ex);
9298
}
9399
}
94100

@@ -97,34 +103,40 @@ internal async void OpenFile()
97103
/// </summary>
98104
internal async void OpenFolder()
99105
{
100-
FolderPickerOpenOptions options = new FolderPickerOpenOptions();
101-
options.Title = "Open a folder containing VP files";
102-
if (Settings.LastVPLoadPath != null)
103-
{
104-
options.SuggestedStartLocation = await MainWindow.Instance!.StorageProvider.TryGetFolderFromPathAsync(Settings.LastVPLoadPath);
105-
}
106-
var result = await MainWindow.Instance!.StorageProvider.OpenFolderPickerAsync(options);
107-
108-
if (result != null && result.Count > 0)
106+
try
109107
{
110-
111-
string[] files = Directory.GetFiles(result[0].Path.LocalPath,"*.vp*");
112-
if (Settings.LastVPLoadPath != result[0].Path.LocalPath)
108+
FolderPickerOpenOptions options = new FolderPickerOpenOptions();
109+
options.Title = "Open a folder containing VP files";
110+
if (Settings.LastVPLoadPath != null)
113111
{
114-
Settings.LastVPLoadPath = result[0].Path.LocalPath;
115-
Settings.Save();
112+
options.SuggestedStartLocation = await MainWindow.Instance!.StorageProvider.TryGetFolderFromPathAsync(Settings.LastVPLoadPath);
116113
}
117-
foreach (var file in files)
114+
var result = await MainWindow.Instance!.StorageProvider.OpenFolderPickerAsync(options);
115+
116+
if (result != null && result.Count > 0)
118117
{
119-
try
118+
119+
string[] files = Directory.GetFiles(result[0].Path.LocalPath, "*.vp*");
120+
if (Settings.LastVPLoadPath != result[0].Path.LocalPath)
120121
{
121-
AddWorkingFile(file);
122+
Settings.LastVPLoadPath = result[0].Path.LocalPath;
123+
Settings.Save();
122124
}
123-
catch (Exception ex)
125+
foreach (var file in files)
124126
{
125-
System.Diagnostics.Debug.WriteLine(ex.ToString());
127+
try
128+
{
129+
AddWorkingFile(file);
130+
}
131+
catch (Exception ex)
132+
{
133+
Log.Add(Log.LogSeverity.Error, "MainWindowViewModel.OpenFolder(1)", ex);
134+
}
126135
}
127136
}
137+
} catch (Exception ex)
138+
{
139+
Log.Add(Log.LogSeverity.Error, "MainWindowViewModel.OpenFolder(2)", ex);
128140
}
129141
}
130142

@@ -136,13 +148,19 @@ internal async void OpenFolder()
136148
internal void RemoveFile(VpViewModel file)
137149
{
138150
Dispatcher.UIThread.Invoke(() =>
139-
{
140-
//We are closing the file it is currently open in view?
141-
if(FolderViewModel.VpFilePath == file.VpPath)
151+
{
152+
try
153+
{
154+
//We are closing the file it is currently open in view?
155+
if (FolderViewModel.VpFilePath == file.VpPath)
156+
{
157+
FolderViewModel.ResetView();
158+
}
159+
WorkingFiles.Remove(file);
160+
}catch (Exception ex)
142161
{
143-
FolderViewModel.ResetView();
162+
Log.Add(Log.LogSeverity.Error, "MainWindowViewModel.RemoveFile()", ex);
144163
}
145-
WorkingFiles.Remove(file);
146164
});
147165
}
148166

VP.NET.GUI/ViewModels/MultiToolViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ await Task.Run(async () => {
121121
}
122122
catch (Exception ex)
123123
{
124+
Log.Add(Log.LogSeverity.Error, "MultiToolViewModel.CompressVPCs()", ex);
124125
Dispatcher.UIThread.Invoke(() => { Message += "\nError: " + ex.Message; });
125126
}
126127
}
@@ -234,6 +235,7 @@ await Task.Run(async () => {
234235
}
235236
catch (Exception ex)
236237
{
238+
Log.Add(Log.LogSeverity.Error, "MultiToolViewModel.DecompressVPCs()", ex);
237239
Dispatcher.UIThread.Invoke(() => { Message += "\nError: " + ex.Message; });
238240
}
239241
}
@@ -341,6 +343,7 @@ public async void DecompressLZ41Files()
341343
}
342344
catch (Exception ex)
343345
{
346+
Log.Add(Log.LogSeverity.Error, "MultiToolViewModel.DecompressLZ41Files()", ex);
344347
Dispatcher.UIThread.Invoke(() => { Message += "\nError: " + ex.Message; });
345348
}
346349
}
@@ -447,6 +450,7 @@ public async void CompressLZ41Files()
447450
}
448451
catch (Exception ex)
449452
{
453+
Log.Add(Log.LogSeverity.Error, "MultiToolViewModel.CompressLZ41Files()", ex);
450454
Dispatcher.UIThread.Invoke(() => { Message += "\nError: " + ex.Message; });
451455
}
452456
}

0 commit comments

Comments
 (0)