| | | 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 Chronicler.Diagnostics; |
| | | 9 | | |
| | | 10 | | using System; |
| | | 11 | | using System.Diagnostics.CodeAnalysis; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides reusable logger state and diagnostic channels for libraries using Chronicler 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>Creates normal and debug channels for a library logger.</summary> |
| | | 28 | | /// <param name="channelName">The shared channel label. Null or blank uses <c>Default</c>.</param> |
| | | 29 | | /// <remarks>Normal diagnostics start at Warning. Debug diagnostics start disabled.</remarks> |
| | 7 | 30 | | protected DiagnosticLogger(string channelName) |
| | | 31 | | { |
| | 7 | 32 | | _channel = CreateChannel(channelName); |
| | 7 | 33 | | _debugChannel = CreateChannel(channelName); |
| | 7 | 34 | | Name = _channel.Name; |
| | 7 | 35 | | _logHandler = DefaultLogHandler; |
| | 7 | 36 | | _customFormatter = DefaultLogFormatter; |
| | 7 | 37 | | MinimumLevel = DiagnosticLevel.Warning; |
| | 7 | 38 | | RefreshDebugMinimumLevel(); |
| | 7 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary>Gets the logger label.</summary> |
| | | 42 | | /// <value>The normalized name shared by its two channels.</value> |
| | | 43 | | public string Name { get; } |
| | | 44 | | |
| | | 45 | | /// <summary>Gets the normal diagnostics channel.</summary> |
| | | 46 | | /// <value>The same mutable channel throughout the logger's lifetime. Its initial threshold is Warning.</value> |
| | 3 | 47 | | public DiagnosticChannel Channel => _channel; |
| | | 48 | | |
| | | 49 | | /// <summary>Gets the independently gated debug channel.</summary> |
| | | 50 | | /// <value>The same mutable channel throughout the logger's lifetime. Its initial threshold is None.</value> |
| | 5 | 51 | | public DiagnosticChannel DebugChannel => _debugChannel; |
| | | 52 | | |
| | | 53 | | /// <summary>Gets or sets the debug gate.</summary> |
| | | 54 | | /// <value>False by default. Setting this property selects the normal threshold or disables the debug channel.</valu |
| | | 55 | | public bool EnableDebugLogging |
| | | 56 | | { |
| | 1 | 57 | | get => _enableDebugLogging; |
| | | 58 | | set |
| | | 59 | | { |
| | 1 | 60 | | _enableDebugLogging = value; |
| | 1 | 61 | | RefreshDebugMinimumLevel(); |
| | 1 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>Gets or sets the normal channel's threshold.</summary> |
| | | 66 | | /// <value>Setting the property also updates the debug threshold when debug logging is enabled.</value> |
| | | 67 | | public DiagnosticLevel MinimumLevel |
| | | 68 | | { |
| | 1 | 69 | | get => _channel.MinimumLevel; |
| | | 70 | | set |
| | | 71 | | { |
| | 11 | 72 | | _channel.MinimumLevel = value; |
| | 11 | 73 | | RefreshDebugMinimumLevel(); |
| | 11 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary>Gets or sets the log dispatch callback.</summary> |
| | | 78 | | /// <value>The retained callback receiving severity, message, and resolved source. Null restores <see cref="DefaultL |
| | | 79 | | [AllowNull] |
| | | 80 | | public Action<DiagnosticLevel, string, string> LogHandler |
| | | 81 | | { |
| | 1 | 82 | | get => _logHandler; |
| | 4 | 83 | | set => _logHandler = value ?? DefaultLogHandler; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary>Gets or sets the formatter used by the default log handler.</summary> |
| | | 87 | | /// <value>The retained formatter mapping severity, message, and source to one entry. Null restores <see cref="Defau |
| | | 88 | | [AllowNull] |
| | | 89 | | public Func<DiagnosticLevel, string, string, string> CustomFormatter |
| | | 90 | | { |
| | 4 | 91 | | get => _customFormatter; |
| | 2 | 92 | | set => _customFormatter = value ?? DefaultLogFormatter; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Determines whether normal diagnostics at the specified level are currently enabled. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="level">The diagnostic level to evaluate.</param> |
| | | 99 | | /// <returns><see langword="true"/> when messages at <paramref name="level"/> will be emitted; otherwise, <see langw |
| | | 100 | | public bool IsEnabled(DiagnosticLevel level) |
| | | 101 | | { |
| | 2 | 102 | | return _channel.IsEnabled(level); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary>Formats a message and writes one console line.</summary> |
| | | 106 | | /// <param name="level">Error goes to standard error. Other levels go to standard output.</param> |
| | | 107 | | /// <param name="message">The text passed to the current formatter.</param> |
| | | 108 | | /// <param name="source">The origin label passed to the current formatter.</param> |
| | | 109 | | /// <remarks>Calling directly bypasses both channel thresholds.</remarks> |
| | | 110 | | public virtual void DefaultLogHandler(DiagnosticLevel level, string message, string source) |
| | | 111 | | { |
| | 2 | 112 | | string entry = CustomFormatter(level, message, source); |
| | 2 | 113 | | if (level == DiagnosticLevel.Error) |
| | 1 | 114 | | Console.Error.WriteLine(entry); |
| | | 115 | | else |
| | 1 | 116 | | Console.WriteLine(entry); |
| | 1 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary>Formats a logger entry with severity and origin.</summary> |
| | | 120 | | /// <param name="level">The severity written in brackets.</param> |
| | | 121 | | /// <param name="message">The text appended after the logger and source prefix.</param> |
| | | 122 | | /// <param name="source">The suffix on the logger name. Null or whitespace omits the suffix.</param> |
| | | 123 | | /// <returns>Text of the form <c>[level] Name.Source: message</c>. The formatter adds no line break.</returns> |
| | | 124 | | public virtual string DefaultLogFormatter(DiagnosticLevel level, string message, string source) |
| | | 125 | | { |
| | 5 | 126 | | if (string.IsNullOrWhiteSpace(source)) |
| | 2 | 127 | | return $"[{level}] {Name}: {message}"; |
| | | 128 | | |
| | 3 | 129 | | return $"[{level}] {Name}.{source}: {message}"; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary>Dispatches an event from either channel through the current log handler.</summary> |
| | | 133 | | /// <param name="diagnostic">The already enabled event whose severity, message, and resolved source are forwarded.</ |
| | | 134 | | protected virtual void HandleDiagnosticEvent(in DiagnosticEvent diagnostic) |
| | | 135 | | { |
| | 2 | 136 | | _logHandler(diagnostic.Level, diagnostic.Message, ResolveSource(diagnostic)); |
| | 2 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary>Selects the origin label forwarded to the log handler.</summary> |
| | | 140 | | /// <param name="diagnostic">The event whose source and channel names are inspected.</param> |
| | | 141 | | /// <returns>The event's source, or its channel name when the source is blank.</returns> |
| | | 142 | | protected virtual string ResolveSource(in DiagnosticEvent diagnostic) |
| | | 143 | | { |
| | 2 | 144 | | return string.IsNullOrWhiteSpace(diagnostic.Source) |
| | 2 | 145 | | ? diagnostic.Channel |
| | 2 | 146 | | : diagnostic.Source; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | private DiagnosticChannel CreateChannel(string channelName) |
| | | 150 | | { |
| | 14 | 151 | | return new DiagnosticChannel(channelName) |
| | 14 | 152 | | { |
| | 14 | 153 | | MinimumLevel = DiagnosticLevel.Warning, |
| | 14 | 154 | | Sink = HandleDiagnosticEvent |
| | 14 | 155 | | }; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private void RefreshDebugMinimumLevel() |
| | | 159 | | { |
| | 19 | 160 | | _debugChannel.MinimumLevel = _enableDebugLogging |
| | 19 | 161 | | ? _channel.MinimumLevel |
| | 19 | 162 | | : DiagnosticLevel.None; |
| | 19 | 163 | | } |
| | | 164 | | } |