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

Clock.pudu

Pudu43 lines1.3 KB

GitHub ↗
1/** @Log.Clock.Seam — where event timestamps come from */2module PuduLangLog.Clock34import Std.Sync as Sync5import Std.Time as Time6import PuduLangLog as Log78/** @Log.Clock.Source — a function answering the current moment */9export type Clock = fn() -> Log.Timestamp1011/// The system clock with the machine's current offset.12export fn system() -> Clock {13  fn() -> Log.Timestamp { Log.Timestamp{millis: Time.toMillis(&Time.currentInstant()), offset: Time.localOffsetMinutes()} }14}1516/// The system clock at offset zero.17export fn utc() -> Clock {18  fn() -> Log.Timestamp { Log.Timestamp{millis: Time.toMillis(&Time.currentInstant()), offset: 0} }19}2021/// A clock that always answers the same moment.22export fn fixed(moment: Log.Timestamp) -> Clock { fn() -> Log.Timestamp { moment } }2324/// A clock that starts at a moment and advances by `step` milliseconds on every reading.25export fn stepping(start: Log.Timestamp, step: Int) -> Clock {26  let next = Sync.cell(start.millis)27  let lock = Sync.mutex()28  fn() -> Log.Timestamp {29    let taken = Sync.withLock(&lock, fn() -> Int {30        let current = match Sync.get(&next) {31          case Ok(value) => value32          case Err(_) => start.millis33        }34        let _stored = Sync.set(&next, current + step)35        current36      })37    match taken {38      case Ok(millis) => Log.Timestamp{millis: millis, offset: start.offset}39      case Err(_) => start40    }41  }42}43