/
Debug.cs
466 lines (396 loc) · 22.7 KB
/
Debug.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Do not remove this, it is needed to retain calls to these conditional methods in release builds
#define DEBUG
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace System.Diagnostics
{
/// <summary>
/// Provides a set of properties and methods for debugging code.
/// </summary>
public static partial class Debug
{
private static volatile DebugProvider s_provider = new DebugProvider();
public static DebugProvider SetProvider(DebugProvider provider)
{
ArgumentNullException.ThrowIfNull(provider);
return Interlocked.Exchange(ref s_provider, provider);
}
public static bool AutoFlush
{
get => true;
set { }
}
[ThreadStatic]
private static int t_indentLevel;
public static int IndentLevel
{
get => t_indentLevel;
set
{
t_indentLevel = value < 0 ? 0 : value;
s_provider.OnIndentLevelChanged(t_indentLevel);
}
}
private static volatile int s_indentSize = 4;
public static int IndentSize
{
get => s_indentSize;
set
{
s_indentSize = value < 0 ? 0 : value;
s_provider.OnIndentSizeChanged(s_indentSize);
}
}
[Conditional("DEBUG")]
public static void Close() { }
[Conditional("DEBUG")]
public static void Flush() { }
[Conditional("DEBUG")]
public static void Indent() =>
IndentLevel++;
[Conditional("DEBUG")]
public static void Unindent() =>
IndentLevel--;
[Conditional("DEBUG")]
public static void Print(string? message) =>
WriteLine(message);
[Conditional("DEBUG")]
public static void Print([StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params object?[] args) =>
WriteLine(string.Format(null, format, args));
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition) =>
Assert(condition, string.Empty, string.Empty);
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition, string? message) =>
Assert(condition, message, string.Empty);
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition, [InterpolatedStringHandlerArgument(nameof(condition))] ref AssertInterpolatedStringHandler message) =>
Assert(condition, message.ToStringAndClear());
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition, string? message, string? detailMessage)
{
if (!condition)
{
Fail(message, detailMessage);
}
}
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition, [InterpolatedStringHandlerArgument(nameof(condition))] ref AssertInterpolatedStringHandler message, [InterpolatedStringHandlerArgument(nameof(condition))] ref AssertInterpolatedStringHandler detailMessage) =>
Assert(condition, message.ToStringAndClear(), detailMessage.ToStringAndClear());
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition, string? message, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string detailMessageFormat, params object?[] args) =>
Assert(condition, message, string.Format(detailMessageFormat, args));
internal static void ContractFailure(string message, string detailMessage, string failureKindMessage)
{
string stackTrace;
try
{
stackTrace = new StackTrace(2, true).ToString(StackTrace.TraceFormat.Normal);
}
catch
{
stackTrace = "";
}
s_provider.WriteAssert(stackTrace, message, detailMessage);
DebugProvider.FailCore(stackTrace, message, detailMessage, failureKindMessage);
}
[Conditional("DEBUG")]
[DoesNotReturn]
public static void Fail(string? message) =>
Fail(message, string.Empty);
[Conditional("DEBUG")]
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)] // Preserve the frame for debugger
public static void Fail(string? message, string? detailMessage) =>
s_provider.Fail(message, detailMessage);
[Conditional("DEBUG")]
public static void WriteLine(string? message) =>
s_provider.WriteLine(message);
[Conditional("DEBUG")]
public static void Write(string? message) =>
s_provider.Write(message);
[Conditional("DEBUG")]
public static void WriteLine(object? value) =>
WriteLine(value?.ToString());
[Conditional("DEBUG")]
public static void WriteLine(object? value, string? category) =>
WriteLine(value?.ToString(), category);
[Conditional("DEBUG")]
public static void WriteLine([StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params object?[] args) =>
WriteLine(string.Format(null, format, args));
[Conditional("DEBUG")]
public static void WriteLine(string? message, string? category)
{
if (category == null)
{
WriteLine(message);
}
else
{
WriteLine(category + ": " + message);
}
}
[Conditional("DEBUG")]
public static void Write(object? value) =>
Write(value?.ToString());
[Conditional("DEBUG")]
public static void Write(string? message, string? category)
{
if (category == null)
{
Write(message);
}
else
{
Write(category + ": " + message);
}
}
[Conditional("DEBUG")]
public static void Write(object? value, string? category) =>
Write(value?.ToString(), category);
[Conditional("DEBUG")]
public static void WriteIf(bool condition, string? message)
{
if (condition)
{
Write(message);
}
}
[Conditional("DEBUG")]
public static void WriteIf(bool condition, [InterpolatedStringHandlerArgument(nameof(condition))] ref WriteIfInterpolatedStringHandler message) =>
WriteIf(condition, message.ToStringAndClear());
[Conditional("DEBUG")]
public static void WriteIf(bool condition, object? value)
{
if (condition)
{
Write(value);
}
}
[Conditional("DEBUG")]
public static void WriteIf(bool condition, string? message, string? category)
{
if (condition)
{
Write(message, category);
}
}
[Conditional("DEBUG")]
public static void WriteIf(bool condition, [InterpolatedStringHandlerArgument(nameof(condition))] ref WriteIfInterpolatedStringHandler message, string? category) =>
WriteIf(condition, message.ToStringAndClear(), category);
[Conditional("DEBUG")]
public static void WriteIf(bool condition, object? value, string? category)
{
if (condition)
{
Write(value, category);
}
}
[Conditional("DEBUG")]
public static void WriteLineIf(bool condition, object? value)
{
if (condition)
{
WriteLine(value);
}
}
[Conditional("DEBUG")]
public static void WriteLineIf(bool condition, object? value, string? category)
{
if (condition)
{
WriteLine(value, category);
}
}
[Conditional("DEBUG")]
public static void WriteLineIf(bool condition, string? message)
{
if (condition)
{
WriteLine(message);
}
}
[Conditional("DEBUG")]
public static void WriteLineIf(bool condition, [InterpolatedStringHandlerArgument(nameof(condition))] ref WriteIfInterpolatedStringHandler message) =>
WriteLineIf(condition, message.ToStringAndClear());
[Conditional("DEBUG")]
public static void WriteLineIf(bool condition, string? message, string? category)
{
if (condition)
{
WriteLine(message, category);
}
}
[Conditional("DEBUG")]
public static void WriteLineIf(bool condition, [InterpolatedStringHandlerArgument(nameof(condition))] ref WriteIfInterpolatedStringHandler message, string? category) =>
WriteLineIf(condition, message.ToStringAndClear(), category);
/// <summary>Provides an interpolated string handler for <see cref="Debug.Assert"/> that only performs formatting if the assert fails.</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[InterpolatedStringHandler]
public struct AssertInterpolatedStringHandler
{
/// <summary>The handler we use to perform the formatting.</summary>
private StringBuilder.AppendInterpolatedStringHandler _stringBuilderHandler;
/// <summary>Creates an instance of the handler..</summary>
/// <param name="literalLength">The number of constant characters outside of interpolation expressions in the interpolated string.</param>
/// <param name="formattedCount">The number of interpolation expressions in the interpolated string.</param>
/// <param name="condition">The condition Boolean passed to the <see cref="Debug"/> method.</param>
/// <param name="shouldAppend">A value indicating whether formatting should proceed.</param>
/// <remarks>This is intended to be called only by compiler-generated code. Arguments are not validated as they'd otherwise be for members intended to be used directly.</remarks>
public AssertInterpolatedStringHandler(int literalLength, int formattedCount, bool condition, out bool shouldAppend)
{
if (condition)
{
_stringBuilderHandler = default;
shouldAppend = false;
}
else
{
// Only used when failing an assert. Additional allocation here doesn't matter; just create a new StringBuilder.
_stringBuilderHandler = new StringBuilder.AppendInterpolatedStringHandler(literalLength, formattedCount, new StringBuilder());
shouldAppend = true;
}
}
/// <summary>Extracts the built string from the handler.</summary>
internal string ToStringAndClear()
{
string s = _stringBuilderHandler._stringBuilder is StringBuilder sb ?
sb.ToString() :
string.Empty;
_stringBuilderHandler = default;
return s;
}
/// <summary>Writes the specified string to the handler.</summary>
/// <param name="value">The string to write.</param>
public void AppendLiteral(string value) => _stringBuilderHandler.AppendLiteral(value);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value) => _stringBuilderHandler.AppendFormatted(value);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="format">The format string.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value, string? format) => _stringBuilderHandler.AppendFormatted(value, format);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value, int alignment) => _stringBuilderHandler.AppendFormatted(value, alignment);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="format">The format string.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value, int alignment, string? format) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
/// <summary>Writes the specified character span to the handler.</summary>
/// <param name="value">The span to write.</param>
public void AppendFormatted(ReadOnlySpan<char> value) => _stringBuilderHandler.AppendFormatted(value);
/// <summary>Writes the specified string of chars to the handler.</summary>
/// <param name="value">The span to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted(ReadOnlySpan<char> value, int alignment = 0, string? format = null) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
public void AppendFormatted(string? value) => _stringBuilderHandler.AppendFormatted(value);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted(string? value, int alignment = 0, string? format = null) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted(object? value, int alignment = 0, string? format = null) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
}
/// <summary>Provides an interpolated string handler for <see cref="Debug.WriteIf"/> and <see cref="Debug.WriteLineIf"/> that only performs formatting if the condition applies.</summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[InterpolatedStringHandler]
public struct WriteIfInterpolatedStringHandler
{
/// <summary>The handler we use to perform the formatting.</summary>
private StringBuilder.AppendInterpolatedStringHandler _stringBuilderHandler;
/// <summary>Creates an instance of the handler..</summary>
/// <param name="literalLength">The number of constant characters outside of interpolation expressions in the interpolated string.</param>
/// <param name="formattedCount">The number of interpolation expressions in the interpolated string.</param>
/// <param name="condition">The condition Boolean passed to the <see cref="Debug"/> method.</param>
/// <param name="shouldAppend">A value indicating whether formatting should proceed.</param>
/// <remarks>This is intended to be called only by compiler-generated code. Arguments are not validated as they'd otherwise be for members intended to be used directly.</remarks>
public WriteIfInterpolatedStringHandler(int literalLength, int formattedCount, bool condition, out bool shouldAppend)
{
if (condition)
{
// Only used in debug, but could be used on non-failure code paths, so use a cached builder.
_stringBuilderHandler = new StringBuilder.AppendInterpolatedStringHandler(literalLength, formattedCount,
StringBuilderCache.Acquire(DefaultInterpolatedStringHandler.GetDefaultLength(literalLength, formattedCount)));
shouldAppend = true;
}
else
{
_stringBuilderHandler = default;
shouldAppend = false;
}
}
/// <summary>Extracts the built string from the handler.</summary>
internal string ToStringAndClear()
{
string s = _stringBuilderHandler._stringBuilder is StringBuilder sb ?
StringBuilderCache.GetStringAndRelease(sb) :
string.Empty;
_stringBuilderHandler = default;
return s;
}
/// <summary>Writes the specified string to the handler.</summary>
/// <param name="value">The string to write.</param>
public void AppendLiteral(string value) => _stringBuilderHandler.AppendLiteral(value);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value) => _stringBuilderHandler.AppendFormatted(value);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="format">The format string.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value, string? format) => _stringBuilderHandler.AppendFormatted(value, format);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value, int alignment) => _stringBuilderHandler.AppendFormatted(value, alignment);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="format">The format string.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value, int alignment, string? format) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
/// <summary>Writes the specified character span to the handler.</summary>
/// <param name="value">The span to write.</param>
public void AppendFormatted(ReadOnlySpan<char> value) => _stringBuilderHandler.AppendFormatted(value);
/// <summary>Writes the specified string of chars to the handler.</summary>
/// <param name="value">The span to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted(ReadOnlySpan<char> value, int alignment = 0, string? format = null) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
public void AppendFormatted(string? value) => _stringBuilderHandler.AppendFormatted(value);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted(string? value, int alignment = 0, string? format = null) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="alignment">Minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted(object? value, int alignment = 0, string? format = null) => _stringBuilderHandler.AppendFormatted(value, alignment, format);
}
}
}