< 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: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
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%

File(s)

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

#LineLine coverage
 1namespace SwiftCollections.Diagnostics;
 2
 3/// <summary>
 4/// Receives diagnostic events emitted by a <see cref="DiagnosticChannel"/>.
 5/// </summary>
 6/// <param name="diagnostic">The emitted diagnostic event.</param>
 7public delegate void DiagnosticSink(in DiagnosticEvent diagnostic);
 8
 9/// <summary>
 10/// Represents a named diagnostic channel with a configurable sink and minimum level.
 11/// </summary>
 12public sealed class DiagnosticChannel
 13{
 314    private static readonly DiagnosticSink NoopSink = static (in DiagnosticEvent _) => { };
 15    private DiagnosticSink _sink;
 16
 17    /// <summary>
 18    /// Initializes a new instance of the <see cref="DiagnosticChannel"/> class.
 19    /// </summary>
 20    /// <param name="name">The channel name. Blank names fall back to <c>Default</c>.</param>
 821    public DiagnosticChannel(string name)
 822    {
 823        Name = string.IsNullOrWhiteSpace(name) ? "Default" : name;
 824        _sink = NoopSink;
 825        MinimumLevel = DiagnosticLevel.None;
 826    }
 27
 28    /// <summary>
 29    /// Gets the channel name.
 30    /// </summary>
 631    public string Name { get; }
 32
 33    /// <summary>
 34    /// Gets or sets the minimum level required for diagnostics to be emitted.
 35    /// </summary>
 3136    public DiagnosticLevel MinimumLevel { get; set; }
 37
 38    /// <summary>
 39    /// Gets or sets the sink that receives emitted diagnostics.
 40    /// Assigning <c>null</c> restores the default no-op sink.
 41    /// </summary>
 42    public DiagnosticSink Sink
 43    {
 244        get => _sink;
 345        set => _sink = value ?? NoopSink;
 46    }
 47
 48    /// <summary>
 49    /// Determines whether the specified level is enabled for this channel.
 50    /// </summary>
 51    /// <param name="level">The level to evaluate.</param>
 52    /// <returns><c>true</c> when the level is enabled; otherwise, <c>false</c>.</returns>
 53    public bool IsEnabled(DiagnosticLevel level)
 1054    {
 1055        return MinimumLevel != DiagnosticLevel.None
 1056            && level >= MinimumLevel
 1057            && level != DiagnosticLevel.None;
 1058    }
 59
 60    /// <summary>
 61    /// Emits a diagnostic event when the specified level is enabled.
 62    /// </summary>
 63    /// <param name="level">The severity level of the diagnostic event.</param>
 64    /// <param name="message">The diagnostic message.</param>
 65    /// <param name="source">An optional source identifier.</param>
 66    public void Write(DiagnosticLevel level, string message, string source = "")
 467    {
 468        if (!IsEnabled(level))
 269            return;
 70
 271        _sink(new DiagnosticEvent(Name, level, message, source));
 472    }
 73}