tracing/tracing-futures/examples/spawny-thing.rs
Eliza Weisman 2520f97964
fmt, subscriber: move fmt into subscriber (#311)
## Motivation

As discussed in #308, there are a large number of crates in this
repository, which can be confusing for users and can increase the
maintainance burden for maintainers. Also, the `tracing-fmt` and
`tracing-subscriber` crates both contain filtering implementations with
similar behaviour and APIs, but `tracing-subscriber`'s filter module
offers more advanced features (filtering on field values), and is usable
with any subscriber implementation. Two separate filter implementations
also has the potential to be confusing for users. 

## Solution

This branch moves most of the code from `tracing-fmt` into a module in
`tracing-subscriber`, and changes the `tracing-fmt` builder APIs to use
the `Filter` type in `tracing-subscriber`. The `tracing-subscriber/fmt`
feature flag can be used to disable the formatting subscriber when it is
not used.

The `tracing-fmt` crate has been updated to re-export the APIs from
`tracing-subscriber`, and marked as deprecated. Once we've published a
new version of `tracing-subscriber` with the format APIs, we can publish
a final release of `tracing-fmt` that will update the documentation &
mark all APIs as deprecated, so that users know to move to the
`tracing-subscriber` crate.

Refs: #308

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2019-09-02 08:53:58 -07:00

40 lines
1.2 KiB
Rust

#![deny(rust_2018_idioms)]
use futures::future::{self, Future};
use tracing::{debug, info, span, Level};
use tracing_futures::Instrument;
fn parent_task(subtasks: usize) -> impl Future<Item = (), Error = ()> {
future::lazy(move || {
info!("spawning subtasks...");
let subtasks = (1..=subtasks)
.map(|number| {
debug!(message = "creating subtask;", number);
subtask(number)
})
.collect::<Vec<_>>();
future::join_all(subtasks)
})
.map(|result| {
debug!("all subtasks completed");
let sum: usize = result.into_iter().sum();
info!(sum = sum);
})
.instrument(span!(Level::TRACE, "parent_task", subtasks))
}
fn subtask(number: usize) -> impl Future<Item = usize, Error = ()> {
future::lazy(move || {
info!("polling subtask...");
Ok(number)
})
.instrument(span!(Level::TRACE, "subtask", number))
}
fn main() {
use tracing_subscriber::fmt;
let subscriber = fmt::Subscriber::builder().with_filter("trace").finish();
let _ = tracing::subscriber::set_global_default(subscriber);
tokio::run(parent_task(10));
}