diff --git a/examples/Cargo.toml b/examples/Cargo.toml index d3de6c941c..cb2f199119 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -30,7 +30,7 @@ futures = "0.3" tokio = { version = "0.2.12", features = ["full"] } # env-logger example -env_logger = "0.7" +env_logger = "0.8" # tower examples tower = "0.3" diff --git a/tracing-core/src/metadata.rs b/tracing-core/src/metadata.rs index 816c9cd28d..a8de8bfa1e 100644 --- a/tracing-core/src/metadata.rs +++ b/tracing-core/src/metadata.rs @@ -1,5 +1,6 @@ //! Metadata describing trace data. use super::{callsite, field}; +#[cfg(feature = "alloc")] use alloc::borrow::Cow; use core::{ cmp, fmt, @@ -61,22 +62,34 @@ use core::{ /// [callsite identifier]: super::callsite::Identifier pub struct Metadata<'a> { /// The name of the span described by this metadata. + #[cfg(feature = "alloc")] name: Cow<'a, str>, + #[cfg(not(feature = "alloc"))] + name: &'a str, /// The part of the system that the span that this metadata describes /// occurred in. + #[cfg(feature = "alloc")] target: Cow<'a, str>, + #[cfg(not(feature = "alloc"))] + target: &'a str, /// The level of verbosity of the described span. level: Level, /// The name of the Rust module where the span occurred, or `None` if this /// could not be determined. + #[cfg(feature = "alloc")] module_path: Option>, + #[cfg(not(feature = "alloc"))] + module_path: Option<&'a str>, /// The name of the source code file where the span occurred, or `None` if /// this could not be determined. + #[cfg(feature = "alloc")] file: Option>, + #[cfg(not(feature = "alloc"))] + file: Option<&'a str>, /// The line number in the source code file where the span occurred, or /// `None` if this could not be determined. @@ -132,6 +145,7 @@ impl<'a> Metadata<'a> { fields: field::FieldSet, kind: Kind, ) -> Self { + #[cfg(feature = "alloc")] let file = { if let Some(file) = file { Some(Cow::Borrowed(file)) @@ -139,6 +153,7 @@ impl<'a> Metadata<'a> { None } }; + #[cfg(feature = "alloc")] let module_path = { if let Some(module_path) = module_path { Some(Cow::Borrowed(module_path)) @@ -146,9 +161,16 @@ impl<'a> Metadata<'a> { None } }; + Metadata { + #[cfg(feature = "alloc")] name: Cow::Borrowed(name), + #[cfg(not(feature = "alloc"))] + name, + #[cfg(feature = "alloc")] target: Cow::Borrowed(target), + #[cfg(not(feature = "alloc"))] + target, level, module_path, file, @@ -158,6 +180,32 @@ impl<'a> Metadata<'a> { } } + /// Construct new metadata for a span or event, with a name, target, level, field + /// names, and optional source code location using dynamically allocated data. + #[cfg(feature = "alloc")] + #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] + pub fn from_cow( + name: impl Into>, + target: impl Into>, + level: Level, + file: Option>>, + line: Option, + module_path: Option>>, + fields: field::FieldSet, + kind: Kind, + ) -> Self { + Metadata { + name: name.into(), + target: target.into(), + level, + module_path: module_path.map(Into::into), + file: file.map(Into::into), + line, + fields, + kind, + } + } + /// Returns the names of the fields on the described span or event. pub fn fields(&self) -> &field::FieldSet { &self.fields @@ -185,13 +233,13 @@ impl<'a> Metadata<'a> { /// Returns the path to the Rust module where the span occurred, or /// `None` if the module path is unknown. pub fn module_path(&'a self) -> Option<&'a str> { - self.module_path.as_ref().map(|p| p.as_ref() ) + self.module_path.as_ref().map(|p| p.as_ref()) } /// Returns the name of the source code file where the span /// occurred, or `None` if the file is unknown pub fn file(&'a self) -> Option<&'a str> { - self.file.as_ref().map(|f| f.as_ref() ) + self.file.as_ref().map(|f| f.as_ref()) } /// Returns the line number in the source code file where the span @@ -814,6 +862,12 @@ impl PartialOrd for LevelFilter { #[cfg(test)] mod tests { use super::*; + #[cfg(feature = "alloc")] + use crate::{ + callsite::{Callsite, Identifier}, + field::FieldSet, + Interest, + }; use core::mem; #[test] @@ -878,4 +932,31 @@ mod tests { assert_eq!(expected, repr, "repr changed for {:?}", filter) } } + + #[cfg(feature = "alloc")] + #[test] + fn create_metadata_from_dynamic_data() { + struct TestCallsite; + static CS1: TestCallsite = TestCallsite; + + impl Callsite for TestCallsite { + fn set_interest(&self, _interest: Interest) {} + fn metadata(&self) -> &Metadata<'_> { + unimplemented!("not needed for this test") + } + } + let callsite_id = Identifier(&CS1); + let field_set = FieldSet::new(&["one", "fine", "day"], callsite_id); + + let _metadata = Metadata::from_cow( + "a name".to_string(), + "a target", + Level::TRACE, + Some("a file".to_string()), + None, + None::>, + field_set, + Kind::EVENT, + ); + } } diff --git a/tracing-futures/Cargo.toml b/tracing-futures/Cargo.toml index 4f0c603e49..edacd59468 100644 --- a/tracing-futures/Cargo.toml +++ b/tracing-futures/Cargo.toml @@ -28,7 +28,7 @@ std = ["tracing/std"] futures_01 = { package = "futures", version = "0.1", optional = true } futures = { version = "0.3.0", optional = true } futures-task = { version = "0.3", optional = true } -pin-project = { version = "0.4", optional = true } +pin-project = { version = "1.0", optional = true } tracing = { path = "../tracing", version = "0.2", default-features = false } tokio-executor = { version = "0.1", optional = true } tokio = { version = "0.1", optional = true } diff --git a/tracing-log/Cargo.toml b/tracing-log/Cargo.toml index b109c114ad..e7c7078db9 100644 --- a/tracing-log/Cargo.toml +++ b/tracing-log/Cargo.toml @@ -17,16 +17,15 @@ license = "MIT" readme = "README.md" [features] -default = ["log-tracer", "trace-logger", "std"] +default = ["log-tracer", "std"] std = ["log/std"] log-tracer = [] -trace-logger = [] [dependencies] tracing-core = { path = "../tracing-core", version = "0.2"} log = { version = "0.4" } lazy_static = "1.3.0" -env_logger = { version = "0.7", optional = true } +env_logger = { version = "0.8", optional = true } [dev-dependencies] tracing = { path = "../tracing", version = "0.2"} diff --git a/tracing-log/src/lib.rs b/tracing-log/src/lib.rs index cb6a2fdc7e..57f286139f 100644 --- a/tracing-log/src/lib.rs +++ b/tracing-log/src/lib.rs @@ -59,9 +59,9 @@ //! //! Note that logger implementations that convert log records to trace events //! should not be used with `Subscriber`s that convert trace events _back_ into -//! log records (such as the `TraceLogger`), as doing so will result in the -//! event recursing between the subscriber and the logger forever (or, in real -//! life, probably overflowing the call stack). +//! log records, as doing so will result in the event recursing between the +//! subscriber and the logger forever (or, in real life, probably overflowing +//! the call stack). //! //! If the logging of trace events generated from log records produced by the //! `log` crate is desired, either the `log` crate should not be used to @@ -69,8 +69,6 @@ //! required to avoid infinitely converting between `Event` and `log::Record`. //! //! # Feature Flags -//! * `trace-logger`: enables an experimental `log` subscriber, deprecated since -//! version 0.1.1. //! * `log-tracer`: enables the `LogTracer` type (on by default) //! * `env_logger`: enables the `env_logger` module, with helpers for working //! with the [`env_logger` crate]. @@ -94,7 +92,6 @@ //! [`AsTrace`]: trait.AsTrace.html //! [`AsLog`]: trait.AsLog.html //! [`LogTracer`]: struct.LogTracer.html -//! [`TraceLogger`]: struct.TraceLogger.html //! [`env_logger`]: env_logger/index.html //! [`tracing`]: https://crates.io/crates/tracing //! [`log`]: https://crates.io/crates/log @@ -151,25 +148,11 @@ use tracing_core::{ #[cfg_attr(docsrs, doc(cfg(feature = "log-tracer")))] pub mod log_tracer; -#[cfg(feature = "trace-logger")] -#[cfg_attr(docsrs, doc(cfg(feature = "trace-logger")))] -pub mod trace_logger; - #[cfg(feature = "log-tracer")] #[cfg_attr(docsrs, doc(cfg(feature = "log-tracer")))] #[doc(inline)] pub use self::log_tracer::LogTracer; -#[cfg(feature = "trace-logger")] -#[cfg_attr(docsrs, doc(cfg(feature = "trace-logger")))] -#[deprecated( - since = "0.1.1", - note = "use the `tracing` crate's \"log\" feature flag instead" -)] -#[allow(deprecated)] -#[doc(inline)] -pub use self::trace_logger::TraceLogger; - #[cfg(feature = "env_logger")] #[cfg_attr(docsrs, doc(cfg(feature = "env_logger")))] pub mod env_logger; @@ -213,7 +196,7 @@ pub trait AsLog<'a>: crate::sealed::Sealed { /// The `log` type that this type can be converted into. type Log; /// Returns the `log` equivalent of `self`. - fn as_log<'b: 'a>(&'b self) -> Self::Log; + fn as_log(&'a self) -> Self::Log; } /// Trait implemented for `log` types that can be converted to a `tracing` @@ -229,7 +212,7 @@ impl<'a> crate::sealed::Sealed for Metadata<'a> {} impl<'a> AsLog<'a> for Metadata<'a> { type Log = log::Metadata<'a>; - fn as_log<'b: 'a>(&'b self) -> Self::Log { + fn as_log(&'a self) -> Self::Log { log::Metadata::builder() .level(self.level().as_log()) .target(&self.target()) @@ -354,7 +337,7 @@ impl crate::sealed::Sealed for tracing_core::Level {} impl<'a> AsLog<'a> for tracing_core::Level { type Log = log::Level; - fn as_log<'b: 'a>(&'b self) -> log::Level { + fn as_log(&self) -> log::Level { match *self { tracing_core::Level::ERROR => log::Level::Error, tracing_core::Level::WARN => log::Level::Warn, diff --git a/tracing-macros/Cargo.toml b/tracing-macros/Cargo.toml index c49adb98c5..82fb8b579e 100644 --- a/tracing-macros/Cargo.toml +++ b/tracing-macros/Cargo.toml @@ -17,11 +17,10 @@ keywords = ["logging", "tracing"] license = "MIT" [dependencies] -tracing = "0.1.18" +tracing = "0.1.20" [dev-dependencies] -tracing-log = "0.1" -env_logger = "0.7" +tracing-subscriber = "0.2" [badges] maintenance = { status = "experimental" } diff --git a/tracing-macros/examples/factorial.rs b/tracing-macros/examples/factorial.rs index c5257f08f8..a089e019e1 100644 --- a/tracing-macros/examples/factorial.rs +++ b/tracing-macros/examples/factorial.rs @@ -12,9 +12,7 @@ fn factorial(n: u32) -> u32 { } fn main() { - env_logger::Builder::new().parse_filters("trace").init(); - #[allow(deprecated)] - let subscriber = tracing_log::TraceLogger::new(); + let subscriber = tracing_subscriber::fmt().finish(); tracing::subscriber::with_default(subscriber, || dbg!(factorial(4))); } diff --git a/tracing-subscriber/Cargo.toml b/tracing-subscriber/Cargo.toml index 9448fff3ba..e79ca3d17a 100644 --- a/tracing-subscriber/Cargo.toml +++ b/tracing-subscriber/Cargo.toml @@ -84,3 +84,7 @@ harness = false [[bench]] name = "fmt" harness = false + +[[bench]] +name = "enter" +harness = false diff --git a/tracing-subscriber/benches/enter.rs b/tracing-subscriber/benches/enter.rs new file mode 100644 index 0000000000..49c6e730a8 --- /dev/null +++ b/tracing-subscriber/benches/enter.rs @@ -0,0 +1,64 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use tracing_subscriber::prelude::*; + +fn enter(c: &mut Criterion) { + let mut group = c.benchmark_group("enter"); + let _subscriber = tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + .finish() + .set_default(); + group.bench_function("enabled", |b| { + let span = tracing::info_span!("foo"); + b.iter_with_large_drop(|| span.enter()) + }); + group.bench_function("disabled", |b| { + let span = tracing::debug_span!("foo"); + b.iter_with_large_drop(|| span.enter()) + }); +} + +fn enter_exit(c: &mut Criterion) { + let mut group = c.benchmark_group("enter_exit"); + let _subscriber = tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + .finish() + .set_default(); + group.bench_function("enabled", |b| { + let span = tracing::info_span!("foo"); + b.iter(|| span.enter()) + }); + group.bench_function("disabled", |b| { + let span = tracing::debug_span!("foo"); + b.iter(|| span.enter()) + }); +} + +fn enter_many(c: &mut Criterion) { + let mut group = c.benchmark_group("enter_many"); + let _subscriber = tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + .finish() + .set_default(); + group.bench_function("enabled", |b| { + let span1 = tracing::info_span!("span1"); + let _e1 = span1.enter(); + let span2 = tracing::info_span!("span2"); + let _e2 = span2.enter(); + let span3 = tracing::info_span!("span3"); + let _e3 = span3.enter(); + let span = tracing::info_span!("foo"); + b.iter_with_large_drop(|| span.enter()) + }); + group.bench_function("disabled", |b| { + let span1 = tracing::info_span!("span1"); + let _e1 = span1.enter(); + let span2 = tracing::info_span!("span2"); + let _e2 = span2.enter(); + let span3 = tracing::info_span!("span3"); + let _e3 = span3.enter(); + let span = tracing::debug_span!("foo"); + b.iter_with_large_drop(|| span.enter()) + }); +} +criterion_group!(benches, enter, enter_exit, enter_many); +criterion_main!(benches); diff --git a/tracing-subscriber/src/registry/sharded.rs b/tracing-subscriber/src/registry/sharded.rs index 5a2202ffe4..38a1fed94c 100644 --- a/tracing-subscriber/src/registry/sharded.rs +++ b/tracing-subscriber/src/registry/sharded.rs @@ -202,19 +202,21 @@ impl Subscriber for Registry { fn event(&self, _: &Event<'_, '_>) {} fn enter(&self, id: &span::Id) { - self.current_spans + if self + .current_spans .get_or_default() .borrow_mut() - .push(self.clone_span(id)); + .push(id.clone()) + { + self.clone_span(id); + } } fn exit(&self, id: &span::Id) { - if let Some(id) = self - .current_spans - .get() - .and_then(|spans| spans.borrow_mut().pop(id)) - { - dispatcher::get_default(|dispatch| dispatch.try_close(id.clone())); + if let Some(spans) = self.current_spans.get() { + if spans.borrow_mut().pop(id) { + dispatcher::get_default(|dispatch| dispatch.try_close(id.clone())); + } } } diff --git a/tracing-subscriber/src/registry/stack.rs b/tracing-subscriber/src/registry/stack.rs index e85cb7c3cf..b0b372c13f 100644 --- a/tracing-subscriber/src/registry/stack.rs +++ b/tracing-subscriber/src/registry/stack.rs @@ -1,5 +1,3 @@ -use std::collections::HashSet; - pub(crate) use tracing_core::span::Id; #[derive(Debug)] @@ -15,19 +13,18 @@ struct ContextId { #[derive(Debug, Default)] pub(crate) struct SpanStack { stack: Vec, - ids: HashSet, } impl SpanStack { - pub(crate) fn push(&mut self, id: Id) { - let duplicate = self.ids.contains(&id); - if !duplicate { - self.ids.insert(id.clone()); - } - self.stack.push(ContextId { id, duplicate }) + #[inline] + pub(crate) fn push(&mut self, id: Id) -> bool { + let duplicate = self.stack.iter().any(|i| i.id == id); + self.stack.push(ContextId { id, duplicate }); + !duplicate } - pub(crate) fn pop(&mut self, expected_id: &Id) -> Option { + #[inline] + pub(crate) fn pop(&mut self, expected_id: &Id) -> bool { if let Some((idx, _)) = self .stack .iter() @@ -35,14 +32,10 @@ impl SpanStack { .rev() .find(|(_, ctx_id)| ctx_id.id == *expected_id) { - let ContextId { id, duplicate } = self.stack.remove(idx); - if !duplicate { - self.ids.remove(&id); - } - Some(id) - } else { - None + let ContextId { id: _, duplicate } = self.stack.remove(idx); + return !duplicate; } + false } #[inline] @@ -65,7 +58,7 @@ mod tests { let id = Id::from_u64(1); stack.push(id.clone()); - assert_eq!(Some(id.clone()), stack.pop(&id)); + assert!(stack.pop(&id)); } #[test] @@ -75,6 +68,6 @@ mod tests { stack.push(Id::from_u64(2)); let id = Id::from_u64(1); - assert_eq!(Some(id.clone()), stack.pop(&id)); + assert!(stack.pop(&id)); } } diff --git a/tracing-tower/Cargo.toml b/tracing-tower/Cargo.toml index 7b0c9e695c..9441c2391f 100644 --- a/tracing-tower/Cargo.toml +++ b/tracing-tower/Cargo.toml @@ -30,7 +30,7 @@ futures = "0.3" tower-service = "0.3" tower-layer = { version = "0.3", optional = true } tower_make = { package = "tower-make", version = "0.3", optional = true } -pin-project = { version = "0.4", optional = true } +pin-project = { version = "1.0", optional = true } http = { version = "0.2", optional = true } [badges] diff --git a/tracing/README.md b/tracing/README.md index 408a475c7a..072a68b3fe 100644 --- a/tracing/README.md +++ b/tracing/README.md @@ -324,20 +324,6 @@ be invoked with the same syntax as the similarly-named macros from the `log` crate. Often, the process of converting a project to use `tracing` can begin with a simple drop-in replacement. -## Supported Rust Versions - -Tracing is built against the latest stable release. The minimum supported -version is 1.42. The current Tracing version is not guaranteed to build on Rust -versions earlier than the minimum supported version. - -Tracing follows the same compiler support policies as the rest of the Tokio -project. The current stable Rust compiler and the three most recent minor -versions before it will always be supported. For example, if the current stable -compiler version is 1.45, the minimum supported version will not be increased -past 1.42, three minor versions prior. Increasing the minimum supported compiler -version is not considered a semver breaking change as long as doing so complies -with this policy. - ## Ecosystem ### Related Crates