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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*!
Structured diagnostics for Rust applications.
`emit` is a framework for adding diagnostics to your Rust applications with a simple, powerful data model and an expressive syntax inspired by [Message Templates](https://messagetemplates.org).
These are the technical API docs for `emit`. Also see [the guide](https://emit-rs.io) for a complete introduction.
## Getting started
```toml
[dependencies.emit]
version = "1.17.2"
[dependencies.emit_term]
version = "1.17.2"
```
```rust
# mod emit_term { pub fn stdout() -> impl emit::Emitter { emit::emitter::from_fn(|_| {}) } }
# #[cfg(not(all(feature = "implicit_rt", feature = "std")))] fn main() {}
# #[cfg(all(feature = "implicit_rt", feature = "std"))]
fn main() {
let rt = emit::setup()
.emit_to(emit_term::stdout())
.init();
#[emit::span("Greet {user}")]
fn greet(user: &str) {
emit::info!("Hello, {user}!");
}
// Your app code goes here
greet("Rust");
rt.blocking_flush(std::time::Duration::from_secs(5));
}
```
The [`setup()`] function configures `emit` with an [`Emitter`] to write [`Event`]s to. The [`macro@emit`] macro emits an event, capturing any ambient state referred to in its template. The [`macro@span`] macro instruments a function, timing its execution and correlating any other events emitted within it together.
## Macros
`emit` makes extensive use of procedural macros for reducing boilerplate when producing and emitting diagnostics.
Its macros can be grouped based on the kind of diagnostic signal they relate to.
### Logging macros
- [`macro@emit`]: Emit an event.
- [`macro@debug`]: Emit an event at the debug level.
- [`macro@info`]: Emit an event at the info level.
- [`macro@warn`]: Emit an event at the warning level.
- [`macro@error`]: Emit an event at the error level.
### Tracing macros
- [`macro@span`]: Trace the execution of a function.
- [`macro@debug_span`]: Trace the execution of a function at the debug level.
- [`macro@info_span`]: Trace the execution of a function at the info level.
- [`macro@warn_span`]: Trace the execution of a function at the warning level.
- [`macro@error_span`]: Trace the execution of a function at the error level.
- [`macro@new_span`]: Create a span that can be started and completed manually.
- [`macro@new_debug_span`]: Create a span at the debug level that can be started and completed manually.
- [`macro@new_info_span`]: Create a span at the info level that can be started and completed manually.
- [`macro@new_warn_span`]: Create a span at the warning level that can be started and completed manually.
- [`macro@new_error_span`]: Create a span at the error level that can be started and completed manually.
### Metrics macros
- [`macro@sample`]: Emit a metric sample.
- [`macro@count_sample`]: Emit a metric sample with a monotonic count.
- [`macro@sum_sample`]: Emit a metric sample with a non-monotonic sum.
- [`macro@min_sample`]: Emit a metric sample with the minimum value.
- [`macro@max_sample`]: Emit a metric sample with the maximum value.
- [`macro@last_sample`]: Emit a metric sample with the latest value.
- [`macro@metric`]: Construct a metric sample.
- [`macro@count_metric`]: Construct a metric sample with a monotonic count.
- [`macro@sum_metric`]: Construct a metric sample with a non-monotonic sum.
- [`macro@min_metric`]: Construct a metric sample with the minimum value.
- [`macro@max_metric`]: Construct a metric sample with the maximum value.
- [`macro@last_metric`]: Construct a metric sample with the latest value.
## Stable vs nightly toolchains
`emit` works on stable versions of Rust, but can provide more accurate compiler messages on nightly toolchains.
## Crate features
- `std` (default): Enable support for the standard library. Enable capturing properties as errors. Implies `alloc`.
- `alloc`: Enable APIs that require an allocator.
- `implicit_rt` (default): Enable configuring the default shared runtime and calling [`macro@emit`] and [`macro@span`] without needing to specify a runtime manually.
- `implicit_internal_rt` (default): Enable configuring the internal runtime for `emit`'s own diagnostics.
- `sval`: Enable capturing complex properties using `sval`.
- `serde`: Enable capturing complex properties using `serde`.
- `web` (default): Use JavaScript built-in APIs on WebAssembly targets for platform support. This feature is a no-op outside of `wasm32-unknown-unknown`.
- `rand` (default): Use `rand` as the default source of randomness on targets with a default provider. The specific version of `rand` is not guaranteed to remain the same. This feature is a no-op outside of the following targets:
- `linux`
- `windows`
- `macos`
- `ios`
- `android`
- `wasi`
## Troubleshooting
Emitters write their own diagnostics to an alternative `emit` runtime, which you can configure via [`Setup::init_internal`] to debug them:
```
# mod emit_term { pub fn stdout() -> impl emit::runtime::InternalEmitter { emit::runtime::AssertInternal(emit::emitter::from_fn(|_| {})) } }
# #[cfg(not(feature = "std"))] fn main() {}
# #[cfg(feature = "std")]
fn main() {
// Configure the internal runtime before your regular setup
let internal_rt = emit::setup()
.emit_to(emit_term::stdout())
.init_internal();
let rt = emit::setup()
.emit_to(emit::emitter::from_fn(|evt| println!("{evt:#?}")))
.init();
// Your app code goes here
rt.blocking_flush(std::time::Duration::from_secs(5));
// Flush the internal runtime after your regular setup
internal_rt.blocking_flush(std::time::Duration::from_secs(5));
}
```
*/
extern crate alloc;
extern crate core;
/**
Get a [`Path`] of the executing module for use in [`Event::mdl`].
This macro uses the standard `module_path!` macro.
*/
/**
Get a [`Path`] of the package name for use in [`Event::mdl`].
This macro uses the build-time `CARGO_PKG_NAME` environment variable.
*/
pub use *;
pub use *;
pub use ;
pub use ;
/**
Get the shared emitter.
This method will use the [`Emitter`] from [`runtime::shared()`].
Calling `emitter::emit()` is different from `runtime::shared().emit()`:
1. `emitter::emit()` won't apply the filter.
2. `emitter::emit()` won't add a timestamp to events if they don't have one.
3. `emitter::emit()` won't add ambient properties.
*/
/**
Get the shared filter.
This method will use the [`Filter`] from [`runtime::shared()`].
*/
/**
Get the shared clock.
This method will use the [`Clock`] from [`runtime::shared()`].
*/
/**
Get the shared context.
This method will use the [`Ctxt`] from [`runtime::shared()`].
The returned context can be used with [`Frame`]s to manage the ambient state added to diagnostic events.
*/
/**
Get the shared random generator.
This method will use the [`Rng`] from [`runtime::shared()`].
*/
/**
Flush the runtime, ensuring all diagnostic events are fully processed.
This method will use [`runtime::shared()`].
This method forwards to [`Emitter::blocking_flush`], which has details on how the timeout is handled.
*/
/**
Sample a metric source.
This method will use [`runtime::shared()`] as the source, emitting samples as events directly through the runtime.
*/