Skip to content

Commit

Permalink
Merge branch 'dp-target-is-cow' into dp-event-non-static-metadata
Browse files Browse the repository at this point in the history
* dp-target-is-cow:
  subscriber: remove TraceLogger (tokio-rs#1052)
  subscriber: make Registry::enter/exit much faster (tokio-rs#1058)
  Use impl Into<Cow<'a, str>
  chore(deps): update env_logger requirement from 0.7 to 0.8 (tokio-rs#1050)
  No need for extra lifetime
  Add constructor for dynamic data
  chore: fix tracing-macros::dbg (tokio-rs#1054)
  Feature gate usage of Cow in Metadata
  chore(deps): update pin-project requirement from 0.4 to 1.0 (tokio-rs#1038)
  chore: remove duplicated section from tracing/README.md (tokio-rs#1046)
  • Loading branch information
dvdplm committed Oct 22, 2020
2 parents 9e3f4fe + 4225d96 commit e4c99b3
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 78 deletions.
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
85 changes: 83 additions & 2 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Metadata describing trace data.
use super::{callsite, field};
#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
use core::{
cmp, fmt,
Expand Down Expand Up @@ -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<Cow<'a, str>>,
#[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<Cow<'a, str>>,
#[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.
Expand Down Expand Up @@ -132,23 +145,32 @@ impl<'a> Metadata<'a> {
fields: field::FieldSet,
kind: Kind,
) -> Self {
#[cfg(feature = "alloc")]
let file = {
if let Some(file) = file {
Some(Cow::Borrowed(file))
} else {
None
}
};
#[cfg(feature = "alloc")]
let module_path = {
if let Some(module_path) = module_path {
Some(Cow::Borrowed(module_path))
} else {
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,
Expand All @@ -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<Cow<'a, str>>,
target: impl Into<Cow<'a, str>>,
level: Level,
file: Option<impl Into<Cow<'a, str>>>,
line: Option<u32>,
module_path: Option<impl Into<Cow<'a, str>>>,
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -814,6 +862,12 @@ impl PartialOrd<Level> for LevelFilter {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "alloc")]
use crate::{
callsite::{Callsite, Identifier},
field::FieldSet,
Interest,
};
use core::mem;

#[test]
Expand Down Expand Up @@ -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::<Cow<'_, str>>,
field_set,
Kind::EVENT,
);
}
}
2 changes: 1 addition & 1 deletion tracing-futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
5 changes: 2 additions & 3 deletions tracing-log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
29 changes: 6 additions & 23 deletions tracing-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@
//!
//! 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
//! implement this logging, or an additional layer of filtering will be
//! 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].
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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`
Expand All @@ -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())
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions tracing-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
4 changes: 1 addition & 3 deletions tracing-macros/examples/factorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
4 changes: 4 additions & 0 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ harness = false
[[bench]]
name = "fmt"
harness = false

[[bench]]
name = "enter"
harness = false
64 changes: 64 additions & 0 deletions tracing-subscriber/benches/enter.rs
Original file line number Diff line number Diff line change
@@ -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);
Loading

0 comments on commit e4c99b3

Please sign in to comment.