| | | 1 | | namespace SwiftCollections.Diagnostics; |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Describes a fixed diagnostic level for an interpolated diagnostic message handler. |
| | | 8 | | /// </summary> |
| | | 9 | | public interface IDiagnosticLevelProvider |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Gets the diagnostic level for this provider. |
| | | 13 | | /// </summary> |
| | | 14 | | DiagnosticLevel Level { get; } |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Provides the info diagnostic level for fixed-level interpolated logging. |
| | | 19 | | /// </summary> |
| | | 20 | | public readonly struct InfoDiagnosticLevel : IDiagnosticLevelProvider |
| | | 21 | | { |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public DiagnosticLevel Level => DiagnosticLevel.Info; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Provides the warning diagnostic level for fixed-level interpolated logging. |
| | | 28 | | /// </summary> |
| | | 29 | | public readonly struct WarningDiagnosticLevel : IDiagnosticLevelProvider |
| | | 30 | | { |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public DiagnosticLevel Level => DiagnosticLevel.Warning; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Provides the error diagnostic level for fixed-level interpolated logging. |
| | | 37 | | /// </summary> |
| | | 38 | | public readonly struct ErrorDiagnosticLevel : IDiagnosticLevelProvider |
| | | 39 | | { |
| | | 40 | | /// <inheritdoc /> |
| | 2 | 41 | | public DiagnosticLevel Level => DiagnosticLevel.Error; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Provides a placeholder level for dynamic-level interpolated logging. |
| | | 46 | | /// </summary> |
| | | 47 | | public readonly struct DynamicDiagnosticLevel : IDiagnosticLevelProvider |
| | | 48 | | { |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public DiagnosticLevel Level => DiagnosticLevel.None; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Builds interpolated diagnostic messages only when the requested diagnostic level is enabled. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <typeparam name="TLevel">The fixed diagnostic level provider used by fixed-level helpers.</typeparam> |
| | | 57 | | [InterpolatedStringHandler] |
| | | 58 | | public ref struct DiagnosticMessageHandler<TLevel> |
| | | 59 | | where TLevel : struct, IDiagnosticLevelProvider |
| | | 60 | | { |
| | | 61 | | private DiagnosticInterpolatedStringHandler _message; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Initializes a new handler for a fixed-level diagnostic message. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="literalLength">The combined length of literal portions in the interpolated string.</param> |
| | | 67 | | /// <param name="formattedCount">The number of formatted expressions in the interpolated string.</param> |
| | | 68 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 69 | | /// <param name="isEnabled">Set to <see langword="true"/> when formatted expressions should be evaluated.</param> |
| | | 70 | | public DiagnosticMessageHandler( |
| | | 71 | | int literalLength, |
| | | 72 | | int formattedCount, |
| | | 73 | | DiagnosticChannel channel, |
| | | 74 | | out bool isEnabled) |
| | | 75 | | : this(literalLength, formattedCount, channel, default(TLevel).Level, out isEnabled) |
| | | 76 | | { |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Initializes a new handler for a dynamic-level diagnostic message. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="literalLength">The combined length of literal portions in the interpolated string.</param> |
| | | 83 | | /// <param name="formattedCount">The number of formatted expressions in the interpolated string.</param> |
| | | 84 | | /// <param name="channel">The diagnostic channel that receives the message.</param> |
| | | 85 | | /// <param name="level">The diagnostic level being evaluated.</param> |
| | | 86 | | /// <param name="isEnabled">Set to <see langword="true"/> when formatted expressions should be evaluated.</param> |
| | | 87 | | public DiagnosticMessageHandler( |
| | | 88 | | int literalLength, |
| | | 89 | | int formattedCount, |
| | | 90 | | DiagnosticChannel channel, |
| | | 91 | | DiagnosticLevel level, |
| | | 92 | | out bool isEnabled) |
| | | 93 | | { |
| | | 94 | | SwiftThrowHelper.ThrowIfNull(channel, nameof(channel)); |
| | | 95 | | _message = new DiagnosticInterpolatedStringHandler(literalLength, formattedCount, channel, level, out isEnabled) |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets whether the handler is actively building a diagnostic message. |
| | | 100 | | /// </summary> |
| | | 101 | | public bool IsEnabled => _message.IsEnabled; |
| | | 102 | | |
| | | 103 | | internal DiagnosticInterpolatedStringHandler Message => _message; |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Appends a literal string segment. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="value">The literal string segment.</param> |
| | | 109 | | public void AppendLiteral(string value) |
| | | 110 | | { |
| | | 111 | | _message.AppendLiteral(value); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Appends a formatted value. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 118 | | /// <param name="value">The value to append.</param> |
| | | 119 | | public void AppendFormatted<T>(T value) |
| | | 120 | | { |
| | | 121 | | _message.AppendFormatted(value); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Appends a formatted value using the specified format string. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 128 | | /// <param name="value">The value to append.</param> |
| | | 129 | | /// <param name="format">The format string to apply.</param> |
| | | 130 | | public void AppendFormatted<T>(T value, string format) |
| | | 131 | | { |
| | | 132 | | _message.AppendFormatted(value, format); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Appends a formatted value with the specified alignment. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 139 | | /// <param name="value">The value to append.</param> |
| | | 140 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 141 | | public void AppendFormatted<T>(T value, int alignment) |
| | | 142 | | { |
| | | 143 | | _message.AppendFormatted(value, alignment); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Appends a formatted value with the specified alignment and format string. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <typeparam name="T">The type of value to append.</typeparam> |
| | | 150 | | /// <param name="value">The value to append.</param> |
| | | 151 | | /// <param name="alignment">The minimum width for the formatted value.</param> |
| | | 152 | | /// <param name="format">The format string to apply.</param> |
| | | 153 | | public void AppendFormatted<T>(T value, int alignment, string format) |
| | | 154 | | { |
| | | 155 | | _message.AppendFormatted(value, alignment, format); |
| | | 156 | | } |
| | | 157 | | } |