Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion LabApi/Features/Console/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,31 @@ public static class Logger
/// <param name="color">The color of the message.</param>
public static void Raw(string message, ConsoleColor color) => ServerConsole.AddLog(message, color);

/// <summary>
/// An O(1) set of Assemblies that should display Debug messages.
Comment thread
tayjay marked this conversation as resolved.
Outdated
/// Populated automatically by <see cref="LabApi.Loader.Features.Plugins.Configuration.Properties.Debug"/>
Comment thread
tayjay marked this conversation as resolved.
Outdated
/// </summary>
public static System.Collections.Generic.HashSet<Assembly> DebugEnabled { get; } = new();

/// <summary>
/// Logs a debug message to the server console.
/// Checks <see cref="DebugEnabled"/> before sending the message.
/// </summary>
/// <param name="message">The message to log.</param>
public static void Debug(object message) => Debug(
message,
Loader.PluginLoader.Config?.DebugOverride == true || DebugEnabled.Contains(Assembly.GetCallingAssembly()));

/// <summary>
/// Logs a debug message to the server console.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="canBePrinted">Whether the message can be printed.</param>
public static void Debug(object message, bool canBePrinted = true)
/// <remarks>
/// Uses explicit <paramref name="canBePrinted"/>.
/// Can be replaced with the single parameter overload to use <see cref="LabApi.Loader.Features.Plugins.Configuration.Properties.Debug"/> Property instead.
Comment thread
tayjay marked this conversation as resolved.
Outdated
/// </remarks>
public static void Debug(object message, bool canBePrinted)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forcing a parameter on devs is in fact, breaking change.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overloaded Debug method on line 36 will be used if only 1 parameter is provided. So it is not forced, just explicitly defined. Plugins that are already built will have the default "true" parameter included automatically and the IL will point them to Logger::Debug(object,bool) which is present.

{
if (!canBePrinted)
{
Expand Down
7 changes: 7 additions & 0 deletions LabApi/Loader/Features/Configuration/LabApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ public class LabApiConfig
/// <seealso cref="LabApi.Loader.Features.Plugins.Configuration.Properties.UnsupportedLoading"/>
[Description("Whether to allow loading plugins even if they were built for a different major version of LabAPI.")]
public bool LoadUnsupportedPlugins { get; set; }

/// <summary>
/// Whether debug logs should appear regardless of pre-plugin properties.
/// </summary>
/// <seealso cref="LabApi.Loader.Features.Plugins.Configuration.Properties.Debug"/>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line 27, was following the same way LoadUnsupportedPlugins was commented.

[Description("Whether debug logs should be printed regardless of per-plugin properties. Only recommended during Plugin development.")]
public bool DebugOverride { get; set; } = false;
}
9 changes: 9 additions & 0 deletions LabApi/Loader/Features/Plugins/Configuration/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public class Properties
[Description("Whether or not the plugin is enabled.")]
public bool IsEnabled { get; set; } = true;

/// <summary>
/// Whether <see cref="LabApi.Features.Console.Logger.Debug"/> should print logs.
/// </summary>
/// <remarks>
/// Using the same naming as EXILED to ease transition for devs moving to LabAPI.
/// </remarks>
Comment thread
tayjay marked this conversation as resolved.
Outdated
[Description("Whether or not [DEBUG] log messages should appear in the console. Default is false.")]
public bool Debug { get; set; } = false;

/// <summary>
/// Whether to allow loading the plugin even if it was built for a different major version of LabAPI.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions LabApi/Loader/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.IO;
using System.Linq;
using System.Reflection;
using NorthwoodLib.Pools;
using Utils.NonAllocLINQ;

namespace LabApi.Loader;

Expand Down Expand Up @@ -233,6 +235,7 @@ public static void LoadPlugins(IEnumerable<FileInfo> files)
/// <param name="plugins">The sorted collection of <see cref="Plugin"/>s.</param>
public static void EnablePlugins(IEnumerable<Plugin> plugins)
{
Logger.DebugEnabled.Clear();
foreach (Plugin plugin in plugins)
Comment thread
tayjay marked this conversation as resolved.
{
// We try to load the configuration of the plugin
Expand All @@ -253,6 +256,20 @@ public static void EnablePlugins(IEnumerable<Plugin> plugins)
continue;
}

// Check if the plugin associated with this assembly wants Debug logs enabled.
if (plugin.Properties?.Debug == true)
{
// This body will not be run on most live servers.
if (Plugins.TryGetValue(plugin, out Assembly asm))
{
if (Logger.DebugEnabled.Add(asm))
{
// Only want to print when the first plugin in an assembly enables Debug
Logger.Info($"Debug logging enabled for {asm.FullName}");
}
}
}

// We finally enable the plugin
EnablePlugin(plugin);
}
Expand Down