Landin library reference discuss source

Shared: available on every enabled target.

Bounded diagnostic storage and streaming diagnostics through a writer capability.

Severity is the atom set warning | error, so a match over it is checked for both.

Log Made by Behaviour
bounded diag.new Stores at most its fixed number of entries, and drops messages exceeding 256 bytes. It counts dropped entries and remembers error diagnostics even when they are dropped.
streaming diag.stream Holds a copy of an erased writer and a file. Writes each note as W: or E:, the decimal byte offset, : and the message, and propagates I/O failure.

Items

public log: type = concept (logger: type)

Record a diagnostic, copying or consuming the message during the call. I/O failure is returned separately from error severity.

    note: (self: ptr mut logger, where: text.position, kind: severity,
           what: []u8) -> none ! io.io_failed

Report whether an error diagnostic has been observed.

    failed: (self: ptr logger) -> (yes: bool)
end log

Diagnostic capability. note records severity, position and message; failed reports whether an error diagnostic has been observed. An I/O failure of note is returned separately from error severity.

public bounded: type (fixed capacity: usize) = bounded_value(capacity)

Fixed-capacity diagnostic log. It stores entries without allocating, counts the messages it drops because it is full or they are too long, and remembers an error even when its entry is dropped.

public entry: type = entry_value

Opaque stored diagnostic containing severity, position and a bounded message copy.

public severity: type = warning | error

Diagnostic severity: warning or error.

public streaming: type = streaming_value

Diagnostic provider holding an erased writer and destination handle. Each note is written as W: or E:, the position's decimal byte offset, : and the message bytes as given, with no terminator; write failures propagate to the caller.

public error: atom

Error severity; recording it marks the log as failed.

public warning: atom

Warning severity; recording it does not by itself mark a log as failed.

message_capacity

value core/diag/diag.ldn:30 ↑ index
public message_capacity: usize = 256

Maximum byte count stored for one bounded diagnostic message. Longer messages are dropped as whole entries.

public dropped: (fixed capacity: usize, value: ptr bounded(capacity))
                -> (count: usize)

Return the number of entries not retained because the log was full or the message exceeded message_capacity.

public get: (fixed capacity: usize, value: ptr bounded(capacity), index: usize)
            -> (result: entry) ! mem.out_of_bounds

Copy a retained entry by index. Reports mem.out_of_bounds when no entry is stored at index.

public length: (fixed capacity: usize, value: ptr bounded(capacity))
               -> (count: usize)

Return the number of retained diagnostic entries. The log is passed by read-only pointer, so the owner of a mutable or an immutable log can ask without copying the bounded note array.

public message: (value: ptr entry) -> (bytes: []u8 from value)

Lend the retained message bytes of the entry value points at, for as long as the entry stays as it is.

message_byte

function core/diag/diag.ldn:136 ↑ index
public message_byte: (value: entry, index: usize)
                     -> (byte: u8) ! mem.out_of_bounds

Read one retained message byte. Reports mem.out_of_bounds when index is at or past the message length.

message_length

function core/diag/diag.ldn:124 ↑ index
public message_length: (value: entry) -> (length: usize)

Return the number of message bytes retained in an entry.

public new: (fixed capacity: usize) -> (result: bounded(capacity))

Create an empty bounded log with zero stored and dropped counts.

position_of

function core/diag/diag.ldn:114 ↑ index
public position_of: (value: entry) -> (where: usize)

Return the caller-supplied source position associated with an entry.

severity_of

function core/diag/diag.ldn:119 ↑ index
public severity_of: (value: entry) -> (kind: severity)

Return the severity recorded in an entry.

public stream: (host: any io.writer, where_to: io.file)
               -> (result: streaming from host)

Create a streaming logger over an erased writer and file identifier. The logger holds a copy of both; keep the writer's handle and its state alive while logging. A streaming log needs only a writer, and an any io.world converts to one (D1952).

Example program

This complete program is one of the compiler's runtime tests. View source.

The program, 20 lines
compiler/tests/fixtures/runtime/library-shared-diag/main.ldn20 lines
import core/diag
import core/mem

exercise: () -> (ok: bool) ! mem.out_of_bounds =
    ok = false
    mut value := diag.new(capacity: 1)
    return when diag.length(addr value) <> 0 or diag.dropped(addr value) <> 0
    _ = diag.get(addr value, usize(0)) else (problem)
        ok = problem == mem.out_of_bounds
        return
    end
end exercise

public main: () -> (code: i32) =
    code = 1
    ok := exercise() else false
    if ok then
        code = 42
    end if
end main