| | | 1 | | //======================================================================= |
| | | 2 | | // DiagnosticMessageHandler.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 | | |
| | | 8 | | namespace Chronicler.Diagnostics; |
| | | 9 | | |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | /// <summary>Builds a diagnostic message using an enablement decision captured at construction.</summary> |
| | | 13 | | /// <typeparam name="TLevel">The default provider for fixed-level calls. The dynamic constructor takes a level explicitl |
| | | 14 | | /// <remarks>The compiler uses the constructor's gate to skip interpolation expressions when disabled. Append calls on a |
| | | 15 | | [InterpolatedStringHandler] |
| | | 16 | | public ref struct DiagnosticMessageHandler<TLevel> |
| | | 17 | | where TLevel : struct, IDiagnosticLevelProvider |
| | | 18 | | { |
| | | 19 | | private DiagnosticInterpolatedStringHandler _message; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new handler for a fixed-level diagnostic message. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="literalLength">The compiler-supplied count of literal UTF-16 code units.</param> |
| | | 25 | | /// <param name="formattedCount">The compiler-supplied number of interpolation holes.</param> |
| | | 26 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 27 | | /// <param name="isEnabled">Set to true to evaluate interpolation expressions, or false to skip them.</param> |
| | | 28 | | /// <exception cref="System.ArgumentNullException">The channel is null.</exception> |
| | | 29 | | public DiagnosticMessageHandler( |
| | | 30 | | int literalLength, |
| | | 31 | | int formattedCount, |
| | | 32 | | DiagnosticChannel channel, |
| | | 33 | | out bool isEnabled) |
| | 212 | 34 | | : this(literalLength, formattedCount, channel, default(TLevel).Level, out isEnabled) |
| | | 35 | | { |
| | 211 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new handler for a dynamic-level diagnostic message. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="literalLength">The compiler-supplied count of literal UTF-16 code units.</param> |
| | | 42 | | /// <param name="formattedCount">The compiler-supplied number of interpolation holes.</param> |
| | | 43 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 44 | | /// <param name="level">The diagnostic level being evaluated.</param> |
| | | 45 | | /// <param name="isEnabled">Set to true to evaluate interpolation expressions, or false to skip them.</param> |
| | | 46 | | /// <exception cref="System.ArgumentNullException">The channel is null.</exception> |
| | | 47 | | public DiagnosticMessageHandler( |
| | | 48 | | int literalLength, |
| | | 49 | | int formattedCount, |
| | | 50 | | DiagnosticChannel channel, |
| | | 51 | | DiagnosticLevel level, |
| | | 52 | | out bool isEnabled) |
| | | 53 | | { |
| | 217 | 54 | | ChronicleGuard.ThrowIfNull(channel, nameof(channel)); |
| | 215 | 55 | | _message = new DiagnosticInterpolatedStringHandler(literalLength, formattedCount, channel, level, out isEnabled) |
| | 215 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary>Gets the captured formatting gate.</summary> |
| | | 59 | | /// <value>True when the handler was constructed to build a message.</value> |
| | 214 | 60 | | public bool IsEnabled => _message.IsEnabled; |
| | | 61 | | |
| | 8 | 62 | | internal DiagnosticInterpolatedStringHandler Message => _message; |
| | | 63 | | |
| | | 64 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendLiteral(string)"/> |
| | | 65 | | public void AppendLiteral(string value) |
| | | 66 | | { |
| | 11 | 67 | | _message.AppendLiteral(value); |
| | 11 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T)"/> |
| | | 71 | | public void AppendFormatted<T>(T value) |
| | | 72 | | { |
| | 7 | 73 | | _message.AppendFormatted(value); |
| | 7 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T, string)"/> |
| | | 77 | | public void AppendFormatted<T>(T value, string format) |
| | | 78 | | { |
| | 1 | 79 | | _message.AppendFormatted(value, format); |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T, int)"/> |
| | | 83 | | public void AppendFormatted<T>(T value, int alignment) |
| | | 84 | | { |
| | 1 | 85 | | _message.AppendFormatted(value, alignment); |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc cref="DiagnosticInterpolatedStringHandler.AppendFormatted{T}(T, int, string)"/> |
| | | 89 | | public void AppendFormatted<T>(T value, int alignment, string format) |
| | | 90 | | { |
| | 1 | 91 | | _message.AppendFormatted(value, alignment, format); |
| | 1 | 92 | | } |
| | | 93 | | } |