using System;
using System.Collections.Generic;
using GFramework.Core.Abstractions.Logging;
namespace GFramework.Godot.Logging;
///
/// Godot logger formatting and routing options.
///
public sealed class GodotLoggerOptions
{
private static readonly IReadOnlyDictionary DefaultColors = new Dictionary
{
[LogLevel.Trace] = "gray",
[LogLevel.Debug] = "cyan",
[LogLevel.Info] = "white",
[LogLevel.Warning] = "orange",
[LogLevel.Error] = "red",
[LogLevel.Fatal] = "deep_pink"
};
///
/// Gets or sets the output mode.
///
public GodotLoggerMode Mode { get; set; } = GodotLoggerMode.Debug;
///
/// Gets or sets the minimum level used by .
///
public LogLevel DebugMinLevel { get; set; } = LogLevel.Debug;
///
/// Gets or sets the minimum level used by .
///
public LogLevel ReleaseMinLevel { get; set; } = LogLevel.Info;
///
/// Gets or sets the BBCode-capable template used by .
///
#pragma warning disable MA0016 // Keep configuration mutable for object initializer and serializer scenarios.
public string DebugOutputTemplate { get; set; } =
"[{timestamp:yyyy-MM-dd HH:mm:ss.fff}] [color={color}][{level:u3}][/color] [{category:l16}] {message}{properties}";
#pragma warning restore MA0016
///
/// Gets or sets the plain text template used by .
///
#pragma warning disable MA0016 // Keep configuration mutable for object initializer and serializer scenarios.
public string ReleaseOutputTemplate { get; set; } =
"[{timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{level:u3}] [{category:l16}] {message}{properties}";
#pragma warning restore MA0016
///
/// Gets or sets Godot named colors by log level.
///
#pragma warning disable MA0016 // Keep configuration mutable for object initializer and serializer scenarios.
public Dictionary Colors { get; set; } = new(DefaultColors);
#pragma warning restore MA0016
///
/// Creates options that preserve the previous Godot logger defaults for a fixed minimum level.
///
/// The minimum enabled level.
/// Options equivalent to the previous fixed-format logger behavior.
public static GodotLoggerOptions ForMinimumLevel(LogLevel minLevel)
{
return new GodotLoggerOptions
{
Mode = GodotLoggerMode.Debug,
DebugMinLevel = minLevel,
ReleaseMinLevel = minLevel,
DebugOutputTemplate = "[{timestamp:yyyy-MM-dd HH:mm:ss.fff}] {level:padded} [{category}] {message}{properties}",
ReleaseOutputTemplate = "[{timestamp:yyyy-MM-dd HH:mm:ss.fff}] {level:padded} [{category}] {message}{properties}",
Colors = new Dictionary(DefaultColors)
};
}
///
/// Returns the configured color for the specified level.
///
/// The level.
/// The Godot named color.
public string GetColor(LogLevel level)
{
if (Colors.TryGetValue(level, out var color) && !string.IsNullOrWhiteSpace(color))
{
return color;
}
return DefaultColors[level];
}
internal LogLevel GetEffectiveMinLevel()
{
return Mode == GodotLoggerMode.Debug ? DebugMinLevel : ReleaseMinLevel;
}
internal GodotLoggerOptions WithMinimumLevelFloor(LogLevel minLevel)
{
return new GodotLoggerOptions
{
Mode = Mode,
DebugMinLevel = Max(DebugMinLevel, minLevel),
ReleaseMinLevel = Max(ReleaseMinLevel, minLevel),
DebugOutputTemplate = DebugOutputTemplate,
ReleaseOutputTemplate = ReleaseOutputTemplate,
Colors = new Dictionary(Colors)
};
}
private static LogLevel Max(LogLevel left, LogLevel right)
{
return left > right ? left : right;
}
}