refactor: Use Serilog

This commit is contained in:
Mira 2024-03-01 08:05:36 +01:00
parent e5003d22e8
commit 42f620673c
Signed by untrusted user who does not match committer: Xorog
GPG key ID: 983798ED9C3E7C36
4 changed files with 27 additions and 45 deletions

View file

@ -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; }
}

View file

@ -7,12 +7,10 @@
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY // but WITHOUT ANY WARRANTY
global using Xorog.Logger;
global using Xorog.UniversalExtensions; global using Xorog.UniversalExtensions;
global using Xorog.UniversalExtensions.Entities; global using Xorog.UniversalExtensions.Entities;
global using static TranslationSourceGenerator.Log;
global using static Xorog.UniversalExtensions.UniversalExtensions;
global using Newtonsoft.Json; global using Newtonsoft.Json;
global using Serilog;
using System.Reflection; using System.Reflection;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -62,25 +60,27 @@ public class Translations : ITranslations
if (!Directory.Exists($"{new FileInfo(Assembly.GetExecutingAssembly().FullName).Directory.FullName}/logs")) if (!Directory.Exists($"{new FileInfo(Assembly.GetExecutingAssembly().FullName).Directory.FullName}/logs"))
_ = Directory.CreateDirectory($"{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 () => _ = Task.Run(async () =>
{ {
if (!File.Exists(args.Length > 0 ? args[0] : "e252zru2rjb2rtb23jbrj2bthj2bthjb2jtb4jbtb2jtb4hj")) if (!File.Exists(args.Length > 0 ? args[0] : "e252zru2rjb2rtb23jbrj2bthj2bthjb2jtb4jbtb2jtb4hj"))
{ {
_logger.LogError("Translation json expected as arg0"); Log.Error("Translation json expected as arg0");
return; return;
} }
if (!File.Exists(args.Length > 1 ? args[1] : "e252zru2rjb2rtb23jbrj2bthj2bthjb2jtb4jbtb2jtb4hj")) if (!File.Exists(args.Length > 1 ? args[1] : "e252zru2rjb2rtb23jbrj2bthj2bthjb2jtb4jbtb2jtb4hj"))
{ {
_logger.LogError("Translation json expected as arg1"); Log.Error("Translation json expected as arg1");
return; return;
} }
if (args.Length < 2) if (args.Length < 2)
{ {
_logger.LogError("Namespace expected as arg2"); Log.Error("Namespace expected as arg2");
return; return;
} }
@ -88,9 +88,9 @@ public class Translations : ITranslations
this.TranslationCs = args[1]; this.TranslationCs = args[1];
this.Namespace = args[2]; this.Namespace = args[2];
_logger.LogDebug("Project Makoto Translation Source Generator started up."); Log.Debug("Project Makoto Translation Source Generator started up.");
_logger.LogDebug("Strings.json Location: {0}", Path.GetFullPath(this.StringsJson)); Log.Debug("Strings.json Location: {0}", Path.GetFullPath(this.StringsJson));
_logger.LogDebug("Translation.cs Location: {0}", Path.GetFullPath(this.TranslationCs)); Log.Debug("Translation.cs Location: {0}", Path.GetFullPath(this.TranslationCs));
DateTime lastModify = new(); DateTime lastModify = new();
while (true) while (true)
{ {
@ -107,7 +107,7 @@ public class Translations : ITranslations
List<string> Warnings = new(); List<string> Warnings = new();
var Insert = ""; 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)); var jsonFile = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(this.StringsJson));
@ -170,7 +170,7 @@ public class Translations : ITranslations
if (addedFields[IndexPath].Contains(line)) if (addedFields[IndexPath].Contains(line))
continue; continue;
_logger.LogDebug("Found SingleKey '{0}'", item.Key); Log.Debug("Found SingleKey '{0}'", item.Key);
Insert = Insert.Insert(InsertPosition, $"\n{line}"); Insert = Insert.Insert(InsertPosition, $"\n{line}");
addedFields[IndexPath].Add(line); addedFields[IndexPath].Add(line);
@ -185,7 +185,7 @@ public class Translations : ITranslations
if (addedFields[IndexPath].Contains(line)) if (addedFields[IndexPath].Contains(line))
continue; continue;
_logger.LogDebug("Found MultiKey '{0}'", item.Key); Log.Debug("Found MultiKey '{0}'", item.Key);
Insert = Insert.Insert(InsertPosition, $"\n{line}"); Insert = Insert.Insert(InsertPosition, $"\n{line}");
addedFields[IndexPath].Add(line); addedFields[IndexPath].Add(line);
@ -197,7 +197,7 @@ public class Translations : ITranslations
} }
else 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" + var line = $"\n{new string(' ', depth * 4)}public {className} {fieldName};\n" +
$"{new string(' ', depth * 4)}public sealed class {className}\n" + $"{new string(' ', depth * 4)}public sealed class {className}\n" +
@ -217,7 +217,7 @@ public class Translations : ITranslations
} }
case JTokenType.Integer: 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)};"; var line = $"\n{new string(' ', depth * 4)}public int {CreateValidValueName(item.Key)};";
@ -230,7 +230,7 @@ public class Translations : ITranslations
} }
case JTokenType.Array: 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" + var line = $"\n{new string(' ', depth * 4)}public {className}[] {fieldName};\n" +
$"{new string(' ', depth * 4)}public sealed class {className}\n" + $"{new string(' ', depth * 4)}public sealed class {className}\n" +
@ -261,20 +261,20 @@ public class Translations : ITranslations
RecursiveHandle(jsonFile, "", 1); RecursiveHandle(jsonFile, "", 1);
foreach (var b in Warnings) 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"))); 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()))); 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) 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) catch (Exception ex)
{ {
_logger.LogError("Failed to watch file", ex); Log.Error(ex, "Failed to watch file");
await Task.Delay(10000); await Task.Delay(10000);
} }
} }

View file

@ -9,7 +9,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Dependencies\Xorog.Logger\Xorog.Logger.csproj" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dependencies\Xorog.UniversalExtensions\Xorog.UniversalExtensions.csproj" /> <ProjectReference Include="..\Dependencies\Xorog.UniversalExtensions\Xorog.UniversalExtensions.csproj" />
</ItemGroup> </ItemGroup>

View file

@ -5,8 +5,6 @@ VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TranslationSourceGenerator", "TranslationSourceGenerator.csproj", "{1962E64F-BFA8-4ECA-83C6-FD09959D7E98}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TranslationSourceGenerator", "TranslationSourceGenerator.csproj", "{1962E64F-BFA8-4ECA-83C6-FD09959D7E98}"
EndProject 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}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xorog.UniversalExtensions", "..\Dependencies\Xorog.UniversalExtensions\Xorog.UniversalExtensions.csproj", "{1082AEE5-F298-412D-BE0D-12253D25AF1C}"
EndProject EndProject
Global Global
@ -22,12 +20,6 @@ Global
{1962E64F-BFA8-4ECA-83C6-FD09959D7E98}.Release|x64.Build.0 = Release|x64 {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.ActiveCfg = Debug|x64
{1962E64F-BFA8-4ECA-83C6-FD09959D7E98}.x64|x64.Build.0 = 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.ActiveCfg = Debug|x64
{1082AEE5-F298-412D-BE0D-12253D25AF1C}.Debug|x64.Build.0 = Debug|x64 {1082AEE5-F298-412D-BE0D-12253D25AF1C}.Debug|x64.Build.0 = Debug|x64
{1082AEE5-F298-412D-BE0D-12253D25AF1C}.Release|x64.ActiveCfg = Release|x64 {1082AEE5-F298-412D-BE0D-12253D25AF1C}.Release|x64.ActiveCfg = Release|x64