Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-log

Structured event logging for Pudu: message templates, enrichment, filtering, formatting, and sinks

0.1.0Apache-2.01

InstallClose

Diagnostic.pudu

Pudu52 lines1.9 KB

GitHub ↗
1/** @Log.Web.Diagnostic.Collector — properties gathered while a request runs */2module PuduLangLog.Web.Diagnostic34import Std.Sync as Sync5import PuduLangLog as Log67/** @Log.Web.Diagnostic.Entry — a value set by a handler and how to capture it */8export type Entry = { name: Str, value: Log.Value, destructure: Bool }910/** @Log.Web.Diagnostic.Collector — what a handler added to its request's completion event */11export type Collector = { entries: Sync.Cell[Array[Entry]], failure: Sync.Cell[Option[Log.Failure]] }1213/// An empty collector.14export fn create() -> Collector {15  let none: Array[Entry] = []16  let noFailure: Option[Log.Failure] = None17  Collector{entries: Sync.cell(none), failure: Sync.cell(noFailure)}18}1920/// Sets a property of the completion event, replacing an earlier value of the same name.21export fn set(collector: &Collector, name: Str, held: Log.Value) -> () { store(collector, Entry{name: name, value: held, destructure: false}) }2223/// Sets a property of the completion event that keeps its structure.24export fn setDestructured(collector: &Collector, name: Str, held: Log.Value) -> () { store(collector, Entry{name: name, value: held, destructure: true}) }2526/// Stores an entry in place of any of the same name.27fn store(collector: &Collector, entry: Entry) -> () {28  let current = entries(collector).filter(|existing: Entry| existing.name != entry.name)29  let _stored = Sync.set(&collector.entries, current.push(entry))30}3132/// Attaches a failure to the completion event.33export fn setFailure(collector: &Collector, problem: Log.Failure) -> () {34  let _stored = Sync.set(&collector.failure, Some(problem))35}3637/// The entries set, oldest first.38export fn entries(collector: &Collector) -> Array[Entry] {39  match Sync.get(&collector.entries) {40    case Ok(held) => held41    case Err(_) => []42  }43}4445/// The failure set, if any.46export fn failure(collector: &Collector) -> Option[Log.Failure] {47  match Sync.get(&collector.failure) {48    case Ok(held) => held49    case Err(_) => None50  }51}52