< Summary

Information
Class: SwiftCollections.Diagnostics.DiagnosticChannel
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Utility/Diagnostics/DiagnosticChannel.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 93
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_Name()100%11100%
get_MinimumLevel()100%11100%
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/Utility/Diagnostics/DiagnosticChannel.cs

#LineLine coverage
 1namespace SwiftCollections.Diagnostics;
 2
 3using 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>
 9public delegate void DiagnosticSink(in DiagnosticEvent diagnostic);
 10
 11/// <summary>
 12/// Represents a named diagnostic channel with a configurable sink and minimum level.
 13/// </summary>
 14public sealed class DiagnosticChannel
 15{
 216    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>
 3623    public DiagnosticChannel(string name)
 24    {
 3625        Name = string.IsNullOrWhiteSpace(name) ? "Default" : name;
 3626        _sink = NoopSink;
 3627        MinimumLevel = DiagnosticLevel.None;
 3628    }
 29
 30    /// <summary>
 31    /// Gets the channel name.
 32    /// </summary>
 2533    public string Name { get; }
 34
 35    /// <summary>
 36    /// Gets or sets the minimum level required for diagnostics to be emitted.
 37    /// </summary>
 20438    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    {
 346        get => _sink;
 2847        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    {
 7057        return MinimumLevel != DiagnosticLevel.None
 7058            && level >= MinimumLevel
 7059            && 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    {
 4670        if (!IsEnabled(level))
 4071            return;
 72
 673        _sink(new DiagnosticEvent(Name, level, message, source));
 674    }
 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    {
 788        if (!message.IsEnabled)
 189            return;
 90
 691        _sink(new DiagnosticEvent(Name, level, message.GetFormattedText(), source));
 692    }
 93}