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