Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion LabApi/Features/Console/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace LabApi.Features.Console;
Expand All @@ -22,12 +23,25 @@ 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>
/// List of assemblies to display Debug level logs for.
/// </summary>
public static HashSet<Assembly> DebugEnabled { get; } = new();

/// <summary>
/// Logs a debug message to the server console.
/// </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)
public static void Debug(object message, bool canBePrinted)
Copy link
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
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
10 changes: 9 additions & 1 deletion LabApi/Loader/Features/Configuration/LabApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LabApi.Loader.Features.Paths;
using LabApi.Loader.Features.Plugins.Configuration;
using System.Collections.Generic;
using System.ComponentModel;

Expand All @@ -24,7 +25,14 @@ public class LabApiConfig
/// <summary>
/// Whether to allow loading plugins even if they were built for a different major version of LabAPI.
/// </summary>
/// <seealso cref="LabApi.Loader.Features.Plugins.Configuration.Properties.UnsupportedLoading"/>
/// <seealso cref="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="Properties.Debug"/>
[Description("Whether debug logs should be printed regardless of per-plugin properties. Only recommended during Plugin development.")]
public bool DebugOverride { get; set; } = false;
}
6 changes: 6 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,12 @@ public class Properties
[Description("Whether or not the plugin is enabled.")]
public bool IsEnabled { get; set; } = true;

/// <summary>
/// Whether Debug logs should be printed for this plugin.
/// </summary>
[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
16 changes: 16 additions & 0 deletions LabApi/Loader/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ 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)
{
// We try to load the configuration of the plugin
Expand All @@ -253,6 +255,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