mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
## Motivation I was just trying to use tokio-trace for a greenfield project, but I was frustrated to discover that I couldn't really use it easily. I was using the [`runtime`](https://docs.rs/runtime/0.3.0-alpha.4/runtime/) crate, which transparently spawns a thread pool executor for futures. In that thread pool, there's no way to set a tokio-trace subscriber for the duration of each thread, since you don't control the thread initialization. You *might* be able to wrap every future you spawn with a subscriber call, but that's a lot of work. I was also confused because the documentation said that setting a subscriber in the main thread would use that subscriber for the rest of the program. That isn't the case, though -- the subscriber will be used only on the main thread, and not on worker threads, etc. ## Solution I added a function `set_global_default`, which works similarly to the `log` crate: ```rust tokio_trace::subscriber::set_global_default(FooSubscriber::new()); ``` The global subscriber (actually a global `Dispatch`) is a `static mut` protected by an atomic; implementation is copied from the `log` crate. It is used as a fallback if a thread has no `Dispatch` currently set. This is extremely simple to use, and doesn't break any existing functionality. Performance-wise, thread-local `Dispatch` lookup goes from ~4.5ns to ~5ns, according to the benchmarks. So, barely any runtime overhead. (Presumably there's a little compile-time overhead but idk how to measure that.) Since the atomic guard is only ever written once, it will be shared among a CPU's cores and read very cheaply. I added some docs to partially address #1151. I also switched the tokio-trace benchmarks to criterion because the nightly benchmarks weren't compiling (missing `dyn` flags?)
55 lines
1.4 KiB
TOML
55 lines
1.4 KiB
TOML
[package]
|
|
name = "tokio-trace"
|
|
# When releasing to crates.io:
|
|
# - Remove path dependencies
|
|
# - Update html_root_url.
|
|
# - Update doc url
|
|
# - Cargo.toml
|
|
# - README.md
|
|
# - Update CHANGELOG.md.
|
|
# - Create "v0.1.x" git tag
|
|
version = "0.1.0"
|
|
authors = ["Tokio Contributors <[email protected]>"]
|
|
license = "MIT"
|
|
readme = "README.md"
|
|
repository = "https://github.com/tokio-rs/tokio"
|
|
homepage = "https://tokio.rs"
|
|
documentation = "https://docs.rs/tokio-trace/0.1.0/tokio_trace"
|
|
description = """
|
|
A scoped, structured logging and diagnostics system.
|
|
"""
|
|
categories = ["development-tools::debugging", "asynchronous"]
|
|
keywords = ["logging", "tracing"]
|
|
|
|
[dependencies]
|
|
tokio-trace-core = { version = "0.2", path = "tokio-trace-core" }
|
|
log = { version = "0.4", optional = true }
|
|
cfg-if = "0.1.7"
|
|
|
|
[dev-dependencies]
|
|
ansi_term = "0.11"
|
|
humantime = "1.1.1"
|
|
futures = "0.1"
|
|
log = "0.4"
|
|
# These are used for the "basic" example from the tokio-trace-prototype repo,
|
|
# which is currently not included as it used the `tokio-trace-log` crate, and
|
|
# that crate is currently unstable.
|
|
# env_logger = "0.5"
|
|
# tokio-trace-log = { path = "../tokio-trace-log" }
|
|
|
|
[features]
|
|
max_level_off = []
|
|
max_level_error = []
|
|
max_level_warn = []
|
|
max_level_info = []
|
|
max_level_debug = []
|
|
max_level_trace = []
|
|
|
|
release_max_level_off = []
|
|
release_max_level_error = []
|
|
release_max_level_warn = []
|
|
release_max_level_info = []
|
|
release_max_level_debug = []
|
|
release_max_level_trace = []
|
|
|