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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Non-blocking stdout emit for wide events.
//!
//! The default `default_emit` function (generated by [`wide_log!`](crate::wide_log))
//! hands the serialized JSON to this module, which forwards it to a dedicated
//! writer thread. The writer owns a `BufWriter<Stdout>` and flushes after every
//! message so logs land promptly without blocking the calling thread.
//!
//! [`submit`] never blocks: it sends the `String` over an unbounded
//! `std::sync::mpsc` channel to the writer thread. If the channel is closed
//! (e.g. the writer thread has exited during process teardown), the payload
//! is dropped silently and an atomic counter is incremented; the count is
//! exposed via [`dropped_events`].
//!
//! Because the channel `Sender` lives in a process-global `OnceLock`, it is
//! never dropped on normal process exit — the writer thread would be killed
//! by the runtime before draining its buffer. Call [`flush`] at program exit
//! (e.g. at the end of `main`) to block until all pending events have been
//! written and the `BufWriter` flushed. (Forced termination such as `SIGKILL`
//! will still lose any bytes buffered in the writer thread.)
//!
//! [`submit`]: submit
//! [`dropped_events`]: dropped_events
//! [`flush`]: flush
use Write;
use ;
use ;
use OnceLock;
static DROPPED: AtomicU64 = new;
static SENDER: = new;
/// Number of events dropped because the writer thread's channel was closed.
///
/// Incremented on a best-effort basis whenever [`submit`] fails to enqueue a
/// payload. Read this for optional metrics; a non-zero value typically
/// indicates the writer thread has exited (e.g. during process teardown).
/// Enqueue a serialized wide-event JSON line for the writer thread.
///
/// Appends a trailing `'\n'` and sends the payload over an unbounded channel
/// to a single dedicated writer thread that owns a `BufWriter<Stdout>`. This
/// function never blocks on I/O: if the channel is closed, the payload is
/// dropped silently and [`dropped_events`] is incremented.
///
/// The writer thread is started lazily on the first call.
///
/// Call [`flush`] at program exit to guarantee all pending lines are written
/// before the process terminates.
/// Block until all previously-submitted events have been written and the
/// writer's `BufWriter` has been flushed.
///
/// Call this at program exit (e.g. at the end of `main`) to guarantee no
/// pending events are lost when the process terminates. The `OnceLock`-held
/// `Sender` is never dropped on normal exit, so without an explicit `flush`
/// the writer thread would be killed by the runtime before draining its
/// buffer.
///
/// This function may block briefly while the writer thread drains. It is
/// safe to call multiple times.