|
| 1 | +using System.Threading.Channels; |
| 2 | +using Microsoft.Extensions.Hosting; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | + |
| 5 | +namespace Elastic.Markdown.Diagnostics; |
| 6 | + |
| 7 | +public class DiagnosticsChannel |
| 8 | +{ |
| 9 | + private readonly Channel<Diagnostic> _channel; |
| 10 | + private readonly CancellationTokenSource _ctxSource; |
| 11 | + public ChannelReader<Diagnostic> Reader => _channel.Reader; |
| 12 | + |
| 13 | + public CancellationToken CancellationToken => _ctxSource.Token; |
| 14 | + |
| 15 | + public DiagnosticsChannel() |
| 16 | + { |
| 17 | + var options = new UnboundedChannelOptions { SingleReader = true, SingleWriter = false }; |
| 18 | + _ctxSource = new CancellationTokenSource(); |
| 19 | + _channel = Channel.CreateUnbounded<Diagnostic>(options); |
| 20 | + } |
| 21 | + |
| 22 | + public void TryComplete(Exception? exception = null) |
| 23 | + { |
| 24 | + _channel.Writer.TryComplete(exception); |
| 25 | + _ctxSource.Cancel(); |
| 26 | + } |
| 27 | + |
| 28 | + public void Write(Diagnostic diagnostic) |
| 29 | + { |
| 30 | + var written = _channel.Writer.TryWrite(diagnostic); |
| 31 | + if (!written) |
| 32 | + { |
| 33 | + //TODO |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +public enum Severity { Error, Warning } |
| 40 | + |
| 41 | +public readonly record struct Diagnostic |
| 42 | +{ |
| 43 | + public Severity Severity { get; init; } |
| 44 | + public int Line { get; init; } |
| 45 | + public int? Position { get; init; } |
| 46 | + public string File { get; init; } |
| 47 | + public string Message { get; init; } |
| 48 | +} |
| 49 | + |
| 50 | +public interface IDiagnosticsOutput |
| 51 | +{ |
| 52 | + public void Write(Diagnostic diagnostic); |
| 53 | +} |
| 54 | + |
| 55 | +public class LogDiagnosticOutput(ILogger logger) : IDiagnosticsOutput |
| 56 | +{ |
| 57 | + public void Write(Diagnostic diagnostic) |
| 58 | + { |
| 59 | + if (diagnostic.Severity == Severity.Error) |
| 60 | + logger.LogError($"{diagnostic.Message} ({diagnostic.File}:{diagnostic.Line})"); |
| 61 | + else |
| 62 | + logger.LogWarning($"{diagnostic.Message} ({diagnostic.File}:{diagnostic.Line})"); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +public class DiagnosticsCollector(ILoggerFactory loggerFactory, IReadOnlyCollection<IDiagnosticsOutput> outputs) |
| 68 | + : IHostedService |
| 69 | +{ |
| 70 | + private readonly IReadOnlyCollection<IDiagnosticsOutput> _outputs = |
| 71 | + [new LogDiagnosticOutput(loggerFactory.CreateLogger<LogDiagnosticOutput>()), ..outputs]; |
| 72 | + |
| 73 | + public DiagnosticsChannel Channel { get; } = new(); |
| 74 | + |
| 75 | + private long _errors; |
| 76 | + private long _warnings; |
| 77 | + public long Warnings => _warnings; |
| 78 | + public long Errors => _errors; |
| 79 | + |
| 80 | + public async Task StartAsync(Cancel ctx) |
| 81 | + { |
| 82 | + while (!Channel.CancellationToken.IsCancellationRequested) |
| 83 | + { |
| 84 | + while (await Channel.Reader.WaitToReadAsync(Channel.CancellationToken)) |
| 85 | + Drain(); |
| 86 | + } |
| 87 | + Drain(); |
| 88 | + |
| 89 | + void Drain() |
| 90 | + { |
| 91 | + while (Channel.Reader.TryRead(out var item)) |
| 92 | + { |
| 93 | + IncrementSeverityCount(item); |
| 94 | + HandleItem(item); |
| 95 | + foreach (var output in _outputs) |
| 96 | + output.Write(item); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void IncrementSeverityCount(Diagnostic item) |
| 102 | + { |
| 103 | + if (item.Severity == Severity.Error) |
| 104 | + Interlocked.Increment(ref _errors); |
| 105 | + else if (item.Severity == Severity.Warning) |
| 106 | + Interlocked.Increment(ref _warnings); |
| 107 | + } |
| 108 | + |
| 109 | + protected virtual void HandleItem(Diagnostic diagnostic) {} |
| 110 | + |
| 111 | + public virtual Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
| 112 | +} |
0 commit comments