-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
213 lines (167 loc) · 5.57 KB
/
lib.rs
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
/*!
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 = "0.11.0-alpha.21"
[dependencies.emit_term]
version = "0.11.0-alpha.21"
```
```rust
# mod emit_term { pub fn stdout() -> impl emit::Emitter { emit::emitter::from_fn(|_| {}) } }
# #[cfg(not(feature = "std"))] fn main() {}
# #[cfg(feature = "std")]
fn main() {
let rt = emit::setup()
.emit_to(emit_term::stdout())
.init();
// Your app code goes here
greet("Rust");
rt.blocking_flush(std::time::Duration::from_secs(5));
}
#[emit::span("Greet {user}")]
fn greet(user: &str) {
emit::info!("Hello, {user}!");
}
```
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.
## 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`.
## 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));
}
```
*/
#![doc(html_logo_url = "https://raw.githubusercontent.com/emit-rs/emit/main/asset/logo.svg")]
#![deny(missing_docs)]
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#[cfg(feature = "alloc")]
extern crate alloc;
extern crate core;
/**
Get a [`Path`] of the executing module for use in [`Event::mdl`].
This defers uses the standard `module_path` macro.
*/
#[macro_export]
macro_rules! mdl {
() => {
$crate::Path::new_unchecked($crate::__private::core::module_path!())
};
}
/**
Get a [`Path`] of the package name for use in [`Event::mdl`].
This macro uses the build-time `CARGO_PKG_NAME` environment variable.
*/
#[macro_export]
macro_rules! pkg {
() => {
$crate::Path::new_unchecked($crate::__private::core::env!("CARGO_PKG_NAME"))
};
}
#[doc(inline)]
pub use emit_macros::*;
#[doc(inline)]
pub use emit_core::*;
pub mod frame;
pub mod kind;
pub mod level;
pub mod metric;
pub mod platform;
pub mod span;
pub mod timer;
#[cfg(feature = "std")]
pub mod err;
pub use self::{
clock::Clock,
ctxt::Ctxt,
emitter::Emitter,
empty::Empty,
event::Event,
extent::Extent,
filter::Filter,
frame::Frame,
kind::Kind,
level::Level,
metric::Metric,
path::Path,
props::Props,
rng::Rng,
span::{Span, SpanCtxt, SpanId, TraceId},
str::Str,
template::Template,
timer::Timer,
timestamp::Timestamp,
value::Value,
};
mod macro_hooks;
#[cfg(feature = "std")]
pub mod setup;
#[cfg(feature = "std")]
pub use setup::{setup, Setup};
/**
Get the shared clock.
This method will use the [`Clock`] from [`runtime::shared()`].
*/
#[cfg(feature = "implicit_rt")]
pub fn clock() -> runtime::AmbientClock<'static> {
*runtime::shared().clock()
}
/**
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.
*/
#[cfg(feature = "implicit_rt")]
pub fn ctxt() -> runtime::AmbientCtxt<'static> {
*runtime::shared().ctxt()
}
/**
Get the shared random generator.
This method will use the [`Rng`] from [`runtime::shared()`].
*/
#[cfg(feature = "implicit_rt")]
pub fn rng() -> runtime::AmbientRng<'static> {
*runtime::shared().rng()
}
/**
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.
*/
#[cfg(feature = "implicit_rt")]
pub fn blocking_flush(timeout: core::time::Duration) -> bool {
runtime::shared().blocking_flush(timeout)
}
#[doc(hidden)]
pub mod __private {
pub extern crate core;
pub use crate::macro_hooks::*;
}
mod internal {
pub struct Erased<T>(pub(crate) T);
}