< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Info(...)100%22100%
Warn(...)100%22100%
Error(...)100%22100%
Log(...)100%22100%
WriteCore(...)100%11100%
ResolveSource(...)100%66100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// DiagnosticChannelLoggingExtensions.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.IO;
 11using System.Runtime.CompilerServices;
 12
 13/// <summary>
 14/// Provides interpolated diagnostic logging helpers for <see cref="DiagnosticChannel"/>.
 15/// </summary>
 16public static class DiagnosticChannelLoggingExtensions
 17{
 18    /// <summary>
 19    /// Writes an info diagnostic message without evaluating formatted expressions when info diagnostics are disabled.
 20    /// </summary>
 21    /// <param name="channel">The diagnostic channel that receives the message.</param>
 22    /// <param name="message">The interpolated diagnostic message.</param>
 23    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 24    /// <param name="method">The calling method name, automatically captured.</param>
 25    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 26    public static void Info(
 27        this DiagnosticChannel channel,
 28        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<InfoDiagnosticLevel> message,
 29        string source = "",
 30        [CallerMemberName] string method = "",
 31        [CallerFilePath] string filePath = "")
 32    {
 4333        if (!message.IsEnabled)
 3734            return;
 35
 636        WriteCore(channel, DiagnosticLevel.Info, message.Message, ResolveSource(source, method, filePath));
 537    }
 38
 39    /// <summary>
 40    /// Writes a warning diagnostic message without evaluating formatted expressions when warning diagnostics are disabl
 41    /// </summary>
 42    /// <param name="channel">The diagnostic channel that receives the message.</param>
 43    /// <param name="message">The interpolated diagnostic message.</param>
 44    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 45    /// <param name="method">The calling method name, automatically captured.</param>
 46    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 47    public static void Warn(
 48        this DiagnosticChannel channel,
 49        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<WarningDiagnosticLevel> message,
 50        string source = "",
 51        [CallerMemberName] string method = "",
 52        [CallerFilePath] string filePath = "")
 53    {
 354        if (!message.IsEnabled)
 155            return;
 56
 257        WriteCore(channel, DiagnosticLevel.Warning, message.Message, ResolveSource(source, method, filePath));
 258    }
 59
 60    /// <summary>
 61    /// Writes an error diagnostic message without evaluating formatted expressions when error diagnostics are disabled.
 62    /// </summary>
 63    /// <param name="channel">The diagnostic channel that receives the message.</param>
 64    /// <param name="message">The interpolated diagnostic message.</param>
 65    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 66    /// <param name="method">The calling method name, automatically captured.</param>
 67    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 68    public static void Error(
 69        this DiagnosticChannel channel,
 70        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<ErrorDiagnosticLevel> message,
 71        string source = "",
 72        [CallerMemberName] string method = "",
 73        [CallerFilePath] string filePath = "")
 74    {
 475        if (!message.IsEnabled)
 276            return;
 77
 278        WriteCore(channel, DiagnosticLevel.Error, message.Message, ResolveSource(source, method, filePath));
 279    }
 80
 81    /// <summary>
 82    /// Writes a dynamic-level diagnostic message without evaluating formatted expressions when the level is disabled.
 83    /// </summary>
 84    /// <param name="channel">The diagnostic channel that receives the message.</param>
 85    /// <param name="level">The severity level of the diagnostic event.</param>
 86    /// <param name="message">The interpolated diagnostic message.</param>
 87    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 88    /// <param name="method">The calling method name, automatically captured.</param>
 89    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 90    public static void Log(
 91        this DiagnosticChannel channel,
 92        DiagnosticLevel level,
 93        [InterpolatedStringHandlerArgument("channel", "level")] DiagnosticMessageHandler<DynamicDiagnosticLevel> message
 94        string source = "",
 95        [CallerMemberName] string method = "",
 96        [CallerFilePath] string filePath = "")
 97    {
 398        if (!message.IsEnabled)
 199            return;
 100
 2101        WriteCore(channel, level, message.Message, ResolveSource(source, method, filePath));
 2102    }
 103
 104    private static void WriteCore(
 105        DiagnosticChannel channel,
 106        DiagnosticLevel level,
 107        DiagnosticInterpolatedStringHandler message,
 108        string source)
 109    {
 12110        SwiftThrowHelper.ThrowIfNull(channel, nameof(channel));
 11111        channel.Write(level, message, source);
 11112    }
 113
 114    private static string ResolveSource(string source, string method, string filePath)
 115    {
 12116        if (!string.IsNullOrEmpty(source))
 7117            return source;
 118
 5119        string className = Path.GetFileNameWithoutExtension(filePath);
 5120        if (string.IsNullOrEmpty(className))
 1121            return method;
 122
 4123        if (string.IsNullOrEmpty(method))
 1124            return className;
 125
 3126        return $"{className}.{method}";
 127    }
 128}