using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Brutal.Logging.Internal;
using Microsoft.Extensions.Logging;
namespace Brutal.Logging;
///
/// Represents an abstract logging category that provides methods for structured logging.
///
///
/// A logging category is designed to group related logging messages and control the logging level.
/// This class provides various logging methods to handle message logging at different levels.
/// Implementations of this abstract class define specific behavior for each category.
///
public sealed class LogCategory
{
#region Categories
internal static readonly Dictionary Categories = [];
///
/// Gets a logging category by name.
///
/// Thrown if the category does not exist.
public static LogCategory Get(string categoryName)
{
return Categories[categoryName];
}
///
/// Tries to get a logging category by name.
///
public static bool TryGet(string categoryName, [MaybeNullWhen(false)] out LogCategory category)
{
return Categories.TryGetValue(categoryName, out category);
}
///
/// Tries to get a logging category by name, or creates it if it does not exist.
///
public static LogCategory Make(string categoryName)
{
if (Categories.TryGetValue(categoryName, out var category))
return category;
category = new LogCategory(categoryName) { Logger = LogSystem.Factory?.CreateLogger(categoryName) };
Categories[categoryName] = category;
return category;
}
#endregion
///
/// Represents the name of the logging category.
/// This property is used to uniquely identify a specific logging category, allowing
/// for categorization and filtering of logged messages.
///
public string Name { get; }
///
/// Represents whether this category is enabled and will display logs
///
public bool IsCategoryEnabled = true;
///
/// Represents whether the caller info (member name, file path, line number) should be displayed in the logs
///
public bool AppendCallerInfo = false;
public ILogger? Logger { get; internal set; }
///
/// Represents a logging category that provides methods for structured and leveled logging.
///
///
/// A logging category is a logical grouping for log entries, enabling control over log level filtering and organization.
/// The class serves as a base for defining specific logging behavior.
///
private LogCategory(string name)
{
Name = name;
}
///
/// Logging will be skipped for this category if these conditions are not met
///
private bool LoggingEnabled => LogSystem.IsEnabled && IsCategoryEnabled;
///
/// Is logging enabled for this category at this level?
///
public bool IsEnabled(LogLevel level) =>
LogSystem.IsEnabled && IsCategoryEnabled && Logger?.IsEnabled(level) == true;
///
/// Logs a message at the given level, with optional details about the source member, file, and line number.
///
/// The level to log on.
/// The message to log.
/// The name of the method that invokes this log call. Automatically populated by the CallerMemberName attribute if not specified.
/// The file path of the caller. Automatically populated by the CallerFilePath attribute if not specified.
/// The line number in the source file where the log call originates. Automatically populated by the CallerLineNumber attribute if not specified.
/// For internal use, to forcibly append caller information even if is false.
/// Defaults to null i.e. follow .
private void Log(
LogLevel logLevel,
[InterpolatedStringHandlerArgument("")] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0,
bool? appendCallerInfo = null
)
where TLevel : unmanaged, ILogLevel
{
// Thread-safety paranoia: drain the handler first to avoid leaking the rented StringBuilder
// in case a guard below mismatches due to a race condition
// (level/logger state flipped on another thread between ctor and here).
var messageString = message.ToStringAndClear();
if (!LoggingEnabled)
return;
if (Logger is null)
throw new LogNotInitializedException();
if (!Logger.IsEnabled(logLevel))
return;
// A span pass-through materializes only now: unlike the rented SB drained above it holds
// no pooled resource, so deferring past the guards keeps suppressed logs allocation-free.
messageString ??= message.Span.ToString();
if (appendCallerInfo ?? AppendCallerInfo)
{
Logger.Log(
logLevel,
"{Message} {SourceMemberName} (in {SourceFilePath}:{SourceLineNumber})",
messageString,
sourceMemberName,
sourceFilePath,
sourceLineNumber
);
}
else
Logger.Log(logLevel, messageString);
}
///
public void Log(
LogLevel logLevel,
[InterpolatedStringHandlerArgument("", nameof(logLevel))] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(logLevel, message, sourceMemberName, sourceFilePath, sourceLineNumber);
}
///
public void Trace(
[InterpolatedStringHandlerArgument("")] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(LogLevel.Trace, message, sourceMemberName, sourceFilePath, sourceLineNumber);
}
///
public void Debug(
[InterpolatedStringHandlerArgument("")] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(LogLevel.Debug, message, sourceMemberName, sourceFilePath, sourceLineNumber);
}
///
public void Info(
[InterpolatedStringHandlerArgument("")] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(LogLevel.Information, message, sourceMemberName, sourceFilePath, sourceLineNumber);
}
///
public void Warning(
[InterpolatedStringHandlerArgument("")] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(LogLevel.Warning, message, sourceMemberName, sourceFilePath, sourceLineNumber);
}
///
public void Error(
[InterpolatedStringHandlerArgument("")] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(LogLevel.Error, message, sourceMemberName, sourceFilePath, sourceLineNumber);
}
///
public void Critical(
[InterpolatedStringHandlerArgument("")] LogString message = default,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(LogLevel.Critical, message, sourceMemberName, sourceFilePath, sourceLineNumber);
}
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
///
/// The exception object containing details for the logged error message.
public void Error(
Exception exception,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(
LogLevel.Error,
$"{exception}",
sourceMemberName,
sourceFilePath,
sourceLineNumber,
appendCallerInfo: true
);
}
///
/// The exception object containing details for the logged critical message.
public void Critical(
Exception exception,
[CallerMemberName] string sourceMemberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
Log(
LogLevel.Critical,
$"{exception}",
sourceMemberName,
sourceFilePath,
sourceLineNumber,
appendCallerInfo: true
);
}
#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
}