Skip to content

Commit 4ade73c

Browse files
Overhauled logging (#9)
* Overhauled logging - Migrated all console logs to use Serilog - Moved SRMM logs to their own folder - Allow logs to rotate - Removed legacy logging system
1 parent 699660d commit 4ade73c

File tree

15 files changed

+349
-407
lines changed

15 files changed

+349
-407
lines changed

ShinRyuModManager-CE/ModLoadOrder/Dependency/CPKGen/CPK.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using Serilog;
23

34
namespace CriPakTools {
45
public class CPK(Tools tool) {
@@ -61,7 +62,7 @@ public bool ReadCPK(string sPath) {
6162
}
6263
} catch (Exception ex) {
6364
//MessageBox.Show(ex.ToString());
64-
Console.WriteLine(ex.ToString());
65+
Log.Error(ex, "");
6566
}
6667

6768
TocOffset = (ulong)GetColumnsData2(Utf, 0, "TocOffset", 3);
@@ -660,8 +661,7 @@ public static object GetColumnData(UTF utf, int row, string name) {
660661
break;
661662
}
662663
} catch (Exception ex) {
663-
//MessageBox.Show(ex.ToString());
664-
Console.WriteLine(ex.ToString());
664+
Log.Error(ex, "");
665665

666666
return null;
667667
}
@@ -682,8 +682,7 @@ public static long GetColumnPosition(UTF utf, int row, string name) {
682682
break;
683683
}
684684
} catch (Exception ex) {
685-
//MessageBox.Show(ex.ToString());
686-
Console.WriteLine(ex.ToString());
685+
Log.Error(ex, "");
687686

688687
return -1;
689688
}
@@ -704,8 +703,7 @@ public static Type GetColumnType(UTF utf, int row, string name) {
704703
break;
705704
}
706705
} catch (Exception ex) {
707-
//MessageBox.Show(ex.ToString());
708-
Console.WriteLine(ex.ToString());
706+
Log.Error(ex, "");
709707

710708
return null;
711709
}

ShinRyuModManager-CE/ModLoadOrder/Generator.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Serilog;
2+
using Serilog.Events;
23
using ShinRyuModManager.ModLoadOrder.Mods;
34
using Utils;
45

@@ -92,19 +93,19 @@ public static async Task<MLO> GenerateModeLoadOrder(List<string> mods, bool loos
9293
modsObjects[i] = mod;
9394
}
9495

95-
Log.Information("Added {ModCount} mod(s) and {FilesCount} file(s)!\n", mods.Count, files.Count);
96+
Log.Information("Added {ModCount} mod(s) and {FilesCount} file(s)!", mods.Count, files.Count);
9697

9798
// Reverse the list because the last mod in the list should have the highest priority
9899
mods.Reverse();
99100

100-
Console.Write($"Generating {Constants.MLO} file...");
101+
Log.Information($"Generating {Constants.MLO} file...");
101102

102103
// Generate MLO
103104
var mlo = new MLO(modIndices, mods, files, loose.ParlessFolders, cpkDictionary);
104105

105106
mlo.WriteMLO(Path.Combine(GamePath.FullGamePath, Constants.MLO));
106107

107-
Console.WriteLine(" DONE!\n");
108+
Log.Information("Finished generating MLO.");
108109

109110
// Check if a mod has a par that will override the repacked par, and skip repacking it in that case
110111
foreach (var key in parDictionary.Keys.ToList()) {
@@ -140,21 +141,21 @@ public static async Task<MLO> GenerateModeLoadOrder(List<string> mods, bool loos
140141
if (cpkRepackingEnabled) {
141142
await CpkPatcher.RepackDictionary(cpkRepackDict);
142143
}
143-
144-
if (ConsoleOutput.ShowWarnings) {
145-
foreach (var key in modsWithFoldersNotFound.Keys.ToList()) {
146-
Console.WriteLine($"Warning: Some folders in the root of \"{key}\" do not exist in the game's data. Check if the mod was extracted correctly.");
147-
148-
if (ConsoleOutput.Verbose) {
149-
foreach (var folder in modsWithFoldersNotFound[key]) {
150-
Console.WriteLine($"Folder not found: {folder}");
151-
}
152-
}
153-
154-
Console.WriteLine();
144+
145+
if (Program.LogLevel > LogEventLevel.Warning)
146+
return mlo;
147+
148+
foreach (var key in modsWithFoldersNotFound.Keys.ToList()) {
149+
Log.Warning("Warning: Some folders in the root of \"{Key}\" do not exist in the game's data. Check if the mod was extracted correctly.", key);
150+
151+
if (Program.LogLevel != LogEventLevel.Verbose)
152+
continue;
153+
154+
foreach (var folder in modsWithFoldersNotFound[key]) {
155+
Log.Warning("Folder not found: {Folder}", folder);
155156
}
156157
}
157-
158+
158159
return mlo;
159160
}
160161
}

ShinRyuModManager-CE/ModLoadOrder/Mods/Mod.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using Serilog;
23
using Utils;
34

45
namespace ShinRyuModManager.ModLoadOrder.Mods;
@@ -26,45 +27,38 @@ public class Mod {
2627
/// </summary>
2728
public List<string> RepackCpKs { get; }
2829

29-
protected readonly ConsoleOutput ConsoleOutput;
30-
31-
public Mod(string name, int indent = 2) {
30+
public Mod(string name) {
3231
Name = name;
3332
Files = [];
3433
ParFolders = [];
3534
CpkFolders = [];
3635
RepackCpKs = [];
3736

38-
ConsoleOutput = new ConsoleOutput(indent);
39-
ConsoleOutput.WriteLine($"Reading directory: {name} ...");
37+
Log.Information("Reading directory: {Name} ...", name);
4038
}
4139

4240
public void PrintInfo() {
43-
ConsoleOutput.WriteLineIfVerbose();
44-
4541
if (Files.Count > 0 || ParFolders.Count > 0) {
4642
if (Files.Count > 0) {
47-
ConsoleOutput.WriteLine($"Added {Files.Count} file(s)");
43+
Log.Information("Added {FilesCount} file(s)", Files.Count);
4844
}
4945

5046
if (ParFolders.Count > 0) {
51-
ConsoleOutput.WriteLine($"Added {ParFolders.Count} folder(s) to be repacked");
47+
Log.Information("Added {ParFoldersCount} folder(s) to be repacked", ParFolders.Count);
5248
}
5349

5450
if (CpkFolders.Count > 0) {
55-
ConsoleOutput.WriteLine($"Added {CpkFolders.Count} CPK folder(s) to be bound");
51+
Log.Information("Added {CpkFoldersCount} CPK folder(s) to be bound", CpkFolders.Count);
5652
}
5753
} else {
58-
ConsoleOutput.WriteLine($"Nothing found for {Name}, skipping");
54+
Log.Information("Nothing found for {Name}, skipping", Name);
5955
}
60-
61-
ConsoleOutput.Flush();
6256
}
6357

6458
public void AddFiles(string path, string check) {
6559
var needsRepack = false;
6660
var basename = GamePath.GetBasename(path);
67-
var parentDir = new DirectoryInfo(path).Parent.Name;
61+
var parentDir = new DirectoryInfo(path).Parent!.Name;
6862

6963
// Check if this path does not need repacking
7064
if (Name != "Parless") {
@@ -144,7 +138,7 @@ public void AddFiles(string path, string check) {
144138

145139
if (GamePath.CurrentGame == Game.Yakuza5) {
146140
CpkFolders.Add(cpkDataPath + ".cpk");
147-
ConsoleOutput.WriteLineIfVerbose($"Adding CPK folder: {cpkDataPath}");
141+
Log.Verbose("Adding CPK folder: {CpkDataPath}", cpkDataPath);
148142
} else {
149143
if (GamePath.CurrentGame <= Game.YakuzaKiwami) {
150144
RepackCpKs.Add(cpkDataPath + ".cpk");
@@ -163,15 +157,15 @@ public void AddFiles(string path, string check) {
163157

164158
if (GamePath.CurrentGame is Game.Judgment or Game.LostJudgment) {
165159
CpkFolders.Add(cpkDataPath + ".par");
166-
ConsoleOutput.WriteLineIfVerbose($"Adding CPK folder: {cpkDataPath}");
160+
Log.Verbose("Adding CPK folder: {CpkDataPath}", cpkDataPath);
167161
}
168162

169163
break;
170164
case "gv_files":
171165
cpkDataPath = GamePath.RemoveModPath(path);
172166

173167
CpkFolders.Add($"{cpkDataPath}.cpk");
174-
ConsoleOutput.WriteLineIfVerbose($"Adding CPK folder: {cpkDataPath}");
168+
Log.Verbose("Adding CPK folder: {CpkDataPath}", cpkDataPath);
175169

176170
break;
177171
}
@@ -241,14 +235,14 @@ public void AddFiles(string path, string check) {
241235

242236
// Add this folder to the list of folders to be repacked and stop recursing
243237
ParFolders.Add(dataPath);
244-
ConsoleOutput.WriteLineIfVerbose($"Adding repackable folder: {dataPath}");
238+
Log.Verbose("Adding repackable folder: {DataPath}", dataPath);
245239
} else {
246240
// Add files in current directory
247241
var files = Directory.GetFiles(path).Where(f => !f.EndsWith(Constants.VORTEX_MANAGED_FILE)).Select(GamePath.GetDataPathFrom);
248242

249243
foreach (var p in files) {
250244
Files.Add(p);
251-
ConsoleOutput.WriteLineIfVerbose($"Adding file: {p}");
245+
Log.Verbose("Adding file: {file}", p);
252246
}
253247

254248
var isParlessMod = GetType() == typeof(ParlessMod);

ShinRyuModManager-CE/ModLoadOrder/Mods/ParlessMod.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
using Serilog;
12
using Utils;
23

34
namespace ShinRyuModManager.ModLoadOrder.Mods;
45

5-
public class ParlessMod() : Mod(Constants.PARLESS_NAME, 0) {
6+
public class ParlessMod() : Mod(Constants.PARLESS_NAME) {
67
public List<ParlessFolder> ParlessFolders { get; } = [];
78

89
public new void PrintInfo() {
9-
ConsoleOutput.WriteLineIfVerbose();
10-
1110
if (ParlessFolders.Count > 0) {
12-
ConsoleOutput.WriteLine($"Added {ParlessFolders.Count} .parless path(s)");
11+
Log.Information("Added {ParlessFoldersCount} .parless path(s)", ParlessFolders.Count);
1312
}
1413

1514
base.PrintInfo();
@@ -35,7 +34,7 @@ public class ParlessMod() : Mod(Constants.PARLESS_NAME, 0) {
3534

3635
ParlessFolders.Add(folder);
3736

38-
ConsoleOutput.WriteLineIfVerbose($"Adding .parless path: {loosePath}");
37+
Log.Verbose("Adding .parless path: {LoosePath}", loosePath);
3938
} else {
4039
// Continue recursing until we find the next ".parless"
4140
foreach (var folder in Directory.GetDirectories(path)) {

ShinRyuModManager-CE/ParRepacker.cs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ public static void RemoveOldRepackedPars() {
3131
if (!Directory.Exists(pathToParlessMods))
3232
return;
3333

34-
Console.Write("Removing old pars...");
34+
Log.Information("Removing old pars...");
3535

3636
try {
3737
DeleteDirectory(pathToParlessMods);
3838
} catch {
39-
Console.WriteLine($" FAIL! {pathToParlessMods}\n");
39+
Log.Warning("Failed to remove old pars! {PathToParlessMods}", pathToParlessMods);
4040
}
4141

42-
Console.WriteLine(" DONE!\n");
42+
Log.Information("Removed old pars.");
4343
}
4444

4545
public static async Task RepackDictionary(Dictionary<string, List<string>> parDictionary) {
46-
var parTasks = new List<Task<ConsoleOutput>>();
46+
var parTasks = new List<Task>();
4747

4848
if (parDictionary.Count == 0) {
4949
Log.Information("No pars to repack.");
@@ -54,31 +54,19 @@ public static async Task RepackDictionary(Dictionary<string, List<string>> parDi
5454
Log.Information("Repacking pars...");
5555

5656
foreach (var parModPair in parDictionary) {
57-
var consoleOutput = new ConsoleOutput(2);
58-
59-
parTasks.Add(Task.Run(() => RepackPar(parModPair.Key, parModPair.Value, consoleOutput)));
57+
parTasks.Add(Task.Run(() => RepackPar(parModPair.Key, parModPair.Value)));
6058
}
6159

62-
while (parTasks.Count > 0) {
63-
var console = await Task.WhenAny(parTasks);
64-
65-
console.Result?.Flush();
66-
67-
parTasks.Remove(console);
68-
}
60+
await Task.WhenAll(parTasks);
6961

7062
/*foreach (var parModPair in parDictionary) {
71-
var consoleOutput = new ConsoleOutput(2);
72-
73-
RepackPar(parModPair.Key, parModPair.Value, consoleOutput);
74-
75-
consoleOutput.Flush();
63+
RepackPar(parModPair.Key, parModPair.Value);
7664
}*/
7765

7866
Log.Information("Repacked {ParDictionaryCount} par(s)!", parDictionary.Count);
7967
}
8068

81-
private static ConsoleOutput RepackPar(string parPath, List<string> mods, ConsoleOutput console) {
69+
private static void RepackPar(string parPath, List<string> mods) {
8270
parPath = parPath.TrimStart(Path.DirectorySeparatorChar);
8371

8472
var parPathReal = GamePath.GetRootParPath(parPath + ".par");
@@ -104,7 +92,7 @@ private static ConsoleOutput RepackPar(string parPath, List<string> mods, Consol
10492

10593
// Populate fileDict with the files inside each mod
10694
foreach (var mod in mods) {
107-
foreach (var modFile in GetModFiles(parPath, mod, console)) {
95+
foreach (var modFile in GetModFiles(parPath, mod)) {
10896
fileDict.TryAdd(modFile, mod);
10997
}
11098
}
@@ -195,39 +183,36 @@ private static ConsoleOutput RepackPar(string parPath, List<string> mods, Consol
195183
// Remove the .partemp directory
196184
DeleteDirectory(pathToTempPar);
197185

198-
console.WriteLineIfVerbose();
199-
console.WriteLine($"Repacked {fileDict.Count} file(s) in {parPath + ".par"}!");
200-
201-
return console;
186+
Log.Information("Repacked {FileDictCount} file(s) in {ParPath}!", fileDict.Count, parPath + ".par");
202187
}
203188

204-
private static List<string> GetModFiles(string par, string mod, ConsoleOutput console) {
189+
private static List<string> GetModFiles(string par, string mod) {
205190
List<string> result;
206191

207192
if (mod.StartsWith(Constants.PARLESS_NAME)) {
208193
// Get index of ".parless" in par path
209194
// 15 = ParlessMod.NAME.Length + 1
210-
result = GetModFiles(Path.Combine(GamePath.DataPath, par.Insert(int.Parse(mod[15..]) - 1, ".parless")), console);
195+
result = GetModFiles(Path.Combine(GamePath.DataPath, par.Insert(int.Parse(mod[15..]) - 1, ".parless")));
211196
} else {
212-
result = GetModFiles(GamePath.GetModPathFromDataPath(mod, par), console);
197+
result = GetModFiles(GamePath.GetModPathFromDataPath(mod, par));
213198
}
214199

215200
// Get file path relative to par
216201
return result.Select(f => f.Replace(".parless", "")[(f.Replace(".parless", "").IndexOf(par, StringComparison.Ordinal) + par.Length + 1)..]).ToList();
217202
}
218203

219-
private static List<string> GetModFiles(string path, ConsoleOutput console) {
204+
private static List<string> GetModFiles(string path) {
220205
List<string> files = [];
221206

222207
// Add files in current directory
223208
foreach (var p in Directory.GetFiles(path).Where(f => !f.EndsWith(Constants.VORTEX_MANAGED_FILE)).Select(GamePath.GetDataPathFrom)) {
224209
files.Add(p);
225-
console.WriteLineIfVerbose($"Adding file: {p}");
210+
Log.Verbose("Adding file: {file}", p);
226211
}
227212

228213
// Get files for all subdirectories
229214
foreach (var folder in Directory.GetDirectories(path)) {
230-
files.AddRange(GetModFiles(folder, console));
215+
files.AddRange(GetModFiles(folder));
231216
}
232217

233218
return files;

0 commit comments

Comments
 (0)