IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
ConsoleDiagnosticFormatter.cs
Go to the documentation of this file.
1using System;
3
5
6using Spectre.Console;
7
9{
10
15 {
16
17 static readonly Markup TRACE = new Markup("[grey42]TRACE[/]");
18 static readonly Markup INFO = new Markup("[silver]INFO[/]");
19 static readonly Markup WARNING = new Markup("[yellow]WARNING[/]");
20 static readonly Markup ERROR = new Markup("[maroon]ERROR[/]");
21 static readonly Markup FATAL = new Markup("[red]FATAL[/]");
22
23 readonly ConsoleDiagnosticFormatterOptions _options;
24
30 {
31 _options = options ?? throw new ArgumentNullException(nameof(options));
32 }
33
35 public void Write(in DiagnosticEvent @event)
36 {
37 try
38 {
40 }
41 catch (Exception e)
42 {
43 AnsiConsole.WriteException(e);
44 }
45 }
46
53 protected virtual Markup WriteDiagnosticLevel(DiagnosticLevel level)
54 {
55 return level switch
56 {
57 DiagnosticLevel.Trace => TRACE,
58 DiagnosticLevel.Info => INFO,
59 DiagnosticLevel.Warning => WARNING,
60 DiagnosticLevel.Error => ERROR,
61 DiagnosticLevel.Fatal => FATAL,
62 _ => throw new InvalidOperationException(),
63 };
64 }
65
71 protected virtual void WriteDiagnosticCode(int code)
72 {
73 AnsiConsole.Write("IKVM");
74 AnsiConsole.Write($"{code:D4}");
75 }
76
82 protected virtual void WriteDiagnosticMessage(string message, ReadOnlySpan<object?> args)
83 {
84 AnsiConsole.MarkupInterpolated(FormattableStringFactory.Create(message, args.ToArray()));
85 }
86
91 protected virtual void WriteDiagnosticEvent(in DiagnosticEvent @event)
92 {
93 WriteDiagnosticLevel(@event.Diagnostic.Level);
94 AnsiConsole.Write(" ");
95 WriteDiagnosticCode(@event.Diagnostic.Id);
96 AnsiConsole.Write(": ");
97 WriteDiagnosticMessage(@event.Diagnostic.Message, @event.Args);
98 AnsiConsole.WriteLine();
99
100 if (@event.Exception != null)
101 AnsiConsole.WriteException(@event.Exception, ExceptionFormats.Default);
102 }
103
104 }
105
106}
Outputs diagnostics to the console using Spectre.
virtual void WriteDiagnosticCode(int code)
Formats the output text for the given diagnostic code.
ConsoleDiagnosticFormatter(ConsoleDiagnosticFormatterOptions options)
Initializes a new instance.
virtual void WriteDiagnosticMessage(string message, ReadOnlySpan< object?> args)
Formats the specified message text.
virtual void WriteDiagnosticEvent(in DiagnosticEvent @event)
Formats the DiagnosticEvent.
virtual Markup WriteDiagnosticLevel(DiagnosticLevel level)
Returns the output text for the given DiagnosticLevel.
void Write(in DiagnosticEvent @event)
Writes the event.
DiagnosticLevel
Represents the level of security of a diagnostic event.