| | | 1 | | //======================================================================= |
| | | 2 | | // DiagnosticChannel.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.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | /// <summary>Routes diagnostics at enabled levels to a sink.</summary> |
| | | 13 | | /// <remarks>The sink runs synchronously on the caller's thread. The channel starts disabled with a no-op sink.</remarks |
| | | 14 | | public sealed class DiagnosticChannel |
| | | 15 | | { |
| | 1 | 16 | | private static readonly DiagnosticSink NoopSink = static (in DiagnosticEvent _) => { }; |
| | | 17 | | private DiagnosticSink _sink; |
| | | 18 | | |
| | | 19 | | /// <summary>Creates a disabled channel.</summary> |
| | | 20 | | /// <param name="name">The event channel name. Null, empty, or whitespace selects <c>Default</c>.</param> |
| | 46 | 21 | | public DiagnosticChannel(string name) |
| | | 22 | | { |
| | 46 | 23 | | Name = string.IsNullOrWhiteSpace(name) ? "Default" : name; |
| | 46 | 24 | | _sink = NoopSink; |
| | 46 | 25 | | MinimumLevel = DiagnosticLevel.None; |
| | 46 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary>Gets the channel label.</summary> |
| | | 29 | | /// <value>The normalized name included in every event emitted by this channel.</value> |
| | | 30 | | public string Name { get; } |
| | | 31 | | |
| | | 32 | | /// <summary>Gets or sets the severity threshold.</summary> |
| | | 33 | | /// <value>The lowest enabled level. The default, <see cref="DiagnosticLevel.None"/>, disables the channel.</value> |
| | | 34 | | public DiagnosticLevel MinimumLevel { get; set; } |
| | | 35 | | |
| | | 36 | | /// <summary>Gets or sets the dispatch callback.</summary> |
| | | 37 | | /// <value>The retained delegate invoked for emitted events. Assign null to restore the no-op sink.</value> |
| | | 38 | | public DiagnosticSink Sink |
| | | 39 | | { |
| | 2 | 40 | | get => _sink; |
| | 29 | 41 | | set => _sink = value ?? NoopSink; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Checks the channel's current severity threshold.</summary> |
| | | 45 | | /// <param name="level">The severity being considered. <see cref="DiagnosticLevel.None"/> is always disabled.</param |
| | | 46 | | /// <returns>True when the channel is enabled and the severity reaches its threshold.</returns> |
| | | 47 | | public bool IsEnabled(DiagnosticLevel level) |
| | | 48 | | { |
| | 266 | 49 | | return MinimumLevel != DiagnosticLevel.None |
| | 266 | 50 | | && level >= MinimumLevel |
| | 266 | 51 | | && level != DiagnosticLevel.None; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary>Emits an already formatted event when the level is enabled.</summary> |
| | | 55 | | /// <param name="level">The severity tested against the current threshold.</param> |
| | | 56 | | /// <param name="message">The message text, formatted by the caller before this call.</param> |
| | | 57 | | /// <param name="source">An optional origin label copied into the event, or an empty string when omitted.</param> |
| | | 58 | | /// <remarks>Invokes the sink inline. Exceptions from the sink propagate to the caller.</remarks> |
| | | 59 | | public void Write(DiagnosticLevel level, string message, string source = "") |
| | | 60 | | { |
| | 17 | 61 | | if (!IsEnabled(level)) |
| | 4 | 62 | | return; |
| | | 63 | | |
| | 13 | 64 | | _sink(new DiagnosticEvent(Name, level, message, source)); |
| | 13 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary>Emits an interpolated event using the handler's enabled decision.</summary> |
| | | 68 | | /// <param name="level">The event severity used when creating the handler.</param> |
| | | 69 | | /// <param name="message">The handler whose captured gate controls delivery and skips interpolation when disabled.</ |
| | | 70 | | /// <param name="source">An optional origin label copied into the event, or an empty string when omitted.</param> |
| | | 71 | | /// <remarks>Invokes the sink inline. Exceptions from the sink propagate to the caller.</remarks> |
| | | 72 | | public void Write( |
| | | 73 | | DiagnosticLevel level, |
| | | 74 | | [InterpolatedStringHandlerArgument("", nameof(level))] DiagnosticInterpolatedStringHandler message, |
| | | 75 | | string source = "") |
| | | 76 | | { |
| | 8 | 77 | | if (!message.IsEnabled) |
| | 1 | 78 | | return; |
| | | 79 | | |
| | 7 | 80 | | _sink(new DiagnosticEvent(Name, level, message.GetFormattedText(), source)); |
| | 7 | 81 | | } |
| | | 82 | | } |