Eliza Weisman 240d11a7ef subscriber: add minimal #![no_std] support (#1648)
Backports #1648 from `master`.

Depends on #1649

## Motivation

Presently, the `tracing-subscriber` crate requires the Rust standard
library and doesn't build with `#![no_std]` targets. For the most part,
this is fine, as much of `tracing-subscriber` inherently depends on
`std` APIs.

However, `tracing-subscriber` also contains some key abstractions that
are necessary for interoperability: the `Layer` and `LookupSpan`
traits. Since these traits are in `tracing-subscriber`, `no-std` users
cannot currently access them.

Some of the other utilities in this crate, such as the field visitor
combinators, may also be useful for `#![no_std]` projects.

## Solution

This branch adds "std" and "alloc" feature flags to
`tracing-subscriber`, for conditionally enabling `libstd` and
`liballoc`, respectively. The `registry`, `fmt`, `EnvFilter`, and
`reload` APIs all require libstd, and cannot be implemented without it,
but the core `Layer` and `LookupSpan` traits are now available with
`#![no_std]`.

Fixes #999

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2021-10-21 12:39:23 -07:00

40 lines
976 B
Rust

#![cfg(feature = "std")]
mod support;
use self::support::*;
use tracing_subscriber::prelude::*;
#[test]
fn init_ext_works() {
let (subscriber, finished) = subscriber::mock()
.event(
event::mock()
.at_level(tracing::Level::INFO)
.with_target("init_works"),
)
.done()
.run_with_handle();
let _guard = subscriber.set_default();
tracing::info!(target: "init_works", "it worked!");
finished.assert_finished();
}
#[test]
#[cfg(feature = "fmt")]
fn builders_are_init_ext() {
tracing_subscriber::fmt().set_default();
let _ = tracing_subscriber::fmt()
.with_target(false)
.compact()
.try_init();
}
#[test]
#[cfg(all(feature = "fmt", feature = "env-filter"))]
fn layered_is_init_ext() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(tracing_subscriber::EnvFilter::new("foo=info"))
.set_default();
}