Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
0.2.0 Preview 1 · Frozen documentation

AsyncStreamLogSink

Owns an async byte writer and emits sequential diagnostic framing. No intermediate formatting buffer is allocated. Managed task/frame allocation follows the existing runtime contract; message/source loans survive suspension. 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.

Sn is under heavy development. Reference extracted from this release’s bundled standard library. Public API examples are not an exhaustive compatibility test. Read the matching release guide for tested workflows and limitations. Read the status →

Module std.logging

import std.logging;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

AsyncStreamLogSink

pub final class AsyncStreamLogSink<W> where W: Movable, AsyncWriter

Owns an async byte writer and emits sequential diagnostic framing. No intermediate formatting buffer is allocated. Managed task/frame allocation follows the existing runtime contract; message/source loans survive suspension. 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 AsyncWriter, owned without boxing.

Example

module example.async_stream_log_sink;
from std.logging import AsyncStreamLogSink;
from std.io import IoError;
from std.prelude import Result;
from std.task import Task, CancellationToken;
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 async fn WriteAsync(Input: readonly byte[], mut Token: CancellationToken) -> Task<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 async fn FlushAsync(mut Token: CancellationToken) -> Task<Result<void, IoError>> { this.Flushes += 1; return .Ok; }
}
async fn Main() -> Task<i32> {
    var Sink = AsyncStreamLogSink<ShortWriter>(ShortWriter(64));
    if await Sink.WriteAsync(.Warning, "abc") is .Err { Error = _ } { return 1; }
    let Readonly: readonly AsyncStreamLogSink<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 await Sink.WriteAsync(.Off, "ignored") is .Err { Error = _ } { return 4; }
    if Sink.Source().Count != 14 { return 5; }
    if await Sink.FlushAsync() is .Err { Error = _ } { return 6; }
    if Sink.Source().Flushes != 1 { return 7; }
    var Partial = AsyncStreamLogSink<ShortWriter>(ShortWriter(11));
    switch await Partial.WriteAsync(.Warning, "abc") {
        case .Err { Error = PatternPayload319 } => {
            switch PatternPayload319 {
                case .Failure { Reason = PatternPayload320, Transferred = Count } if (PatternPayload320 is .Interrupted) => {
                    if Count != 11 || Partial.Source().Count != 11 { return 8; }
                }
                default => return 9;
            }
        }
        default => return 9;
    }
    Sink.Source().Count = 0;
    return 0;
}

AsyncStreamLogSink.init

pub init(mut Source: W)

Adopts a writer without producing output or allocating a format buffer. Source: Owned writer moved into this sink.

AsyncStreamLogSink.Source

pub readonly fn Source() -> readonly W&

Borrows immutable writer state without copying ownership. Return: Readonly reference valid until sink movement/destruction.

AsyncStreamLogSink.Source

pub fn Source() -> mut W&

Borrows mutable writer state under caller synchronization. Return: Writable reference valid until sink movement/destruction.

AsyncStreamLogSink.WriteAsync

pub async fn WriteAsync(Level: LogLevel, Message: str, mut Token: CancellationToken) -> Task<Result<void, IoError>>

Emits one complete tagged record through bounded short writes. Level: Record severity; Off performs no output operation. Message: UTF-8 backing retained through terminal completion, preserved verbatim. Token: Owned cooperative cancellation observation, inert by default. Return: Task yielding complete acceptance or exact record-relative failure prefix. Warning: Retains writable source and message backing until terminal cleanup; no hidden flush.

AsyncStreamLogSink.WriteAsync

pub fn WriteAsync(mut Level: LogLevel, Message: str) -> Task<Result<void, IoError>>

Supplies the omitted arguments to WriteAsync. Note: Defaults: Token = CancellationToken(). Level: Same input, ownership and lifetime contract as the full operation. Message: Same input, ownership and lifetime contract as the full operation. Return: Exact full-operation result; no additional async frame is created.

AsyncStreamLogSink.FlushAsync

pub async fn FlushAsync(mut Token: CancellationToken) -> Task<Result<void, IoError>>

Explicitly propagates previously accepted downstream bytes. Token: Owned cooperative cancellation observation, inert by default. Return: Task yielding the exact writer flush result; no durability or record atomicity promise.

AsyncStreamLogSink.FlushAsync

pub fn FlushAsync() -> Task<Result<void, IoError>>

Supplies the omitted arguments to FlushAsync. Note: Defaults: Token = CancellationToken(). Return: Exact full-operation result; no additional async frame is created.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library