From 42f620673c7f28b47b0410ba3b2a3939a0bc5045 Mon Sep 17 00:00:00 2001 From: Mira <56395159+TheXorog@users.noreply.github.com> Date: Fri, 1 Mar 2024 08:05:36 +0100 Subject: [PATCH] refactor: Use Serilog --- TranslationSourceGenerator/Log.cs | 15 ------- TranslationSourceGenerator/Program.cs | 42 +++++++++---------- .../TranslationSourceGenerator.csproj | 7 +++- .../TranslationSourceGenerator.sln | 8 ---- 4 files changed, 27 insertions(+), 45 deletions(-) delete mode 100644 TranslationSourceGenerator/Log.cs diff --git a/TranslationSourceGenerator/Log.cs b/TranslationSourceGenerator/Log.cs deleted file mode 100644 index ade0837..0000000 --- a/TranslationSourceGenerator/Log.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Project Makoto -// Copyright (C) 2023 Fortunevale -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY - -namespace TranslationSourceGenerator; - -internal class Log -{ - internal static LoggerClient _logger { get; set; } -} diff --git a/TranslationSourceGenerator/Program.cs b/TranslationSourceGenerator/Program.cs index 0cc9cb6..2999656 100644 --- a/TranslationSourceGenerator/Program.cs +++ b/TranslationSourceGenerator/Program.cs @@ -7,12 +7,10 @@ // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY -global using Xorog.Logger; global using Xorog.UniversalExtensions; global using Xorog.UniversalExtensions.Entities; -global using static TranslationSourceGenerator.Log; -global using static Xorog.UniversalExtensions.UniversalExtensions; global using Newtonsoft.Json; +global using Serilog; using System.Reflection; using Newtonsoft.Json.Linq; using System.Text.RegularExpressions; @@ -62,25 +60,27 @@ public class Translations : ITranslations if (!Directory.Exists($"{new FileInfo(Assembly.GetExecutingAssembly().FullName).Directory.FullName}/logs")) _ = Directory.CreateDirectory($"{new FileInfo(Assembly.GetExecutingAssembly().FullName).Directory.FullName}/logs"); - _logger = LoggerClient.StartLogger($"{new FileInfo(Assembly.GetExecutingAssembly().FullName).Directory.FullName}/logs/{DateTime.UtcNow:dd-MM-yyyy_HH-mm-ss}.log", CustomLogLevel.Debug, DateTime.UtcNow.AddDays(-3), false); + Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .CreateLogger(); _ = Task.Run(async () => { if (!File.Exists(args.Length > 0 ? args[0] : "e252zru2rjb2rtb23jbrj2bthj2bthjb2jtb4jbtb2jtb4hj")) { - _logger.LogError("Translation json expected as arg0"); + Log.Error("Translation json expected as arg0"); return; } if (!File.Exists(args.Length > 1 ? args[1] : "e252zru2rjb2rtb23jbrj2bthj2bthjb2jtb4jbtb2jtb4hj")) { - _logger.LogError("Translation json expected as arg1"); + Log.Error("Translation json expected as arg1"); return; } if (args.Length < 2) { - _logger.LogError("Namespace expected as arg2"); + Log.Error("Namespace expected as arg2"); return; } @@ -88,9 +88,9 @@ public class Translations : ITranslations this.TranslationCs = args[1]; this.Namespace = args[2]; - _logger.LogDebug("Project Makoto Translation Source Generator started up."); - _logger.LogDebug("Strings.json Location: {0}", Path.GetFullPath(this.StringsJson)); - _logger.LogDebug("Translation.cs Location: {0}", Path.GetFullPath(this.TranslationCs)); + Log.Debug("Project Makoto Translation Source Generator started up."); + Log.Debug("Strings.json Location: {0}", Path.GetFullPath(this.StringsJson)); + Log.Debug("Translation.cs Location: {0}", Path.GetFullPath(this.TranslationCs)); DateTime lastModify = new(); while (true) { @@ -107,7 +107,7 @@ public class Translations : ITranslations List Warnings = new(); var Insert = ""; - _logger.LogDebug("Translation file updated. Updating Translations.cs.."); + Log.Debug("Translation file updated. Updating Translations.cs.."); var jsonFile = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(this.StringsJson)); @@ -170,7 +170,7 @@ public class Translations : ITranslations if (addedFields[IndexPath].Contains(line)) continue; - _logger.LogDebug("Found SingleKey '{0}'", item.Key); + Log.Debug("Found SingleKey '{0}'", item.Key); Insert = Insert.Insert(InsertPosition, $"\n{line}"); addedFields[IndexPath].Add(line); @@ -185,7 +185,7 @@ public class Translations : ITranslations if (addedFields[IndexPath].Contains(line)) continue; - _logger.LogDebug("Found MultiKey '{0}'", item.Key); + Log.Debug("Found MultiKey '{0}'", item.Key); Insert = Insert.Insert(InsertPosition, $"\n{line}"); addedFields[IndexPath].Add(line); @@ -197,7 +197,7 @@ public class Translations : ITranslations } else { - _logger.LogDebug("Found Group '{0}'", item.Key); + Log.Debug("Found Group '{0}'", item.Key); var line = $"\n{new string(' ', depth * 4)}public {className} {fieldName};\n" + $"{new string(' ', depth * 4)}public sealed class {className}\n" + @@ -217,7 +217,7 @@ public class Translations : ITranslations } case JTokenType.Integer: { - _logger.LogDebug("Found Int '{0}'", item.Key); + Log.Debug("Found Int '{0}'", item.Key); var line = $"\n{new string(' ', depth * 4)}public int {CreateValidValueName(item.Key)};"; @@ -230,7 +230,7 @@ public class Translations : ITranslations } case JTokenType.Array: { - _logger.LogDebug("Found Array '{0}'", item.Key); + Log.Debug("Found Array '{0}'", item.Key); var line = $"\n{new string(' ', depth * 4)}public {className}[] {fieldName};\n" + $"{new string(' ', depth * 4)}public sealed class {className}\n" + @@ -261,20 +261,20 @@ public class Translations : ITranslations RecursiveHandle(jsonFile, "", 1); foreach (var b in Warnings) { - _logger.LogWarn(b); + Log.Warning(b); } - _logger.LogWarn("Source Generation finished with {Count} warnings.", Warnings.Count); + Log.Warning("Source Generation finished with {Count} warnings.", Warnings.Count); Insert = string.Join("\n", Insert.Split("\n").Where(x => !x.Contains("InsertPoint"))); File.WriteAllText(this.TranslationCs, string.Join("\n", this.MakotoSourceOrigin.Replace("// InsertPoint", Insert).ReplaceLineEndings("\n").Split("\n", StringSplitOptions.RemoveEmptyEntries).Where(x => !x.IsNullOrWhiteSpace()))); - _logger.LogDebug("Updated Translations.cs."); + Log.Debug("Updated Translations.cs."); } catch (Exception ex) { - _logger.LogError("Failed to update Translations.cs.", ex); + Log.Error(ex, "Failed to update Translations.cs."); } } @@ -284,7 +284,7 @@ public class Translations : ITranslations } catch (Exception ex) { - _logger.LogError("Failed to watch file", ex); + Log.Error(ex, "Failed to watch file"); await Task.Delay(10000); } } diff --git a/TranslationSourceGenerator/TranslationSourceGenerator.csproj b/TranslationSourceGenerator/TranslationSourceGenerator.csproj index 26da3df..5f63f87 100644 --- a/TranslationSourceGenerator/TranslationSourceGenerator.csproj +++ b/TranslationSourceGenerator/TranslationSourceGenerator.csproj @@ -9,7 +9,12 @@ - + + + + + + diff --git a/TranslationSourceGenerator/TranslationSourceGenerator.sln b/TranslationSourceGenerator/TranslationSourceGenerator.sln index 1b97e5e..0b3aca6 100644 --- a/TranslationSourceGenerator/TranslationSourceGenerator.sln +++ b/TranslationSourceGenerator/TranslationSourceGenerator.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 17.8.34309.116 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TranslationSourceGenerator", "TranslationSourceGenerator.csproj", "{1962E64F-BFA8-4ECA-83C6-FD09959D7E98}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xorog.Logger", "..\Dependencies\Xorog.Logger\Xorog.Logger.csproj", "{8A8883E2-C01C-48A5-9CFF-16DB013DA0C8}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xorog.UniversalExtensions", "..\Dependencies\Xorog.UniversalExtensions\Xorog.UniversalExtensions.csproj", "{1082AEE5-F298-412D-BE0D-12253D25AF1C}" EndProject Global @@ -22,12 +20,6 @@ Global {1962E64F-BFA8-4ECA-83C6-FD09959D7E98}.Release|x64.Build.0 = Release|x64 {1962E64F-BFA8-4ECA-83C6-FD09959D7E98}.x64|x64.ActiveCfg = Debug|x64 {1962E64F-BFA8-4ECA-83C6-FD09959D7E98}.x64|x64.Build.0 = Debug|x64 - {8A8883E2-C01C-48A5-9CFF-16DB013DA0C8}.Debug|x64.ActiveCfg = Debug|x64 - {8A8883E2-C01C-48A5-9CFF-16DB013DA0C8}.Debug|x64.Build.0 = Debug|x64 - {8A8883E2-C01C-48A5-9CFF-16DB013DA0C8}.Release|x64.ActiveCfg = Release|x64 - {8A8883E2-C01C-48A5-9CFF-16DB013DA0C8}.Release|x64.Build.0 = Release|x64 - {8A8883E2-C01C-48A5-9CFF-16DB013DA0C8}.x64|x64.ActiveCfg = x64|x64 - {8A8883E2-C01C-48A5-9CFF-16DB013DA0C8}.x64|x64.Build.0 = x64|x64 {1082AEE5-F298-412D-BE0D-12253D25AF1C}.Debug|x64.ActiveCfg = Debug|x64 {1082AEE5-F298-412D-BE0D-12253D25AF1C}.Debug|x64.Build.0 = Debug|x64 {1082AEE5-F298-412D-BE0D-12253D25AF1C}.Release|x64.ActiveCfg = Release|x64