1-- Diagnostics are an ordinary capability. A producer sees only `log`;
2-- callers choose whether notes are retained in bounded storage or written
3-- immediately to any io.writer.
4
5import core/io
6import core/mem
7import core/text
8
9--- Warning severity; recording it does not by itself mark a log as failed.
10public warning: atom
11--- Error severity; recording it marks the log as failed.
12public error: atom
13--- Diagnostic severity: `warning` or `error`.
14public severity: type = warning | error
15
16-- An array bound folds only literals and fixed formals (D136), so the
17-- message array spells `message_capacity` out; keep the two equal.
18entry_value: type = struct
19 where: usize
20 kind: severity
21 what_bytes: [256]u8
22 what_length: usize
23end entry_value
24
25--- Opaque stored diagnostic containing severity, position and a bounded
26--- message copy.
27public entry: type = entry_value
28--- Maximum byte count stored for one bounded diagnostic message. Longer
29--- messages are dropped as whole entries.
30public message_capacity: usize = 256
31
32--- Diagnostic capability. `note` records severity, position and message;
33--- `failed` reports whether an error diagnostic has been observed. An I/O
34--- failure of `note` is returned separately from error severity.
35public log: type = concept (logger: type)
36 --- Record a diagnostic, copying or consuming the message during the call.
37 --- I/O failure is returned separately from error severity.
38 note: (self: ptr mut logger, where: text.position, kind: severity,
39 what: []u8) -> none ! io.io_failed
40 --- Report whether an error diagnostic has been observed.
41 failed: (self: ptr logger) -> (yes: bool)
42end log
43
44bounded_value: type (fixed capacity: usize) = struct
45 notes: [capacity]entry
46 stored: usize
47 dropped: usize
48 errors: usize
49end bounded_value
50
51--- Fixed-capacity diagnostic log. It stores entries without allocating,
52--- counts the messages it drops because it is full or they are too long,
53--- and remembers an error even when its entry is dropped.
54public bounded: type (fixed capacity: usize) = bounded_value(capacity)
55
56bounded_note: (fixed capacity: usize, self: ptr mut bounded(capacity),
57 where: text.position, kind: severity, what: []u8)
58 -> none ! io.io_failed =
59 if self.val.stored < capacity and lenof what <= message_capacity then
60 -- An erased producer may pass frame bytes. Retain a copy rather
61 -- than an address into the producer's storage.
62 -- A severity has no zero image, so the entry is built whole.
63 mut saved: entry = (where: text.offset(where), kind: kind,
64 what_bytes: zeroed, what_length: lenof what)
65 mut index: usize = 0
66 while index < lenof what do
67 saved.what_bytes[index] = what[index]
68 inc index
69 end while
70 self.val.notes[self.val.stored] = saved
71 inc self.val.stored
72 else
73 inc self.val.dropped
74 end if
75 if kind == error then
76 inc self.val.errors
77 end if
78end bounded_note
79
80bounded_failed: (fixed capacity: usize, self: ptr bounded(capacity))
81 -> (yes: bool) =
82 yes = self.val.errors > 0
83end bounded_failed
84
85--- Create an empty bounded log with zero stored and dropped counts.
86public new: (fixed capacity: usize) -> (result: bounded(capacity)) =
87 result = (notes: uninit, stored: 0, dropped: 0, errors: 0)
88end new
89
90--- Return the number of retained diagnostic entries. The log is passed by
91--- read-only pointer, so the owner of a mutable or an immutable log can ask
92--- without copying the bounded note array.
93public length: (fixed capacity: usize, value: ptr bounded(capacity))
94 -> (count: usize) =
95 count = value.val.stored
96end length
97
98--- Return the number of entries not retained because the log was full or the
99--- message exceeded `message_capacity`.
100public dropped: (fixed capacity: usize, value: ptr bounded(capacity))
101 -> (count: usize) =
102 count = value.val.dropped
103end dropped
104
105--- Copy a retained entry by index. Reports `mem.out_of_bounds` when no
106--- entry is stored at `index`.
107public get: (fixed capacity: usize, value: ptr bounded(capacity), index: usize)
108 -> (result: entry) ! mem.out_of_bounds =
109 fail mem.out_of_bounds when index >= value.val.stored
110 result = value.val.notes[index]
111end get
112
113--- Return the caller-supplied source position associated with an entry.
114public position_of: (value: entry) -> (where: usize) =
115 where = value.where
116end position_of
117
118--- Return the severity recorded in an entry.
119public severity_of: (value: entry) -> (kind: severity) =
120 kind = value.kind
121end severity_of
122
123--- Return the number of message bytes retained in an entry.
124public message_length: (value: entry) -> (length: usize) =
125 length = value.what_length
126end message_length
127
128--- Lend the retained message bytes of the entry `value` points at, for as
129--- long as the entry stays as it is.
130public message: (value: ptr entry) -> (bytes: []u8 from value) =
131 bytes = value.val.what_bytes[0..<value.val.what_length]
132end message
133
134--- Read one retained message byte. Reports `mem.out_of_bounds` when `index`
135--- is at or past the message length.
136public message_byte: (value: entry, index: usize)
137 -> (byte: u8) ! mem.out_of_bounds =
138 fail mem.out_of_bounds when index >= value.what_length
139 byte = value.what_bytes[index]
140end message_byte
141
142(fixed capacity: usize) bounded_value(capacity) is log
143 (note: bounded_note, failed: bounded_failed)
144
145streaming_value: type = struct
146 host: any io.writer
147 where_to: io.file
148 errors: usize
149end streaming_value
150
151--- Diagnostic provider holding an erased writer and destination handle. Each
152--- note is written as `W:` or `E:`, the position's decimal byte offset, `:`
153--- and the message bytes as given, with no terminator; write failures
154--- propagate to the caller.
155public streaming: type = streaming_value
156
157warning_prefix: [2]u8 = [u8('W'), u8(':')]
158error_prefix: [2]u8 = [u8('E'), u8(':')]
159separator: [1]u8 = [u8(':')]
160
161write_position: (host: any io.writer, where_to: io.file, value: usize)
162 -> none ! io.io_failed =
163 -- Twenty bytes hold every decimal digit of a 64-bit usize, so the
164 -- writer has room and the provider receives one position slice.
165 mut digits: [20]u8 = zeroed
166 mut line := text.over(digits[0..<20])
167 text.append_usize(line, value) else (problem)
168 _ = problem
169 compiler.unreachable()
170 end
171 try host.write(where_to, text.used(line))
172end write_position
173
174--- Create a streaming logger over an erased writer and file identifier. The
175--- logger holds a copy of both; keep the writer's handle and its state alive
176--- while logging. A streaming log needs only a writer, and an
177--- `any io.world` converts to one (D1952).
178public stream: (host: any io.writer, where_to: io.file)
179 -> (result: streaming from host) =
180 result = streaming(host: host, where_to: where_to, errors: 0)
181end stream
182
183stream_note: (self: ptr mut streaming, where: text.position,
184 kind: severity, what: []u8) -> none ! io.io_failed =
185 host: any io.writer = self.val.host
186 where_to: io.file = self.val.where_to
187 if kind == error then
188 inc self.val.errors
189 try host.write(where_to, error_prefix[0..<2])
190 else
191 try host.write(where_to, warning_prefix[0..<2])
192 end if
193
194 try write_position(host, where_to, text.offset(where))
195 try host.write(where_to, separator[0..<1])
196 try host.write(where_to, what)
197end stream_note
198
199stream_failed: (self: ptr streaming) -> (yes: bool) =
200 yes = self.val.errors > 0
201end stream_failed
202
203streaming_value is log (note: stream_note, failed: stream_failed)