< Summary

Information
Class: SwiftCollections.Diagnostics.DiagnosticChannel
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Diagnostics/DiagnosticChannel.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 100
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%22100%
get_Sink()100%11100%
set_Sink(...)100%22100%
IsEnabled(...)100%44100%
Write(...)100%22100%
Write(...)100%22100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Diagnostics/DiagnosticChannel.cs

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