Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
debug log changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zkxs committed May 9, 2022
1 parent 9314b12 commit 22724e5
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion NeosModLoader/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal static class AssemblyLoader
Assembly assembly;
try
{
Logger.DebugInternal($"load assembly {filename}");
Logger.DebugFuncInternal(() => $"load assembly {filename}");
assembly = Assembly.LoadFile(filepath);
}
catch (Exception e)
Expand Down
4 changes: 2 additions & 2 deletions NeosModLoader/DebugInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal static void Log()
{
Logger.MsgInternal($"NeosModLoader v{ModLoader.VERSION} starting up!{(ModLoaderConfiguration.Get().Debug ? " Debug logs will be shown." : "")}");
Logger.MsgInternal($"CLR v{Environment.Version}");
Logger.DebugInternal($"Using .NET Framework: \"{AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}\"");
Logger.DebugInternal($"Using .NET Core: \"{Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName}\"");
Logger.DebugFuncInternal(() => $"Using .NET Framework: \"{AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}\"");
Logger.DebugFuncInternal(() => $"Using .NET Core: \"{Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName}\"");
Logger.MsgInternal($"Using Harmony v{GetAssemblyVersion(typeof(HarmonyLib.Harmony))}");
Logger.MsgInternal($"Using BaseX v{GetAssemblyVersion(typeof(BaseX.floatQ))}");
Logger.MsgInternal($"Using FrooxEngine v{GetAssemblyVersion(typeof(FrooxEngine.IComponent))}");
Expand Down
2 changes: 1 addition & 1 deletion NeosModLoader/JsonConverters/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object? exis
Type underlyingType = Enum.GetUnderlyingType(objectType);
if (TryConvert(reader!.Value!, underlyingType, out object? deserialized))
{
Logger.DebugInternal($"Deserializing a BaseX type: {objectType} from a {reader!.Value!.GetType()}");
Logger.DebugFuncInternal(() => $"Deserializing a BaseX type: {objectType} from a {reader!.Value!.GetType()}");
return deserialized!;
}

Expand Down
27 changes: 24 additions & 3 deletions NeosModLoader/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,46 @@ internal class Logger
// logged for null objects
internal readonly static string NULL_STRING = "null";

internal static bool IsDebugEnabled()
{
return ModLoaderConfiguration.Get().Debug;
}

internal static void DebugFuncInternal(Func<string> messageProducer)
{
if (IsDebugEnabled())
{
LogInternal(LogType.DEBUG, messageProducer());
}
}

internal static void DebugFuncExternal(Func<string> messageProducer)
{
if (IsDebugEnabled())
{
LogInternal(LogType.DEBUG, messageProducer(), SourceFromStackTrace());
}
}

internal static void DebugInternal(string message)
{
if (ModLoaderConfiguration.Get().Debug)
if (IsDebugEnabled())
{
LogInternal(LogType.DEBUG, message);
}
}

internal static void DebugExternal(object message)
{
if (ModLoaderConfiguration.Get().Debug)
if (IsDebugEnabled())
{
LogInternal(LogType.DEBUG, message, SourceFromStackTrace());
}
}

internal static void DebugListExternal(object[] messages)
{
if (ModLoaderConfiguration.Get().Debug)
if (IsDebugEnabled())
{
LogListInternal(LogType.DEBUG, messages, SourceFromStackTrace());
}
Expand Down
11 changes: 7 additions & 4 deletions NeosModLoader/ModConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ private static JsonSerializer CreateJsonSerializer()
};
List<JsonConverter> converters = new();
IList<JsonConverter> defaultConverters = settings.Converters;
if (defaultConverters != null)
if (defaultConverters != null && defaultConverters.Count() != 0)
{
Logger.DebugInternal($"Using {defaultConverters.Count()} default json converters");
Logger.DebugFuncInternal(() => $"Using {defaultConverters.Count()} default json converters");
converters.AddRange(defaultConverters);
}
converters.Add(new EnumConverter());
Expand Down Expand Up @@ -480,14 +480,14 @@ private bool AnyValuesSet()
/// </summary>
public void Save() // this overload is needed for binary compatibility (REMOVE IN NEXT MAJOR VERSION)
{
Save(false);
Save(false, false);
}

/// <summary>
/// Persist this configuration to disk. This method is not called automatically.
/// </summary>
/// <param name="saveDefaultValues">If true, default values will also be persisted</param>
public void Save(bool saveDefaultValues = false)
public void Save(bool saveDefaultValues = false) // this overload is needed for binary compatibility (REMOVE IN NEXT MAJOR VERSION)
{
// prevent saving if we've determined something is amiss with the configuration
if (!LoadedNeosMod.AllowSavingConfiguration)
Expand Down Expand Up @@ -527,6 +527,9 @@ public void Save(bool saveDefaultValues = false)

// I actually cannot believe I have to truncate the file myself
file.SetLength(file.Position);
file.Flush();

Logger.DebugFuncInternal(() => $"Saved ModConfiguration for \"{LoadedNeosMod.NeosMod.Name}\" in {stopwatch.ElapsedMilliseconds}ms");
}

private void FireConfigurationChangedEvent(ModConfigurationKey key, string? label)
Expand Down
4 changes: 2 additions & 2 deletions NeosModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal static void LoadMods()
else if (config.Debug)
{
string owner = owners.FirstOrDefault();
Logger.DebugInternal($"method \"{patchedMethod.FullDescription()}\" has been patched by \"{owner}\"");
Logger.DebugFuncInternal(() => $"method \"{patchedMethod.FullDescription()}\" has been patched by \"{owner}\"");
}
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ private static string TypesForOwner(Patches patches, string owner)
private static void HookMod(LoadedNeosMod mod)
{
SplashChanger.SetCustom($"Starting mod [{mod.NeosMod.Name}/{mod.NeosMod.Version}]");
Logger.DebugInternal($"calling OnEngineInit() for [{mod.NeosMod.Name}]");
Logger.DebugFuncInternal(() => $"calling OnEngineInit() for [{mod.NeosMod.Name}]");
try
{
mod.NeosMod.OnEngineInit();
Expand Down
2 changes: 2 additions & 0 deletions NeosModLoader/NeosMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace NeosModLoader
// contains members that only the modloader or the mod itself are intended to access
public abstract class NeosMod : NeosModBase
{
public static bool IsDebugEnabled() => Logger.IsDebugEnabled();
public static void DebugFunc(Func<string> messageProducer) => Logger.DebugFuncExternal(messageProducer);
public static void Debug(string message) => Logger.DebugExternal(message); // needed for binary compatibility (REMOVE IN NEXT MAJOR VERSION)
public static void Debug(object message) => Logger.DebugExternal(message);
public static void Debug(params object[] messages) => Logger.DebugListExternal(messages);
Expand Down
6 changes: 3 additions & 3 deletions NeosModLoader/NeosVersionReset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static bool SpoofCompatibilityHash(Engine engine)
int? vanillaProtocolVersionMaybe = GetVanillaProtocolVersion();
if (vanillaProtocolVersionMaybe is int vanillaProtocolVersion)
{
Logger.DebugInternal($"Vanilla protocol version is {vanillaProtocolVersion}");
Logger.DebugFuncInternal(() => $"Vanilla protocol version is {vanillaProtocolVersion}");
vanillaCompatibilityHash = CalculateCompatibilityHash(vanillaProtocolVersion);
return SetCompatibilityHash(engine, vanillaCompatibilityHash);
}
Expand Down Expand Up @@ -101,7 +101,7 @@ private static bool SetCompatibilityHash(Engine engine, string Target)
}
else
{
Logger.DebugInternal($"Changing compatibility hash from {engine.CompatibilityHash} to {Target}");
Logger.DebugFuncInternal(() => $"Changing compatibility hash from {engine.CompatibilityHash} to {Target}");
field.SetValue(engine, Target);
return true;
}
Expand All @@ -120,7 +120,7 @@ private static bool SpoofVersionString(Engine engine)
Logger.WarnInternal("Unable to write Engine._versionString");
return false;
}
Logger.DebugInternal($"Changing version string from {engine.VersionString} to {target}");
Logger.DebugFuncInternal(() => $"Changing version string from {engine.VersionString} to {target}");
field.SetValue(engine, target);
}
return true;
Expand Down

0 comments on commit 22724e5

Please sign in to comment.