| | | 1 | | namespace SwiftCollections.Diagnostics; |
| | | 2 | | |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Receives diagnostic events emitted by a <see cref="DiagnosticChannel"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="diagnostic">The emitted diagnostic event.</param> |
| | | 9 | | public delegate void DiagnosticSink(in DiagnosticEvent diagnostic); |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a named diagnostic channel with a configurable sink and minimum level. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class DiagnosticChannel |
| | | 15 | | { |
| | 2 | 16 | | private static readonly DiagnosticSink NoopSink = static (in DiagnosticEvent _) => { }; |
| | | 17 | | private DiagnosticSink _sink; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="DiagnosticChannel"/> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="name">The channel name. Blank names fall back to <c>Default</c>.</param> |
| | 36 | 23 | | public DiagnosticChannel(string name) |
| | | 24 | | { |
| | 36 | 25 | | Name = string.IsNullOrWhiteSpace(name) ? "Default" : name; |
| | 36 | 26 | | _sink = NoopSink; |
| | 36 | 27 | | MinimumLevel = DiagnosticLevel.None; |
| | 36 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the channel name. |
| | | 32 | | /// </summary> |
| | 25 | 33 | | public string Name { get; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the minimum level required for diagnostics to be emitted. |
| | | 37 | | /// </summary> |
| | 204 | 38 | | public DiagnosticLevel MinimumLevel { get; set; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the sink that receives emitted diagnostics. |
| | | 42 | | /// Assigning <c>null</c> restores the default no-op sink. |
| | | 43 | | /// </summary> |
| | | 44 | | public DiagnosticSink Sink |
| | | 45 | | { |
| | 3 | 46 | | get => _sink; |
| | 28 | 47 | | set => _sink = value ?? NoopSink; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Determines whether the specified level is enabled for this channel. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="level">The level to evaluate.</param> |
| | | 54 | | /// <returns><c>true</c> when the level is enabled; otherwise, <c>false</c>.</returns> |
| | | 55 | | public bool IsEnabled(DiagnosticLevel level) |
| | | 56 | | { |
| | 70 | 57 | | return MinimumLevel != DiagnosticLevel.None |
| | 70 | 58 | | && level >= MinimumLevel |
| | 70 | 59 | | && level != DiagnosticLevel.None; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Emits a diagnostic event when the specified level is enabled. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="level">The severity level of the diagnostic event.</param> |
| | | 66 | | /// <param name="message">The diagnostic message.</param> |
| | | 67 | | /// <param name="source">An optional source identifier.</param> |
| | | 68 | | public void Write(DiagnosticLevel level, string message, string source = "") |
| | | 69 | | { |
| | 46 | 70 | | if (!IsEnabled(level)) |
| | 40 | 71 | | return; |
| | | 72 | | |
| | 6 | 73 | | _sink(new DiagnosticEvent(Name, level, message, source)); |
| | 6 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Emits a diagnostic event from an interpolated message when the specified level is enabled. |
| | | 78 | | /// Disabled levels do not evaluate formatted interpolation expressions. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="level">The severity level of the diagnostic event.</param> |
| | | 81 | | /// <param name="message">The interpolated diagnostic message.</param> |
| | | 82 | | /// <param name="source">An optional source identifier.</param> |
| | | 83 | | public void Write( |
| | | 84 | | DiagnosticLevel level, |
| | | 85 | | [InterpolatedStringHandlerArgument("", nameof(level))] DiagnosticInterpolatedStringHandler message, |
| | | 86 | | string source = "") |
| | | 87 | | { |
| | 7 | 88 | | if (!message.IsEnabled) |
| | 1 | 89 | | return; |
| | | 90 | | |
| | 6 | 91 | | _sink(new DiagnosticEvent(Name, level, message.GetFormattedText(), source)); |
| | 6 | 92 | | } |
| | | 93 | | } |