| | | 1 | | //======================================================================= |
| | | 2 | | // DiagnosticLogger.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | namespace SwiftCollections.Diagnostics; |
| | | 9 | | |
| | | 10 | | using System; |
| | | 11 | | using System.Diagnostics.CodeAnalysis; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides reusable logger state and diagnostic channels for libraries built on SwiftCollections diagnostics. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// Static logger facades can delegate to a derived instance of this type while keeping their existing public API. |
| | | 18 | | /// </remarks> |
| | | 19 | | public abstract class DiagnosticLogger |
| | | 20 | | { |
| | | 21 | | private readonly DiagnosticChannel _channel; |
| | | 22 | | private readonly DiagnosticChannel _debugChannel; |
| | | 23 | | private Action<DiagnosticLevel, string, string> _logHandler; |
| | | 24 | | private Func<DiagnosticLevel, string, string, string> _customFormatter; |
| | | 25 | | private bool _enableDebugLogging; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="DiagnosticLogger"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="channelName">The diagnostic channel name.</param> |
| | 7 | 31 | | protected DiagnosticLogger(string channelName) |
| | | 32 | | { |
| | 7 | 33 | | _channel = CreateChannel(channelName); |
| | 7 | 34 | | _debugChannel = CreateChannel(channelName); |
| | 7 | 35 | | Name = _channel.Name; |
| | 7 | 36 | | _logHandler = DefaultLogHandler; |
| | 7 | 37 | | _customFormatter = DefaultLogFormatter; |
| | 7 | 38 | | MinimumLevel = DiagnosticLevel.Warning; |
| | 7 | 39 | | RefreshDebugMinimumLevel(); |
| | 7 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the logger name. |
| | | 44 | | /// </summary> |
| | | 45 | | public string Name { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the diagnostic channel used for normal diagnostics. |
| | | 49 | | /// </summary> |
| | 3 | 50 | | public DiagnosticChannel Channel => _channel; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the diagnostic channel used for verbose debug diagnostics. |
| | | 54 | | /// </summary> |
| | 5 | 55 | | public DiagnosticChannel DebugChannel => _debugChannel; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets or sets a value indicating whether verbose debug diagnostics should be emitted. |
| | | 59 | | /// </summary> |
| | | 60 | | public bool EnableDebugLogging |
| | | 61 | | { |
| | 1 | 62 | | get => _enableDebugLogging; |
| | | 63 | | set |
| | | 64 | | { |
| | 1 | 65 | | _enableDebugLogging = value; |
| | 1 | 66 | | RefreshDebugMinimumLevel(); |
| | 1 | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets or sets the minimum severity required for normal diagnostics to be emitted. |
| | | 72 | | /// </summary> |
| | | 73 | | public DiagnosticLevel MinimumLevel |
| | | 74 | | { |
| | 1 | 75 | | get => _channel.MinimumLevel; |
| | | 76 | | set |
| | | 77 | | { |
| | 11 | 78 | | _channel.MinimumLevel = value; |
| | 11 | 79 | | RefreshDebugMinimumLevel(); |
| | 11 | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets or sets the delegate used to write formatted log messages. |
| | | 85 | | /// Assigning <see langword="null"/> restores <see cref="DefaultLogHandler"/>. |
| | | 86 | | /// </summary> |
| | | 87 | | [AllowNull] |
| | | 88 | | public Action<DiagnosticLevel, string, string> LogHandler |
| | | 89 | | { |
| | 1 | 90 | | get => _logHandler; |
| | 4 | 91 | | set => _logHandler = value ?? DefaultLogHandler; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets or sets the formatter used to transform log arguments into a final log entry. |
| | | 96 | | /// Assigning <see langword="null"/> restores <see cref="DefaultLogFormatter"/>. |
| | | 97 | | /// </summary> |
| | | 98 | | [AllowNull] |
| | | 99 | | public Func<DiagnosticLevel, string, string, string> CustomFormatter |
| | | 100 | | { |
| | 4 | 101 | | get => _customFormatter; |
| | 2 | 102 | | set => _customFormatter = value ?? DefaultLogFormatter; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Determines whether normal diagnostics at the specified level are currently enabled. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="level">The diagnostic level to evaluate.</param> |
| | | 109 | | /// <returns><see langword="true"/> when messages at <paramref name="level"/> will be emitted; otherwise, <see langw |
| | | 110 | | public bool IsEnabled(DiagnosticLevel level) |
| | | 111 | | { |
| | 2 | 112 | | return _channel.IsEnabled(level); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Writes a log message using the current <see cref="CustomFormatter"/>. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="level">The severity level of the log message.</param> |
| | | 119 | | /// <param name="message">The log message.</param> |
| | | 120 | | /// <param name="source">The source of the log message.</param> |
| | | 121 | | public virtual void DefaultLogHandler(DiagnosticLevel level, string message, string source) |
| | | 122 | | { |
| | 2 | 123 | | string entry = CustomFormatter(level, message, source); |
| | 2 | 124 | | if (level == DiagnosticLevel.Error) |
| | 1 | 125 | | Console.Error.WriteLine(entry); |
| | | 126 | | else |
| | 1 | 127 | | Console.WriteLine(entry); |
| | 1 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Formats a log entry using a deterministic, source-first layout. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="level">The severity level of the log message.</param> |
| | | 134 | | /// <param name="message">The log message.</param> |
| | | 135 | | /// <param name="source">The source of the log message.</param> |
| | | 136 | | /// <returns>A formatted log entry.</returns> |
| | | 137 | | public virtual string DefaultLogFormatter(DiagnosticLevel level, string message, string source) |
| | | 138 | | { |
| | 5 | 139 | | if (string.IsNullOrWhiteSpace(source)) |
| | 2 | 140 | | return $"[{level}] {Name}: {message}"; |
| | | 141 | | |
| | 3 | 142 | | return $"[{level}] {Name}.{source}: {message}"; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Receives diagnostics emitted by either channel. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="diagnostic">The emitted diagnostic event.</param> |
| | | 149 | | protected virtual void HandleDiagnosticEvent(in DiagnosticEvent diagnostic) |
| | | 150 | | { |
| | 2 | 151 | | _logHandler(diagnostic.Level, diagnostic.Message, ResolveSource(diagnostic)); |
| | 2 | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Resolves the source value passed to <see cref="LogHandler"/>. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="diagnostic">The emitted diagnostic event.</param> |
| | | 158 | | /// <returns>The resolved source.</returns> |
| | | 159 | | protected virtual string ResolveSource(in DiagnosticEvent diagnostic) |
| | | 160 | | { |
| | 2 | 161 | | return string.IsNullOrWhiteSpace(diagnostic.Source) |
| | 2 | 162 | | ? diagnostic.Channel |
| | 2 | 163 | | : diagnostic.Source; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private DiagnosticChannel CreateChannel(string channelName) |
| | | 167 | | { |
| | 14 | 168 | | return new DiagnosticChannel(channelName) |
| | 14 | 169 | | { |
| | 14 | 170 | | MinimumLevel = DiagnosticLevel.Warning, |
| | 14 | 171 | | Sink = HandleDiagnosticEvent |
| | 14 | 172 | | }; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private void RefreshDebugMinimumLevel() |
| | | 176 | | { |
| | 19 | 177 | | _debugChannel.MinimumLevel = _enableDebugLogging |
| | 19 | 178 | | ? _channel.MinimumLevel |
| | 19 | 179 | | : DiagnosticLevel.None; |
| | 19 | 180 | | } |
| | | 181 | | } |