Skip to content

Commit

Permalink
Another iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Dec 10, 2024
1 parent f966aaf commit 4339657
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 146 deletions.
34 changes: 31 additions & 3 deletions ModTek/Features/Logging/LogLevelExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using HBS.Logging;

namespace ModTek.Features.Logging;
Expand All @@ -7,7 +8,7 @@ internal static class LogLevelExtension
{
internal static string LogToString(LogLevel level)
{
var eLogLevel = Convert(level);
var eLogLevel = ConvertHbsLogLevelToELogLevel(level);
return eLogLevel switch // fast switch with static string, in order of most occuring
{
ELogLevels.Trace => "TRACE",
Expand All @@ -23,10 +24,10 @@ internal static string LogToString(LogLevel level)

internal static bool IsLogLevelGreaterThan(LogLevel loggerLevel, LogLevel messageLevel)
{
return Convert(messageLevel) >= Convert(loggerLevel);
return ConvertHbsLogLevelToELogLevel(messageLevel) >= ConvertHbsLogLevelToELogLevel(loggerLevel);
}

private static ELogLevels Convert(LogLevel level)
private static ELogLevels ConvertHbsLogLevelToELogLevel(LogLevel level)
{
var intLevel = (int)level;
if (intLevel is >= (int)LogLevel.Debug and <= (int)LogLevel.Error)
Expand All @@ -36,6 +37,33 @@ private static ELogLevels Convert(LogLevel level)
return (ELogLevels)intLevel;
}

internal static TraceLevel GetLevelFilter(ILog log)
{
if (log is not Logger.LogImpl logImpl)
{
return TraceLevel.Verbose; // either off or verbose, better too much
}
var effectiveLevel = logImpl.EffectiveLevel;
var eLogLevel = ConvertHbsLogLevelToELogLevel(effectiveLevel);
return ConvertELogLevelToTraceLevel(eLogLevel);
}

private static TraceLevel ConvertELogLevelToTraceLevel(ELogLevels level)
{
level = (ELogLevels)((int)level / 10 * 10);
return level switch
{
ELogLevels.OFF => TraceLevel.Off,
ELogLevels.Fatal => TraceLevel.Error,
ELogLevels.Error => TraceLevel.Error,
ELogLevels.Warning => TraceLevel.Warning,
ELogLevels.Log => TraceLevel.Info,
ELogLevels.Debug => TraceLevel.Verbose,
ELogLevels.Trace => TraceLevel.Verbose,
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
};
}

// log levels
private enum ELogLevels
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Threading;
using BattleTech;
using fastJSON;
using HBS.Util;
using ModTek.Features.Logging;
using ModTek.Misc;
Expand Down Expand Up @@ -86,6 +87,7 @@ MethodBase GetMethod(BindingFlags bindingAttr = BindingFlags.Default)
Log.Main.Trace?.Log("IJsonTemplated.FromJSON " + fromJsonMethod);

yield return genericMethod.MakeGenericMethod(jsonTemplated);
// break;
}
}

Expand All @@ -109,29 +111,64 @@ internal class MyState
{
internal MTStopwatch.Tracker Tracker;
internal long counter = Interlocked.Increment(ref s_counter);

internal string nn;
internal string nh;
internal string hn;
internal string hh;

internal void Save()
{
var differentPopulate = true;
var differentSerializers = false; // tag sets and dictionaries work differently
if ((differentSerializers && nn != nh) || (differentPopulate && hn != nn))
{
File.WriteAllText(Path.Combine(basePath, $"{counter}_NN.json"), nn);
}
if ((differentSerializers && nn != nh) || (differentPopulate && nh != hh))
{
File.WriteAllText(Path.Combine(basePath, $"{counter}_NH.json"), nn);
}
if ((differentSerializers && hn != hh) || (differentPopulate && hn != nn))
{
File.WriteAllText(Path.Combine(basePath, $"{counter}_HN.json"), hn);
}
if ((differentSerializers && hn != hh) || (differentPopulate && nh != hh))
{
File.WriteAllText(Path.Combine(basePath, $"{counter}_HH.json"), hh);
}
}
}

private static long s_counter;

[HarmonyPriority(Priority.First)]
public static void Prefix(object target, string json, ref MyState __state)
{
if (target == null)
{
return;
}
if (typeof(MechDef).Assembly != target.GetType().Assembly)
{
return;
}

__state = new MyState();

if (testNewton) // && target.GetType() == typeof(MechDef)
{
s_newton.Start();
try
{
var mechDef = new MechDef();
HBSJsonUtils.PopulateObject(mechDef, json);
var output = HBSJsonUtils.SerializeObject(mechDef);
var path = Path.Combine(basePath, $"{__state.counter}_N.json");
File.WriteAllText(path, output);
var objectCopy = Activator.CreateInstance(target.GetType());
HBSJsonUtils.PopulateObject(objectCopy, json);
__state.nn = HBSJsonUtils.SerializeObject(objectCopy);
__state.nh = JSON.ToJSON(objectCopy);
}
catch (Exception ex)
{
Log.Main.Error?.Log("Error N PopulateObject SerializeObject" + target.GetType(), ex);
Log.Main.Error?.Log("Error Populating and Serializing " + target.GetType(), ex);
}
finally
{
Expand All @@ -145,19 +182,32 @@ public static void Prefix(object target, string json, ref MyState __state)
[HarmonyPriority(Priority.Last)]
public static void Postfix(object target, string json, ref MyState __state)
{
if (__state == null)
{
return;
}

s_stopwatch.AddMeasurement(__state.Tracker.End());

if (testNewton) // && target.GetType() == typeof(MechDef)
{
try
{
var output = HBSJsonUtils.SerializeObject(target);
var path = Path.Combine(basePath, $"{__state.counter}_H.json");
File.WriteAllText(path, output);
__state.hn = HBSJsonUtils.SerializeObject(target);
__state.hh = JSON.ToJSON(target);
}
catch (Exception ex)
{
Log.Main.Error?.Log("Error Serializing " + target.GetType(), ex);
}

try
{
__state.Save();
}
catch (Exception ex)
{
Log.Main.Error?.Log("Error H SerializeObject " + target.GetType(), ex);
Log.Main.Error?.Log("Error Saving JSONs " + target.GetType(), ex);
}
}
}
Expand Down
Loading

0 comments on commit 4339657

Please sign in to comment.