tracing_core/lib.rs
1//! Core primitives for `tracing`.
2//!
3//! [`tracing`] is a framework for instrumenting Rust programs to collect
4//! structured, event-based diagnostic information. This crate defines the core
5//! primitives of `tracing`.
6//!
7//! This crate provides:
8//!
9//! * [`span::Id`] identifies a span within the execution of a program.
10//!
11//! * [`Event`] represents a single event within a trace.
12//!
13//! * [`Collect`], the trait implemented to collect trace data.
14//!
15//! * [`Metadata`] and [`Callsite`] provide information describing spans and
16//! `Event`s.
17//!
18//! * [`Field`], [`FieldSet`], [`Value`], and [`ValueSet`] represent the
19//! structured data attached to a span.
20//!
21//! * [`Dispatch`] allows spans and events to be dispatched to collectors.
22//!
23//! In addition, it defines the global callsite registry and per-thread current
24//! dispatcher which other components of the tracing system rely on.
25//!
26//! *Compiler support: [requires `rustc` 1.65+][msrv]*
27//!
28//! [msrv]: #supported-rust-versions
29//!
30//! ## Usage
31//!
32//! Application authors will typically not use this crate directly. Instead,
33//! they will use the [`tracing`] crate, which provides a much more
34//! fully-featured API. However, this crate's API will change very infrequently,
35//! so it may be used when dependencies must be very stable.
36//!
37//! Collector implementations may depend on `tracing-core` rather than
38//! `tracing`, as the additional APIs provided by `tracing` are primarily useful
39//! for instrumenting libraries and applications, and are generally not
40//! necessary for collector implementations.
41//!
42//! The [`tokio-rs/tracing`] repository contains less stable crates designed to
43//! be used with the `tracing` ecosystem. It includes a collection of
44//! collector implementations, as well as utility and adapter crates.
45//!
46//! ### `no_std` Support
47//!
48//! In embedded systems and other bare-metal applications, `tracing-core` can be
49//! used without requiring the Rust standard library, although some features are
50//! disabled.
51//!
52//! The dependency on the standard library is controlled by two crate feature
53//! flags, "std", which enables the dependency on [`libstd`], and "alloc", which
54//! enables the dependency on [`liballoc`] (and is enabled by the "std"
55//! feature). These features are enabled by default, but `no_std` users can
56//! disable them using:
57//!
58//! ```toml
59//! # Cargo.toml
60//! tracing-core = { version = "0.2", default-features = false }
61//! ```
62//!
63//! To enable `liballoc` but not `std`, use:
64//!
65//! ```toml
66//! # Cargo.toml
67//! tracing-core = { version = "0.2", default-features = false, features = ["alloc"] }
68//! ```
69//!
70//! When both the "std" and "alloc" feature flags are disabled, `tracing-core`
71//! will not make any dynamic memory allocations at runtime, and does not
72//! require a global memory allocator.
73//!
74//! The "alloc" feature is required to enable the [`Dispatch::new`] function,
75//! which requires dynamic memory allocation to construct a collector trait
76//! object at runtime. When liballoc is disabled, new `Dispatch`s may still be
77//! created from `&'static dyn Collect` references, using
78//! [`Dispatch::from_static`].
79//!
80//! The "std" feature is required to enable the following features:
81//!
82//! * Per-thread scoped trace dispatchers ([`Dispatch::set_default`] and
83//! [`with_default`]. Since setting a thread-local dispatcher inherently
84//! requires a concept of threads to be available, this API is not possible
85//! without the standard library.
86//! * Support for [constructing `Value`s from types implementing
87//! `std::error::Error`][err]. Since the `Error` trait is defined in `std`,
88//! it's not possible to provide this feature without `std`.
89//!
90//! All other features of `tracing-core` should behave identically with and
91//! without `std` and `alloc`.
92//!
93//! [`libstd`]: std
94//! [`Dispatch::new`]: crate::dispatch::Dispatch::new
95//! [`Dispatch::from_static`]: crate::dispatch::Dispatch::from_static
96//! [`Dispatch::set_default`]: crate::dispatch::set_default
97//! [`with_default`]: crate::dispatch::with_default
98//! [err]: crate::field::Visit::record_error
99//!
100//! ### Crate Feature Flags
101//!
102//! The following crate feature flags are available:
103//!
104//! * `std`: Depend on the Rust standard library (enabled by default).
105//! * `alloc`: Depend on [`liballoc`] (enabled by "std").
106//!
107//! [`liballoc`]: alloc
108//!
109//! ## Supported Rust Versions
110//!
111//! Tracing is built against the latest stable release. The minimum supported
112//! version is 1.65. The current Tracing version is not guaranteed to build on
113//! Rust versions earlier than the minimum supported version.
114//!
115//! Tracing follows the same compiler support policies as the rest of the Tokio
116//! project. The current stable Rust compiler and the three most recent minor
117//! versions before it will always be supported. For example, if the current
118//! stable compiler version is 1.69, the minimum supported version will not be
119//! increased past 1.66, three minor versions prior. Increasing the minimum
120//! supported compiler version is not considered a semver breaking change as
121//! long as doing so complies with this policy.
122//!
123//!
124//! [`Event`]: event::Event
125//! [`Collect`]: collect::Collect
126//! [`Metadata`]: metadata::Metadata
127//! [`Callsite`]: callsite::Callsite
128//! [`Field`]: field::Field
129//! [`FieldSet`]: field::FieldSet
130//! [`Value`]: field::Value
131//! [`ValueSet`]: field::ValueSet
132//! [`Dispatch`]: dispatch::Dispatch
133//! [`tokio-rs/tracing`]: https://github.com/tokio-rs/tracing
134//! [`tracing`]: https://crates.io/crates/tracing
135#![doc(
136 html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png",
137 html_favicon_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/favicon.ico",
138 issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/"
139)]
140#![cfg_attr(not(feature = "std"), no_std)]
141#![cfg_attr(docsrs, feature(doc_cfg))]
142#![warn(
143 missing_debug_implementations,
144 missing_docs,
145 rust_2018_idioms,
146 unreachable_pub,
147 bad_style,
148 dead_code,
149 improper_ctypes,
150 non_shorthand_field_patterns,
151 no_mangle_generic_items,
152 overflowing_literals,
153 path_statements,
154 patterns_in_fns_without_body,
155 private_interfaces,
156 private_bounds,
157 unconditional_recursion,
158 unused,
159 unused_allocation,
160 unused_comparisons,
161 unused_parens,
162 while_true
163)]
164
165#[cfg(feature = "alloc")]
166extern crate alloc;
167
168#[doc(hidden)]
169pub mod __macro_support {
170 // Re-export the `core` functions that are used in macros. This allows
171 // a crate to be named `core` and avoid name clashes.
172 // See here: https://github.com/tokio-rs/tracing/issues/2761
173 pub use core::{file, line, module_path, option::Option};
174}
175
176/// Statically constructs an [`Identifier`] for the provided [`Callsite`].
177///
178/// This may be used in contexts, such as static initializers, where the
179/// [`Metadata::callsite`] function is not currently usable.
180///
181/// For example:
182/// ```rust
183/// use tracing_core::{callsite, identify_callsite};
184/// # use tracing_core::{Metadata, collect::Interest};
185/// # fn main() {
186/// pub struct MyCallsite {
187/// // ...
188/// }
189/// impl callsite::Callsite for MyCallsite {
190/// # fn set_interest(&self, _: Interest) { unimplemented!() }
191/// # fn metadata(&self) -> &Metadata { unimplemented!() }
192/// // ...
193/// }
194///
195/// static CALLSITE: MyCallsite = MyCallsite {
196/// // ...
197/// };
198///
199/// static CALLSITE_ID: callsite::Identifier = identify_callsite!(&CALLSITE);
200/// # }
201/// ```
202///
203/// [`Identifier`]: callsite::Identifier
204/// [`Callsite`]: callsite::Callsite
205/// [`Metadata::callsite`]: metadata::Metadata::callsite
206#[macro_export]
207macro_rules! identify_callsite {
208 ($callsite:expr) => {
209 $crate::callsite::Identifier($callsite)
210 };
211}
212
213/// Statically constructs new span [metadata].
214///
215/// /// For example:
216/// ```rust
217/// # use tracing_core::{callsite::Callsite, collect::Interest};
218/// use tracing_core::metadata;
219/// use tracing_core::metadata::{Kind, Level, Metadata};
220/// # fn main() {
221/// # pub struct MyCallsite { }
222/// # impl Callsite for MyCallsite {
223/// # fn set_interest(&self, _: Interest) { unimplemented!() }
224/// # fn metadata(&self) -> &Metadata { unimplemented!() }
225/// # }
226/// #
227/// static FOO_CALLSITE: MyCallsite = MyCallsite {
228/// // ...
229/// };
230///
231/// static FOO_METADATA: Metadata = metadata!{
232/// name: "foo",
233/// target: module_path!(),
234/// level: Level::DEBUG,
235/// fields: &["bar", "baz"],
236/// callsite: &FOO_CALLSITE,
237/// kind: Kind::SPAN,
238/// };
239/// # }
240/// ```
241///
242/// [metadata]: metadata::Metadata
243/// [`Metadata::new`]: metadata::Metadata::new
244#[macro_export]
245macro_rules! metadata {
246 (
247 name: $name:expr,
248 target: $target:expr,
249 level: $level:expr,
250 fields: $fields:expr,
251 callsite: $callsite:expr,
252 kind: $kind:expr
253 ) => {
254 $crate::metadata! {
255 name: $name,
256 target: $target,
257 level: $level,
258 fields: $fields,
259 callsite: $callsite,
260 kind: $kind,
261 }
262 };
263 (
264 name: $name:expr,
265 target: $target:expr,
266 level: $level:expr,
267 fields: $fields:expr,
268 callsite: $callsite:expr,
269 kind: $kind:expr,
270 ) => {
271 $crate::metadata::Metadata::new(
272 $name,
273 $target,
274 $level,
275 $crate::__macro_support::Option::Some($crate::__macro_support::file!()),
276 $crate::__macro_support::Option::Some($crate::__macro_support::line!()),
277 $crate::__macro_support::Option::Some($crate::__macro_support::module_path!()),
278 $crate::field::FieldSet::new($fields, $crate::identify_callsite!($callsite)),
279 $kind,
280 )
281 };
282}
283
284// Facade module: `no_std` uses spinlocks, `std` uses the mutexes in the standard library
285#[cfg(not(feature = "std"))]
286#[doc(hidden)]
287pub type Once = crate::spin::Once<()>;
288
289#[cfg(feature = "std")]
290#[doc(hidden)]
291pub use std::sync::Once;
292
293#[cfg(not(feature = "std"))]
294// Trimmed-down vendored version of spin 0.5.2 (0387621)
295// Required for `Once` in `no_std` builds.
296pub(crate) mod spin;
297
298pub mod callsite;
299pub mod collect;
300pub mod dispatch;
301pub mod event;
302pub mod field;
303pub mod metadata;
304mod parent;
305pub mod span;
306
307#[doc(inline)]
308pub use self::{
309 callsite::Callsite,
310 collect::Collect,
311 dispatch::Dispatch,
312 event::Event,
313 field::Field,
314 metadata::{Level, LevelFilter, Metadata},
315};
316
317pub use self::{collect::Interest, metadata::Kind};
318
319mod sealed {
320 pub trait Sealed {}
321}