IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IKVM.CoreLib.Diagnostics.JsonDiagnosticFormat Class Reference

Formats diagnostic events into a stream of JSON objects. More...

Static Public Member Functions

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 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< 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 .
 

Detailed Description

Formats diagnostic events into a stream of JSON objects.

Definition at line 17 of file JsonDiagnosticFormat.cs.

Member Function Documentation

◆ Write()

static void IKVM.CoreLib.Diagnostics.JsonDiagnosticFormat.Write ( int id,
DiagnosticLevel level,
string message,
object?[] args,
Exception? exception,
DiagnosticLocation location,
TextWriter writer )
static

Writes the given event data to the specified StringWriter.

Parameters
id
level
message
args
exception
location
writer

Definition at line 30 of file JsonDiagnosticFormat.cs.

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 }
Implements the IBufferWriter<T> interface around a Memory<T>.
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.

◆ Write< TWriter >()

static void IKVM.CoreLib.Diagnostics.JsonDiagnosticFormat.Write< TWriter > ( int id,
DiagnosticLevel level,
string message,
object?[] args,
Exception? exception,
DiagnosticLocation location,
ref TWriter writer,
Encoding? encoding = null )
static

Writes the given event data to the specified TWriter .

Template Parameters
TWriter
Parameters
id
level
message
args
exception
location
writer
encoding
Exceptions
NotImplementedException
Type Constraints
TWriter :IBufferWriter<byte> 

Definition at line 85 of file JsonDiagnosticFormat.cs.

86 : 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 }

◆ WriteAsync()

static async ValueTask IKVM.CoreLib.Diagnostics.JsonDiagnosticFormat.WriteAsync ( int id,
DiagnosticLevel level,
string message,
object?[] args,
Exception? exception,
DiagnosticLocation location,
TextWriter writer,
CancellationToken cancellationToken )
static

Writes the given event data to the specified StringWriter.

Parameters
id
level
message
args
exception
location
writer

Definition at line 56 of file JsonDiagnosticFormat.cs.

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 }

The documentation for this class was generated from the following file: