1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! `tracing_subscriber::fmt` integration that uses simulation time as the
//! timestamp prefix.
//!
//! By default, `tracing_subscriber::fmt::layer()` prints wall-clock time —
//! useless in simulation, where logical time skips idle periods and seconds
//! of simulated activity may take milliseconds of wall time. [`SimTime`]
//! prints "current sim time in milliseconds" reported by a [`Clock`] instead.
//!
//! [`Clock`] is the narrow `Send + Sync` source of sim time the formatter
//! needs. [`super::SimulationLayerHandle`] implements it; users can also
//! implement it for testing stubs or alternate time sources.
//!
//! # Usage
//!
//! ```ignore
//! use moonpool_sim::{SimTime, SimulationLayer};
//! use tracing_subscriber::layer::SubscriberExt;
//!
//! let layer = SimulationLayer::new();
//! let handle = layer.handle();
//!
//! // Important: register `SimulationLayer` BEFORE the fmt layer so its
//! // on_event runs first and updates the layer's sim time before fmt
//! // formats the event.
//! let subscriber = tracing_subscriber::registry()
//! .with(layer)
//! .with(
//! tracing_subscriber::fmt::layer()
//! .with_timer(SimTime::new(handle.clone())),
//! );
//!
//! let _guard = tracing::subscriber::set_default(subscriber);
//! ```
//!
//! Output looks like:
//!
//! ```text
//! sim+ 1.234s INFO moonpool::sim: key="delivery.at_most_once" payload=Replied { seq_id: 7 }
//! ```
use fmt;
use Writer;
use FormatTime;
use SimulationLayerHandle;
/// Narrow `Send + Sync` source of simulation time, suitable for plugging into
/// the [`SimTime`] formatter.
///
/// Distinct from [`moonpool_core::TimeProvider`], which is `Send + Sync` but
/// not dyn-compatible (it has a generic `timeout` method). Anything that can
/// hand out a `u64` representing "current sim time in milliseconds" can
/// implement this — typically the active [`SimulationLayerHandle`], but
/// tests and alternate time sources can implement it too.
/// `FormatTime` implementation that writes simulation time reported by a
/// [`Clock`] instead of wall-clock time.
///
/// Holds only a `Box<dyn Clock>` — the formatter does not see the rest of
/// the layer's API.