IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
TextDiagnosticFormat.cs
Go to the documentation of this file.
1using System;
2using System.Buffers;
3using System.IO;
4using System.Text;
5using System.Threading;
6using System.Threading.Tasks;
7
10
12{
13
18 {
19
30 public static void Write(int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location, TextWriter writer)
31 {
32 var buffer = MemoryPool<byte>.Shared.Rent(65535);
33
34 try
35 {
36 var wrt = new MemoryBufferWriter<byte>(buffer.Memory);
37 Write(id, level, message, args, exception, location, ref wrt, writer.Encoding);
38 writer.Write(writer.Encoding.GetString(buffer.Memory.Span[..wrt.WrittenCount]));
39 }
40 finally
41 {
42 buffer.Dispose();
43 }
44 }
45
57 public static async ValueTask WriteAsync(int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location, TextWriter writer, CancellationToken cancellationToken)
58 {
59 var buffer = MemoryPool<byte>.Shared.Rent(65535);
60
61 try
62 {
63 var wrt = new MemoryBufferWriter<byte>(buffer.Memory);
64 Write(id, level, message, args, exception, location, ref wrt, writer.Encoding);
65 await writer.WriteAsync(writer.Encoding.GetString(buffer.Memory.Span[..wrt.WrittenCount]));
66 }
67 finally
68 {
69 buffer.Dispose();
70 }
71 }
72
85 public static void Write<TWriter>(int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location, ref TWriter writer, Encoding? encoding = null)
86 where TWriter : IBufferWriter<byte>
87 {
88 var encoder = new EncodingByteBufferWriter<TWriter>(encoding ?? Encoding.UTF8, ref writer);
89 WriteDiagnosticEvent(ref encoder, id, level, message, args, exception, location);
90 }
91
103 static void WriteDiagnosticEvent<TWriter>(ref EncodingByteBufferWriter<TWriter> writer, int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location)
104 where TWriter : IBufferWriter<byte>
105 {
106 WriteDiagnosticLocation(ref writer, location);
107 WriteDiagnosticLevel(ref writer, level);
108 writer.Write(" ");
109 WriteDiagnosticCode(ref writer, id);
110 writer.Write(": ");
111 WriteDiagnosticMessage(ref writer, message, args);
112 }
113
119 static void WriteDiagnosticLocation<TWriter>(ref EncodingByteBufferWriter<TWriter> writer, in DiagnosticLocation location)
120 where TWriter : IBufferWriter<byte>
121 {
122 var any = false;
123
124 if (location.Path != null)
125 {
126 writer.Write(location.Path);
127 any = true;
128 }
129
130 if (location.StartLine > 0)
131 {
132 any = true;
133
134 if (location.StartColumn > 0)
135 {
136 if (location.EndColumn > 0)
137 {
138 if (location.EndLine > 0)
139 {
140 writer.Write($"({location.StartLine},{location.StartColumn},{location.EndLine},{location.EndColumn})");
141 }
142 else
143 {
144 writer.Write($"({location.StartLine},{location.StartColumn}-{location.EndColumn})");
145 }
146 }
147 else
148 {
149 writer.Write($"({location.StartLine},{location.StartColumn})");
150 }
151 }
152 else if (location.EndLine > 0)
153 {
154 writer.Write($"({location.StartLine}-{location.EndLine})");
155 }
156 else
157 {
158 writer.Write($"({location.StartLine})");
159 }
160 }
161
162 if (any)
163 writer.Write(" : ");
164 }
165
172 static void WriteDiagnosticLevel<TWriter>(ref EncodingByteBufferWriter<TWriter> writer, DiagnosticLevel level)
173 where TWriter : IBufferWriter<byte>
174 {
175 writer.Write(level switch
176 {
177 DiagnosticLevel.Trace => "trace",
178 DiagnosticLevel.Info => "info",
179 DiagnosticLevel.Warning => "warning",
180 DiagnosticLevel.Error => "error",
181 DiagnosticLevel.Fatal => "fatal",
182 _ => throw new InvalidOperationException(),
183 });
184 }
185
191 static void WriteDiagnosticCode<TWriter>(ref EncodingByteBufferWriter<TWriter> writer, int code)
192 where TWriter : IBufferWriter<byte>
193 {
194 writer.Write("IKVM");
195#if NETFRAMEWORK
196 writer.Write($"{code:D4}");
197#else
198 var buf = (Span<char>)stackalloc char[16];
199 if (code.TryFormat(buf, out var l, "D4") == false)
200 throw new InvalidOperationException();
201
202 writer.Write(buf[..l]);
203#endif
204 }
205
212 static void WriteDiagnosticMessage<TWriter>(ref EncodingByteBufferWriter<TWriter> writer, string message, object?[] args)
213 where TWriter : IBufferWriter<byte>
214 {
215 writer.Write(string.Format(null, message, args));
216 }
217
218 }
219
220}
Implements the IBufferWriter<T> interface around a Memory<T>.
Handles writing diagnostic events to text output.
static void Write< TWriter >(int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location, ref TWriter writer, Encoding? encoding=null)
Writes the given event data to the specified IBufferWriter<byte>.
static async ValueTask WriteAsync(int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location, TextWriter writer, CancellationToken cancellationToken)
Writes the given event data to the specified StringWriter.
static void Write(int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location, TextWriter writer)
Writes the given event data to the specified StringWriter.
readonly struct DiagnosticLocation(string Path, int StartLine, int StartColumn, int EndLine, int EndColumn)
Describes the location of a diagnostic event.
DiagnosticLevel
Represents the level of security of a diagnostic event.
Provides an interface to write text to a IBufferWriter<byte>.