| | | 1 | | namespace SwiftCollections.Diagnostics; |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides interpolated diagnostic logging helpers for <see cref="DiagnosticChannel"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class DiagnosticChannelLoggingExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Writes an info diagnostic message without evaluating formatted expressions when info diagnostics are disabled. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 16 | | /// <param name="message">The interpolated diagnostic message.</param> |
| | | 17 | | /// <param name="source">An optional source identifier. When omitted, caller information is used.</param> |
| | | 18 | | /// <param name="method">The calling method name, automatically captured.</param> |
| | | 19 | | /// <param name="filePath">The calling file name, automatically captured to get the class name.</param> |
| | | 20 | | public static void Info( |
| | | 21 | | this DiagnosticChannel channel, |
| | | 22 | | [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<InfoDiagnosticLevel> message, |
| | | 23 | | string source = "", |
| | | 24 | | [CallerMemberName] string method = "", |
| | | 25 | | [CallerFilePath] string filePath = "") |
| | | 26 | | { |
| | 3 | 27 | | if (!message.IsEnabled) |
| | 1 | 28 | | return; |
| | | 29 | | |
| | 2 | 30 | | WriteCore(channel, DiagnosticLevel.Info, message.Message, ResolveSource(source, method, filePath)); |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Writes a warning diagnostic message without evaluating formatted expressions when warning diagnostics are disabl |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 37 | | /// <param name="message">The interpolated diagnostic message.</param> |
| | | 38 | | /// <param name="source">An optional source identifier. When omitted, caller information is used.</param> |
| | | 39 | | /// <param name="method">The calling method name, automatically captured.</param> |
| | | 40 | | /// <param name="filePath">The calling file name, automatically captured to get the class name.</param> |
| | | 41 | | public static void Warn( |
| | | 42 | | this DiagnosticChannel channel, |
| | | 43 | | [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<WarningDiagnosticLevel> message, |
| | | 44 | | string source = "", |
| | | 45 | | [CallerMemberName] string method = "", |
| | | 46 | | [CallerFilePath] string filePath = "") |
| | | 47 | | { |
| | 1 | 48 | | if (!message.IsEnabled) |
| | 0 | 49 | | return; |
| | | 50 | | |
| | 1 | 51 | | WriteCore(channel, DiagnosticLevel.Warning, message.Message, ResolveSource(source, method, filePath)); |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Writes an error diagnostic message without evaluating formatted expressions when error diagnostics are disabled. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 58 | | /// <param name="message">The interpolated diagnostic message.</param> |
| | | 59 | | /// <param name="source">An optional source identifier. When omitted, caller information is used.</param> |
| | | 60 | | /// <param name="method">The calling method name, automatically captured.</param> |
| | | 61 | | /// <param name="filePath">The calling file name, automatically captured to get the class name.</param> |
| | | 62 | | public static void Error( |
| | | 63 | | this DiagnosticChannel channel, |
| | | 64 | | [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<ErrorDiagnosticLevel> message, |
| | | 65 | | string source = "", |
| | | 66 | | [CallerMemberName] string method = "", |
| | | 67 | | [CallerFilePath] string filePath = "") |
| | | 68 | | { |
| | 2 | 69 | | if (!message.IsEnabled) |
| | 1 | 70 | | return; |
| | | 71 | | |
| | 1 | 72 | | WriteCore(channel, DiagnosticLevel.Error, message.Message, ResolveSource(source, method, filePath)); |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Writes a dynamic-level diagnostic message without evaluating formatted expressions when the level is disabled. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 79 | | /// <param name="level">The severity level of the diagnostic event.</param> |
| | | 80 | | /// <param name="message">The interpolated diagnostic message.</param> |
| | | 81 | | /// <param name="source">An optional source identifier. When omitted, caller information is used.</param> |
| | | 82 | | /// <param name="method">The calling method name, automatically captured.</param> |
| | | 83 | | /// <param name="filePath">The calling file name, automatically captured to get the class name.</param> |
| | | 84 | | public static void Log( |
| | | 85 | | this DiagnosticChannel channel, |
| | | 86 | | DiagnosticLevel level, |
| | | 87 | | [InterpolatedStringHandlerArgument("channel", "level")] DiagnosticMessageHandler<DynamicDiagnosticLevel> message |
| | | 88 | | string source = "", |
| | | 89 | | [CallerMemberName] string method = "", |
| | | 90 | | [CallerFilePath] string filePath = "") |
| | | 91 | | { |
| | 3 | 92 | | if (!message.IsEnabled) |
| | 1 | 93 | | return; |
| | | 94 | | |
| | 2 | 95 | | WriteCore(channel, level, message.Message, ResolveSource(source, method, filePath)); |
| | 2 | 96 | | } |
| | | 97 | | |
| | | 98 | | private static void WriteCore( |
| | | 99 | | DiagnosticChannel channel, |
| | | 100 | | DiagnosticLevel level, |
| | | 101 | | DiagnosticInterpolatedStringHandler message, |
| | | 102 | | string source) |
| | | 103 | | { |
| | 6 | 104 | | SwiftThrowHelper.ThrowIfNull(channel, nameof(channel)); |
| | 5 | 105 | | channel.Write(level, message, source); |
| | 5 | 106 | | } |
| | | 107 | | |
| | | 108 | | private static string ResolveSource(string source, string method, string filePath) |
| | | 109 | | { |
| | 6 | 110 | | if (!string.IsNullOrEmpty(source)) |
| | 1 | 111 | | return source; |
| | | 112 | | |
| | 5 | 113 | | string className = Path.GetFileNameWithoutExtension(filePath); |
| | 5 | 114 | | if (string.IsNullOrEmpty(className)) |
| | 1 | 115 | | return method; |
| | | 116 | | |
| | 4 | 117 | | if (string.IsNullOrEmpty(method)) |
| | 1 | 118 | | return className; |
| | | 119 | | |
| | 3 | 120 | | return $"{className}.{method}"; |
| | | 121 | | } |
| | | 122 | | } |