Skip to content

Commit

Permalink
SerilogEnricher
Browse files Browse the repository at this point in the history
  • Loading branch information
LongerWarrior committed Jan 30, 2025
1 parent 32334a0 commit a704612
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 18 deletions.
19 changes: 12 additions & 7 deletions FModel/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,20 @@ protected override void OnStartup(StartupEventArgs e)
Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, "Logs"));
Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, ".data"));

const string template = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Enriched}: {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
#if DEBUG
Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).WriteTo.File(
Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.txt"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [FModel] [{Level:u3}] {Message:lj}{NewLine}{Exception}").CreateLogger();
.Enrich.With<SourceEnricher>()
.MinimumLevel.Verbose()
.WriteTo.Console(outputTemplate: template, theme: AnsiConsoleTheme.Literate)
.WriteTo.File(outputTemplate: template,
path: Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.txt"))
#else
Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).WriteTo.File(
Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.txt"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [FModel] [{Level:u3}] {Message:lj}{NewLine}{Exception}").CreateLogger();
.Enrich.With<CallerEnricher>()
.WriteTo.File(outputTemplate: template,
path: Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.txt"))
#endif
.CreateLogger();

Log.Information("Version {Version} ({CommitId})", Constants.APP_VERSION, Constants.APP_COMMIT_ID);
Log.Information("{OS}", GetOperatingSystemProductName());
Expand Down Expand Up @@ -183,4 +188,4 @@ public static string GetRegistryValue(string path, string name = null, RegistryH
return rk.GetValue(name, null) as string;
return string.Empty;
}
}
}
63 changes: 63 additions & 0 deletions FModel/Framework/SerilogEnricher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Diagnostics;
using System.Reflection;
using Serilog.Core;
using Serilog.Events;

namespace FModel.Framework;

public abstract class SerilogEnricher : ILogEventEnricher
{
public abstract void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory);

protected bool TryGetCaller(out MethodBase method)
{
method = null;

var serilogAssembly = typeof(Serilog.Log).Assembly;
var stack = new StackTrace(3);

foreach (var frame in stack.GetFrames())
{
var m = frame.GetMethod();
if (m?.DeclaringType is null) continue;

if (m.DeclaringType.Assembly != serilogAssembly)
{
method = m;
break;
}
}

return method != null;
}
}

public class SourceEnricher : SerilogEnricher
{
public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var source = "N/A";
if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContext))
{
source = sourceContext.ToString()[1..^1];
}
else if (TryGetCaller(out var method))
{
source = method.DeclaringType?.Namespace;
}

logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", source.Split('.')[0]));
}
}

public class CallerEnricher : SerilogEnricher
{
public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (TryGetCaller(out var method))
{
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", $"{method.DeclaringType?.FullName}.{method.Name}"));
}
}
}
16 changes: 13 additions & 3 deletions FModel/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ await Task.WhenAll(
#if DEBUG
// await _threadWorkerView.Begin(cancellationToken =>
// _applicationView.CUE4Parse.Extract(cancellationToken,
// "FortniteGame/Content/Athena/Apollo/Maps/UI/Apollo_Terrain_Minimap.uasset"));
// "MyProject/Content/FirstPerson/Meshes/FirstPersonProjectileMesh.uasset"));
// await _threadWorkerView.Begin(cancellationToken =>
// _applicationView.CUE4Parse.Extract(cancellationToken,
// "FortniteGame/Content/Environments/Helios/Props/GlacierHotel/GlacierHotel_Globe_A/Meshes/SM_GlacierHotel_Globe_A.uasset"));
// "RED/Content/Chara/ABA/Costume01/Animation/Charaselect/body/stand_body01.uasset"));
#endif
}

Expand Down Expand Up @@ -208,7 +208,7 @@ private async void OnFolderTextureClick(object sender, RoutedEventArgs e)
await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.TextureFolder(cancellationToken, folder); });
FLogger.Append(ELog.Information, () =>
{
FLogger.Text("Successfully saved ", Constants.WHITE);
FLogger.Text("Successfully saved textures from ", Constants.WHITE);
FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.TextureDirectory, true);
});
}
Expand All @@ -219,6 +219,11 @@ private async void OnFolderModelClick(object sender, RoutedEventArgs e)
if (AssetsFolderName.SelectedItem is TreeItem folder)
{
await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.ModelFolder(cancellationToken, folder); });
FLogger.Append(ELog.Information, () =>
{
FLogger.Text("Successfully saved models from ", Constants.WHITE);
FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.ModelDirectory, true);
});
}
}

Expand All @@ -227,6 +232,11 @@ private async void OnFolderAnimationClick(object sender, RoutedEventArgs e)
if (AssetsFolderName.SelectedItem is TreeItem folder)
{
await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.AnimationFolder(cancellationToken, folder); });
FLogger.Append(ELog.Information, () =>
{
FLogger.Text("Successfully saved animations from ", Constants.WHITE);
FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.ModelDirectory, true);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion FModel/Services/DiscordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DiscordHandler

private readonly Assets _staticAssets = new()
{
LargeImageKey = "official_logo", SmallImageKey = "verified", SmallImageText = $"v{Constants.APP_VERSION}"
LargeImageKey = "official_logo", SmallImageKey = "verified", SmallImageText = $"v{Constants.APP_VERSION} ({Constants.APP_SHORT_COMMIT_ID})"
};

private readonly Button[] _buttons =
Expand Down
8 changes: 2 additions & 6 deletions FModel/ViewModels/CUE4ParseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,7 @@ public void ExtractAndScroll(CancellationToken cancellationToken, string fullPat
SnooperViewer.Run();
return true;
}
case UAnimSequence when isNone && ModelIsWaitingAnimation:
case UAnimMontage when isNone && ModelIsWaitingAnimation:
case UAnimComposite when isNone && ModelIsWaitingAnimation:
case UAnimSequenceBase when isNone && ModelIsWaitingAnimation:
{
SnooperViewer.Renderer.Animate(pointer.Object);
SnooperViewer.Run();
Expand All @@ -924,9 +922,7 @@ public void ExtractAndScroll(CancellationToken cancellationToken, string fullPat
case USkeletalMesh when HasFlag(bulk, EBulkType.Meshes):
case USkeleton when UserSettings.Default.SaveSkeletonAsMesh && HasFlag(bulk, EBulkType.Meshes):
// case UMaterialInstance when HasFlag(bulk, EBulkType.Materials): // read the fucking json
case UAnimSequence when HasFlag(bulk, EBulkType.Animations):
case UAnimMontage when HasFlag(bulk, EBulkType.Animations):
case UAnimComposite when HasFlag(bulk, EBulkType.Animations):
case UAnimSequenceBase when HasFlag(bulk, EBulkType.Animations):
{
SaveExport(pointer.Object.Value, updateUi);
return true;
Expand Down

0 comments on commit a704612

Please sign in to comment.