StreamLogSink
Owns a concrete byte writer and emits allocation-free diagnostic framing. Each enabled record is `[level] message\n` in UTF-8. Message bytes, including newlines and zero bytes, are preserved: this is diagnostic output, not an escaped audit format. The writer retains its own blocking and allocation behavior. Synchronize complete records when multiple callers share output; individual short writes do not establish record atomicity. Destruction never flushes. Record failures preserve the exact accepted prefix and do not retry the record automatically, because a retry could duplicate output.
Module std.logging
import std.logging;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
StreamLogSink
pub final class StreamLogSink<W> where W: Movable, WriterOwns a concrete byte writer and emits allocation-free diagnostic framing. Each enabled record is [level] message\n in UTF-8. Message bytes, including newlines and zero bytes, are preserved: this is diagnostic output, not an escaped audit format. The writer retains its own blocking and allocation behavior. Synchronize complete records when multiple callers share output; individual short writes do not establish record atomicity. Destruction never flushes. Record failures preserve the exact accepted prefix and do not retry the record automatically, because a retry could duplicate output. W: Concrete Movable structural Writer, owned without boxing.
Example
module example.stream_log_sink;
from std.logging import StreamLogSink;
from std.io import IoError;
from std.prelude import Result;
struct ShortWriter {
pub Bytes: byte[64];
pub Count: usize;
pub Flushes: usize;
priv Limit: usize;
pub init(Limit: usize) {
this.Bytes = default(byte[64]); this.Count = 0;
this.Flushes = 0; this.Limit = Limit;
}
pub fn Write(Input: readonly byte[]) -> Result<usize, IoError> {
if Input.Length == 0 { return .Ok { Value = 0 }; }
if this.Count >= this.Limit || this.Count >= 64 {
return .Err { Error = .Failure { Reason = .Interrupted, Transferred = 0 } };
}
this.Bytes[this.Count] = Input[0]; this.Count += 1;
return .Ok { Value = 1 };
}
pub fn Flush() -> Result<void, IoError> { this.Flushes += 1; return .Ok; }
}
fn Main() -> i32 {
var Sink = StreamLogSink<ShortWriter>(ShortWriter(64));
if Sink.Write(.Warning, "abc") is .Err { Error = _ } { return 1; }
let Readonly: readonly StreamLogSink<ShortWriter>& = Sink;
if Readonly.Source().Count != 14 || Readonly.Source().Flushes != 0 { return 2; }
if Readonly.Source().Bytes[0] != 0x5B_byte || Readonly.Source().Bytes[9] != 0x20_byte ||
Readonly.Source().Bytes[10] != 0x61_byte || Readonly.Source().Bytes[13] != 0x0A_byte { return 3; }
if Sink.Write(.Off, "ignored") is .Err { Error = _ } { return 4; }
if Sink.Source().Count != 14 { return 5; }
if Sink.Flush() is .Err { Error = _ } { return 6; }
if Sink.Source().Flushes != 1 { return 7; }
var Partial = StreamLogSink<ShortWriter>(ShortWriter(11));
switch Partial.Write(.Warning, "abc") {
case .Err { Error = PatternPayload317 } => {
switch PatternPayload317 {
case .Failure { Reason = PatternPayload318, Transferred = Count } if (PatternPayload318 is .Interrupted) => {
if Count != 11 || Partial.Source().Count != 11 { return 8; }
}
default => return 9;
}
}
default => return 9;
}
Sink.Source().Count = 0;
return 0;
}StreamLogSink.init
pub init(mut Source: W)Adopts a writer without producing output or allocating a format buffer. Source: Owned writer moved into this sink.
StreamLogSink.Source
pub readonly fn Source() -> readonly W&Borrows immutable writer state without copying ownership. Return: Readonly reference valid until sink movement/destruction.
StreamLogSink.Source
pub fn Source() -> mut W&Borrows mutable writer state under caller synchronization. Return: Writable reference valid until sink movement/destruction.
StreamLogSink.Write
pub fn Write(Level: LogLevel, Message: str) -> Result<void, IoError>Emits one complete tagged record through bounded short writes. Level: Record severity; Off performs no output operation. Message: UTF-8 bytes borrowed only through return, preserved verbatim. Return: Complete acceptance or exact record-relative failure prefix. Warning: May block in the supplied writer; performs no hidden flush.
StreamLogSink.Flush
pub fn Flush() -> Result<void, IoError>Explicitly propagates previously accepted downstream bytes. Return: Exact writer flush result; no durability or record atomicity promise.