Skip to content

Commit

Permalink
Fixed logging not working correctly on third-party libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielWillett committed Oct 11, 2024
1 parent b1e13cd commit 82193cb
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Autofac;
using DanielWillett.ReflectionTools;
using DanielWillett.ReflectionTools;
using DanielWillett.ReflectionTools.Formatting;
using SDG.Unturned;
using System.Reflection;
using Uncreated.Warfare.Components;
using Uncreated.Warfare.Events.Models.Barricades;
using Uncreated.Warfare.Patches;
using Uncreated.Warfare.Players;
Expand All @@ -24,16 +21,15 @@ void IHarmonyPatch.Patch(ILogger logger)
if (_target != null)
{
Patcher.Patch(_target, prefix: Accessor.GetMethod(Prefix));
logger.LogDebug("Patched {0} for bedroll claim requested.", Accessor.Formatter.Format(_target));
logger.LogDebug("Patched {0} for bedroll claim requested.", _target);
return;
}

logger.LogError("Failed to find method: {0}.",
Accessor.Formatter.Format(new MethodDefinition(nameof(InteractableBed.ReceiveClaimRequest))
new MethodDefinition(nameof(InteractableBed.ReceiveClaimRequest))
.DeclaredIn<InteractableBed>(isStatic: false)
.WithParameter<string>("newText")
.ReturningVoid()
)
);
}

Expand All @@ -43,7 +39,7 @@ void IHarmonyPatch.Unpatch(ILogger logger)
return;

Patcher.Unpatch(_target, Accessor.GetMethod(Prefix));
logger.LogDebug("Unpatched {0} for sign text updated event.", Accessor.Formatter.Format(_target));
logger.LogDebug("Unpatched {0} for sign text updated event.", _target);
_target = null;
}

Expand Down
149 changes: 97 additions & 52 deletions UncreatedWarfare/Logging/WarfareLoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using Uncreated.Warfare;
using Uncreated.Warfare.Logging.Formatting;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -2274,53 +2275,61 @@ public static void Log(this ILogger logger, LogLevel logLevel, string message, p
logger.Log(logLevel, 0, null, message, args);
}

// /// <summary>
// /// Formats and writes a log message at the specified log level.
// /// </summary>
// /// <param name="logger">The <see cref="ILogger"/> to write to.</param>
// /// <param name="logLevel">Entry will be written on this level.</param>
// /// <param name="eventId">The event id associated with the log.</param>
// /// <param name="message">Format string of the log message.</param>
// /// <param name="args">An object array that contains zero or more objects to format.</param>
// [StringFormatMethod(nameof(message))]
// public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string message, params object?[]? args)
// {
// logger.Log(logLevel, eventId, null, message, args);
// }

// /// <summary>
// /// Formats and writes a log message at the specified log level.
// /// </summary>
// /// <param name="logger">The <see cref="ILogger"/> to write to.</param>
// /// <param name="logLevel">Entry will be written on this level.</param>
// /// <param name="exception">The exception to log.</param>
// /// <param name="message">Format string of the log message.</param>
// /// <param name="args">An object array that contains zero or more objects to format.</param>
// [StringFormatMethod(nameof(message))]
// public static void Log(this ILogger logger, LogLevel logLevel, Exception? exception, string message, params object?[]? args)
// {
// logger.Log(logLevel, 0, exception, message, args);
// }

// /// <summary>
// /// Formats and writes a log message at the specified log level.
// /// </summary>
// /// <param name="logger">The <see cref="ILogger"/> to write to.</param>
// /// <param name="logLevel">Entry will be written on this level.</param>
// /// <param name="eventId">The event id associated with the log.</param>
// /// <param name="exception">The exception to log.</param>
// /// <param name="message">Format string of the log message.</param>
// /// <param name="args">An object array that contains zero or more objects to format.</param>
// [StringFormatMethod(nameof(message))]
// public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, string message, params object?[]? args)
// {
// if (logger == null)
// {
// throw new ArgumentNullException(nameof(logger));
// }

// logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, args ?? Array.Empty<object?>()), exception, MessageFormatter);
// }
/// <summary>
/// Formats and writes a log message at the specified log level.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="logLevel">Entry will be written on this level.</param>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
[StringFormatMethod(nameof(message))]
public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string message, params object?[]? args)
{
logger.Log(logLevel, eventId, null, message, args);
}

/// <summary>
/// Formats and writes a log message at the specified log level.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="logLevel">Entry will be written on this level.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
[StringFormatMethod(nameof(message))]
public static void Log(this ILogger logger, LogLevel logLevel, Exception? exception, string message, params object?[]? args)
{
logger.Log(logLevel, 0, exception, message, args);
}

/// <summary>
/// Formats and writes a log message at the specified log level.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
/// <param name="logLevel">Entry will be written on this level.</param>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
[StringFormatMethod(nameof(message))]
public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, string message, params object?[]? args)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

args ??= Array.Empty<object?>();
if (WarfareModule.IsActive)
{
logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, args), exception, MessageFormatter);
}
else
{
LoggerExtensions.Log(logger, logLevel, eventId, exception, message, args);
}
}

/// <summary>
/// Formats and writes a log message at the specified log level.
Expand Down Expand Up @@ -2376,7 +2385,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId,
throw new ArgumentNullException(nameof(logger));
}

logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, Array.Empty<object>()), exception, MessageFormatter);
if (WarfareModule.IsActive)
{
logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, Array.Empty<object>()), exception, MessageFormatter);
}
else
{
LoggerExtensions.Log(logger, logLevel, eventId, exception, message, Array.Empty<object>());
}
}

/// <summary>
Expand Down Expand Up @@ -2433,7 +2449,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId,
throw new ArgumentNullException(nameof(logger));
}

logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1), exception, MessageFormatter);
if (WarfareModule.IsActive)
{
logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1), exception, MessageFormatter);
}
else
{
LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1 ]);
}
}

/// <summary>
Expand Down Expand Up @@ -2490,7 +2513,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId,
throw new ArgumentNullException(nameof(logger));
}

logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2), exception, MessageFormatter);
if (WarfareModule.IsActive)
{
logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2), exception, MessageFormatter);
}
else
{
LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1, arg2 ]);
}
}

/// <summary>
Expand Down Expand Up @@ -2547,7 +2577,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId,
throw new ArgumentNullException(nameof(logger));
}

logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3), exception, MessageFormatter);
if (WarfareModule.IsActive)
{
logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3), exception, MessageFormatter);
}
else
{
LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1, arg2, arg3 ]);
}
}

/// <summary>
Expand Down Expand Up @@ -2604,7 +2641,15 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId,
throw new ArgumentNullException(nameof(logger));
}

logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3, arg4), exception, MessageFormatter);

if (WarfareModule.IsActive)
{
logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3, arg4), exception, MessageFormatter);
}
else
{
LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1, arg2, arg3, arg4 ]);
}
}

private static readonly Func<WarfareFormattedLogValues, Exception, string> MessageFormatter = MessageFormatterMtd;
Expand Down
6 changes: 3 additions & 3 deletions UncreatedWarfare/UncreatedWarfare.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PropertyGroup>

<VersionPrefix>4.0.0</VersionPrefix>
<VersionBuild>002</VersionBuild>
<VersionBuild>003</VersionBuild>
<VersionSuffix>prerelease$(VersionBuild)</VersionSuffix>

</PropertyGroup>
Expand Down Expand Up @@ -137,9 +137,9 @@
<PackageReference Include="DanielWillett.SpeedBytes" Version="[1.1.2,)" />
<PackageReference Include="DanielWillett.SpeedBytes.Unity" Version="[1.1.1,)" />
<PackageReference Include="DanielWillett.JavaPropertiesParser" Version="[1.0.0,)" />
<PackageReference Include="DanielWillett.ModularRPCs" Version="[1.0.0-prerelease04, 2)" />
<PackageReference Include="DanielWillett.ModularRPCs" Version="[1.0.0-prerelease06, 2)" />
<PackageReference Include="DanielWillett.ModularRPCs.Unity" Version="[1.0.0-prerelease03, 2)" />
<PackageReference Include="DanielWillett.ModularRPCs.WebSockets" Version="[1.0.0-prerelease03, 2)" />
<PackageReference Include="DanielWillett.ModularRPCs.WebSockets" Version="[1.0.0-prerelease06, 2)" />
<PackageReference Include="Uncreated.UI" Version="[1.0.0,)" />
<PackageReference Include="Stripe.net" Version="[43.4.0,)" />
<PackageReference Include="JetBrains.Annotations" Version="[2018.2.1,)" Aliases="JetBrains" />
Expand Down
7 changes: 7 additions & 0 deletions UncreatedWarfare/WarfareModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public sealed class WarfareModule : IModuleNexus
/// <remarks>Do not use unless in a patch.</remarks>
public static WarfareModule Singleton { get; private set; }

/// <summary>
/// If Uncreated.Warfare is loaded as a module instead of as a library.
/// </summary>
public static bool IsActive { get; private set; }

#nullable restore

private bool _unloadedHostedServices = true;
Expand Down Expand Up @@ -147,6 +152,8 @@ public CancellationToken UnloadToken

void IModuleNexus.initialize()
{
IsActive = true;

AppDomain.CurrentDomain.AssemblyResolve += HandleAssemblyResolve;

// will setup the main thread in GameThread before asserting
Expand Down

0 comments on commit 82193cb

Please sign in to comment.