| | | 1 | | namespace SwiftCollections.Diagnostics; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Receives diagnostic events emitted by a <see cref="DiagnosticChannel"/>. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <param name="diagnostic">The emitted diagnostic event.</param> |
| | | 7 | | public delegate void DiagnosticSink(in DiagnosticEvent diagnostic); |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a named diagnostic channel with a configurable sink and minimum level. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class DiagnosticChannel |
| | | 13 | | { |
| | 3 | 14 | | private static readonly DiagnosticSink NoopSink = static (in DiagnosticEvent _) => { }; |
| | | 15 | | private DiagnosticSink _sink; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="DiagnosticChannel"/> class. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="name">The channel name. Blank names fall back to <c>Default</c>.</param> |
| | 8 | 21 | | public DiagnosticChannel(string name) |
| | 8 | 22 | | { |
| | 8 | 23 | | Name = string.IsNullOrWhiteSpace(name) ? "Default" : name; |
| | 8 | 24 | | _sink = NoopSink; |
| | 8 | 25 | | MinimumLevel = DiagnosticLevel.None; |
| | 8 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the channel name. |
| | | 30 | | /// </summary> |
| | 6 | 31 | | public string Name { get; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets or sets the minimum level required for diagnostics to be emitted. |
| | | 35 | | /// </summary> |
| | 31 | 36 | | public DiagnosticLevel MinimumLevel { get; set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets the sink that receives emitted diagnostics. |
| | | 40 | | /// Assigning <c>null</c> restores the default no-op sink. |
| | | 41 | | /// </summary> |
| | | 42 | | public DiagnosticSink Sink |
| | | 43 | | { |
| | 2 | 44 | | get => _sink; |
| | 3 | 45 | | set => _sink = value ?? NoopSink; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Determines whether the specified level is enabled for this channel. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="level">The level to evaluate.</param> |
| | | 52 | | /// <returns><c>true</c> when the level is enabled; otherwise, <c>false</c>.</returns> |
| | | 53 | | public bool IsEnabled(DiagnosticLevel level) |
| | 10 | 54 | | { |
| | 10 | 55 | | return MinimumLevel != DiagnosticLevel.None |
| | 10 | 56 | | && level >= MinimumLevel |
| | 10 | 57 | | && level != DiagnosticLevel.None; |
| | 10 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Emits a diagnostic event when the specified level is enabled. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="level">The severity level of the diagnostic event.</param> |
| | | 64 | | /// <param name="message">The diagnostic message.</param> |
| | | 65 | | /// <param name="source">An optional source identifier.</param> |
| | | 66 | | public void Write(DiagnosticLevel level, string message, string source = "") |
| | 4 | 67 | | { |
| | 4 | 68 | | if (!IsEnabled(level)) |
| | 2 | 69 | | return; |
| | | 70 | | |
| | 2 | 71 | | _sink(new DiagnosticEvent(Name, level, message, source)); |
| | 4 | 72 | | } |
| | | 73 | | } |