| | | 1 | | //======================================================================= |
| | | 2 | | // DiagnosticInterpolatedStringHandler.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; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | #if !NET6_0_OR_GREATER |
| | | 13 | | using System.Text; |
| | | 14 | | #endif |
| | | 15 | | |
| | | 16 | | /// <summary>Builds a diagnostic message using an enablement decision captured at construction.</summary> |
| | | 17 | | /// <remarks>The compiler uses the constructor's gate to skip interpolation expressions when disabled. Append calls on a |
| | | 18 | | [InterpolatedStringHandler] |
| | | 19 | | public ref struct DiagnosticInterpolatedStringHandler |
| | | 20 | | { |
| | | 21 | | private readonly bool _isEnabled; |
| | | 22 | | #if NET6_0_OR_GREATER |
| | | 23 | | private DefaultInterpolatedStringHandler _builder; |
| | | 24 | | #else |
| | | 25 | | private StringBuilder? _builder; |
| | | 26 | | #endif |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="DiagnosticInterpolatedStringHandler"/> struct. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="literalLength">The compiler-supplied count of literal UTF-16 code units.</param> |
| | | 32 | | /// <param name="formattedCount">The compiler-supplied number of interpolation holes.</param> |
| | | 33 | | /// <param name="channel">The channel whose threshold controls formatting. Null disables the handler.</param> |
| | | 34 | | /// <param name="level">The severity level being evaluated.</param> |
| | | 35 | | /// <param name="isEnabled"> |
| | | 36 | | /// Set to <c>true</c> when formatted expressions should be evaluated; otherwise, <c>false</c>. |
| | | 37 | | /// </param> |
| | | 38 | | public DiagnosticInterpolatedStringHandler( |
| | | 39 | | int literalLength, |
| | | 40 | | int formattedCount, |
| | | 41 | | DiagnosticChannel channel, |
| | | 42 | | DiagnosticLevel level, |
| | | 43 | | out bool isEnabled) |
| | 226 | 44 | | : this(literalLength, formattedCount, channel != null && channel.IsEnabled(level), out isEnabled) |
| | 226 | 45 | | { } |
| | | 46 | | |
| | | 47 | | internal DiagnosticInterpolatedStringHandler( |
| | | 48 | | int literalLength, |
| | | 49 | | int formattedCount, |
| | | 50 | | bool shouldFormat, |
| | | 51 | | out bool isEnabled) |
| | | 52 | | { |
| | 248 | 53 | | isEnabled = shouldFormat; |
| | 248 | 54 | | _isEnabled = isEnabled; |
| | | 55 | | #if NET6_0_OR_GREATER |
| | 248 | 56 | | _builder = isEnabled ? new DefaultInterpolatedStringHandler(literalLength, formattedCount) : default; |
| | | 57 | | #else |
| | | 58 | | _ = formattedCount; |
| | | 59 | | _builder = isEnabled ? new StringBuilder(literalLength) : null; |
| | | 60 | | #endif |
| | 248 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary>Gets the captured formatting gate.</summary> |
| | | 64 | | /// <value>True when the handler was constructed to build a message.</value> |
| | 226 | 65 | | public bool IsEnabled => _isEnabled; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Appends a literal string segment. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="value">The literal string segment.</param> |
| | | 71 | | public void AppendLiteral(string value) |
| | | 72 | | { |
| | 59 | 73 | | if (!_isEnabled) |
| | 2 | 74 | | return; |
| | | 75 | | |
| | | 76 | | #if NET6_0_OR_GREATER |
| | 57 | 77 | | _builder.AppendLiteral(value); |
| | | 78 | | #else |
| | | 79 | | _builder!.Append(value); |
| | | 80 | | #endif |
| | 57 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Appends a formatted value. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <typeparam name="T">The static type of the interpolation expression.</typeparam> |
| | | 87 | | /// <param name="value">The expression result. Null contributes no text before alignment.</param> |
| | | 88 | | public void AppendFormatted<T>(T value) |
| | | 89 | | { |
| | 19 | 90 | | if (!_isEnabled) |
| | 1 | 91 | | return; |
| | | 92 | | |
| | | 93 | | #if NET6_0_OR_GREATER |
| | 18 | 94 | | _builder.AppendFormatted(value); |
| | | 95 | | #else |
| | | 96 | | _builder!.Append(value); |
| | | 97 | | #endif |
| | 18 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Appends a formatted value using the specified format string. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <typeparam name="T">The static type of the interpolation expression.</typeparam> |
| | | 104 | | /// <param name="value">The expression result. Null contributes no text before alignment.</param> |
| | | 105 | | /// <param name="format">The format passed to the value's formatter. Null selects its default.</param> |
| | | 106 | | public void AppendFormatted<T>(T value, string? format) |
| | | 107 | | { |
| | 4 | 108 | | if (!_isEnabled) |
| | 1 | 109 | | return; |
| | | 110 | | |
| | | 111 | | #if NET6_0_OR_GREATER |
| | 3 | 112 | | _builder.AppendFormatted(value, format); |
| | | 113 | | #else |
| | | 114 | | AppendFormattedValue(value, 0, format); |
| | | 115 | | #endif |
| | 3 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Appends a formatted value with the specified alignment. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <typeparam name="T">The static type of the interpolation expression.</typeparam> |
| | | 122 | | /// <param name="value">The expression result. Null contributes no text before alignment.</param> |
| | | 123 | | /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab |
| | | 124 | | public void AppendFormatted<T>(T value, int alignment) |
| | | 125 | | { |
| | 4 | 126 | | if (!_isEnabled) |
| | 1 | 127 | | return; |
| | | 128 | | |
| | | 129 | | #if NET6_0_OR_GREATER |
| | 3 | 130 | | _builder.AppendFormatted(value, alignment); |
| | | 131 | | #else |
| | | 132 | | AppendFormattedValue(value, alignment, null); |
| | | 133 | | #endif |
| | 3 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Appends a formatted value with the specified alignment and format string. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <typeparam name="T">The static type of the interpolation expression.</typeparam> |
| | | 140 | | /// <param name="value">The expression result. Null contributes no text before alignment.</param> |
| | | 141 | | /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab |
| | | 142 | | /// <param name="format">The format passed to the value's formatter. Null selects its default.</param> |
| | | 143 | | public void AppendFormatted<T>(T value, int alignment, string? format) |
| | | 144 | | { |
| | 10 | 145 | | if (!_isEnabled) |
| | 1 | 146 | | return; |
| | | 147 | | |
| | | 148 | | #if NET6_0_OR_GREATER |
| | 9 | 149 | | _builder.AppendFormatted(value, alignment, format); |
| | | 150 | | #else |
| | | 151 | | AppendFormattedValue(value, alignment, format); |
| | | 152 | | #endif |
| | 9 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Appends a string value. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="value">The text to append. Null contributes no characters.</param> |
| | | 159 | | public void AppendFormatted(string? value) |
| | | 160 | | { |
| | 4 | 161 | | if (!_isEnabled) |
| | 1 | 162 | | return; |
| | | 163 | | |
| | | 164 | | #if NET6_0_OR_GREATER |
| | 3 | 165 | | _builder.AppendFormatted(value); |
| | | 166 | | #else |
| | | 167 | | _builder!.Append(value); |
| | | 168 | | #endif |
| | 3 | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Appends a string value with the specified alignment. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="value">The expression result. Null contributes no text before alignment.</param> |
| | | 175 | | /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab |
| | | 176 | | public void AppendFormatted(string? value, int alignment) |
| | | 177 | | { |
| | 3 | 178 | | if (!_isEnabled) |
| | 1 | 179 | | return; |
| | | 180 | | |
| | | 181 | | #if NET6_0_OR_GREATER |
| | 2 | 182 | | _builder.AppendFormatted(value, alignment); |
| | | 183 | | #else |
| | | 184 | | AppendAligned(value, alignment); |
| | | 185 | | #endif |
| | 2 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Appends a string value with the specified alignment and format string. |
| | | 190 | | /// </summary> |
| | | 191 | | /// <param name="value">The expression result. Null contributes no text before alignment.</param> |
| | | 192 | | /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab |
| | | 193 | | /// <param name="format">Accepted for interpolation syntax. The input is appended as text.</param> |
| | | 194 | | public void AppendFormatted(string? value, int alignment, string? format) |
| | | 195 | | { |
| | 3 | 196 | | if (!_isEnabled) |
| | 1 | 197 | | return; |
| | | 198 | | |
| | | 199 | | #if NET6_0_OR_GREATER |
| | 2 | 200 | | _builder.AppendFormatted(value, alignment, format); |
| | | 201 | | #else |
| | | 202 | | AppendAligned(value, alignment); |
| | | 203 | | #endif |
| | 2 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Appends a character span. |
| | | 208 | | /// </summary> |
| | | 209 | | /// <param name="value">The span whose characters are copied into the message.</param> |
| | | 210 | | public void AppendFormatted(ReadOnlySpan<char> value) |
| | | 211 | | { |
| | 3 | 212 | | if (!_isEnabled) |
| | 1 | 213 | | return; |
| | | 214 | | |
| | | 215 | | #if NET6_0_OR_GREATER |
| | 2 | 216 | | _builder.AppendFormatted(value); |
| | | 217 | | #else |
| | | 218 | | _builder!.Append(value); |
| | | 219 | | #endif |
| | 2 | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Appends a character span with the specified alignment. |
| | | 224 | | /// </summary> |
| | | 225 | | /// <param name="value">The span whose characters are copied into the message.</param> |
| | | 226 | | /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab |
| | | 227 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment) |
| | | 228 | | { |
| | 9 | 229 | | if (!_isEnabled) |
| | 1 | 230 | | return; |
| | | 231 | | |
| | | 232 | | #if NET6_0_OR_GREATER |
| | 8 | 233 | | _builder.AppendFormatted(value, alignment); |
| | | 234 | | #else |
| | | 235 | | AppendAligned(value, alignment); |
| | | 236 | | #endif |
| | 8 | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Appends a character span with the specified alignment and format string. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <param name="value">The span whose characters are copied into the message.</param> |
| | | 243 | | /// <param name="alignment">Minimum width in characters. Positive right-aligns, negative left-aligns, and zero disab |
| | | 244 | | /// <param name="format">Accepted for interpolation syntax. The input is appended as text.</param> |
| | | 245 | | public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format) |
| | | 246 | | { |
| | 3 | 247 | | if (!_isEnabled) |
| | 1 | 248 | | return; |
| | | 249 | | |
| | | 250 | | #if NET6_0_OR_GREATER |
| | 2 | 251 | | _builder.AppendFormatted(value, alignment, format); |
| | | 252 | | #else |
| | | 253 | | AppendAligned(value, alignment); |
| | | 254 | | #endif |
| | 2 | 255 | | } |
| | | 256 | | |
| | | 257 | | internal string GetFormattedText() |
| | | 258 | | { |
| | | 259 | | #if NET6_0_OR_GREATER |
| | 27 | 260 | | return _builder.ToStringAndClear(); |
| | | 261 | | #else |
| | | 262 | | return _builder?.ToString() ?? string.Empty; |
| | | 263 | | #endif |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | #if !NET6_0_OR_GREATER |
| | | 267 | | private void AppendFormattedValue<T>(T value, int alignment, string? format) |
| | | 268 | | { |
| | | 269 | | string? text = value is IFormattable formattable |
| | | 270 | | ? formattable.ToString(format, null) |
| | | 271 | | : value?.ToString(); |
| | | 272 | | |
| | | 273 | | AppendAligned(text, alignment); |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | private void AppendAligned(string? value, int alignment) |
| | | 277 | | { |
| | | 278 | | value ??= string.Empty; |
| | | 279 | | |
| | | 280 | | if (alignment == 0) |
| | | 281 | | { |
| | | 282 | | _builder!.Append(value); |
| | | 283 | | return; |
| | | 284 | | } |
| | | 285 | | |
| | | 286 | | int width = alignment < 0 ? -alignment : alignment; |
| | | 287 | | int padding = width - value.Length; |
| | | 288 | | |
| | | 289 | | if (padding <= 0) |
| | | 290 | | { |
| | | 291 | | _builder!.Append(value); |
| | | 292 | | return; |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | if (alignment > 0) |
| | | 296 | | _builder!.Append(' ', padding); |
| | | 297 | | |
| | | 298 | | _builder!.Append(value); |
| | | 299 | | |
| | | 300 | | if (alignment < 0) |
| | | 301 | | _builder!.Append(' ', padding); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | private void AppendAligned(ReadOnlySpan<char> value, int alignment) |
| | | 305 | | { |
| | | 306 | | if (alignment == 0) |
| | | 307 | | { |
| | | 308 | | _builder!.Append(value); |
| | | 309 | | return; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | int width = alignment < 0 ? -alignment : alignment; |
| | | 313 | | int padding = width - value.Length; |
| | | 314 | | |
| | | 315 | | if (padding <= 0) |
| | | 316 | | { |
| | | 317 | | _builder!.Append(value); |
| | | 318 | | return; |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | if (alignment > 0) |
| | | 322 | | _builder!.Append(' ', padding); |
| | | 323 | | |
| | | 324 | | _builder!.Append(value); |
| | | 325 | | |
| | | 326 | | if (alignment < 0) |
| | | 327 | | _builder!.Append(' ', padding); |
| | | 328 | | } |
| | | 329 | | #endif |
| | | 330 | | } |