| | | 1 | | global using SwiftCollections.Diagnostics; |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides centralized diagnostics for Trailblazer runtime code. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Warning and error diagnostics are controlled through <see cref="MinimumLevel"/>. |
| | | 12 | | /// Trace-style diagnostics that were previously debug-only are additionally gated by |
| | | 13 | | /// <see cref="EnableDebugLogging"/> so hosts can opt into verbose runtime output explicitly. |
| | | 14 | | /// </remarks> |
| | | 15 | | public static class TrailblazerLogger |
| | | 16 | | { |
| | 1 | 17 | | private static readonly TrailblazerDiagnosticLogger _logger = new(); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets or sets a value indicating whether verbose debug diagnostics should be emitted. |
| | | 21 | | /// </summary> |
| | | 22 | | public static bool EnableDebugLogging |
| | | 23 | | { |
| | 6 | 24 | | get => _logger.EnableDebugLogging; |
| | 11 | 25 | | set => _logger.EnableDebugLogging = value; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the diagnostic channel used for Trailblazer warnings and errors. |
| | | 30 | | /// </summary> |
| | 62 | 31 | | public static DiagnosticChannel Channel => _logger.Channel; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the diagnostic channel used for verbose debug diagnostics. |
| | | 35 | | /// </summary> |
| | 2118 | 36 | | public static DiagnosticChannel DebugChannel => _logger.DebugChannel; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets the minimum severity required for non-debug diagnostics to be emitted. |
| | | 40 | | /// </summary> |
| | | 41 | | public static DiagnosticLevel MinimumLevel |
| | | 42 | | { |
| | 6 | 43 | | get => _logger.MinimumLevel; |
| | 14 | 44 | | set => _logger.MinimumLevel = value; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets or sets the delegate used to write formatted log messages. |
| | | 49 | | /// Assigning <see langword="null"/> restores <see cref="DefaultLogHandler"/>. |
| | | 50 | | /// </summary> |
| | | 51 | | public static Action<DiagnosticLevel, string, string> LogHandler |
| | | 52 | | { |
| | 6 | 53 | | get => _logger.LogHandler; |
| | 10 | 54 | | set => _logger.LogHandler = value; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets or sets the formatter used to transform log arguments into a final log entry. |
| | | 59 | | /// Assigning <see langword="null"/> restores <see cref="DefaultLogFormatter"/>. |
| | | 60 | | /// </summary> |
| | | 61 | | public static Func<DiagnosticLevel, string, string, string> CustomFormatter |
| | | 62 | | { |
| | 6 | 63 | | get => _logger.CustomFormatter; |
| | 6 | 64 | | set => _logger.CustomFormatter = value; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Determines whether non-debug diagnostics at the specified level are currently enabled. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="level">The diagnostic level to evaluate.</param> |
| | | 71 | | /// <returns><see langword="true"/> when messages at <paramref name="level"/> will be emitted; otherwise, <see langw |
| | | 72 | | public static bool IsEnabled(DiagnosticLevel level) |
| | | 73 | | { |
| | 4 | 74 | | return _logger.IsEnabled(level); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// The default handler for Trailblazer log messages. |
| | | 79 | | /// </summary> |
| | | 80 | | public static void DefaultLogHandler(DiagnosticLevel level, string message, string source) |
| | | 81 | | { |
| | 1 | 82 | | _logger.DefaultLogHandler(level, message, source); |
| | 1 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Formats a Trailblazer log entry using a deterministic, source-first layout. |
| | | 87 | | /// </summary> |
| | | 88 | | public static string DefaultLogFormatter(DiagnosticLevel level, string message, string source) |
| | | 89 | | { |
| | 1 | 90 | | return _logger.DefaultLogFormatter(level, message, source); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private sealed class TrailblazerDiagnosticLogger : DiagnosticLogger |
| | | 94 | | { |
| | | 95 | | public TrailblazerDiagnosticLogger() |
| | 1 | 96 | | : base("Trailblazer") |
| | | 97 | | { |
| | 1 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |