IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
JsonDiagnosticFormat.cs
Go to the documentation of this file.
1using System;
2using System.Buffers;
3using System.IO;
4using System.Text;
5using System.Text.Json;
6using System.Threading;
7using System.Threading.Tasks;
8
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
56 public static async ValueTask WriteAsync(int id, DiagnosticLevel level, string message, object?[] args, Exception? exception, DiagnosticLocation location, TextWriter writer, CancellationToken cancellationToken)
57 {
58 var buffer = MemoryPool<byte>.Shared.Rent(65535);
59
60 try
61 {
62 var wrt = new MemoryBufferWriter<byte>(buffer.Memory);
63 Write(id, level, message, args, exception, location, ref wrt, writer.Encoding);
64 await writer.WriteAsync(writer.Encoding.GetString(buffer.Memory.Span[..wrt.WrittenCount]));
65 }
66 finally
67 {
68 buffer.Dispose();
69 }
70 }
71
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 encoding ??= Encoding.UTF8;
89
90 var mem = default(IMemoryOwner<byte>);
91 var buf = (IBufferWriter<byte>)writer;
92
93 try
94 {
95 // if we're not writing UTF8, stage to a temporary buffer
96 if (encoding is not UTF8Encoding)
97 {
98 mem = MemoryPool<byte>.Shared.Rent(65535);
99 buf = new MemoryBufferWriter<byte>(mem.Memory);
100 }
101
102 using var json = new Utf8JsonWriter(buf, new JsonWriterOptions() { Indented = false });
103
104 try
105 {
106 json.WriteStartObject();
107 json.WriteNumber("id", id);
108 json.WriteString("level", level switch
109 {
110 DiagnosticLevel.Trace => "trace",
111 DiagnosticLevel.Info => "info",
112 DiagnosticLevel.Warning => "warning",
113 DiagnosticLevel.Error => "error",
114 DiagnosticLevel.Fatal => "fatal",
115 _ => throw new NotImplementedException(),
116 });
117
118 json.WriteString("message", message);
119 json.WriteStartArray("args");
120
121 // encode each argument
122 foreach (var arg in args)
123 JsonSerializer.Serialize(json, arg, arg?.GetType() ?? typeof(object), JsonSerializerOptions.Default);
124
125 json.WriteEndArray();
126
127 if (location.Path != null ||
128 location.StartLine != 0 ||
129 location.StartColumn != 0 ||
130 location.EndLine != 0 ||
131 location.EndColumn != 0)
132 {
133 json.WriteStartObject("location");
134 json.WriteString("path", location.Path);
135 json.WriteStartArray("position");
136 json.WriteNumberValue(location.StartLine);
137 json.WriteNumberValue(location.StartColumn);
138 json.WriteNumberValue(location.EndLine);
139 json.WriteNumberValue(location.EndColumn);
140 json.WriteEndArray();
141
142 json.WriteEndObject();
143 }
144
145 json.WriteEndObject();
146
147 // ensure the JSON is flushed out
148 json.Flush();
149
150 // destination encoding is not UTF8, so convert
151 if (mem != null)
152 {
153 // decode into chars
154 var len = Encoding.UTF8.GetCharCount(mem.Memory.Span[..(int)json.BytesCommitted]);
155 using var tmp = MemoryPool<char>.Shared.Rent(len);
156 len = Encoding.UTF8.GetChars(mem.Memory.Span[..len], tmp.Memory.Span);
157
158 // reencode into target encoding
159 encoding.GetBytes(tmp.Memory.Span[..len], writer);
160 }
161 }
162 catch (ArgumentOutOfRangeException)
163 {
164 // ignore large message
165 }
166 }
167 finally
168 {
169 // dispose of the temporary buffer for non-UTF8
170 mem?.Dispose();
171 }
172 }
173
174 }
175
176}
Implements the IBufferWriter<T> interface around a Memory<T>.
Formats diagnostic events into a stream of JSON objects.
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.
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 TWriter .
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.
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.