diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/tracing | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
185 files changed, 54229 insertions, 0 deletions
diff --git a/vendor/tracing-attributes/.cargo-checksum.json b/vendor/tracing-attributes/.cargo-checksum.json new file mode 100644 index 000000000..995155f68 --- /dev/null +++ b/vendor/tracing-attributes/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"daa6621db253ddaaa095d297d0de2be41b03831a3992f2f9a5107749a689983e","Cargo.toml":"70c8c7dba6890f42c7830651ec9847c0e22b4c0987024d076d4abb3801f26bc8","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"7484033338a94e5ce23f1b281e1bdef1bae590c2329bd9b4d192ce66702e3a31","src/attr.rs":"4d26dad70c765fff3d4677ebe8e71b63599d67ceba5c48dbcb204d247b5394ce","src/expand.rs":"e6b81defa4ed98d71466ede6eb5a8f613cd0d33750b2e1274265467b85756362","src/lib.rs":"4400f3c00b7b4daf9cd61bec3e1f5d794c3d4b131667252a43c95a40041b6711","tests/async_fn.rs":"74126e6027bc9cb6e5e3d5d0e3b2debd5c808c737d25b90303b8e3ca878a3667","tests/destructuring.rs":"26b9800678bad09e06512a113a54556e2fac3ecb15a18dcccefe105fb8911c26","tests/err.rs":"3beb5f065e57a578825c383a56b9d64fb89794a508018bf36e16f113557909b8","tests/fields.rs":"3882bd4e744d6b492f59beac7475e8bf4ff4ca8ad85c6951c305a22c78e75fae","tests/follows_from.rs":"5bc856923e87b34e0558959149118238fe668ac621f1748cc444c21c90a86647","tests/instrument.rs":"9118eb6971d19a6b8d301bb4512b0fda909404a21e14edbef6b01db094cd8aaf","tests/levels.rs":"80ffb684163a4d28c69c40e31a82609ac02daf922086bab8247bca125aec3c69","tests/names.rs":"5afd6c4d526588bcea3141c130a45a21872956495b6868a01b44ddff57749827","tests/parents.rs":"673d3f81eed6ba433f685ec53fd007c5dd957b97d32499d7ea1537e1f289cb2e","tests/ret.rs":"083b4ca456d766c469b2fff7608b59524b422941e5c5e84f07c1ce0d7b345c7a","tests/targets.rs":"95ce1ce1e2d29794062c5b3429d91c1bfaba5813251d5d8440c12cb2db6e11bf"},"package":"11c75893af559bc8e10716548bdef5cb2b983f8e637db9d0e15126b61b484ee2"}
\ No newline at end of file diff --git a/vendor/tracing-attributes/CHANGELOG.md b/vendor/tracing-attributes/CHANGELOG.md new file mode 100644 index 000000000..4fcc81f29 --- /dev/null +++ b/vendor/tracing-attributes/CHANGELOG.md @@ -0,0 +1,306 @@ +# 0.1.22 (July 1, 2022) + +This release fixes an issue where using the `err` or `ret` arguments to +`#[instrument]` along with an overridden target, such as + +```rust +#[instrument(target = "...", err, ret)] +``` + +would not propagate the overridden target to the events generated for +errors/return values. + +### Fixed + +- Error and return value events generated by `#[instrument(err)]` or + `#[instrument(ret)]` not inheriting an overridden target ([#2184]) +- Incorrect default level in documentation ([#2119]) + +Thanks to new contributor @tbraun96 for contributing to this release! + +[#2184]: https://github.com/tokio-rs/tracing/pull/2184 +[#2119]: https://github.com/tokio-rs/tracing/pull/2119 + +# 0.1.21 (April 26, 2022) + +This release adds support for setting explicit parent and follows-from spans +in the `#[instrument]` attribute. + +### Added + +- `#[instrument(follows_from = ...)]` argument for setting one or more + follows-from span ([#2093]) +- `#[instrument(parent = ...)]` argument for overriding the generated span's + parent ([#2091]) + +### Fixed + +- Extra braces around `async` blocks in expanded code (causes a Clippy warning) + ([#2090]) +- Broken documentation links ([#2068], [#2077]) + +Thanks to @jarrodldavis, @ben0x539, and new contributor @jswrenn for +contributing to this release! + + +[#2093]: https://github.com/tokio-rs/tracing/pull/2093 +[#2091]: https://github.com/tokio-rs/tracing/pull/2091 +[#2090]: https://github.com/tokio-rs/tracing/pull/2090 +[#2077]: https://github.com/tokio-rs/tracing/pull/2077 +[#2068]: https://github.com/tokio-rs/tracing/pull/2068 + +# 0.1.20 (March 8, 2022) + +### Fixed + +- Compilation failure with `--minimal-versions` due to a too-permissive `syn` + dependency ([#1960]) + +### Changed + +- Bumped minimum supported Rust version (MSRV) to 1.49.0 ([#1913]) + +Thanks to new contributor @udoprog for contributing to this release! + +[#1960]: https://github.com/tokio-rs/tracing/pull/1960 +[#1913]: https://github.com/tokio-rs/tracing/pull/1913 + +# 0.1.19 (February 3, 2022) + +This release introduces a new `#[instrument(ret)]` argument to emit an event +with the return value of an instrumented function. + +### Added + +- `#[instrument(ret)]` to record the return value of a function ([#1716]) +- added `err(Debug)` argument to cause `#[instrument(err)]` to record errors + with `Debug` rather than `Display ([#1631]) + +### Fixed + +- incorrect code generation for functions returning async blocks ([#1866]) +- incorrect diagnostics when using `rust-analyzer` ([#1634]) + +Thanks to @Swatinem, @hkmatsumoto, @cynecx, and @ciuncan for contributing to +this release! + +[#1716]: https://github.com/tokio-rs/tracing/pull/1716 +[#1631]: https://github.com/tokio-rs/tracing/pull/1631 +[#1634]: https://github.com/tokio-rs/tracing/pull/1634 +[#1866]: https://github.com/tokio-rs/tracing/pull/1866 + +# 0.1.18 (October 5, 2021) + +This release fixes issues introduced in v0.1.17. + +### Fixed + +- fixed mismatched types compiler error that may occur when using + `#[instrument]` on an `async fn` that returns an `impl Trait` value that + includes a closure ([#1616]) +- fixed false positives for `clippy::suspicious_else_formatting` warnings due to + rust-lang/rust-clippy#7760 and rust-lang/rust-clippy#6249 ([#1617]) +- fixed `clippy::let_unit_value` lints when using `#[instrument]` ([#1614]) + +[#1617]: https://github.com/tokio-rs/tracing/pull/1617 +[#1616]: https://github.com/tokio-rs/tracing/pull/1616 +[#1614]: https://github.com/tokio-rs/tracing/pull/1614 + +# 0.1.17 (YANKED) (October 1, 2021) + +This release significantly improves performance when `#[instrument]`-generated +spans are below the maximum enabled level. + +### Added + +- improve performance when skipping `#[instrument]`-generated spans below the + max level ([#1600], [#1605]) + +Thanks to @oli-obk for contributing to this release! + +[#1600]: https://github.com/tokio-rs/tracing/pull/1600 +[#1605]: https://github.com/tokio-rs/tracing/pull/1605 + +# 0.1.16 (September 13, 2021) + +This release adds a new `#[instrument(skip_all)]` option to skip recording *all* +arguments to an instrumented function as fields. Additionally, it adds support +for recording arguments that are `tracing` primitive types as typed values, +rather than as `fmt::Debug`. + +### Added + +- add `skip_all` option to `#[instrument]` ([#1548]) +- record primitive types as primitive values rather than as `fmt::Debug` + ([#1378]) +- added support for `f64`s as typed values ([#1522]) + +Thanks to @Folyd and @jsgf for contributing to this release! + +[#1548]: https://github.com/tokio-rs/tracing/pull/1548 +[#1378]: https://github.com/tokio-rs/tracing/pull/1378 +[#1522]: https://github.com/tokio-rs/tracing/pull/1524 + +# 0.1.15 (March 12, 2021) + +### Fixed + +- `#[instrument]` on functions returning `Box::pin`ned futures incorrectly + skipping function bodies prior to returning a future ([#1297]) + +Thanks to @nightmared for contributing to this release! + +[#1297]: https://github.com/tokio-rs/tracing/pull/1297 + +# 0.1.14 (March 10, 2021) + +### Fixed + +- Compatibility between `#[instrument]` and `async-trait` v0.1.43 and newer + ([#1228]) + +Thanks to @nightmared for lots of hard work on this fix! + +[#1228]: https://github.com/tokio-rs/tracing/pull/1228 + +# 0.1.13 (February 17, 2021) + +### Fixed + +- Compiler error when using `#[instrument(err)]` on functions which return `impl + Trait` ([#1236]) + +[#1236]: https://github.com/tokio-rs/tracing/pull/1236 + +# 0.1.12 (February 4, 2021) + +### Fixed + +- Compiler error when using `#[instrument(err)]` on functions with mutable + parameters ([#1167]) +- Missing function visibility modifier when using `#[instrument]` with + `async-trait` ([#977]) +- Multiple documentation fixes and improvements ([#965], [#981], [#1215]) + +### Changed + +- `tracing-futures` dependency is no longer required when using `#[instrument]` + on async functions ([#808]) + +Thanks to @nagisa, @Txuritan, @TaKO8Ki, and @okready for contributing to this +release! + +[#1167]: https://github.com/tokio-rs/tracing/pull/1167 +[#977]: https://github.com/tokio-rs/tracing/pull/977 +[#965]: https://github.com/tokio-rs/tracing/pull/965 +[#981]: https://github.com/tokio-rs/tracing/pull/981 +[#1215]: https://github.com/tokio-rs/tracing/pull/1215 +[#808]: https://github.com/tokio-rs/tracing/pull/808 + +# 0.1.11 (August 18, 2020) + +### Fixed + +- Corrected wrong minimum supported Rust version note in docs (#941) +- Removed unused `syn` features (#928) + +Thanks to new contributor @jhpratt for contributing to this release! + +# 0.1.10 (August 10, 2020) + +### Added + +- Support for using `self` in field expressions when instrumenting `async-trait` + functions (#875) +- Several documentation improvements (#832, #897, #911, #913) + +Thanks to @anton-dutov and @nightmared for contributing to this release! + +# 0.1.9 (July 8, 2020) + +### Added + +- Support for arbitrary expressions as fields in `#[instrument]` (#672) + +### Changed + +- `#[instrument]` now emits a compiler warning when ignoring unrecognized + input (#672, #786) + +# 0.1.8 (May 13, 2020) + +### Added + +- Support for using `#[instrument]` on methods that are part of [`async-trait`] + trait implementations (#711) +- Optional `#[instrument(err)]` argument to automatically emit an event if an + instrumented function returns `Err` (#637) + +Thanks to @ilana and @nightmared for contributing to this release! + +[`async-trait`]: https://crates.io/crates/async-trait + +# 0.1.7 (February 26, 2020) + +### Added + +- Support for adding arbitrary literal fields to spans generated by + `#[instrument]` (#569) +- `#[instrument]` now emits a helpful compiler error when attempting to skip a + function parameter (#600) + +Thanks to @Kobzol for contributing to this release! + +# 0.1.6 (December 20, 2019) + +### Added + +- Updated documentation (#468) + +# 0.1.5 (October 22, 2019) + +### Added + +- Support for destructuring in arguments to `#[instrument]`ed functions (#397) +- Generated field for `self` parameters when `#[instrument]`ing methods (#397) + +# 0.1.4 (September 26, 2019) + +### Added + +- Optional `skip` argument to `#[instrument]` for excluding function parameters + from generated spans (#359) + +# 0.1.3 (September 12, 2019) + +### Fixed + +- Fixed `#[instrument]`ed async functions not compiling on `nightly-2019-09-11` + or newer (#342) + +# 0.1.2 (August 19, 2019) + +### Changed + +- Updated `syn` and `quote` dependencies to 1.0 (#292) +- Removed direct dependency on `proc-macro2` to avoid potential version + conflicts (#296) + +### Fixed + +- Outdated idioms in examples (#271, #273) + +# 0.1.1 (August 9, 2019) + +### Changed + +- Using the `#[instrument]` attribute on `async fn`s no longer requires a + feature flag (#258) + +### Fixed + +- The `#[instrument]` macro now works on generic functions (#262) + +# 0.1.0 (August 8, 2019) + +- Initial release diff --git a/vendor/tracing-attributes/Cargo.toml b/vendor/tracing-attributes/Cargo.toml new file mode 100644 index 000000000..3ecaba6fc --- /dev/null +++ b/vendor/tracing-attributes/Cargo.toml @@ -0,0 +1,85 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +rust-version = "1.49.0" +name = "tracing-attributes" +version = "0.1.22" +authors = [ + "Tokio Contributors <team@tokio.rs>", + "Eliza Weisman <eliza@buoyant.io>", + "David Barsky <dbarsky@amazon.com>", +] +description = """ +Procedural macro attributes for automatically instrumenting functions. +""" +homepage = "https://tokio.rs" +readme = "README.md" +keywords = [ + "logging", + "tracing", + "macro", + "instrument", + "log", +] +categories = [ + "development-tools::debugging", + "development-tools::profiling", + "asynchronous", +] +license = "MIT" +repository = "https://github.com/tokio-rs/tracing" + +[lib] +proc-macro = true + +[dependencies.proc-macro2] +version = "1" + +[dependencies.quote] +version = "1" + +[dependencies.syn] +version = "1.0.43" +features = [ + "full", + "parsing", + "printing", + "visit", + "visit-mut", + "clone-impls", + "extra-traits", + "proc-macro", +] +default-features = false + +[dev-dependencies.async-trait] +version = "0.1.44" + +[dev-dependencies.tokio-test] +version = "0.3.0" + +[dev-dependencies.tracing] +version = "0.1" + +[dev-dependencies.tracing-core] +version = "0.1" + +[dev-dependencies.tracing-subscriber] +version = "0.3" +features = ["env-filter"] + +[features] +async-await = [] + +[badges.maintenance] +status = "experimental" diff --git a/vendor/tracing-attributes/LICENSE b/vendor/tracing-attributes/LICENSE new file mode 100644 index 000000000..cdb28b4b5 --- /dev/null +++ b/vendor/tracing-attributes/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2019 Tokio Contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/tracing-attributes/README.md b/vendor/tracing-attributes/README.md new file mode 100644 index 000000000..636179468 --- /dev/null +++ b/vendor/tracing-attributes/README.md @@ -0,0 +1,91 @@ +![Tracing — Structured, application-level diagnostics][splash] + +[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg + +# tracing-attributes + +Macro attributes for application-level tracing. + +[![Crates.io][crates-badge]][crates-url] +[![Documentation][docs-badge]][docs-url] +[![Documentation (master)][docs-master-badge]][docs-master-url] +[![MIT licensed][mit-badge]][mit-url] +[![Build Status][actions-badge]][actions-url] +[![Discord chat][discord-badge]][discord-url] + +[Documentation][docs-url] | [Chat][discord-url] + +[crates-badge]: https://img.shields.io/crates/v/tracing-attributes.svg +[crates-url]: https://crates.io/crates/tracing-attributes +[docs-badge]: https://docs.rs/tracing-attributes/badge.svg +[docs-url]: https://docs.rs/tracing-attributes/0.1.22 +[docs-master-badge]: https://img.shields.io/badge/docs-master-blue +[docs-master-url]: https://tracing-rs.netlify.com/tracing_attributes +[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg +[mit-url]: LICENSE +[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg +[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI +[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white +[discord-url]: https://discord.gg/EeF3cQw + +## Overview + +[`tracing`] is a framework for instrumenting Rust programs to collect +structured, event-based diagnostic information. This crate provides the +`#[instrument]` attribute for automatically instrumenting functions using +`tracing`. + +Note that this macro is also re-exported by the main `tracing` crate. + +*Compiler support: [requires `rustc` 1.49+][msrv]* + +[msrv]: #supported-rust-versions + +## Usage + +First, add this to your `Cargo.toml`: + +```toml +[dependencies] +tracing-attributes = "0.1.22" +``` + + +This crate provides the `#[instrument]` attribute for instrumenting a function +with a `tracing` [span]. For example: + +```rust +use tracing_attributes::instrument; + +#[instrument] +pub fn my_function(my_arg: usize) { + // ... +} +``` + +[`tracing`]: https://crates.io/crates/tracing +[span]: https://docs.rs/tracing/latest/tracing/span/index.html + +## Supported Rust Versions + +Tracing is built against the latest stable release. The minimum supported +version is 1.49. 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. + +## License + +This project is licensed under the [MIT license](LICENSE). + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in Tokio by you, shall be licensed as MIT, without any additional +terms or conditions. diff --git a/vendor/tracing-attributes/src/attr.rs b/vendor/tracing-attributes/src/attr.rs new file mode 100644 index 000000000..ff875e179 --- /dev/null +++ b/vendor/tracing-attributes/src/attr.rs @@ -0,0 +1,413 @@ +use std::collections::HashSet; +use syn::{punctuated::Punctuated, Expr, Ident, LitInt, LitStr, Path, Token}; + +use proc_macro2::TokenStream; +use quote::{quote, quote_spanned, ToTokens}; +use syn::ext::IdentExt as _; +use syn::parse::{Parse, ParseStream}; + +#[derive(Clone, Default, Debug)] +pub(crate) struct InstrumentArgs { + level: Option<Level>, + pub(crate) name: Option<LitStr>, + target: Option<LitStr>, + pub(crate) parent: Option<Expr>, + pub(crate) follows_from: Option<Expr>, + pub(crate) skips: HashSet<Ident>, + pub(crate) skip_all: bool, + pub(crate) fields: Option<Fields>, + pub(crate) err_mode: Option<FormatMode>, + pub(crate) ret_mode: Option<FormatMode>, + /// Errors describing any unrecognized parse inputs that we skipped. + parse_warnings: Vec<syn::Error>, +} + +impl InstrumentArgs { + pub(crate) fn level(&self) -> impl ToTokens { + fn is_level(lit: &LitInt, expected: u64) -> bool { + match lit.base10_parse::<u64>() { + Ok(value) => value == expected, + Err(_) => false, + } + } + + match &self.level { + Some(Level::Str(ref lit)) if lit.value().eq_ignore_ascii_case("trace") => { + quote!(tracing::Level::TRACE) + } + Some(Level::Str(ref lit)) if lit.value().eq_ignore_ascii_case("debug") => { + quote!(tracing::Level::DEBUG) + } + Some(Level::Str(ref lit)) if lit.value().eq_ignore_ascii_case("info") => { + quote!(tracing::Level::INFO) + } + Some(Level::Str(ref lit)) if lit.value().eq_ignore_ascii_case("warn") => { + quote!(tracing::Level::WARN) + } + Some(Level::Str(ref lit)) if lit.value().eq_ignore_ascii_case("error") => { + quote!(tracing::Level::ERROR) + } + Some(Level::Int(ref lit)) if is_level(lit, 1) => quote!(tracing::Level::TRACE), + Some(Level::Int(ref lit)) if is_level(lit, 2) => quote!(tracing::Level::DEBUG), + Some(Level::Int(ref lit)) if is_level(lit, 3) => quote!(tracing::Level::INFO), + Some(Level::Int(ref lit)) if is_level(lit, 4) => quote!(tracing::Level::WARN), + Some(Level::Int(ref lit)) if is_level(lit, 5) => quote!(tracing::Level::ERROR), + Some(Level::Path(ref pat)) => quote!(#pat), + Some(_) => quote! { + compile_error!( + "unknown verbosity level, expected one of \"trace\", \ + \"debug\", \"info\", \"warn\", or \"error\", or a number 1-5" + ) + }, + None => quote!(tracing::Level::INFO), + } + } + + pub(crate) fn target(&self) -> impl ToTokens { + if let Some(ref target) = self.target { + quote!(#target) + } else { + quote!(module_path!()) + } + } + + /// Generate "deprecation" warnings for any unrecognized attribute inputs + /// that we skipped. + /// + /// For backwards compatibility, we need to emit compiler warnings rather + /// than errors for unrecognized inputs. Generating a fake deprecation is + /// the only way to do this on stable Rust right now. + pub(crate) fn warnings(&self) -> impl ToTokens { + let warnings = self.parse_warnings.iter().map(|err| { + let msg = format!("found unrecognized input, {}", err); + let msg = LitStr::new(&msg, err.span()); + // TODO(eliza): This is a bit of a hack, but it's just about the + // only way to emit warnings from a proc macro on stable Rust. + // Eventually, when the `proc_macro::Diagnostic` API stabilizes, we + // should definitely use that instead. + quote_spanned! {err.span()=> + #[warn(deprecated)] + { + #[deprecated(since = "not actually deprecated", note = #msg)] + const TRACING_INSTRUMENT_WARNING: () = (); + let _ = TRACING_INSTRUMENT_WARNING; + } + } + }); + quote! { + { #(#warnings)* } + } + } +} + +impl Parse for InstrumentArgs { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let mut args = Self::default(); + while !input.is_empty() { + let lookahead = input.lookahead1(); + if lookahead.peek(kw::name) { + if args.name.is_some() { + return Err(input.error("expected only a single `name` argument")); + } + let name = input.parse::<StrArg<kw::name>>()?.value; + args.name = Some(name); + } else if lookahead.peek(LitStr) { + // XXX: apparently we support names as either named args with an + // sign, _or_ as unnamed string literals. That's weird, but + // changing it is apparently breaking. + if args.name.is_some() { + return Err(input.error("expected only a single `name` argument")); + } + args.name = Some(input.parse()?); + } else if lookahead.peek(kw::target) { + if args.target.is_some() { + return Err(input.error("expected only a single `target` argument")); + } + let target = input.parse::<StrArg<kw::target>>()?.value; + args.target = Some(target); + } else if lookahead.peek(kw::parent) { + if args.target.is_some() { + return Err(input.error("expected only a single `parent` argument")); + } + let parent = input.parse::<ExprArg<kw::parent>>()?; + args.parent = Some(parent.value); + } else if lookahead.peek(kw::follows_from) { + if args.target.is_some() { + return Err(input.error("expected only a single `follows_from` argument")); + } + let follows_from = input.parse::<ExprArg<kw::follows_from>>()?; + args.follows_from = Some(follows_from.value); + } else if lookahead.peek(kw::level) { + if args.level.is_some() { + return Err(input.error("expected only a single `level` argument")); + } + args.level = Some(input.parse()?); + } else if lookahead.peek(kw::skip) { + if !args.skips.is_empty() { + return Err(input.error("expected only a single `skip` argument")); + } + if args.skip_all { + return Err(input.error("expected either `skip` or `skip_all` argument")); + } + let Skips(skips) = input.parse()?; + args.skips = skips; + } else if lookahead.peek(kw::skip_all) { + if args.skip_all { + return Err(input.error("expected only a single `skip_all` argument")); + } + if !args.skips.is_empty() { + return Err(input.error("expected either `skip` or `skip_all` argument")); + } + let _ = input.parse::<kw::skip_all>()?; + args.skip_all = true; + } else if lookahead.peek(kw::fields) { + if args.fields.is_some() { + return Err(input.error("expected only a single `fields` argument")); + } + args.fields = Some(input.parse()?); + } else if lookahead.peek(kw::err) { + let _ = input.parse::<kw::err>(); + let mode = FormatMode::parse(input)?; + args.err_mode = Some(mode); + } else if lookahead.peek(kw::ret) { + let _ = input.parse::<kw::ret>()?; + let mode = FormatMode::parse(input)?; + args.ret_mode = Some(mode); + } else if lookahead.peek(Token![,]) { + let _ = input.parse::<Token![,]>()?; + } else { + // We found a token that we didn't expect! + // We want to emit warnings for these, rather than errors, so + // we'll add it to the list of unrecognized inputs we've seen so + // far and keep going. + args.parse_warnings.push(lookahead.error()); + // Parse the unrecognized token tree to advance the parse + // stream, and throw it away so we can keep parsing. + let _ = input.parse::<proc_macro2::TokenTree>(); + } + } + Ok(args) + } +} + +struct StrArg<T> { + value: LitStr, + _p: std::marker::PhantomData<T>, +} + +impl<T: Parse> Parse for StrArg<T> { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let _ = input.parse::<T>()?; + let _ = input.parse::<Token![=]>()?; + let value = input.parse()?; + Ok(Self { + value, + _p: std::marker::PhantomData, + }) + } +} + +struct ExprArg<T> { + value: Expr, + _p: std::marker::PhantomData<T>, +} + +impl<T: Parse> Parse for ExprArg<T> { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let _ = input.parse::<T>()?; + let _ = input.parse::<Token![=]>()?; + let value = input.parse()?; + Ok(Self { + value, + _p: std::marker::PhantomData, + }) + } +} + +struct Skips(HashSet<Ident>); + +impl Parse for Skips { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let _ = input.parse::<kw::skip>(); + let content; + let _ = syn::parenthesized!(content in input); + let names: Punctuated<Ident, Token![,]> = content.parse_terminated(Ident::parse_any)?; + let mut skips = HashSet::new(); + for name in names { + if skips.contains(&name) { + return Err(syn::Error::new( + name.span(), + "tried to skip the same field twice", + )); + } else { + skips.insert(name); + } + } + Ok(Self(skips)) + } +} + +#[derive(Clone, Debug, Hash, PartialEq, Eq)] +pub(crate) enum FormatMode { + Default, + Display, + Debug, +} + +impl Default for FormatMode { + fn default() -> Self { + FormatMode::Default + } +} + +impl Parse for FormatMode { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + if !input.peek(syn::token::Paren) { + return Ok(FormatMode::default()); + } + let content; + let _ = syn::parenthesized!(content in input); + let maybe_mode: Option<Ident> = content.parse()?; + maybe_mode.map_or(Ok(FormatMode::default()), |ident| { + match ident.to_string().as_str() { + "Debug" => Ok(FormatMode::Debug), + "Display" => Ok(FormatMode::Display), + _ => Err(syn::Error::new( + ident.span(), + "unknown error mode, must be Debug or Display", + )), + } + }) + } +} + +#[derive(Clone, Debug)] +pub(crate) struct Fields(pub(crate) Punctuated<Field, Token![,]>); + +#[derive(Clone, Debug)] +pub(crate) struct Field { + pub(crate) name: Punctuated<Ident, Token![.]>, + pub(crate) value: Option<Expr>, + pub(crate) kind: FieldKind, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub(crate) enum FieldKind { + Debug, + Display, + Value, +} + +impl Parse for Fields { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let _ = input.parse::<kw::fields>(); + let content; + let _ = syn::parenthesized!(content in input); + let fields: Punctuated<_, Token![,]> = content.parse_terminated(Field::parse)?; + Ok(Self(fields)) + } +} + +impl ToTokens for Fields { + fn to_tokens(&self, tokens: &mut TokenStream) { + self.0.to_tokens(tokens) + } +} + +impl Parse for Field { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let mut kind = FieldKind::Value; + if input.peek(Token![%]) { + input.parse::<Token![%]>()?; + kind = FieldKind::Display; + } else if input.peek(Token![?]) { + input.parse::<Token![?]>()?; + kind = FieldKind::Debug; + }; + let name = Punctuated::parse_separated_nonempty_with(input, Ident::parse_any)?; + let value = if input.peek(Token![=]) { + input.parse::<Token![=]>()?; + if input.peek(Token![%]) { + input.parse::<Token![%]>()?; + kind = FieldKind::Display; + } else if input.peek(Token![?]) { + input.parse::<Token![?]>()?; + kind = FieldKind::Debug; + }; + Some(input.parse()?) + } else { + None + }; + Ok(Self { name, value, kind }) + } +} + +impl ToTokens for Field { + fn to_tokens(&self, tokens: &mut TokenStream) { + if let Some(ref value) = self.value { + let name = &self.name; + let kind = &self.kind; + tokens.extend(quote! { + #name = #kind#value + }) + } else if self.kind == FieldKind::Value { + // XXX(eliza): I don't like that fields without values produce + // empty fields rather than local variable shorthand...but, + // we've released a version where field names without values in + // `instrument` produce empty field values, so changing it now + // is a breaking change. agh. + let name = &self.name; + tokens.extend(quote!(#name = tracing::field::Empty)) + } else { + self.kind.to_tokens(tokens); + self.name.to_tokens(tokens); + } + } +} + +impl ToTokens for FieldKind { + fn to_tokens(&self, tokens: &mut TokenStream) { + match self { + FieldKind::Debug => tokens.extend(quote! { ? }), + FieldKind::Display => tokens.extend(quote! { % }), + _ => {} + } + } +} + +#[derive(Clone, Debug)] +enum Level { + Str(LitStr), + Int(LitInt), + Path(Path), +} + +impl Parse for Level { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let _ = input.parse::<kw::level>()?; + let _ = input.parse::<Token![=]>()?; + let lookahead = input.lookahead1(); + if lookahead.peek(LitStr) { + Ok(Self::Str(input.parse()?)) + } else if lookahead.peek(LitInt) { + Ok(Self::Int(input.parse()?)) + } else if lookahead.peek(Ident) { + Ok(Self::Path(input.parse()?)) + } else { + Err(lookahead.error()) + } + } +} + +mod kw { + syn::custom_keyword!(fields); + syn::custom_keyword!(skip); + syn::custom_keyword!(skip_all); + syn::custom_keyword!(level); + syn::custom_keyword!(target); + syn::custom_keyword!(parent); + syn::custom_keyword!(follows_from); + syn::custom_keyword!(name); + syn::custom_keyword!(err); + syn::custom_keyword!(ret); +} diff --git a/vendor/tracing-attributes/src/expand.rs b/vendor/tracing-attributes/src/expand.rs new file mode 100644 index 000000000..81ee6ed90 --- /dev/null +++ b/vendor/tracing-attributes/src/expand.rs @@ -0,0 +1,760 @@ +use std::iter; + +use proc_macro2::TokenStream; +use quote::{quote, quote_spanned, ToTokens}; +use syn::{ + punctuated::Punctuated, spanned::Spanned, Block, Expr, ExprAsync, ExprCall, FieldPat, FnArg, + Ident, Item, ItemFn, Pat, PatIdent, PatReference, PatStruct, PatTuple, PatTupleStruct, PatType, + Path, Signature, Stmt, Token, TypePath, +}; + +use crate::{ + attr::{Field, Fields, FormatMode, InstrumentArgs}, + MaybeItemFnRef, +}; + +/// Given an existing function, generate an instrumented version of that function +pub(crate) fn gen_function<'a, B: ToTokens + 'a>( + input: MaybeItemFnRef<'a, B>, + args: InstrumentArgs, + instrumented_function_name: &str, + self_type: Option<&syn::TypePath>, +) -> proc_macro2::TokenStream { + // these are needed ahead of time, as ItemFn contains the function body _and_ + // isn't representable inside a quote!/quote_spanned! macro + // (Syn's ToTokens isn't implemented for ItemFn) + let MaybeItemFnRef { + attrs, + vis, + sig, + block, + } = input; + + let Signature { + output: return_type, + inputs: params, + unsafety, + asyncness, + constness, + abi, + ident, + generics: + syn::Generics { + params: gen_params, + where_clause, + .. + }, + .. + } = sig; + + let warnings = args.warnings(); + + let body = gen_block( + block, + params, + asyncness.is_some(), + args, + instrumented_function_name, + self_type, + ); + + quote!( + #(#attrs) * + #vis #constness #unsafety #asyncness #abi fn #ident<#gen_params>(#params) #return_type + #where_clause + { + #warnings + #body + } + ) +} + +/// Instrument a block +fn gen_block<B: ToTokens>( + block: &B, + params: &Punctuated<FnArg, Token![,]>, + async_context: bool, + mut args: InstrumentArgs, + instrumented_function_name: &str, + self_type: Option<&syn::TypePath>, +) -> proc_macro2::TokenStream { + // generate the span's name + let span_name = args + // did the user override the span's name? + .name + .as_ref() + .map(|name| quote!(#name)) + .unwrap_or_else(|| quote!(#instrumented_function_name)); + + let level = args.level(); + + let follows_from = args.follows_from.iter(); + let follows_from = quote! { + #(for cause in #follows_from { + __tracing_attr_span.follows_from(cause); + })* + }; + + // generate this inside a closure, so we can return early on errors. + let span = (|| { + // Pull out the arguments-to-be-skipped first, so we can filter results + // below. + let param_names: Vec<(Ident, (Ident, RecordType))> = params + .clone() + .into_iter() + .flat_map(|param| match param { + FnArg::Typed(PatType { pat, ty, .. }) => { + param_names(*pat, RecordType::parse_from_ty(&*ty)) + } + FnArg::Receiver(_) => Box::new(iter::once(( + Ident::new("self", param.span()), + RecordType::Debug, + ))), + }) + // Little dance with new (user-exposed) names and old (internal) + // names of identifiers. That way, we could do the following + // even though async_trait (<=0.1.43) rewrites "self" as "_self": + // ``` + // #[async_trait] + // impl Foo for FooImpl { + // #[instrument(skip(self))] + // async fn foo(&self, v: usize) {} + // } + // ``` + .map(|(x, record_type)| { + // if we are inside a function generated by async-trait <=0.1.43, we need to + // take care to rewrite "_self" as "self" for 'user convenience' + if self_type.is_some() && x == "_self" { + (Ident::new("self", x.span()), (x, record_type)) + } else { + (x.clone(), (x, record_type)) + } + }) + .collect(); + + for skip in &args.skips { + if !param_names.iter().map(|(user, _)| user).any(|y| y == skip) { + return quote_spanned! {skip.span()=> + compile_error!("attempting to skip non-existent parameter") + }; + } + } + + let target = args.target(); + + let parent = args.parent.iter(); + + // filter out skipped fields + let quoted_fields: Vec<_> = param_names + .iter() + .filter(|(param, _)| { + if args.skip_all || args.skips.contains(param) { + return false; + } + + // If any parameters have the same name as a custom field, skip + // and allow them to be formatted by the custom field. + if let Some(ref fields) = args.fields { + fields.0.iter().all(|Field { ref name, .. }| { + let first = name.first(); + first != name.last() || !first.iter().any(|name| name == ¶m) + }) + } else { + true + } + }) + .map(|(user_name, (real_name, record_type))| match record_type { + RecordType::Value => quote!(#user_name = #real_name), + RecordType::Debug => quote!(#user_name = tracing::field::debug(&#real_name)), + }) + .collect(); + + // replace every use of a variable with its original name + if let Some(Fields(ref mut fields)) = args.fields { + let mut replacer = IdentAndTypesRenamer { + idents: param_names.into_iter().map(|(a, (b, _))| (a, b)).collect(), + types: Vec::new(), + }; + + // when async-trait <=0.1.43 is in use, replace instances + // of the "Self" type inside the fields values + if let Some(self_type) = self_type { + replacer.types.push(("Self", self_type.clone())); + } + + for e in fields.iter_mut().filter_map(|f| f.value.as_mut()) { + syn::visit_mut::visit_expr_mut(&mut replacer, e); + } + } + + let custom_fields = &args.fields; + + quote!(tracing::span!( + target: #target, + #(parent: #parent,)* + #level, + #span_name, + #(#quoted_fields,)* + #custom_fields + + )) + })(); + + let target = args.target(); + + let err_event = match args.err_mode { + Some(FormatMode::Default) | Some(FormatMode::Display) => { + Some(quote!(tracing::error!(target: #target, error = %e))) + } + Some(FormatMode::Debug) => Some(quote!(tracing::error!(target: #target, error = ?e))), + _ => None, + }; + + let ret_event = match args.ret_mode { + Some(FormatMode::Display) => Some(quote!( + tracing::event!(target: #target, #level, return = %x) + )), + Some(FormatMode::Default) | Some(FormatMode::Debug) => Some(quote!( + tracing::event!(target: #target, #level, return = ?x) + )), + _ => None, + }; + + // Generate the instrumented function body. + // If the function is an `async fn`, this will wrap it in an async block, + // which is `instrument`ed using `tracing-futures`. Otherwise, this will + // enter the span and then perform the rest of the body. + // If `err` is in args, instrument any resulting `Err`s. + // If `ret` is in args, instrument any resulting `Ok`s when the function + // returns `Result`s, otherwise instrument any resulting values. + if async_context { + let mk_fut = match (err_event, ret_event) { + (Some(err_event), Some(ret_event)) => quote_spanned!(block.span()=> + async move { + match async move #block.await { + #[allow(clippy::unit_arg)] + Ok(x) => { + #ret_event; + Ok(x) + }, + Err(e) => { + #err_event; + Err(e) + } + } + } + ), + (Some(err_event), None) => quote_spanned!(block.span()=> + async move { + match async move #block.await { + #[allow(clippy::unit_arg)] + Ok(x) => Ok(x), + Err(e) => { + #err_event; + Err(e) + } + } + } + ), + (None, Some(ret_event)) => quote_spanned!(block.span()=> + async move { + let x = async move #block.await; + #ret_event; + x + } + ), + (None, None) => quote_spanned!(block.span()=> + async move #block + ), + }; + + return quote!( + let __tracing_attr_span = #span; + let __tracing_instrument_future = #mk_fut; + if !__tracing_attr_span.is_disabled() { + #follows_from + tracing::Instrument::instrument( + __tracing_instrument_future, + __tracing_attr_span + ) + .await + } else { + __tracing_instrument_future.await + } + ); + } + + let span = quote!( + // These variables are left uninitialized and initialized only + // if the tracing level is statically enabled at this point. + // While the tracing level is also checked at span creation + // time, that will still create a dummy span, and a dummy guard + // and drop the dummy guard later. By lazily initializing these + // variables, Rust will generate a drop flag for them and thus + // only drop the guard if it was created. This creates code that + // is very straightforward for LLVM to optimize out if the tracing + // level is statically disabled, while not causing any performance + // regression in case the level is enabled. + let __tracing_attr_span; + let __tracing_attr_guard; + if tracing::level_enabled!(#level) { + __tracing_attr_span = #span; + #follows_from + __tracing_attr_guard = __tracing_attr_span.enter(); + } + ); + + match (err_event, ret_event) { + (Some(err_event), Some(ret_event)) => quote_spanned! {block.span()=> + #span + #[allow(clippy::redundant_closure_call)] + match (move || #block)() { + #[allow(clippy::unit_arg)] + Ok(x) => { + #ret_event; + Ok(x) + }, + Err(e) => { + #err_event; + Err(e) + } + } + }, + (Some(err_event), None) => quote_spanned!(block.span()=> + #span + #[allow(clippy::redundant_closure_call)] + match (move || #block)() { + #[allow(clippy::unit_arg)] + Ok(x) => Ok(x), + Err(e) => { + #err_event; + Err(e) + } + } + ), + (None, Some(ret_event)) => quote_spanned!(block.span()=> + #span + #[allow(clippy::redundant_closure_call)] + let x = (move || #block)(); + #ret_event; + x + ), + (None, None) => quote_spanned!(block.span() => + // Because `quote` produces a stream of tokens _without_ whitespace, the + // `if` and the block will appear directly next to each other. This + // generates a clippy lint about suspicious `if/else` formatting. + // Therefore, suppress the lint inside the generated code... + #[allow(clippy::suspicious_else_formatting)] + { + #span + // ...but turn the lint back on inside the function body. + #[warn(clippy::suspicious_else_formatting)] + #block + } + ), + } +} + +/// Indicates whether a field should be recorded as `Value` or `Debug`. +enum RecordType { + /// The field should be recorded using its `Value` implementation. + Value, + /// The field should be recorded using `tracing::field::debug()`. + Debug, +} + +impl RecordType { + /// Array of primitive types which should be recorded as [RecordType::Value]. + const TYPES_FOR_VALUE: &'static [&'static str] = &[ + "bool", + "str", + "u8", + "i8", + "u16", + "i16", + "u32", + "i32", + "u64", + "i64", + "f32", + "f64", + "usize", + "isize", + "NonZeroU8", + "NonZeroI8", + "NonZeroU16", + "NonZeroI16", + "NonZeroU32", + "NonZeroI32", + "NonZeroU64", + "NonZeroI64", + "NonZeroUsize", + "NonZeroIsize", + "Wrapping", + ]; + + /// Parse `RecordType` from [syn::Type] by looking up + /// the [RecordType::TYPES_FOR_VALUE] array. + fn parse_from_ty(ty: &syn::Type) -> Self { + match ty { + syn::Type::Path(syn::TypePath { path, .. }) + if path + .segments + .iter() + .last() + .map(|path_segment| { + let ident = path_segment.ident.to_string(); + Self::TYPES_FOR_VALUE.iter().any(|&t| t == ident) + }) + .unwrap_or(false) => + { + RecordType::Value + } + syn::Type::Reference(syn::TypeReference { elem, .. }) => { + RecordType::parse_from_ty(&*elem) + } + _ => RecordType::Debug, + } + } +} + +fn param_names(pat: Pat, record_type: RecordType) -> Box<dyn Iterator<Item = (Ident, RecordType)>> { + match pat { + Pat::Ident(PatIdent { ident, .. }) => Box::new(iter::once((ident, record_type))), + Pat::Reference(PatReference { pat, .. }) => param_names(*pat, record_type), + // We can't get the concrete type of fields in the struct/tuple + // patterns by using `syn`. e.g. `fn foo(Foo { x, y }: Foo) {}`. + // Therefore, the struct/tuple patterns in the arguments will just + // always be recorded as `RecordType::Debug`. + Pat::Struct(PatStruct { fields, .. }) => Box::new( + fields + .into_iter() + .flat_map(|FieldPat { pat, .. }| param_names(*pat, RecordType::Debug)), + ), + Pat::Tuple(PatTuple { elems, .. }) => Box::new( + elems + .into_iter() + .flat_map(|p| param_names(p, RecordType::Debug)), + ), + Pat::TupleStruct(PatTupleStruct { + pat: PatTuple { elems, .. }, + .. + }) => Box::new( + elems + .into_iter() + .flat_map(|p| param_names(p, RecordType::Debug)), + ), + + // The above *should* cover all cases of irrefutable patterns, + // but we purposefully don't do any funny business here + // (such as panicking) because that would obscure rustc's + // much more informative error message. + _ => Box::new(iter::empty()), + } +} + +/// The specific async code pattern that was detected +enum AsyncKind<'a> { + /// Immediately-invoked async fn, as generated by `async-trait <= 0.1.43`: + /// `async fn foo<...>(...) {...}; Box::pin(foo<...>(...))` + Function(&'a ItemFn), + /// A function returning an async (move) block, optionally `Box::pin`-ed, + /// as generated by `async-trait >= 0.1.44`: + /// `Box::pin(async move { ... })` + Async { + async_expr: &'a ExprAsync, + pinned_box: bool, + }, +} + +pub(crate) struct AsyncInfo<'block> { + // statement that must be patched + source_stmt: &'block Stmt, + kind: AsyncKind<'block>, + self_type: Option<syn::TypePath>, + input: &'block ItemFn, +} + +impl<'block> AsyncInfo<'block> { + /// Get the AST of the inner function we need to hook, if it looks like a + /// manual future implementation. + /// + /// When we are given a function that returns a (pinned) future containing the + /// user logic, it is that (pinned) future that needs to be instrumented. + /// Were we to instrument its parent, we would only collect information + /// regarding the allocation of that future, and not its own span of execution. + /// + /// We inspect the block of the function to find if it matches any of the + /// following patterns: + /// + /// - Immediately-invoked async fn, as generated by `async-trait <= 0.1.43`: + /// `async fn foo<...>(...) {...}; Box::pin(foo<...>(...))` + /// + /// - A function returning an async (move) block, optionally `Box::pin`-ed, + /// as generated by `async-trait >= 0.1.44`: + /// `Box::pin(async move { ... })` + /// + /// We the return the statement that must be instrumented, along with some + /// other information. + /// 'gen_body' will then be able to use that information to instrument the + /// proper function/future. + /// + /// (this follows the approach suggested in + /// https://github.com/dtolnay/async-trait/issues/45#issuecomment-571245673) + pub(crate) fn from_fn(input: &'block ItemFn) -> Option<Self> { + // are we in an async context? If yes, this isn't a manual async-like pattern + if input.sig.asyncness.is_some() { + return None; + } + + let block = &input.block; + + // list of async functions declared inside the block + let inside_funs = block.stmts.iter().filter_map(|stmt| { + if let Stmt::Item(Item::Fn(fun)) = &stmt { + // If the function is async, this is a candidate + if fun.sig.asyncness.is_some() { + return Some((stmt, fun)); + } + } + None + }); + + // last expression of the block: it determines the return value of the + // block, this is quite likely a `Box::pin` statement or an async block + let (last_expr_stmt, last_expr) = block.stmts.iter().rev().find_map(|stmt| { + if let Stmt::Expr(expr) = stmt { + Some((stmt, expr)) + } else { + None + } + })?; + + // is the last expression an async block? + if let Expr::Async(async_expr) = last_expr { + return Some(AsyncInfo { + source_stmt: last_expr_stmt, + kind: AsyncKind::Async { + async_expr, + pinned_box: false, + }, + self_type: None, + input, + }); + } + + // is the last expression a function call? + let (outside_func, outside_args) = match last_expr { + Expr::Call(ExprCall { func, args, .. }) => (func, args), + _ => return None, + }; + + // is it a call to `Box::pin()`? + let path = match outside_func.as_ref() { + Expr::Path(path) => &path.path, + _ => return None, + }; + if !path_to_string(path).ends_with("Box::pin") { + return None; + } + + // Does the call take an argument? If it doesn't, + // it's not gonna compile anyway, but that's no reason + // to (try to) perform an out of bounds access + if outside_args.is_empty() { + return None; + } + + // Is the argument to Box::pin an async block that + // captures its arguments? + if let Expr::Async(async_expr) = &outside_args[0] { + return Some(AsyncInfo { + source_stmt: last_expr_stmt, + kind: AsyncKind::Async { + async_expr, + pinned_box: true, + }, + self_type: None, + input, + }); + } + + // Is the argument to Box::pin a function call itself? + let func = match &outside_args[0] { + Expr::Call(ExprCall { func, .. }) => func, + _ => return None, + }; + + // "stringify" the path of the function called + let func_name = match **func { + Expr::Path(ref func_path) => path_to_string(&func_path.path), + _ => return None, + }; + + // Was that function defined inside of the current block? + // If so, retrieve the statement where it was declared and the function itself + let (stmt_func_declaration, func) = inside_funs + .into_iter() + .find(|(_, fun)| fun.sig.ident == func_name)?; + + // If "_self" is present as an argument, we store its type to be able to rewrite "Self" (the + // parameter type) with the type of "_self" + let mut self_type = None; + for arg in &func.sig.inputs { + if let FnArg::Typed(ty) = arg { + if let Pat::Ident(PatIdent { ref ident, .. }) = *ty.pat { + if ident == "_self" { + let mut ty = *ty.ty.clone(); + // extract the inner type if the argument is "&self" or "&mut self" + if let syn::Type::Reference(syn::TypeReference { elem, .. }) = ty { + ty = *elem; + } + + if let syn::Type::Path(tp) = ty { + self_type = Some(tp); + break; + } + } + } + } + } + + Some(AsyncInfo { + source_stmt: stmt_func_declaration, + kind: AsyncKind::Function(func), + self_type, + input, + }) + } + + pub(crate) fn gen_async( + self, + args: InstrumentArgs, + instrumented_function_name: &str, + ) -> proc_macro::TokenStream { + // let's rewrite some statements! + let mut out_stmts: Vec<TokenStream> = self + .input + .block + .stmts + .iter() + .map(|stmt| stmt.to_token_stream()) + .collect(); + + if let Some((iter, _stmt)) = self + .input + .block + .stmts + .iter() + .enumerate() + .find(|(_iter, stmt)| *stmt == self.source_stmt) + { + // instrument the future by rewriting the corresponding statement + out_stmts[iter] = match self.kind { + // `Box::pin(immediately_invoked_async_fn())` + AsyncKind::Function(fun) => gen_function( + fun.into(), + args, + instrumented_function_name, + self.self_type.as_ref(), + ), + // `async move { ... }`, optionally pinned + AsyncKind::Async { + async_expr, + pinned_box, + } => { + let instrumented_block = gen_block( + &async_expr.block, + &self.input.sig.inputs, + true, + args, + instrumented_function_name, + None, + ); + let async_attrs = &async_expr.attrs; + if pinned_box { + quote! { + Box::pin(#(#async_attrs) * async move { #instrumented_block }) + } + } else { + quote! { + #(#async_attrs) * async move { #instrumented_block } + } + } + } + }; + } + + let vis = &self.input.vis; + let sig = &self.input.sig; + let attrs = &self.input.attrs; + quote!( + #(#attrs) * + #vis #sig { + #(#out_stmts) * + } + ) + .into() + } +} + +// Return a path as a String +fn path_to_string(path: &Path) -> String { + use std::fmt::Write; + // some heuristic to prevent too many allocations + let mut res = String::with_capacity(path.segments.len() * 5); + for i in 0..path.segments.len() { + write!(&mut res, "{}", path.segments[i].ident) + .expect("writing to a String should never fail"); + if i < path.segments.len() - 1 { + res.push_str("::"); + } + } + res +} + +/// A visitor struct to replace idents and types in some piece +/// of code (e.g. the "self" and "Self" tokens in user-supplied +/// fields expressions when the function is generated by an old +/// version of async-trait). +struct IdentAndTypesRenamer<'a> { + types: Vec<(&'a str, TypePath)>, + idents: Vec<(Ident, Ident)>, +} + +impl<'a> syn::visit_mut::VisitMut for IdentAndTypesRenamer<'a> { + // we deliberately compare strings because we want to ignore the spans + // If we apply clippy's lint, the behavior changes + #[allow(clippy::cmp_owned)] + fn visit_ident_mut(&mut self, id: &mut Ident) { + for (old_ident, new_ident) in &self.idents { + if id.to_string() == old_ident.to_string() { + *id = new_ident.clone(); + } + } + } + + fn visit_type_mut(&mut self, ty: &mut syn::Type) { + for (type_name, new_type) in &self.types { + if let syn::Type::Path(TypePath { path, .. }) = ty { + if path_to_string(path) == *type_name { + *ty = syn::Type::Path(new_type.clone()); + } + } + } + } +} + +// A visitor struct that replace an async block by its patched version +struct AsyncTraitBlockReplacer<'a> { + block: &'a Block, + patched_block: Block, +} + +impl<'a> syn::visit_mut::VisitMut for AsyncTraitBlockReplacer<'a> { + fn visit_block_mut(&mut self, i: &mut Block) { + if i == self.block { + *i = self.patched_block.clone(); + } + } +} diff --git a/vendor/tracing-attributes/src/lib.rs b/vendor/tracing-attributes/src/lib.rs new file mode 100644 index 000000000..49b3c794a --- /dev/null +++ b/vendor/tracing-attributes/src/lib.rs @@ -0,0 +1,659 @@ +//! A procedural macro attribute for instrumenting functions with [`tracing`]. +//! +//! [`tracing`] is a framework for instrumenting Rust programs to collect +//! structured, event-based diagnostic information. This crate provides the +//! [`#[instrument]`][instrument] procedural macro attribute. +//! +//! Note that this macro is also re-exported by the main `tracing` crate. +//! +//! *Compiler support: [requires `rustc` 1.49+][msrv]* +//! +//! [msrv]: #supported-rust-versions +//! +//! ## Usage +//! +//! First, add this to your `Cargo.toml`: +//! +//! ```toml +//! [dependencies] +//! tracing-attributes = "0.1.22" +//! ``` +//! +//! The [`#[instrument]`][instrument] attribute can now be added to a function +//! to automatically create and enter `tracing` [span] when that function is +//! called. For example: +//! +//! ``` +//! use tracing_attributes::instrument; +//! +//! #[instrument] +//! pub fn my_function(my_arg: usize) { +//! // ... +//! } +//! +//! # fn main() {} +//! ``` +//! +//! [`tracing`]: https://crates.io/crates/tracing +//! [span]: https://docs.rs/tracing/latest/tracing/span/index.html +//! [instrument]: macro@self::instrument +//! +//! ## Supported Rust Versions +//! +//! Tracing is built against the latest stable release. The minimum supported +//! version is 1.49. 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. +//! +#![doc(html_root_url = "https://docs.rs/tracing-attributes/0.1.22")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png", + issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" +)] +#![cfg_attr(docsrs, deny(rustdoc::broken_intra_doc_links))] +#![warn( + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + unreachable_pub, + bad_style, + const_err, + dead_code, + improper_ctypes, + non_shorthand_field_patterns, + no_mangle_generic_items, + overflowing_literals, + path_statements, + patterns_in_fns_without_body, + private_in_public, + unconditional_recursion, + unused_allocation, + unused_comparisons, + unused_parens, + while_true +)] +// TODO: once `tracing` bumps its MSRV to 1.42, remove this allow. +#![allow(unused)] +extern crate proc_macro; + +use proc_macro2::TokenStream; +use quote::ToTokens; +use syn::parse::{Parse, ParseStream}; +use syn::{Attribute, Block, ItemFn, Signature, Visibility}; + +mod attr; +mod expand; +/// Instruments a function to create and enter a `tracing` [span] every time +/// the function is called. +/// +/// Unless overriden, a span with the [`INFO`] [level] will be generated. +/// The generated span's name will be the name of the function. +/// By default, all arguments to the function are included as fields on the +/// span. Arguments that are `tracing` [primitive types] implementing the +/// [`Value` trait] will be recorded as fields of that type. Types which do +/// not implement `Value` will be recorded using [`std::fmt::Debug`]. +/// +/// [primitive types]: https://docs.rs/tracing/latest/tracing/field/trait.Value.html#foreign-impls +/// [`Value` trait]: https://docs.rs/tracing/latest/tracing/field/trait.Value.html. +/// +/// # Overriding Span Attributes +/// +/// To change the [name] of the generated span, add a `name` argument to the +/// `#[instrument]` macro, followed by an equals sign and a string literal. For +/// example: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// +/// // The generated span's name will be "my_span" rather than "my_function". +/// #[instrument(name = "my_span")] +/// pub fn my_function() { +/// // ... do something incredibly interesting and important ... +/// } +/// ``` +/// +/// To override the [target] of the generated span, add a `target` argument to +/// the `#[instrument]` macro, followed by an equals sign and a string literal +/// for the new target. The [module path] is still recorded separately. For +/// example: +/// +/// ``` +/// pub mod my_module { +/// # use tracing_attributes::instrument; +/// // The generated span's target will be "my_crate::some_special_target", +/// // rather than "my_crate::my_module". +/// #[instrument(target = "my_crate::some_special_target")] +/// pub fn my_function() { +/// // ... all kinds of neat code in here ... +/// } +/// } +/// ``` +/// +/// Finally, to override the [level] of the generated span, add a `level` +/// argument, followed by an equals sign and a string literal with the name of +/// the desired level. Level names are not case sensitive. For example: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// // The span's level will be TRACE rather than INFO. +/// #[instrument(level = "trace")] +/// pub fn my_function() { +/// // ... I have written a truly marvelous implementation of this function, +/// // which this example is too narrow to contain ... +/// } +/// ``` +/// +/// # Skipping Fields +/// +/// To skip recording one or more arguments to a function or method, pass +/// the argument's name inside the `skip()` argument on the `#[instrument]` +/// macro. This can be used when an argument to an instrumented function does +/// not implement [`fmt::Debug`], or to exclude an argument with a verbose or +/// costly `Debug` implementation. Note that: +/// +/// - multiple argument names can be passed to `skip`. +/// - arguments passed to `skip` do _not_ need to implement `fmt::Debug`. +/// +/// You can also use `skip_all` to skip all arguments. +/// +/// ## Examples +/// +/// ``` +/// # use tracing_attributes::instrument; +/// # use std::collections::HashMap; +/// // This type doesn't implement `fmt::Debug`! +/// struct NonDebug; +/// +/// // `arg` will be recorded, while `non_debug` will not. +/// #[instrument(skip(non_debug))] +/// fn my_function(arg: usize, non_debug: NonDebug) { +/// // ... +/// } +/// +/// // These arguments are huge +/// #[instrument(skip_all)] +/// fn my_big_data_function(large: Vec<u8>, also_large: HashMap<String, String>) { +/// // ... +/// } +/// ``` +/// +/// Skipping the `self` parameter: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[derive(Debug)] +/// struct MyType { +/// data: Vec<u8>, // Suppose this buffer is often quite long... +/// } +/// +/// impl MyType { +/// // Suppose we don't want to print an entire kilobyte of `data` +/// // every time this is called... +/// #[instrument(skip(self))] +/// pub fn my_method(&mut self, an_interesting_argument: usize) { +/// // ... do something (hopefully, using all that `data`!) +/// } +/// } +/// ``` +/// +/// # Adding Fields +/// +/// Additional fields (key-value pairs with arbitrary data) may be added to the +/// generated span using the `fields` argument on the `#[instrument]` macro. Any +/// Rust expression can be used as a field value in this manner. These +/// expressions will be evaluated at the beginning of the function's body, so +/// arguments to the function may be used in these expressions. Field names may +/// also be specified *without* values. Doing so will result in an [empty field] +/// whose value may be recorded later within the function body. +/// +/// This supports the same [field syntax] as the `span!` and `event!` macros. +/// +/// Note that overlap between the names of fields and (non-skipped) arguments +/// will result in a compile error. +/// +/// ## Examples +/// +/// Adding a new field based on the value of an argument: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// +/// // This will record a field named "i" with the value of `i` *and* a field +/// // named "next" with the value of `i` + 1. +/// #[instrument(fields(next = i + 1))] +/// pub fn my_function(i: usize) { +/// // ... +/// } +/// ``` +/// +/// Recording specific properties of a struct as their own fields: +/// +/// ``` +/// # mod http { +/// # pub struct Error; +/// # pub struct Response<B> { pub(super) _b: std::marker::PhantomData<B> } +/// # pub struct Request<B> { _b: B } +/// # impl<B> std::fmt::Debug for Request<B> { +/// # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +/// # f.pad("request") +/// # } +/// # } +/// # impl<B> Request<B> { +/// # pub fn uri(&self) -> &str { "fake" } +/// # pub fn method(&self) -> &str { "GET" } +/// # } +/// # } +/// # use tracing_attributes::instrument; +/// +/// // This will record the request's URI and HTTP method as their own separate +/// // fields. +/// #[instrument(fields(http.uri = req.uri(), http.method = req.method()))] +/// pub fn handle_request<B>(req: http::Request<B>) -> http::Response<B> { +/// // ... handle the request ... +/// # http::Response { _b: std::marker::PhantomData } +/// } +/// ``` +/// +/// This can be used in conjunction with `skip` or `skip_all` to record only +/// some fields of a struct: +/// ``` +/// # use tracing_attributes::instrument; +/// // Remember the struct with the very large `data` field from the earlier +/// // example? Now it also has a `name`, which we might want to include in +/// // our span. +/// #[derive(Debug)] +/// struct MyType { +/// name: &'static str, +/// data: Vec<u8>, +/// } +/// +/// impl MyType { +/// // This will skip the `data` field, but will include `self.name`, +/// // formatted using `fmt::Display`. +/// #[instrument(skip(self), fields(self.name = %self.name))] +/// pub fn my_method(&mut self, an_interesting_argument: usize) { +/// // ... do something (hopefully, using all that `data`!) +/// } +/// } +/// ``` +/// +/// Adding an empty field to be recorded later: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// +/// // This function does a very interesting and important mathematical calculation. +/// // Suppose we want to record both the inputs to the calculation *and* its result... +/// #[instrument(fields(result))] +/// pub fn do_calculation(input_1: usize, input_2: usize) -> usize { +/// // Rerform the calculation. +/// let result = input_1 + input_2; +/// +/// // Record the result as part of the current span. +/// tracing::Span::current().record("result", &result); +/// +/// // Now, the result will also be included on this event! +/// tracing::info!("calculation complete!"); +/// +/// // ... etc ... +/// # 0 +/// } +/// ``` +/// +/// # Examples +/// +/// Instrumenting a function: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument] +/// pub fn my_function(my_arg: usize) { +/// // This event will be recorded inside a span named `my_function` with the +/// // field `my_arg`. +/// tracing::info!("inside my_function!"); +/// // ... +/// } +/// ``` +/// Setting the level for the generated span: +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(level = "debug")] +/// pub fn my_function() { +/// // ... +/// } +/// ``` +/// Overriding the generated span's name: +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(name = "my_name")] +/// pub fn my_function() { +/// // ... +/// } +/// ``` +/// Overriding the generated span's target: +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(target = "my_target")] +/// pub fn my_function() { +/// // ... +/// } +/// ``` +/// Overriding the generated span's parent: +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(parent = None)] +/// pub fn my_function() { +/// // ... +/// } +/// ``` +/// ``` +/// # use tracing_attributes::instrument; +/// // A struct which owns a span handle. +/// struct MyStruct +/// { +/// span: tracing::Span +/// } +/// +/// impl MyStruct +/// { +/// // Use the struct's `span` field as the parent span +/// #[instrument(parent = &self.span, skip(self))] +/// fn my_method(&self) {} +/// } +/// ``` +/// Specifying [`follows_from`] relationships: +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(follows_from = causes)] +/// pub fn my_function(causes: &[tracing::Id]) { +/// // ... +/// } +/// ``` +/// Any expression of type `impl IntoIterator<Item = impl Into<Option<Id>>>` +/// may be provided to `follows_from`; e.g.: +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(follows_from = [cause])] +/// pub fn my_function(cause: &tracing::span::EnteredSpan) { +/// // ... +/// } +/// ``` +/// +/// +/// To skip recording an argument, pass the argument's name to the `skip`: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// struct NonDebug; +/// +/// #[instrument(skip(non_debug))] +/// fn my_function(arg: usize, non_debug: NonDebug) { +/// // ... +/// } +/// ``` +/// +/// To add an additional context to the span, pass key-value pairs to `fields`: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(fields(foo="bar", id=1, show=true))] +/// fn my_function(arg: usize) { +/// // ... +/// } +/// ``` +/// +/// Adding the `ret` argument to `#[instrument]` will emit an event with the function's +/// return value when the function returns: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(ret)] +/// fn my_function() -> i32 { +/// 42 +/// } +/// ``` +/// The return value event will have the same level as the span generated by `#[instrument]`. +/// By default, this will be [`INFO`], but if the level is overridden, the event will be at the same +/// level. +/// +/// **Note**: if the function returns a `Result<T, E>`, `ret` will record returned values if and +/// only if the function returns [`Result::Ok`]. +/// +/// By default, returned values will be recorded using their [`std::fmt::Debug`] implementations. +/// If a returned value implements [`std::fmt::Display`], it can be recorded using its `Display` +/// implementation instead, by writing `ret(Display)`: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(ret(Display))] +/// fn my_function() -> i32 { +/// 42 +/// } +/// ``` +/// +/// If the function returns a `Result<T, E>` and `E` implements `std::fmt::Display`, you can add +/// `err` or `err(Display)` to emit error events when the function returns `Err`: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(err)] +/// fn my_function(arg: usize) -> Result<(), std::io::Error> { +/// Ok(()) +/// } +/// ``` +/// +/// By default, error values will be recorded using their `std::fmt::Display` implementations. +/// If an error implements `std::fmt::Debug`, it can be recorded using its `Debug` implementation +/// instead, by writing `err(Debug)`: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(err(Debug))] +/// fn my_function(arg: usize) -> Result<(), std::io::Error> { +/// Ok(()) +/// } +/// ``` +/// +/// If a `target` is specified, both the `ret` and `err` arguments will emit outputs to +/// the declared target (or the default channel if `target` is not specified). +/// +/// The `ret` and `err` arguments can be combined in order to record an event if a +/// function returns [`Result::Ok`] or [`Result::Err`]: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument(err, ret)] +/// fn my_function(arg: usize) -> Result<(), std::io::Error> { +/// Ok(()) +/// } +/// ``` +/// +/// `async fn`s may also be instrumented: +/// +/// ``` +/// # use tracing_attributes::instrument; +/// #[instrument] +/// pub async fn my_function() -> Result<(), ()> { +/// // ... +/// # Ok(()) +/// } +/// ``` +/// +/// It also works with [async-trait](https://crates.io/crates/async-trait) +/// (a crate that allows defining async functions in traits, +/// something not currently possible in Rust), +/// and hopefully most libraries that exhibit similar behaviors: +/// +/// ``` +/// # use tracing::instrument; +/// use async_trait::async_trait; +/// +/// #[async_trait] +/// pub trait Foo { +/// async fn foo(&self, arg: usize); +/// } +/// +/// #[derive(Debug)] +/// struct FooImpl(usize); +/// +/// #[async_trait] +/// impl Foo for FooImpl { +/// #[instrument(fields(value = self.0, tmp = std::any::type_name::<Self>()))] +/// async fn foo(&self, arg: usize) {} +/// } +/// ``` +/// +/// Note than on `async-trait` <= 0.1.43, references to the `Self` +/// type inside the `fields` argument were only allowed when the instrumented +/// function is a method (i.e., the function receives `self` as an argument). +/// For example, this *used to not work* because the instrument function +/// didn't receive `self`: +/// ``` +/// # use tracing::instrument; +/// use async_trait::async_trait; +/// +/// #[async_trait] +/// pub trait Bar { +/// async fn bar(); +/// } +/// +/// #[derive(Debug)] +/// struct BarImpl(usize); +/// +/// #[async_trait] +/// impl Bar for BarImpl { +/// #[instrument(fields(tmp = std::any::type_name::<Self>()))] +/// async fn bar() {} +/// } +/// ``` +/// Instead, you should manually rewrite any `Self` types as the type for +/// which you implement the trait: `#[instrument(fields(tmp = std::any::type_name::<Bar>()))]` +/// (or maybe you can just bump `async-trait`). +/// +/// [span]: https://docs.rs/tracing/latest/tracing/span/index.html +/// [name]: https://docs.rs/tracing/latest/tracing/struct.Metadata.html#method.name +/// [target]: https://docs.rs/tracing/latest/tracing/struct.Metadata.html#method.target +/// [level]: https://docs.rs/tracing/latest/tracing/struct.Level.html +/// [module path]: https://docs.rs/tracing/latest/tracing/struct.Metadata.html#method.module_path +/// [`INFO`]: https://docs.rs/tracing/latest/tracing/struct.Level.html#associatedconstant.INFO +/// [empty field]: https://docs.rs/tracing/latest/tracing/field/struct.Empty.html +/// [field syntax]: https://docs.rs/tracing/latest/tracing/#recording-fields +/// [`follows_from`]: https://docs.rs/tracing/latest/tracing/struct.Span.html#method.follows_from +/// [`tracing`]: https://github.com/tokio-rs/tracing +/// [`fmt::Debug`]: std::fmt::Debug +#[proc_macro_attribute] +pub fn instrument( + args: proc_macro::TokenStream, + item: proc_macro::TokenStream, +) -> proc_macro::TokenStream { + let args = syn::parse_macro_input!(args as attr::InstrumentArgs); + // Cloning a `TokenStream` is cheap since it's reference counted internally. + instrument_precise(args.clone(), item.clone()) + .unwrap_or_else(|_err| instrument_speculative(args, item)) +} + +/// Instrument the function, without parsing the function body (instead using the raw tokens). +fn instrument_speculative( + args: attr::InstrumentArgs, + item: proc_macro::TokenStream, +) -> proc_macro::TokenStream { + let input = syn::parse_macro_input!(item as MaybeItemFn); + let instrumented_function_name = input.sig.ident.to_string(); + expand::gen_function( + input.as_ref(), + args, + instrumented_function_name.as_str(), + None, + ) + .into() +} + +/// Instrument the function, by fully parsing the function body, +/// which allows us to rewrite some statements related to async-like patterns. +fn instrument_precise( + args: attr::InstrumentArgs, + item: proc_macro::TokenStream, +) -> Result<proc_macro::TokenStream, syn::Error> { + let input = syn::parse::<ItemFn>(item)?; + let instrumented_function_name = input.sig.ident.to_string(); + + // check for async_trait-like patterns in the block, and instrument + // the future instead of the wrapper + if let Some(async_like) = expand::AsyncInfo::from_fn(&input) { + return Ok(async_like.gen_async(args, instrumented_function_name.as_str())); + } + + Ok(expand::gen_function( + (&input).into(), + args, + instrumented_function_name.as_str(), + None, + ) + .into()) +} + +/// This is a more flexible/imprecise `ItemFn` type, +/// which's block is just a `TokenStream` (it may contain invalid code). +#[derive(Debug, Clone)] +struct MaybeItemFn { + attrs: Vec<Attribute>, + vis: Visibility, + sig: Signature, + block: TokenStream, +} + +impl MaybeItemFn { + fn as_ref(&self) -> MaybeItemFnRef<'_, TokenStream> { + MaybeItemFnRef { + attrs: &self.attrs, + vis: &self.vis, + sig: &self.sig, + block: &self.block, + } + } +} + +/// This parses a `TokenStream` into a `MaybeItemFn` +/// (just like `ItemFn`, but skips parsing the body). +impl Parse for MaybeItemFn { + fn parse(input: ParseStream<'_>) -> syn::Result<Self> { + let attrs = input.call(syn::Attribute::parse_outer)?; + let vis: Visibility = input.parse()?; + let sig: Signature = input.parse()?; + let block: TokenStream = input.parse()?; + Ok(Self { + attrs, + vis, + sig, + block, + }) + } +} + +/// A generic reference type for `MaybeItemFn`, +/// that takes a generic block type `B` that implements `ToTokens` (eg. `TokenStream`, `Block`). +#[derive(Debug, Clone)] +struct MaybeItemFnRef<'a, B: ToTokens> { + attrs: &'a Vec<Attribute>, + vis: &'a Visibility, + sig: &'a Signature, + block: &'a B, +} + +impl<'a> From<&'a ItemFn> for MaybeItemFnRef<'a, Box<Block>> { + fn from(val: &'a ItemFn) -> Self { + MaybeItemFnRef { + attrs: &val.attrs, + vis: &val.vis, + sig: &val.sig, + block: &val.block, + } + } +} diff --git a/vendor/tracing-attributes/tests/async_fn.rs b/vendor/tracing-attributes/tests/async_fn.rs new file mode 100644 index 000000000..7e27fb5ce --- /dev/null +++ b/vendor/tracing-attributes/tests/async_fn.rs @@ -0,0 +1,449 @@ +use tracing_mock::*; + +use std::convert::Infallible; +use std::{future::Future, pin::Pin, sync::Arc}; +use tracing::subscriber::with_default; +use tracing_attributes::instrument; + +#[instrument] +async fn test_async_fn(polls: usize) -> Result<(), ()> { + let future = PollN::new_ok(polls); + tracing::trace!(awaiting = true); + future.await +} + +// Reproduces a compile error when returning an `impl Trait` from an +// instrumented async fn (see https://github.com/tokio-rs/tracing/issues/1615) +#[instrument] +async fn test_ret_impl_trait(n: i32) -> Result<impl Iterator<Item = i32>, ()> { + let n = n; + Ok((0..10).filter(move |x| *x < n)) +} + +// Reproduces a compile error when returning an `impl Trait` from an +// instrumented async fn (see https://github.com/tokio-rs/tracing/issues/1615) +#[instrument(err)] +async fn test_ret_impl_trait_err(n: i32) -> Result<impl Iterator<Item = i32>, &'static str> { + Ok((0..10).filter(move |x| *x < n)) +} + +#[instrument] +async fn test_async_fn_empty() {} + +// Reproduces https://github.com/tokio-rs/tracing/issues/1613 +#[instrument] +// LOAD-BEARING `#[rustfmt::skip]`! This is necessary to reproduce the bug; +// with the rustfmt-generated formatting, the lint will not be triggered! +#[rustfmt::skip] +#[deny(clippy::suspicious_else_formatting)] +async fn repro_1613(var: bool) { + println!( + "{}", + if var { "true" } else { "false" } + ); +} + +// Reproduces https://github.com/tokio-rs/tracing/issues/1613 +// and https://github.com/rust-lang/rust-clippy/issues/7760 +#[instrument] +#[deny(clippy::suspicious_else_formatting)] +async fn repro_1613_2() { + // hello world + // else +} + +// Reproduces https://github.com/tokio-rs/tracing/issues/1831 +#[instrument] +#[deny(unused_braces)] +fn repro_1831() -> Pin<Box<dyn Future<Output = ()>>> { + Box::pin(async move {}) +} + +// This replicates the pattern used to implement async trait methods on nightly using the +// `type_alias_impl_trait` feature +#[instrument(ret, err)] +#[deny(unused_braces)] +#[allow(clippy::manual_async_fn)] +fn repro_1831_2() -> impl Future<Output = Result<(), Infallible>> { + async { Ok(()) } +} + +#[test] +fn async_fn_only_enters_for_polls() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("test_async_fn")) + .enter(span::mock().named("test_async_fn")) + .event(event::mock().with_fields(field::mock("awaiting").with_value(&true))) + .exit(span::mock().named("test_async_fn")) + .enter(span::mock().named("test_async_fn")) + .exit(span::mock().named("test_async_fn")) + .drop_span(span::mock().named("test_async_fn")) + .done() + .run_with_handle(); + with_default(subscriber, || { + block_on_future(async { test_async_fn(2).await }).unwrap(); + }); + handle.assert_finished(); +} + +#[test] +fn async_fn_nested() { + #[instrument] + async fn test_async_fns_nested() { + test_async_fns_nested_other().await + } + + #[instrument] + async fn test_async_fns_nested_other() { + tracing::trace!(nested = true); + } + + let span = span::mock().named("test_async_fns_nested"); + let span2 = span::mock().named("test_async_fns_nested_other"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .new_span(span2.clone()) + .enter(span2.clone()) + .event(event::mock().with_fields(field::mock("nested").with_value(&true))) + .exit(span2.clone()) + .drop_span(span2) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + block_on_future(async { test_async_fns_nested().await }); + }); + + handle.assert_finished(); +} + +#[test] +fn async_fn_with_async_trait() { + use async_trait::async_trait; + + // test the correctness of the metadata obtained by #[instrument] + // (function name, functions parameters) when async-trait is used + #[async_trait] + pub trait TestA { + async fn foo(&mut self, v: usize); + } + + // test nesting of async fns with aync-trait + #[async_trait] + pub trait TestB { + async fn bar(&self); + } + + // test skip(self) with async-await + #[async_trait] + pub trait TestC { + async fn baz(&self); + } + + #[derive(Debug)] + struct TestImpl(usize); + + #[async_trait] + impl TestA for TestImpl { + #[instrument] + async fn foo(&mut self, v: usize) { + self.baz().await; + self.0 = v; + self.bar().await + } + } + + #[async_trait] + impl TestB for TestImpl { + #[instrument] + async fn bar(&self) { + tracing::trace!(val = self.0); + } + } + + #[async_trait] + impl TestC for TestImpl { + #[instrument(skip(self))] + async fn baz(&self) { + tracing::trace!(val = self.0); + } + } + + let span = span::mock().named("foo"); + let span2 = span::mock().named("bar"); + let span3 = span::mock().named("baz"); + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone() + .with_field(field::mock("self")) + .with_field(field::mock("v")), + ) + .enter(span.clone()) + .new_span(span3.clone()) + .enter(span3.clone()) + .event(event::mock().with_fields(field::mock("val").with_value(&2u64))) + .exit(span3.clone()) + .drop_span(span3) + .new_span(span2.clone().with_field(field::mock("self"))) + .enter(span2.clone()) + .event(event::mock().with_fields(field::mock("val").with_value(&5u64))) + .exit(span2.clone()) + .drop_span(span2) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let mut test = TestImpl(2); + block_on_future(async { test.foo(5).await }); + }); + + handle.assert_finished(); +} + +#[test] +fn async_fn_with_async_trait_and_fields_expressions() { + use async_trait::async_trait; + + #[async_trait] + pub trait Test { + async fn call(&mut self, v: usize); + } + + #[derive(Clone, Debug)] + struct TestImpl; + + impl TestImpl { + fn foo(&self) -> usize { + 42 + } + } + + #[async_trait] + impl Test for TestImpl { + // check that self is correctly handled, even when using async_trait + #[instrument(fields(val=self.foo(), val2=Self::clone(self).foo(), test=%_v+5))] + async fn call(&mut self, _v: usize) {} + } + + let span = span::mock().named("call"); + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("_v") + .with_value(&5usize) + .and(field::mock("test").with_value(&tracing::field::debug(10))) + .and(field::mock("val").with_value(&42u64)) + .and(field::mock("val2").with_value(&42u64)), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + block_on_future(async { TestImpl.call(5).await }); + }); + + handle.assert_finished(); +} + +#[test] +fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() { + use async_trait::async_trait; + + #[async_trait] + pub trait Test { + async fn call(); + async fn call_with_self(&self); + async fn call_with_mut_self(&mut self); + } + + #[derive(Clone, Debug)] + struct TestImpl; + + // we also test sync functions that return futures, as they should be handled just like + // async-trait (>= 0.1.44) functions + impl TestImpl { + #[instrument(fields(Self=std::any::type_name::<Self>()))] + fn sync_fun(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> { + let val = self.clone(); + Box::pin(async move { + let _ = val; + }) + } + } + + #[async_trait] + impl Test for TestImpl { + // instrumenting this is currently not possible, see https://github.com/tokio-rs/tracing/issues/864#issuecomment-667508801 + //#[instrument(fields(Self=std::any::type_name::<Self>()))] + async fn call() {} + + #[instrument(fields(Self=std::any::type_name::<Self>()))] + async fn call_with_self(&self) { + self.sync_fun().await; + } + + #[instrument(fields(Self=std::any::type_name::<Self>()))] + async fn call_with_mut_self(&mut self) {} + } + + //let span = span::mock().named("call"); + let span2 = span::mock().named("call_with_self"); + let span3 = span::mock().named("call_with_mut_self"); + let span4 = span::mock().named("sync_fun"); + let (subscriber, handle) = subscriber::mock() + /*.new_span(span.clone() + .with_field( + field::mock("Self").with_value(&"TestImpler"))) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span)*/ + .new_span( + span2 + .clone() + .with_field(field::mock("Self").with_value(&std::any::type_name::<TestImpl>())), + ) + .enter(span2.clone()) + .new_span( + span4 + .clone() + .with_field(field::mock("Self").with_value(&std::any::type_name::<TestImpl>())), + ) + .enter(span4.clone()) + .exit(span4) + .exit(span2.clone()) + .drop_span(span2) + .new_span( + span3 + .clone() + .with_field(field::mock("Self").with_value(&std::any::type_name::<TestImpl>())), + ) + .enter(span3.clone()) + .exit(span3.clone()) + .drop_span(span3) + .done() + .run_with_handle(); + + with_default(subscriber, || { + block_on_future(async { + TestImpl::call().await; + TestImpl.call_with_self().await; + TestImpl.call_with_mut_self().await + }); + }); + + handle.assert_finished(); +} + +#[test] +fn out_of_scope_fields() { + // Reproduces tokio-rs/tracing#1296 + + struct Thing { + metrics: Arc<()>, + } + + impl Thing { + #[instrument(skip(self, _req), fields(app_id))] + fn call(&mut self, _req: ()) -> Pin<Box<dyn Future<Output = Arc<()>> + Send + Sync>> { + // ... + let metrics = self.metrics.clone(); + // ... + Box::pin(async move { + // ... + metrics // cannot find value `metrics` in this scope + }) + } + } + + let span = span::mock().named("call"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + block_on_future(async { + let mut my_thing = Thing { + metrics: Arc::new(()), + }; + my_thing.call(()).await; + }); + }); + + handle.assert_finished(); +} + +#[test] +fn manual_impl_future() { + #[allow(clippy::manual_async_fn)] + #[instrument] + fn manual_impl_future() -> impl Future<Output = ()> { + async { + tracing::trace!(poll = true); + } + } + + let span = span::mock().named("manual_impl_future"); + let poll_event = || event::mock().with_fields(field::mock("poll").with_value(&true)); + + let (subscriber, handle) = subscriber::mock() + // await manual_impl_future + .new_span(span.clone()) + .enter(span.clone()) + .event(poll_event()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + block_on_future(async { + manual_impl_future().await; + }); + }); + + handle.assert_finished(); +} + +#[test] +fn manual_box_pin() { + #[instrument] + fn manual_box_pin() -> Pin<Box<dyn Future<Output = ()>>> { + Box::pin(async { + tracing::trace!(poll = true); + }) + } + + let span = span::mock().named("manual_box_pin"); + let poll_event = || event::mock().with_fields(field::mock("poll").with_value(&true)); + + let (subscriber, handle) = subscriber::mock() + // await manual_box_pin + .new_span(span.clone()) + .enter(span.clone()) + .event(poll_event()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + block_on_future(async { + manual_box_pin().await; + }); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/destructuring.rs b/vendor/tracing-attributes/tests/destructuring.rs new file mode 100644 index 000000000..09cf1ad53 --- /dev/null +++ b/vendor/tracing-attributes/tests/destructuring.rs @@ -0,0 +1,213 @@ +use tracing::subscriber::with_default; +use tracing_attributes::instrument; +use tracing_mock::*; + +#[test] +fn destructure_tuples() { + #[instrument] + fn my_fn((arg1, arg2): (usize, usize)) {} + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("arg1") + .with_value(&format_args!("1")) + .and(field::mock("arg2").with_value(&format_args!("2"))) + .only(), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn((1, 2)); + }); + + handle.assert_finished(); +} + +#[test] +fn destructure_nested_tuples() { + #[instrument] + fn my_fn(((arg1, arg2), (arg3, arg4)): ((usize, usize), (usize, usize))) {} + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("arg1") + .with_value(&format_args!("1")) + .and(field::mock("arg2").with_value(&format_args!("2"))) + .and(field::mock("arg3").with_value(&format_args!("3"))) + .and(field::mock("arg4").with_value(&format_args!("4"))) + .only(), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(((1, 2), (3, 4))); + }); + + handle.assert_finished(); +} + +#[test] +fn destructure_refs() { + #[instrument] + fn my_fn(&arg1: &usize) {} + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone() + .with_field(field::mock("arg1").with_value(&1usize).only()), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(&1); + }); + + handle.assert_finished(); +} + +#[test] +fn destructure_tuple_structs() { + struct Foo(usize, usize); + + #[instrument] + fn my_fn(Foo(arg1, arg2): Foo) {} + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("arg1") + .with_value(&format_args!("1")) + .and(field::mock("arg2").with_value(&format_args!("2"))) + .only(), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(Foo(1, 2)); + }); + + handle.assert_finished(); +} + +#[test] +fn destructure_structs() { + struct Foo { + bar: usize, + baz: usize, + } + + #[instrument] + fn my_fn( + Foo { + bar: arg1, + baz: arg2, + }: Foo, + ) { + let _ = (arg1, arg2); + } + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("arg1") + .with_value(&format_args!("1")) + .and(field::mock("arg2").with_value(&format_args!("2"))) + .only(), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(Foo { bar: 1, baz: 2 }); + }); + + handle.assert_finished(); +} + +#[test] +fn destructure_everything() { + struct Foo { + bar: Bar, + baz: (usize, usize), + qux: NoDebug, + } + struct Bar((usize, usize)); + struct NoDebug; + + #[instrument] + fn my_fn( + &Foo { + bar: Bar((arg1, arg2)), + baz: (arg3, arg4), + .. + }: &Foo, + ) { + let _ = (arg1, arg2, arg3, arg4); + } + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("arg1") + .with_value(&format_args!("1")) + .and(field::mock("arg2").with_value(&format_args!("2"))) + .and(field::mock("arg3").with_value(&format_args!("3"))) + .and(field::mock("arg4").with_value(&format_args!("4"))) + .only(), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let foo = Foo { + bar: Bar((1, 2)), + baz: (3, 4), + qux: NoDebug, + }; + let _ = foo.qux; // to eliminate unused field warning + my_fn(&foo); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/err.rs b/vendor/tracing-attributes/tests/err.rs new file mode 100644 index 000000000..9e6d6b78c --- /dev/null +++ b/vendor/tracing-attributes/tests/err.rs @@ -0,0 +1,233 @@ +use tracing::subscriber::with_default; +use tracing::Level; +use tracing_attributes::instrument; +use tracing_mock::*; +use tracing_subscriber::filter::EnvFilter; +use tracing_subscriber::layer::SubscriberExt; + +use std::convert::TryFrom; +use std::num::TryFromIntError; + +#[instrument(err)] +fn err() -> Result<u8, TryFromIntError> { + u8::try_from(1234) +} + +#[instrument(err)] +fn err_suspicious_else() -> Result<u8, TryFromIntError> { + {} + u8::try_from(1234) +} + +#[test] +fn test() { + let span = span::mock().named("err"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event(event::mock().at_level(Level::ERROR)) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + with_default(subscriber, || err().ok()); + handle.assert_finished(); +} + +#[instrument(err)] +async fn err_async(polls: usize) -> Result<u8, TryFromIntError> { + let future = PollN::new_ok(polls); + tracing::trace!(awaiting = true); + future.await.ok(); + u8::try_from(1234) +} + +#[test] +fn test_async() { + let span = span::mock().named("err_async"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("awaiting").with_value(&true)) + .at_level(Level::TRACE), + ) + .exit(span.clone()) + .enter(span.clone()) + .event(event::mock().at_level(Level::ERROR)) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + with_default(subscriber, || { + block_on_future(async { err_async(2).await }).ok(); + }); + handle.assert_finished(); +} + +#[instrument(err)] +fn err_mut(out: &mut u8) -> Result<(), TryFromIntError> { + *out = u8::try_from(1234)?; + Ok(()) +} + +#[test] +fn test_mut() { + let span = span::mock().named("err_mut"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event(event::mock().at_level(Level::ERROR)) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + with_default(subscriber, || err_mut(&mut 0).ok()); + handle.assert_finished(); +} + +#[instrument(err)] +async fn err_mut_async(polls: usize, out: &mut u8) -> Result<(), TryFromIntError> { + let future = PollN::new_ok(polls); + tracing::trace!(awaiting = true); + future.await.ok(); + *out = u8::try_from(1234)?; + Ok(()) +} + +#[test] +fn test_mut_async() { + let span = span::mock().named("err_mut_async"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("awaiting").with_value(&true)) + .at_level(Level::TRACE), + ) + .exit(span.clone()) + .enter(span.clone()) + .event(event::mock().at_level(Level::ERROR)) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + with_default(subscriber, || { + block_on_future(async { err_mut_async(2, &mut 0).await }).ok(); + }); + handle.assert_finished(); +} + +#[test] +fn impl_trait_return_type() { + // Reproduces https://github.com/tokio-rs/tracing/issues/1227 + + #[instrument(err)] + fn returns_impl_trait(x: usize) -> Result<impl Iterator<Item = usize>, String> { + Ok(0..x) + } + + let span = span::mock().named("returns_impl_trait"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone() + .with_field(field::mock("x").with_value(&10usize).only()), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + for _ in returns_impl_trait(10).unwrap() { + // nop + } + }); + + handle.assert_finished(); +} + +#[instrument(err(Debug))] +fn err_dbg() -> Result<u8, TryFromIntError> { + u8::try_from(1234) +} + +#[test] +fn test_err_dbg() { + let span = span::mock().named("err_dbg"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock().at_level(Level::ERROR).with_fields( + field::mock("error") + // use the actual error value that will be emitted, so + // that this test doesn't break if the standard library + // changes the `fmt::Debug` output from the error type + // in the future. + .with_value(&tracing::field::debug(u8::try_from(1234).unwrap_err())), + ), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + with_default(subscriber, || err_dbg().ok()); + handle.assert_finished(); +} + +#[test] +fn test_err_display_default() { + let span = span::mock().named("err"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock().at_level(Level::ERROR).with_fields( + field::mock("error") + // by default, errors will be emitted with their display values + .with_value(&tracing::field::display(u8::try_from(1234).unwrap_err())), + ), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + with_default(subscriber, || err().ok()); + handle.assert_finished(); +} + +#[test] +fn test_err_custom_target() { + let filter: EnvFilter = "my_target=error".parse().expect("filter should parse"); + let span = span::mock().named("error_span").with_target("my_target"); + + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .at_level(Level::ERROR) + .with_target("my_target"), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + let error_span = tracing::error_span!(target: "my_target", "error_span"); + + { + let _enter = error_span.enter(); + tracing::error!(target: "my_target", "This should display") + } + }); + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/fields.rs b/vendor/tracing-attributes/tests/fields.rs new file mode 100644 index 000000000..c178fbb3d --- /dev/null +++ b/vendor/tracing-attributes/tests/fields.rs @@ -0,0 +1,160 @@ +use tracing::subscriber::with_default; +use tracing_attributes::instrument; +use tracing_mock::field::mock; +use tracing_mock::span::NewSpan; +use tracing_mock::*; + +#[instrument(fields(foo = "bar", dsa = true, num = 1))] +fn fn_no_param() {} + +#[instrument(fields(foo = "bar"))] +fn fn_param(param: u32) {} + +#[instrument(fields(foo = "bar", empty))] +fn fn_empty_field() {} + +#[instrument(fields(len = s.len()))] +fn fn_expr_field(s: &str) {} + +#[instrument(fields(s.len = s.len(), s.is_empty = s.is_empty()))] +fn fn_two_expr_fields(s: &str) { + let _ = s; +} + +#[instrument(fields(%s, s.len = s.len()))] +fn fn_clashy_expr_field(s: &str) { + let _ = s; +} + +#[instrument(fields(s = "s"))] +fn fn_clashy_expr_field2(s: &str) { + let _ = s; +} + +#[instrument(fields(s = &s))] +fn fn_string(s: String) { + let _ = s; +} + +#[derive(Debug)] +struct HasField { + my_field: &'static str, +} + +impl HasField { + #[instrument(fields(my_field = self.my_field), skip(self))] + fn self_expr_field(&self) {} +} + +#[test] +fn fields() { + let span = span::mock().with_field( + mock("foo") + .with_value(&"bar") + .and(mock("dsa").with_value(&true)) + .and(mock("num").with_value(&1)) + .only(), + ); + run_test(span, || { + fn_no_param(); + }); +} + +#[test] +fn expr_field() { + let span = span::mock().with_field( + mock("s") + .with_value(&"hello world") + .and(mock("len").with_value(&"hello world".len())) + .only(), + ); + run_test(span, || { + fn_expr_field("hello world"); + }); +} + +#[test] +fn two_expr_fields() { + let span = span::mock().with_field( + mock("s") + .with_value(&"hello world") + .and(mock("s.len").with_value(&"hello world".len())) + .and(mock("s.is_empty").with_value(&false)) + .only(), + ); + run_test(span, || { + fn_two_expr_fields("hello world"); + }); +} + +#[test] +fn clashy_expr_field() { + let span = span::mock().with_field( + // Overriding the `s` field should record `s` as a `Display` value, + // rather than as a `Debug` value. + mock("s") + .with_value(&tracing::field::display("hello world")) + .and(mock("s.len").with_value(&"hello world".len())) + .only(), + ); + run_test(span, || { + fn_clashy_expr_field("hello world"); + }); + + let span = span::mock().with_field(mock("s").with_value(&"s").only()); + run_test(span, || { + fn_clashy_expr_field2("hello world"); + }); +} + +#[test] +fn self_expr_field() { + let span = span::mock().with_field(mock("my_field").with_value(&"hello world").only()); + run_test(span, || { + let has_field = HasField { + my_field: "hello world", + }; + has_field.self_expr_field(); + }); +} + +#[test] +fn parameters_with_fields() { + let span = span::mock().with_field( + mock("foo") + .with_value(&"bar") + .and(mock("param").with_value(&1u32)) + .only(), + ); + run_test(span, || { + fn_param(1); + }); +} + +#[test] +fn empty_field() { + let span = span::mock().with_field(mock("foo").with_value(&"bar").only()); + run_test(span, || { + fn_empty_field(); + }); +} + +#[test] +fn string_field() { + let span = span::mock().with_field(mock("s").with_value(&"hello world").only()); + run_test(span, || { + fn_string(String::from("hello world")); + }); +} + +fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) { + let (subscriber, handle) = subscriber::mock() + .new_span(span) + .enter(span::mock()) + .exit(span::mock()) + .done() + .run_with_handle(); + + with_default(subscriber, fun); + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/follows_from.rs b/vendor/tracing-attributes/tests/follows_from.rs new file mode 100644 index 000000000..da0eec635 --- /dev/null +++ b/vendor/tracing-attributes/tests/follows_from.rs @@ -0,0 +1,99 @@ +use tracing::{subscriber::with_default, Id, Level, Span}; +use tracing_attributes::instrument; +use tracing_mock::*; + +#[instrument(follows_from = causes, skip(causes))] +fn with_follows_from_sync(causes: impl IntoIterator<Item = impl Into<Option<Id>>>) {} + +#[instrument(follows_from = causes, skip(causes))] +async fn with_follows_from_async(causes: impl IntoIterator<Item = impl Into<Option<Id>>>) {} + +#[instrument(follows_from = [&Span::current()])] +fn follows_from_current() {} + +#[test] +fn follows_from_sync_test() { + let cause_a = span::mock().named("cause_a"); + let cause_b = span::mock().named("cause_b"); + let cause_c = span::mock().named("cause_c"); + let consequence = span::mock().named("with_follows_from_sync"); + + let (subscriber, handle) = subscriber::mock() + .new_span(cause_a.clone()) + .new_span(cause_b.clone()) + .new_span(cause_c.clone()) + .new_span(consequence.clone()) + .follows_from(consequence.clone(), cause_a) + .follows_from(consequence.clone(), cause_b) + .follows_from(consequence.clone(), cause_c) + .enter(consequence.clone()) + .exit(consequence) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let cause_a = tracing::span!(Level::TRACE, "cause_a"); + let cause_b = tracing::span!(Level::TRACE, "cause_b"); + let cause_c = tracing::span!(Level::TRACE, "cause_c"); + + with_follows_from_sync(&[cause_a, cause_b, cause_c]) + }); + + handle.assert_finished(); +} + +#[test] +fn follows_from_async_test() { + let cause_a = span::mock().named("cause_a"); + let cause_b = span::mock().named("cause_b"); + let cause_c = span::mock().named("cause_c"); + let consequence = span::mock().named("with_follows_from_async"); + + let (subscriber, handle) = subscriber::mock() + .new_span(cause_a.clone()) + .new_span(cause_b.clone()) + .new_span(cause_c.clone()) + .new_span(consequence.clone()) + .follows_from(consequence.clone(), cause_a) + .follows_from(consequence.clone(), cause_b) + .follows_from(consequence.clone(), cause_c) + .enter(consequence.clone()) + .exit(consequence) + .done() + .run_with_handle(); + + with_default(subscriber, || { + block_on_future(async { + let cause_a = tracing::span!(Level::TRACE, "cause_a"); + let cause_b = tracing::span!(Level::TRACE, "cause_b"); + let cause_c = tracing::span!(Level::TRACE, "cause_c"); + + with_follows_from_async(&[cause_a, cause_b, cause_c]).await + }) + }); + + handle.assert_finished(); +} + +#[test] +fn follows_from_current_test() { + let cause = span::mock().named("cause"); + let consequence = span::mock().named("follows_from_current"); + + let (subscriber, handle) = subscriber::mock() + .new_span(cause.clone()) + .enter(cause.clone()) + .new_span(consequence.clone()) + .follows_from(consequence.clone(), cause.clone()) + .enter(consequence.clone()) + .exit(consequence) + .exit(cause) + .done() + .run_with_handle(); + + with_default(subscriber, || { + tracing::span!(Level::TRACE, "cause").in_scope(follows_from_current) + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/instrument.rs b/vendor/tracing-attributes/tests/instrument.rs new file mode 100644 index 000000000..2b2fee71e --- /dev/null +++ b/vendor/tracing-attributes/tests/instrument.rs @@ -0,0 +1,243 @@ +use tracing::subscriber::with_default; +use tracing::Level; +use tracing_attributes::instrument; +use tracing_mock::*; + +#[test] +fn override_everything() { + #[instrument(target = "my_target", level = "debug")] + fn my_fn() {} + + #[instrument(level = "debug", target = "my_target")] + fn my_other_fn() {} + + let span = span::mock() + .named("my_fn") + .at_level(Level::DEBUG) + .with_target("my_target"); + let span2 = span::mock() + .named("my_other_fn") + .at_level(Level::DEBUG) + .with_target("my_target"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .new_span(span2.clone()) + .enter(span2.clone()) + .exit(span2.clone()) + .drop_span(span2) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(); + my_other_fn(); + }); + + handle.assert_finished(); +} + +#[test] +fn fields() { + #[instrument(target = "my_target", level = "debug")] + fn my_fn(arg1: usize, arg2: bool) {} + + let span = span::mock() + .named("my_fn") + .at_level(Level::DEBUG) + .with_target("my_target"); + + let span2 = span::mock() + .named("my_fn") + .at_level(Level::DEBUG) + .with_target("my_target"); + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("arg1") + .with_value(&2usize) + .and(field::mock("arg2").with_value(&false)) + .only(), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .new_span( + span2.clone().with_field( + field::mock("arg1") + .with_value(&3usize) + .and(field::mock("arg2").with_value(&true)) + .only(), + ), + ) + .enter(span2.clone()) + .exit(span2.clone()) + .drop_span(span2) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(2, false); + my_fn(3, true); + }); + + handle.assert_finished(); +} + +#[test] +fn skip() { + struct UnDebug(pub u32); + + #[instrument(target = "my_target", level = "debug", skip(_arg2, _arg3))] + fn my_fn(arg1: usize, _arg2: UnDebug, _arg3: UnDebug) {} + + #[instrument(target = "my_target", level = "debug", skip_all)] + fn my_fn2(_arg1: usize, _arg2: UnDebug, _arg3: UnDebug) {} + + let span = span::mock() + .named("my_fn") + .at_level(Level::DEBUG) + .with_target("my_target"); + + let span2 = span::mock() + .named("my_fn") + .at_level(Level::DEBUG) + .with_target("my_target"); + + let span3 = span::mock() + .named("my_fn2") + .at_level(Level::DEBUG) + .with_target("my_target"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone() + .with_field(field::mock("arg1").with_value(&2usize).only()), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .new_span( + span2 + .clone() + .with_field(field::mock("arg1").with_value(&3usize).only()), + ) + .enter(span2.clone()) + .exit(span2.clone()) + .drop_span(span2) + .new_span(span3.clone()) + .enter(span3.clone()) + .exit(span3.clone()) + .drop_span(span3) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(2, UnDebug(0), UnDebug(1)); + my_fn(3, UnDebug(0), UnDebug(1)); + my_fn2(2, UnDebug(0), UnDebug(1)); + }); + + handle.assert_finished(); +} + +#[test] +fn generics() { + #[derive(Debug)] + struct Foo; + + #[instrument] + fn my_fn<S, T: std::fmt::Debug>(arg1: S, arg2: T) + where + S: std::fmt::Debug, + { + } + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("arg1") + .with_value(&format_args!("Foo")) + .and(field::mock("arg2").with_value(&format_args!("false"))), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + my_fn(Foo, false); + }); + + handle.assert_finished(); +} + +#[test] +fn methods() { + #[derive(Debug)] + struct Foo; + + impl Foo { + #[instrument] + fn my_fn(&self, arg1: usize) {} + } + + let span = span::mock().named("my_fn"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone().with_field( + field::mock("self") + .with_value(&format_args!("Foo")) + .and(field::mock("arg1").with_value(&42usize)), + ), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let foo = Foo; + foo.my_fn(42); + }); + + handle.assert_finished(); +} + +#[test] +fn impl_trait_return_type() { + #[instrument] + fn returns_impl_trait(x: usize) -> impl Iterator<Item = usize> { + 0..x + } + + let span = span::mock().named("returns_impl_trait"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + span.clone() + .with_field(field::mock("x").with_value(&10usize).only()), + ) + .enter(span.clone()) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || { + for _ in returns_impl_trait(10) { + // nop + } + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/levels.rs b/vendor/tracing-attributes/tests/levels.rs new file mode 100644 index 000000000..b074ea4f2 --- /dev/null +++ b/vendor/tracing-attributes/tests/levels.rs @@ -0,0 +1,96 @@ +use tracing::subscriber::with_default; +use tracing::Level; +use tracing_attributes::instrument; +use tracing_mock::*; + +#[test] +fn named_levels() { + #[instrument(level = "trace")] + fn trace() {} + + #[instrument(level = "Debug")] + fn debug() {} + + #[instrument(level = "INFO")] + fn info() {} + + #[instrument(level = "WARn")] + fn warn() {} + + #[instrument(level = "eRrOr")] + fn error() {} + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("trace").at_level(Level::TRACE)) + .enter(span::mock().named("trace").at_level(Level::TRACE)) + .exit(span::mock().named("trace").at_level(Level::TRACE)) + .new_span(span::mock().named("debug").at_level(Level::DEBUG)) + .enter(span::mock().named("debug").at_level(Level::DEBUG)) + .exit(span::mock().named("debug").at_level(Level::DEBUG)) + .new_span(span::mock().named("info").at_level(Level::INFO)) + .enter(span::mock().named("info").at_level(Level::INFO)) + .exit(span::mock().named("info").at_level(Level::INFO)) + .new_span(span::mock().named("warn").at_level(Level::WARN)) + .enter(span::mock().named("warn").at_level(Level::WARN)) + .exit(span::mock().named("warn").at_level(Level::WARN)) + .new_span(span::mock().named("error").at_level(Level::ERROR)) + .enter(span::mock().named("error").at_level(Level::ERROR)) + .exit(span::mock().named("error").at_level(Level::ERROR)) + .done() + .run_with_handle(); + + with_default(subscriber, || { + trace(); + debug(); + info(); + warn(); + error(); + }); + + handle.assert_finished(); +} + +#[test] +fn numeric_levels() { + #[instrument(level = 1)] + fn trace() {} + + #[instrument(level = 2)] + fn debug() {} + + #[instrument(level = 3)] + fn info() {} + + #[instrument(level = 4)] + fn warn() {} + + #[instrument(level = 5)] + fn error() {} + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("trace").at_level(Level::TRACE)) + .enter(span::mock().named("trace").at_level(Level::TRACE)) + .exit(span::mock().named("trace").at_level(Level::TRACE)) + .new_span(span::mock().named("debug").at_level(Level::DEBUG)) + .enter(span::mock().named("debug").at_level(Level::DEBUG)) + .exit(span::mock().named("debug").at_level(Level::DEBUG)) + .new_span(span::mock().named("info").at_level(Level::INFO)) + .enter(span::mock().named("info").at_level(Level::INFO)) + .exit(span::mock().named("info").at_level(Level::INFO)) + .new_span(span::mock().named("warn").at_level(Level::WARN)) + .enter(span::mock().named("warn").at_level(Level::WARN)) + .exit(span::mock().named("warn").at_level(Level::WARN)) + .new_span(span::mock().named("error").at_level(Level::ERROR)) + .enter(span::mock().named("error").at_level(Level::ERROR)) + .exit(span::mock().named("error").at_level(Level::ERROR)) + .done() + .run_with_handle(); + + with_default(subscriber, || { + trace(); + debug(); + info(); + warn(); + error(); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/names.rs b/vendor/tracing-attributes/tests/names.rs new file mode 100644 index 000000000..d97dece9a --- /dev/null +++ b/vendor/tracing-attributes/tests/names.rs @@ -0,0 +1,63 @@ +use tracing::subscriber::with_default; +use tracing_attributes::instrument; +use tracing_mock::*; + +#[instrument] +fn default_name() {} + +#[instrument(name = "my_name")] +fn custom_name() {} + +// XXX: it's weird that we support both of these forms, but apparently we +// managed to release a version that accepts both syntax, so now we have to +// support it! yay! +#[instrument("my_other_name")] +fn custom_name_no_equals() {} + +#[test] +fn default_name_test() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("default_name")) + .enter(span::mock().named("default_name")) + .exit(span::mock().named("default_name")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + default_name(); + }); + + handle.assert_finished(); +} + +#[test] +fn custom_name_test() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("my_name")) + .enter(span::mock().named("my_name")) + .exit(span::mock().named("my_name")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + custom_name(); + }); + + handle.assert_finished(); +} + +#[test] +fn custom_name_no_equals_test() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("my_other_name")) + .enter(span::mock().named("my_other_name")) + .exit(span::mock().named("my_other_name")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + custom_name_no_equals(); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/parents.rs b/vendor/tracing-attributes/tests/parents.rs new file mode 100644 index 000000000..7069b98ea --- /dev/null +++ b/vendor/tracing-attributes/tests/parents.rs @@ -0,0 +1,102 @@ +use tracing::{subscriber::with_default, Id, Level}; +use tracing_attributes::instrument; +use tracing_mock::*; + +#[instrument] +fn with_default_parent() {} + +#[instrument(parent = parent_span, skip(parent_span))] +fn with_explicit_parent<P>(parent_span: P) +where + P: Into<Option<Id>>, +{ +} + +#[test] +fn default_parent_test() { + let contextual_parent = span::mock().named("contextual_parent"); + let child = span::mock().named("with_default_parent"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + contextual_parent + .clone() + .with_contextual_parent(None) + .with_explicit_parent(None), + ) + .new_span( + child + .clone() + .with_contextual_parent(Some("contextual_parent")) + .with_explicit_parent(None), + ) + .enter(child.clone()) + .exit(child.clone()) + .enter(contextual_parent.clone()) + .new_span( + child + .clone() + .with_contextual_parent(Some("contextual_parent")) + .with_explicit_parent(None), + ) + .enter(child.clone()) + .exit(child) + .exit(contextual_parent) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let contextual_parent = tracing::span!(Level::TRACE, "contextual_parent"); + + with_default_parent(); + + contextual_parent.in_scope(|| { + with_default_parent(); + }); + }); + + handle.assert_finished(); +} + +#[test] +fn explicit_parent_test() { + let contextual_parent = span::mock().named("contextual_parent"); + let explicit_parent = span::mock().named("explicit_parent"); + let child = span::mock().named("with_explicit_parent"); + + let (subscriber, handle) = subscriber::mock() + .new_span( + contextual_parent + .clone() + .with_contextual_parent(None) + .with_explicit_parent(None), + ) + .new_span( + explicit_parent + .with_contextual_parent(None) + .with_explicit_parent(None), + ) + .enter(contextual_parent.clone()) + .new_span( + child + .clone() + .with_contextual_parent(Some("contextual_parent")) + .with_explicit_parent(Some("explicit_parent")), + ) + .enter(child.clone()) + .exit(child) + .exit(contextual_parent) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let contextual_parent = tracing::span!(Level::INFO, "contextual_parent"); + let explicit_parent = tracing::span!(Level::INFO, "explicit_parent"); + + contextual_parent.in_scope(|| { + with_explicit_parent(&explicit_parent); + }); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/ret.rs b/vendor/tracing-attributes/tests/ret.rs new file mode 100644 index 000000000..cfd2de10d --- /dev/null +++ b/vendor/tracing-attributes/tests/ret.rs @@ -0,0 +1,255 @@ +use std::convert::TryFrom; +use std::num::TryFromIntError; +use tracing_mock::*; + +use tracing::{subscriber::with_default, Level}; +use tracing_attributes::instrument; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::EnvFilter; + +#[instrument(ret)] +fn ret() -> i32 { + 42 +} + +#[instrument(target = "my_target", ret)] +fn ret_with_target() -> i32 { + 42 +} + +#[test] +fn test() { + let span = span::mock().named("ret"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("return").with_value(&tracing::field::debug(42))) + .at_level(Level::INFO), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, ret); + handle.assert_finished(); +} + +#[test] +fn test_custom_target() { + let filter: EnvFilter = "my_target=info".parse().expect("filter should parse"); + let span = span::mock() + .named("ret_with_target") + .with_target("my_target"); + + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("return").with_value(&tracing::field::debug(42))) + .at_level(Level::INFO) + .with_target("my_target"), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + let subscriber = subscriber.with(filter); + + with_default(subscriber, ret_with_target); + handle.assert_finished(); +} + +#[instrument(level = "warn", ret)] +fn ret_warn() -> i32 { + 42 +} + +#[test] +fn test_warn() { + let span = span::mock().named("ret_warn"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("return").with_value(&tracing::field::debug(42))) + .at_level(Level::WARN), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, ret_warn); + handle.assert_finished(); +} + +#[instrument(ret)] +fn ret_mut(a: &mut i32) -> i32 { + *a *= 2; + tracing::info!(?a); + *a +} + +#[test] +fn test_mut() { + let span = span::mock().named("ret_mut"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("a").with_value(&tracing::field::display(2))) + .at_level(Level::INFO), + ) + .event( + event::mock() + .with_fields(field::mock("return").with_value(&tracing::field::debug(2))) + .at_level(Level::INFO), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || ret_mut(&mut 1)); + handle.assert_finished(); +} + +#[instrument(ret)] +async fn ret_async() -> i32 { + 42 +} + +#[test] +fn test_async() { + let span = span::mock().named("ret_async"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("return").with_value(&tracing::field::debug(42))) + .at_level(Level::INFO), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || block_on_future(async { ret_async().await })); + handle.assert_finished(); +} + +#[instrument(ret)] +fn ret_impl_type() -> impl Copy { + 42 +} + +#[test] +fn test_impl_type() { + let span = span::mock().named("ret_impl_type"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("return").with_value(&tracing::field::debug(42))) + .at_level(Level::INFO), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, ret_impl_type); + handle.assert_finished(); +} + +#[instrument(ret(Display))] +fn ret_display() -> i32 { + 42 +} + +#[test] +fn test_dbg() { + let span = span::mock().named("ret_display"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields(field::mock("return").with_value(&tracing::field::display(42))) + .at_level(Level::INFO), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, ret_display); + handle.assert_finished(); +} + +#[instrument(err, ret)] +fn ret_and_err() -> Result<u8, TryFromIntError> { + u8::try_from(1234) +} + +#[test] +fn test_ret_and_err() { + let span = span::mock().named("ret_and_err"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields( + field::mock("error") + .with_value(&tracing::field::display(u8::try_from(1234).unwrap_err())) + .only(), + ) + .at_level(Level::ERROR), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || ret_and_err().ok()); + handle.assert_finished(); +} + +#[instrument(err, ret)] +fn ret_and_ok() -> Result<u8, TryFromIntError> { + u8::try_from(123) +} + +#[test] +fn test_ret_and_ok() { + let span = span::mock().named("ret_and_ok"); + let (subscriber, handle) = subscriber::mock() + .new_span(span.clone()) + .enter(span.clone()) + .event( + event::mock() + .with_fields( + field::mock("return") + .with_value(&tracing::field::debug(u8::try_from(123).unwrap())) + .only(), + ) + .at_level(Level::INFO), + ) + .exit(span.clone()) + .drop_span(span) + .done() + .run_with_handle(); + + with_default(subscriber, || ret_and_ok().ok()); + handle.assert_finished(); +} diff --git a/vendor/tracing-attributes/tests/targets.rs b/vendor/tracing-attributes/tests/targets.rs new file mode 100644 index 000000000..363f628f3 --- /dev/null +++ b/vendor/tracing-attributes/tests/targets.rs @@ -0,0 +1,97 @@ +use tracing::subscriber::with_default; +use tracing_attributes::instrument; +use tracing_mock::*; + +#[instrument] +fn default_target() {} + +#[instrument(target = "my_target")] +fn custom_target() {} + +mod my_mod { + use tracing_attributes::instrument; + + pub const MODULE_PATH: &str = module_path!(); + + #[instrument] + pub fn default_target() {} + + #[instrument(target = "my_other_target")] + pub fn custom_target() {} +} + +#[test] +fn default_targets() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock() + .named("default_target") + .with_target(module_path!()), + ) + .enter( + span::mock() + .named("default_target") + .with_target(module_path!()), + ) + .exit( + span::mock() + .named("default_target") + .with_target(module_path!()), + ) + .new_span( + span::mock() + .named("default_target") + .with_target(my_mod::MODULE_PATH), + ) + .enter( + span::mock() + .named("default_target") + .with_target(my_mod::MODULE_PATH), + ) + .exit( + span::mock() + .named("default_target") + .with_target(my_mod::MODULE_PATH), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + default_target(); + my_mod::default_target(); + }); + + handle.assert_finished(); +} + +#[test] +fn custom_targets() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("custom_target").with_target("my_target")) + .enter(span::mock().named("custom_target").with_target("my_target")) + .exit(span::mock().named("custom_target").with_target("my_target")) + .new_span( + span::mock() + .named("custom_target") + .with_target("my_other_target"), + ) + .enter( + span::mock() + .named("custom_target") + .with_target("my_other_target"), + ) + .exit( + span::mock() + .named("custom_target") + .with_target("my_other_target"), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + custom_target(); + my_mod::custom_target(); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing-core/.cargo-checksum.json b/vendor/tracing-core/.cargo-checksum.json new file mode 100644 index 000000000..157a61ad4 --- /dev/null +++ b/vendor/tracing-core/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"291325b2d5ae0101a9cb9dadfa64810c1c98d04f43c7c1fcee4dc585cb34cf37","Cargo.toml":"d4029f638b3bbb553470ab93080fe78a155db13780e8e52d073d6ca10939614d","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"31f1a3504716da08f771f2fd10e06298a1fd6ecfdc2c78fd1cd66cce693d4e1c","src/callsite.rs":"6a69e08ba8e70234f4ca2a5c6215cdb5ce8d1b0e637102501787169448cbf2a1","src/dispatcher.rs":"5ffade623f3f19a7c497bc72da2d15fcdbd1ab65b1f393c9a9271dd742d99867","src/event.rs":"f2673bf5d266972e567e521c9cd92fb33f28b0c7e010937e3bc2bf9eb483087f","src/field.rs":"ec7f20bdb7a9d79b900777fc444ce82282bdfc4be4afe6223e2b7617da63964d","src/lazy.rs":"318e3558e4446abf26294287167c0788e343044a28072f9217bd985929809087","src/lib.rs":"088b29ecce4bdb5b68df9cbe3984f4b22d7124988866d5aace0e7029ea033d58","src/metadata.rs":"1019ffecd3d8a3636708c6654f34074c041af88b6221c4950037c6bbf09af835","src/parent.rs":"5d5ad733343280a64a1feb6a008e186c39305ec554f14279012b8d7915821471","src/span.rs":"dcf2135e4ca158c1be45007f0be25426c375a4081f8f3c5a4d7f7382d8a950a4","src/spin/LICENSE":"58545fed1565e42d687aecec6897d35c6d37ccb71479a137c0deb2203e125c79","src/spin/mod.rs":"c458ce5e875acb7fbfb279f23254f4924d7c6d6fee419b740800d2e8087d1524","src/spin/mutex.rs":"4d30ff2b59b18fd7909f016e1abdf9aa0c04aa11d047a46e98cffe1319e32dad","src/spin/once.rs":"3781fd4eae0db04d80c03a039906c99b1e01d1583b29ac0144e6fbbd5a0fef0b","src/stdlib.rs":"698693062b8109cace3ffea02e9c2372b7d5b5d43c0b11cb4800b0e7b1a69971","src/subscriber.rs":"916d315ca324a752f70fc3cc557089418a63779ceda54955f55f0ab3c1f3fc7b","tests/common/mod.rs":"0bbb217baa17df0f96cc1ff57dfa74ccc5a959e7f66b15bb7d25d5f43358a278","tests/dispatch.rs":"d3f000fab43734a854c82a7783142910c5e79f806cbd3f8ec5eded598c59ddb1","tests/global_dispatch.rs":"cdc05d77e448ee8b50bfb930abafa3f19b4c6f922b7bebc7797fa1dbdaa1d398","tests/macros.rs":"b1603d888b349c8d103794deceec3b1ae4538b8d3eba805f3f561899e8ad0dd2"},"package":"7b7358be39f2f274f322d2aaed611acc57f382e8eb1e5b48cb9ae30933495ce7"}
\ No newline at end of file diff --git a/vendor/tracing-core/CHANGELOG.md b/vendor/tracing-core/CHANGELOG.md new file mode 100644 index 000000000..c6afcb077 --- /dev/null +++ b/vendor/tracing-core/CHANGELOG.md @@ -0,0 +1,454 @@ +# 0.1.28 (June 23, 2022) + +This release of `tracing-core` adds new `Value` implementations, including one +for `String`, to allow recording `&String` as a value without having to call +`as_str()` or similar, and for 128-bit integers (`i128` and `u128`). In +addition, it adds new methods and trait implementations for `Subscriber`s. + +### Added + +- `Value` implementation for `String` ([#2164]) +- `Value` implementation for `u128` and `i28` ([#2166]) +- `downcast_ref` and `is` methods for `dyn Subscriber + Sync`, + `dyn Subscriber + Send`, and `dyn Subscriber + Send + Sync` ([#2160]) +- `Subscriber::event_enabled` method to enable filtering based on `Event` field + values ([#2008]) +- `Subscriber` implementation for `Box<S: Subscriber + ?Sized>` and + `Arc<S: Subscriber + ?Sized>` ([#2161]) + +Thanks to @jswrenn and @CAD97 for contributing to this release! + +[#2164]: https://github.com/tokio-rs/tracing/pull/2164 +[#2166]: https://github.com/tokio-rs/tracing/pull/2166 +[#2160]: https://github.com/tokio-rs/tracing/pull/2160 +[#2008]: https://github.com/tokio-rs/tracing/pull/2008 +[#2161]: https://github.com/tokio-rs/tracing/pull/2161 + +# 0.1.27 (June 7, 2022) + +This release of `tracing-core` introduces a new `DefaultCallsite` type, which +can be used by instrumentation crates rather than implementing their own +callsite types. Using `DefaultCallsite` may offer reduced overhead from callsite +registration. + +### Added + +- `DefaultCallsite`, a pre-written `Callsite` implementation for use in + instrumentation crates ([#2083]) +- `ValueSet::len` and `Record::len` methods returning the number of fields in a + `ValueSet` or `Record` ([#2152]) + +### Changed + +- Replaced `lazy_static` dependency with `once_cell` ([#2147]) + +### Documented + +- Added documentation to the `callsite` module ([#2088], [#2149]) + +Thanks to new contributors @jamesmunns and @james7132 for contributing to this +release! + +[#2083]: https://github.com/tokio-rs/tracing/pull/2083 +[#2152]: https://github.com/tokio-rs/tracing/pull/2152 +[#2147]: https://github.com/tokio-rs/tracing/pull/2147 +[#2088]: https://github.com/tokio-rs/tracing/pull/2088 +[#2149]: https://github.com/tokio-rs/tracing/pull/2149 + +# 0.1.26 (April 14, 2022) + +This release adds a `Value` implementation for `Box<T: Value>` to allow +recording boxed values more conveniently. In particular, this should improve +the ergonomics of the implementations for `dyn std::error::Error` trait objects, +including those added in [v0.1.25]. + +### Added + +- `Value` implementation for `Box<T> where T: Value` ([#2071]) + +### Fixed + +- Broken documentation links ([#2068]) + +Thanks to new contributor @ben0x539 for contributing to this release! + + +[v0.1.25]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.25 +[#2071]: https://github.com/tokio-rs/tracing/pull/2071 +[#2068]: https://github.com/tokio-rs/tracing/pull/2068 + +# 0.1.25 (April 12, 2022) + +This release adds additional `Value` implementations for `std::error::Error` +trait objects with auto trait bounds (`Send` and `Sync`), as Rust will not +auto-coerce trait objects. Additionally, it fixes a bug when setting scoped +dispatchers that was introduced in the previous release ([v0.1.24]). + +### Added + +- `Value` implementations for `dyn Error + Send + 'static`, `dyn Error + Send + + Sync + 'static`, `dyn Error + Sync + 'static` ([#2066]) + +### Fixed + +- Failure to use the global default dispatcher if a thread has set a scoped + default prior to setting the global default, and unset the scoped default + after setting the global default ([#2065]) + +Thanks to @lilyball for contributing to this release! + +[v0.1.24]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.24 +[#2066]: https://github.com/tokio-rs/tracing/pull/2066 +[#2065]: https://github.com/tokio-rs/tracing/pull/2065 + +# 0.1.24 (April 1, 2022) + +This release fixes a bug where setting `NoSubscriber` as the local default would +not disable the global default subscriber locally. + +### Fixed + +- Setting `NoSubscriber` as the local default now correctly disables the global + default subscriber ([#2001]) +- Fixed compilation warnings with the "std" feature disabled ([#2022]) + +### Changed + +- Removed unnecessary use of `write!` and `format_args!` macros ([#1988]) + +[#1988]: https://github.com/tokio-rs/tracing/pull/1988 +[#2001]: https://github.com/tokio-rs/tracing/pull/2001 +[#2022]: https://github.com/tokio-rs/tracing/pull/2022 + +# 0.1.23 (March 8, 2022) + +### Changed + +- Removed `#[inline]` attributes from some `Dispatch` methods whose + callers are now inlined ([#1974]) +- Bumped minimum supported Rust version (MSRV) to Rust 1.49.0 ([#1913]) + +[#1913]: https://github.com/tokio-rs/tracing/pull/1913 +[#1974]: https://github.com/tokio-rs/tracing/pull/1974 + +# 0.1.22 (February 3, 2022) + +This release adds *experimental* support for recording structured field values +using the [`valuable`] crate. See [this blog post][post] for details on +`valuable`. + +Note that `valuable` support currently requires `--cfg tracing_unstable`. See +the documentation for details. + +### Added + +- **field**: Experimental support for recording field values using the + [`valuable`] crate ([#1608], [#1888], [#1887]) +- **field**: Added `ValueSet::record` method ([#1823]) +- **subscriber**: `Default` impl for `NoSubscriber` ([#1785]) +- **metadata**: New `Kind::HINT` to support the `enabled!` macro in `tracing` + ([#1883], [#1891]) +### Fixed + +- Fixed a number of documentation issues ([#1665], [#1692], [#1737]) + +Thanks to @xd009642, @Skepfyr, @guswynn, @Folyd, and @mbergkvist for +contributing to this release! + +[`valuable`]: https://crates.io/crates/valuable +[post]: https://tokio.rs/blog/2021-05-valuable +[#1608]: https://github.com/tokio-rs/tracing/pull/1608 +[#1888]: https://github.com/tokio-rs/tracing/pull/1888 +[#1887]: https://github.com/tokio-rs/tracing/pull/1887 +[#1823]: https://github.com/tokio-rs/tracing/pull/1823 +[#1785]: https://github.com/tokio-rs/tracing/pull/1785 +[#1883]: https://github.com/tokio-rs/tracing/pull/1883 +[#1891]: https://github.com/tokio-rs/tracing/pull/1891 +[#1665]: https://github.com/tokio-rs/tracing/pull/1665 +[#1692]: https://github.com/tokio-rs/tracing/pull/1692 +[#1737]: https://github.com/tokio-rs/tracing/pull/1737 + +# 0.1.21 (October 1, 2021) + +This release adds support for recording `Option<T> where T: Value` as typed +`tracing` field values. + +### Added + +- **field**: `Value` impl for `Option<T> where T: Value` ([#1585]) + +### Fixed + +- Fixed deprecation warnings when building with `default-features` disabled + ([#1603], [#1606]) +- Documentation fixes and improvements ([#1595], [#1601]) + +Thanks to @brianburgers, @DCjanus, and @matklad for contributing to this +release! + +[#1585]: https://github.com/tokio-rs/tracing/pull/1585 +[#1595]: https://github.com/tokio-rs/tracing/pull/1595 +[#1601]: https://github.com/tokio-rs/tracing/pull/1601 +[#1603]: https://github.com/tokio-rs/tracing/pull/1603 +[#1606]: https://github.com/tokio-rs/tracing/pull/1606 + +# 0.1.20 (September 12, 2021) + +This release adds support for `f64` as one of the `tracing-core` +primitive field values, allowing floating-point values to be recorded as +typed values rather than with `fmt::Debug`. Additionally, it adds +`NoSubscriber`, a `Subscriber` implementation that does nothing. + +### Added + +- **subscriber**: `NoSubscriber`, a no-op `Subscriber` implementation + ([#1549]) +- **field**: Added `Visit::record_f64` and support for recording + floating-point values ([#1507]) + +Thanks to new contributors @jsgf and @maxburke for contributing to this +release! + +[#1549]: https://github.com/tokio-rs/tracing/pull/1549 +[#1507]: https://github.com/tokio-rs/tracing/pull/1507 + +# 0.1.19 (August 17, 2021) +### Added + +- `Level::as_str` ([#1413]) +- `Hash` implementation for `Level` and `LevelFilter` ([#1456]) +- `Value` implementation for `&mut T where T: Value` ([#1385]) +- Multiple documentation fixes and improvements ([#1435], [#1446]) + +Thanks to @Folyd, @teozkr, and @dvdplm for contributing to this release! + +[#1413]: https://github.com/tokio-rs/tracing/pull/1413 +[#1456]: https://github.com/tokio-rs/tracing/pull/1456 +[#1385]: https://github.com/tokio-rs/tracing/pull/1385 +[#1435]: https://github.com/tokio-rs/tracing/pull/1435 +[#1446]: https://github.com/tokio-rs/tracing/pull/1446 + +# 0.1.18 (April 30, 2021) + +### Added + +- `Subscriber` impl for `Box<dyn Subscriber + Send + Sync + 'static>` ([#1358]) +- `Subscriber` impl for `Arc<dyn Subscriber + Send + Sync + 'static>` ([#1374]) +- Symmetric `From` impls for existing `Into` impls on `Current` and `Option<Id>` + ([#1335]) +- `Attributes::fields` accessor that returns the set of fields defined on a + span's `Attributes` ([#1331]) + + +Thanks to @Folyd for contributing to this release! + +[#1358]: https://github.com/tokio-rs/tracing/pull/1358 +[#1374]: https://github.com/tokio-rs/tracing/pull/1374 +[#1335]: https://github.com/tokio-rs/tracing/pull/1335 +[#1331]: https://github.com/tokio-rs/tracing/pull/1331 + +# 0.1.17 (September 28, 2020) + +### Fixed + +- Incorrect inlining of `Event::dispatch` and `Event::child_of`, which could + result in `dispatcher::get_default` being inlined at the callsite ([#994]) + +### Added + +- `Copy` implementations for `Level` and `LevelFilter` ([#992]) + +Thanks to new contributors @jyn514 and @TaKO8Ki for contributing to this +release! + +[#994]: https://github.com/tokio-rs/tracing/pull/994 +[#992]: https://github.com/tokio-rs/tracing/pull/992 + +# 0.1.16 (September 8, 2020) + +### Fixed + +- Added a conversion from `Option<Level>` to `LevelFilter`. This resolves a + previously unreported regression where `Option<Level>` was no longer + a valid LevelFilter. ([#966](https://github.com/tokio-rs/tracing/pull/966)) + +# 0.1.15 (August 22, 2020) + +### Fixed + +- When combining `Interest` from multiple subscribers, if the interests differ, + the current subscriber is now always asked if a callsite should be enabled + (#927) + +## Added + +- Internal API changes to support optimizations in the `tracing` crate (#943) +- **docs**: Multiple fixes and improvements (#913, #941) + +# 0.1.14 (August 10, 2020) + +### Fixed + +- Incorrect calculation of global max level filter which could result in fast + filtering paths not being taken (#908) + +# 0.1.13 (August 4, 2020) + +### Fixed + +- Missing `fmt::Display` impl for `field::DisplayValue` causing a compilation + failure when the "log" feature is enabled (#887) + +Thanks to @d-e-s-o for contributing to this release! + +# 0.1.12 (July 31, 2020) + +### Added + +- `LevelFilter` type and `LevelFilter::current()` for returning the highest level + that any subscriber will enable (#853) +- `Subscriber::max_level_hint` optional trait method, for setting the value + returned by `LevelFilter::current()` (#853) + +### Fixed + +- **docs**: Removed outdated reference to a Tokio API that no longer exists + (#857) + +Thanks to new contributor @dignati for contributing to this release! + +# 0.1.11 (June 8, 2020) + +### Changed + +- Replaced use of `inner_local_macros` with `$crate::` (#729) + +### Added + +- `must_use` warning to guards returned by `dispatcher::set_default` (#686) +- `fmt::Debug` impl to `dyn Value`s (#696) +- Functions to convert between `span::Id` and `NonZeroU64` (#770) +- More obvious warnings in documentation (#769) + +### Fixed + +- Compiler error when `tracing-core/std` feature is enabled but `tracing/std` is + not (#760) +- Clippy warning on vtable address comparison in `callsite::Identifier` (#749) +- Documentation formatting issues (#715, #771) + +Thanks to @bkchr, @majecty, @taiki-e, @nagisa, and @nvzqz for contributing to +this release! + +# 0.1.10 (January 24, 2020) + +### Added + +- `field::Empty` type for declaring empty fields whose values will be recorded + later (#548) +- `field::Value` implementations for `Wrapping` and `NonZero*` numbers (#538) + +### Fixed + +- Broken and unresolvable links in RustDoc (#595) + +Thanks to @oli-cosmian for contributing to this release! + +# 0.1.9 (January 10, 2020) + +### Added + +- API docs now show what feature flags are required to enable each item (#523) + +### Fixed + +- A panic when the current default subscriber subscriber calls + `dispatcher::with_default` as it is being dropped (#522) +- Incorrect documentation for `Subscriber::drop_span` (#524) + +# 0.1.8 (December 20, 2019) + +### Added + +- `Default` impl for `Dispatch` (#411) + +### Fixed + +- Removed duplicate `lazy_static` dependencies (#424) +- Fixed no-std dependencies being enabled even when `std` feature flag is set + (#424) +- Broken link to `Metadata` in `Event` docs (#461) + +# 0.1.7 (October 18, 2019) + +### Added + +- Added `dispatcher::set_default` API which returns a drop guard (#388) + +### Fixed + +- Added missing `Value` impl for `u8` (#392) +- Broken links in docs. + +# 0.1.6 (September 12, 2019) + +### Added + +- Internal APIs to support performance optimizations (#326) + +### Fixed + +- Clarified wording in `field::display` documentation (#340) + +# 0.1.5 (August 16, 2019) + +### Added + +- `std::error::Error` as a new primitive `Value` type (#277) +- `Event::new` and `Event::new_child_of` to manually construct `Event`s (#281) + +# 0.1.4 (August 9, 2019) + +### Added + +- Support for `no-std` + `liballoc` (#256) + +### Fixed + +- Broken links in RustDoc (#259) + +# 0.1.3 (August 8, 2019) + +### Added + +- `std::fmt::Display` implementation for `Level` (#194) +- `std::str::FromStr` implementation for `Level` (#195) + +# 0.1.2 (July 10, 2019) + +### Deprecated + +- `Subscriber::drop_span` in favor of new `Subscriber::try_close` (#168) + +### Added + +- `Into<Option<&Id>>`, `Into<Option<Id>>`, and + `Into<Option<&'static Metadata<'static>>>` impls for `span::Current` (#170) +- `Subscriber::try_close` method (#153) +- Improved documentation for `dispatcher` (#171) + +# 0.1.1 (July 6, 2019) + +### Added + +- `Subscriber::current_span` API to return the current span (#148). +- `span::Current` type, representing the `Subscriber`'s view of the current + span (#148). + +### Fixed + +- Typos and broken links in documentation (#123, #124, #128, #154) + +# 0.1.0 (June 27, 2019) + +- Initial release diff --git a/vendor/tracing-core/Cargo.toml b/vendor/tracing-core/Cargo.toml new file mode 100644 index 000000000..40d797d54 --- /dev/null +++ b/vendor/tracing-core/Cargo.toml @@ -0,0 +1,66 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +rust-version = "1.49.0" +name = "tracing-core" +version = "0.1.28" +authors = ["Tokio Contributors <team@tokio.rs>"] +description = """ +Core primitives for application-level tracing. +""" +homepage = "https://tokio.rs" +readme = "README.md" +keywords = [ + "logging", + "tracing", + "profiling", +] +categories = [ + "development-tools::debugging", + "development-tools::profiling", + "asynchronous", +] +license = "MIT" +repository = "https://github.com/tokio-rs/tracing" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = [ + "--cfg", + "docsrs", + "--cfg", + "tracing_unstable", +] +rustc-args = [ + "--cfg", + "tracing_unstable", +] + +[dependencies.once_cell] +version = "1.12" +optional = true + +[features] +default = [ + "std", + "valuable/std", +] +std = ["once_cell"] + +[target."cfg(tracing_unstable)".dependencies.valuable] +version = "0.1.0" +optional = true +default_features = false + +[badges.maintenance] +status = "actively-developed" diff --git a/vendor/tracing-core/LICENSE b/vendor/tracing-core/LICENSE new file mode 100644 index 000000000..cdb28b4b5 --- /dev/null +++ b/vendor/tracing-core/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2019 Tokio Contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/tracing-core/README.md b/vendor/tracing-core/README.md new file mode 100644 index 000000000..f3afec07e --- /dev/null +++ b/vendor/tracing-core/README.md @@ -0,0 +1,121 @@ +![Tracing — Structured, application-level diagnostics][splash] + +[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg + +# tracing-core + +Core primitives for application-level tracing. + +[![Crates.io][crates-badge]][crates-url] +[![Documentation][docs-badge]][docs-url] +[![Documentation (master)][docs-master-badge]][docs-master-url] +[![MIT licensed][mit-badge]][mit-url] +[![Build Status][actions-badge]][actions-url] +[![Discord chat][discord-badge]][discord-url] + +[Documentation][docs-url] | [Chat][discord-url] + +[crates-badge]: https://img.shields.io/crates/v/tracing-core.svg +[crates-url]: https://crates.io/crates/tracing-core/0.1.28 +[docs-badge]: https://docs.rs/tracing-core/badge.svg +[docs-url]: https://docs.rs/tracing-core/0.1.28 +[docs-master-badge]: https://img.shields.io/badge/docs-master-blue +[docs-master-url]: https://tracing-rs.netlify.com/tracing_core +[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg +[mit-url]: LICENSE +[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg +[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI +[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white +[discord-url]: https://discord.gg/EeF3cQw + +## Overview + +[`tracing`] is a framework for instrumenting Rust programs to collect +structured, event-based diagnostic information. This crate defines the core +primitives of `tracing`. + +The crate provides: + +* [`span::Id`] identifies a span within the execution of a program. + +* [`Event`] represents a single event within a trace. + +* [`Subscriber`], the trait implemented to collect trace data. + +* [`Metadata`] and [`Callsite`] provide information describing spans and + events. + +* [`Field`], [`FieldSet`], [`Value`], and [`ValueSet`] represent the + structured data attached to spans and events. + +* [`Dispatch`] allows spans and events to be dispatched to `Subscriber`s. + +In addition, it defines the global callsite registry and per-thread current +dispatcher which other components of the tracing system rely on. + +*Compiler support: [requires `rustc` 1.49+][msrv]* + +[msrv]: #supported-rust-versions + +## Usage + +Application authors will typically not use this crate directly. Instead, they +will use the [`tracing`] crate, which provides a much more fully-featured +API. However, this crate's API will change very infrequently, so it may be used +when dependencies must be very stable. + +`Subscriber` implementations may depend on `tracing-core` rather than `tracing`, +as the additional APIs provided by `tracing` are primarily useful for +instrumenting libraries and applications, and are generally not necessary for +`Subscriber` implementations. + +### Crate Feature Flags + +The following crate feature flags are available: + +* `std`: Depend on the Rust standard library (enabled by default). + + `no_std` users may disable this feature with `default-features = false`: + + ```toml + [dependencies] + tracing-core = { version = "0.1.28", default-features = false } + ``` + + **Note**:`tracing-core`'s `no_std` support requires `liballoc`. + +[`tracing`]: ../tracing +[`span::Id`]: https://docs.rs/tracing-core/0.1.28/tracing_core/span/struct.Id.html +[`Event`]: https://docs.rs/tracing-core/0.1.28/tracing_core/event/struct.Event.html +[`Subscriber`]: https://docs.rs/tracing-core/0.1.28/tracing_core/subscriber/trait.Subscriber.html +[`Metadata`]: https://docs.rs/tracing-core/0.1.28/tracing_core/metadata/struct.Metadata.html +[`Callsite`]: https://docs.rs/tracing-core/0.1.28/tracing_core/callsite/trait.Callsite.html +[`Field`]: https://docs.rs/tracing-core/0.1.28/tracing_core/field/struct.Field.html +[`FieldSet`]: https://docs.rs/tracing-core/0.1.28/tracing_core/field/struct.FieldSet.html +[`Value`]: https://docs.rs/tracing-core/0.1.28/tracing_core/field/trait.Value.html +[`ValueSet`]: https://docs.rs/tracing-core/0.1.28/tracing_core/field/struct.ValueSet.html +[`Dispatch`]: https://docs.rs/tracing-core/0.1.28/tracing_core/dispatcher/struct.Dispatch.html + +## Supported Rust Versions + +Tracing is built against the latest stable release. The minimum supported +version is 1.49. 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. + +## License + +This project is licensed under the [MIT license](LICENSE). + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in Tokio by you, shall be licensed as MIT, without any additional +terms or conditions. diff --git a/vendor/tracing-core/src/callsite.rs b/vendor/tracing-core/src/callsite.rs new file mode 100644 index 000000000..d0fe1a3d6 --- /dev/null +++ b/vendor/tracing-core/src/callsite.rs @@ -0,0 +1,611 @@ +//! Callsites represent the source locations from which spans or events +//! originate. +//! +//! # What Are Callsites? +//! +//! Every span or event in `tracing` is associated with a [`Callsite`]. A +//! callsite is a small `static` value that is responsible for the following: +//! +//! * Storing the span or event's [`Metadata`], +//! * Uniquely [identifying](Identifier) the span or event definition, +//! * Caching the subscriber's [`Interest`][^1] in that span or event, to avoid +//! re-evaluating filters, +//! * Storing a [`Registration`] that allows the callsite to be part of a global +//! list of all callsites in the program. +//! +//! # Registering Callsites +//! +//! When a span or event is recorded for the first time, its callsite +//! [`register`]s itself with the global callsite registry. Registering a +//! callsite calls the [`Subscriber::register_callsite`][`register_callsite`] +//! method with that callsite's [`Metadata`] on every currently active +//! subscriber. This serves two primary purposes: informing subscribers of the +//! callsite's existence, and performing static filtering. +//! +//! ## Callsite Existence +//! +//! If a [`Subscriber`] implementation wishes to allocate storage for each +//! unique span/event location in the program, or pre-compute some value +//! that will be used to record that span or event in the future, it can +//! do so in its [`register_callsite`] method. +//! +//! ## Performing Static Filtering +//! +//! The [`register_callsite`] method returns an [`Interest`] value, +//! which indicates that the subscriber either [always] wishes to record +//! that span or event, [sometimes] wishes to record it based on a +//! dynamic filter evaluation, or [never] wishes to record it. +//! +//! When registering a new callsite, the [`Interest`]s returned by every +//! currently active subscriber are combined, and the result is stored at +//! each callsite. This way, when the span or event occurs in the +//! future, the cached [`Interest`] value can be checked efficiently +//! to determine if the span or event should be recorded, without +//! needing to perform expensive filtering (i.e. calling the +//! [`Subscriber::enabled`] method every time a span or event occurs). +//! +//! ### Rebuilding Cached Interest +//! +//! When a new [`Dispatch`] is created (i.e. a new subscriber becomes +//! active), any previously cached [`Interest`] values are re-evaluated +//! for all callsites in the program. This way, if the new subscriber +//! will enable a callsite that was not previously enabled, the +//! [`Interest`] in that callsite is updated. Similarly, when a +//! subscriber is dropped, the interest cache is also re-evaluated, so +//! that any callsites enabled only by that subscriber are disabled. +//! +//! In addition, the [`rebuild_interest_cache`] function in this module can be +//! used to manually invalidate all cached interest and re-register those +//! callsites. This function is useful in situations where a subscriber's +//! interest can change, but it does so relatively infrequently. The subscriber +//! may wish for its interest to be cached most of the time, and return +//! [`Interest::always`][always] or [`Interest::never`][never] in its +//! [`register_callsite`] method, so that its [`Subscriber::enabled`] method +//! doesn't need to be evaluated every time a span or event is recorded. +//! However, when the configuration changes, the subscriber can call +//! [`rebuild_interest_cache`] to re-evaluate the entire interest cache with its +//! new configuration. This is a relatively costly operation, but if the +//! configuration changes infrequently, it may be more efficient than calling +//! [`Subscriber::enabled`] frequently. +//! +//! # Implementing Callsites +//! +//! In most cases, instrumenting code using `tracing` should *not* require +//! implementing the [`Callsite`] trait directly. When using the [`tracing` +//! crate's macros][macros] or the [`#[instrument]` attribute][instrument], a +//! `Callsite` is automatically generated. +//! +//! However, code which provides alternative forms of `tracing` instrumentation +//! may need to interact with the callsite system directly. If +//! instrumentation-side code needs to produce a `Callsite` to emit spans or +//! events, the [`DefaultCallsite`] struct provided in this module is a +//! ready-made `Callsite` implementation that is suitable for most uses. When +//! possible, the use of `DefaultCallsite` should be preferred over implementing +//! [`Callsite`] for user types, as `DefaultCallsite` may benefit from +//! additional performance optimizations. +//! +//! [^1]: Returned by the [`Subscriber::register_callsite`][`register_callsite`] +//! method. +//! +//! [`Metadata`]: crate::metadata::Metadata +//! [`Interest`]: crate::subscriber::Interest +//! [`Subscriber`]: crate::subscriber::Subscriber +//! [`register_callsite`]: crate::subscriber::Subscriber::register_callsite +//! [`Subscriber::enabled`]: crate::subscriber::Subscriber::enabled +//! [always]: crate::subscriber::Interest::always +//! [sometimes]: crate::subscriber::Interest::sometimes +//! [never]: crate::subscriber::Interest::never +//! [`Dispatch`]: crate::dispatch::Dispatch +//! [macros]: https://docs.rs/tracing/latest/tracing/#macros +//! [instrument]: https://docs.rs/tracing/latest/tracing/attr.instrument.html +use crate::stdlib::{ + any::TypeId, + fmt, + hash::{Hash, Hasher}, + ptr, + sync::{ + atomic::{AtomicBool, AtomicPtr, AtomicU8, Ordering}, + Mutex, + }, + vec::Vec, +}; +use crate::{ + dispatcher::Dispatch, + lazy::Lazy, + metadata::{LevelFilter, Metadata}, + subscriber::Interest, +}; + +use self::dispatchers::Dispatchers; + +/// Trait implemented by callsites. +/// +/// These functions are only intended to be called by the callsite registry, which +/// correctly handles determining the common interest between all subscribers. +/// +/// See the [module-level documentation](crate::callsite) for details on +/// callsites. +pub trait Callsite: Sync { + /// Sets the [`Interest`] for this callsite. + /// + /// See the [documentation on callsite interest caching][cache-docs] for + /// details. + /// + /// [`Interest`]: super::subscriber::Interest + /// [cache-docs]: crate::callsite#performing-static-filtering + fn set_interest(&self, interest: Interest); + + /// Returns the [metadata] associated with the callsite. + /// + /// [metadata]: super::metadata::Metadata + fn metadata(&self) -> &Metadata<'_>; + + /// This method is an *internal implementation detail* of `tracing-core`. It + /// is *not* intended to be called or overridden from downstream code. + /// + /// The `Private` type can only be constructed from within `tracing-core`. + /// Because this method takes a `Private` as an argument, it cannot be + /// called from (safe) code external to `tracing-core`. Because it must + /// *return* a `Private`, the only valid implementation possible outside of + /// `tracing-core` would have to always unconditionally panic. + /// + /// THIS IS BY DESIGN. There is currently no valid reason for code outside + /// of `tracing-core` to override this method. + // TODO(eliza): this could be used to implement a public downcasting API + // for `&dyn Callsite`s in the future. + #[doc(hidden)] + #[inline] + fn private_type_id(&self, _: private::Private<()>) -> private::Private<TypeId> + where + Self: 'static, + { + private::Private(TypeId::of::<Self>()) + } +} + +/// Uniquely identifies a [`Callsite`] +/// +/// Two `Identifier`s are equal if they both refer to the same callsite. +/// +/// [`Callsite`]: super::callsite::Callsite +#[derive(Clone)] +pub struct Identifier( + /// **Warning**: The fields on this type are currently `pub` because it must + /// be able to be constructed statically by macros. However, when `const + /// fn`s are available on stable Rust, this will no longer be necessary. + /// Thus, these fields are *not* considered stable public API, and they may + /// change warning. Do not rely on any fields on `Identifier`. When + /// constructing new `Identifier`s, use the `identify_callsite!` macro + /// instead. + #[doc(hidden)] + pub &'static dyn Callsite, +); + +/// A default [`Callsite`] implementation. +#[derive(Debug)] +pub struct DefaultCallsite { + interest: AtomicU8, + registration: AtomicU8, + meta: &'static Metadata<'static>, + next: AtomicPtr<Self>, +} + +/// Clear and reregister interest on every [`Callsite`] +/// +/// This function is intended for runtime reconfiguration of filters on traces +/// when the filter recalculation is much less frequent than trace events are. +/// The alternative is to have the [`Subscriber`] that supports runtime +/// reconfiguration of filters always return [`Interest::sometimes()`] so that +/// [`enabled`] is evaluated for every event. +/// +/// This function will also re-compute the global maximum level as determined by +/// the [`max_level_hint`] method. If a [`Subscriber`] +/// implementation changes the value returned by its `max_level_hint` +/// implementation at runtime, then it **must** call this function after that +/// value changes, in order for the change to be reflected. +/// +/// See the [documentation on callsite interest caching][cache-docs] for +/// additional information on this function's usage. +/// +/// [`max_level_hint`]: super::subscriber::Subscriber::max_level_hint +/// [`Callsite`]: super::callsite::Callsite +/// [`enabled`]: super::subscriber::Subscriber#tymethod.enabled +/// [`Interest::sometimes()`]: super::subscriber::Interest::sometimes +/// [`Subscriber`]: super::subscriber::Subscriber +/// [cache-docs]: crate::callsite#rebuilding-cached-interest +pub fn rebuild_interest_cache() { + CALLSITES.rebuild_interest(DISPATCHERS.rebuilder()); +} + +/// Register a new [`Callsite`] with the global registry. +/// +/// This should be called once per callsite after the callsite has been +/// constructed. +/// +/// See the [documentation on callsite registration][reg-docs] for details +/// on the global callsite registry. +/// +/// [`Callsite`]: crate::callsite::Callsite +/// [reg-docs]: crate::callsite#registering-callsites +pub fn register(callsite: &'static dyn Callsite) { + rebuild_callsite_interest(callsite, &DISPATCHERS.rebuilder()); + + // Is this a `DefaultCallsite`? If so, use the fancy linked list! + if callsite.private_type_id(private::Private(())).0 == TypeId::of::<DefaultCallsite>() { + let callsite = unsafe { + // Safety: the pointer cast is safe because the type id of the + // provided callsite matches that of the target type for the cast + // (`DefaultCallsite`). Because user implementations of `Callsite` + // cannot override `private_type_id`, we can trust that the callsite + // is not lying about its type ID. + &*(callsite as *const dyn Callsite as *const DefaultCallsite) + }; + CALLSITES.push_default(callsite); + return; + } + + CALLSITES.push_dyn(callsite); +} + +static CALLSITES: Callsites = Callsites { + list_head: AtomicPtr::new(ptr::null_mut()), + has_locked_callsites: AtomicBool::new(false), +}; + +static DISPATCHERS: Dispatchers = Dispatchers::new(); + +static LOCKED_CALLSITES: Lazy<Mutex<Vec<&'static dyn Callsite>>> = Lazy::new(Default::default); + +struct Callsites { + list_head: AtomicPtr<DefaultCallsite>, + has_locked_callsites: AtomicBool, +} + +// === impl DefaultCallsite === + +impl DefaultCallsite { + const UNREGISTERED: u8 = 0; + const REGISTERING: u8 = 1; + const REGISTERED: u8 = 2; + + const INTEREST_NEVER: u8 = 0; + const INTEREST_SOMETIMES: u8 = 1; + const INTEREST_ALWAYS: u8 = 2; + + /// Returns a new `DefaultCallsite` with the specified `Metadata`. + pub const fn new(meta: &'static Metadata<'static>) -> Self { + Self { + interest: AtomicU8::new(0xFF), + meta, + next: AtomicPtr::new(ptr::null_mut()), + registration: AtomicU8::new(Self::UNREGISTERED), + } + } + + /// Registers this callsite with the global callsite registry. + /// + /// If the callsite is already registered, this does nothing. When using + /// [`DefaultCallsite`], this method should be preferred over + /// [`tracing_core::callsite::register`], as it ensures that the callsite is + /// only registered a single time. + /// + /// Other callsite implementations will generally ensure that + /// callsites are not re-registered through another mechanism. + /// + /// See the [documentation on callsite registration][reg-docs] for details + /// on the global callsite registry. + /// + /// [`Callsite`]: crate::callsite::Callsite + /// [reg-docs]: crate::callsite#registering-callsites + #[inline(never)] + // This only happens once (or if the cached interest value was corrupted). + #[cold] + pub fn register(&'static self) -> Interest { + // Attempt to advance the registration state to `REGISTERING`... + match self.registration.compare_exchange( + Self::UNREGISTERED, + Self::REGISTERING, + Ordering::AcqRel, + Ordering::Acquire, + ) { + Ok(_) => { + // Okay, we advanced the state, try to register the callsite. + rebuild_callsite_interest(self, &DISPATCHERS.rebuilder()); + CALLSITES.push_default(self); + self.registration.store(Self::REGISTERED, Ordering::Release); + } + // Great, the callsite is already registered! Just load its + // previous cached interest. + Err(Self::REGISTERED) => {} + // Someone else is registering... + Err(_state) => { + debug_assert_eq!( + _state, + Self::REGISTERING, + "weird callsite registration state" + ); + // Just hit `enabled` this time. + return Interest::sometimes(); + } + } + + match self.interest.load(Ordering::Relaxed) { + Self::INTEREST_NEVER => Interest::never(), + Self::INTEREST_ALWAYS => Interest::always(), + _ => Interest::sometimes(), + } + } + + /// Returns the callsite's cached `Interest`, or registers it for the + /// first time if it has not yet been registered. + #[inline] + pub fn interest(&'static self) -> Interest { + match self.interest.load(Ordering::Relaxed) { + Self::INTEREST_NEVER => Interest::never(), + Self::INTEREST_SOMETIMES => Interest::sometimes(), + Self::INTEREST_ALWAYS => Interest::always(), + _ => self.register(), + } + } +} + +impl Callsite for DefaultCallsite { + fn set_interest(&self, interest: Interest) { + let interest = match () { + _ if interest.is_never() => Self::INTEREST_NEVER, + _ if interest.is_always() => Self::INTEREST_ALWAYS, + _ => Self::INTEREST_SOMETIMES, + }; + self.interest.store(interest, Ordering::SeqCst); + } + + #[inline(always)] + fn metadata(&self) -> &Metadata<'static> { + self.meta + } +} + +// ===== impl Identifier ===== + +impl PartialEq for Identifier { + fn eq(&self, other: &Identifier) -> bool { + core::ptr::eq( + self.0 as *const _ as *const (), + other.0 as *const _ as *const (), + ) + } +} + +impl Eq for Identifier {} + +impl fmt::Debug for Identifier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Identifier({:p})", self.0) + } +} + +impl Hash for Identifier { + fn hash<H>(&self, state: &mut H) + where + H: Hasher, + { + (self.0 as *const dyn Callsite).hash(state) + } +} + +// === impl Callsites === + +impl Callsites { + /// Rebuild `Interest`s for all callsites in the registry. + /// + /// This also re-computes the max level hint. + fn rebuild_interest(&self, dispatchers: dispatchers::Rebuilder<'_>) { + let mut max_level = LevelFilter::OFF; + dispatchers.for_each(|dispatch| { + // If the subscriber did not provide a max level hint, assume + // that it may enable every level. + let level_hint = dispatch.max_level_hint().unwrap_or(LevelFilter::TRACE); + if level_hint > max_level { + max_level = level_hint; + } + }); + + self.for_each(|callsite| { + rebuild_callsite_interest(callsite, &dispatchers); + }); + LevelFilter::set_max(max_level); + } + + /// Push a `dyn Callsite` trait object to the callsite registry. + /// + /// This will attempt to lock the callsites vector. + fn push_dyn(&self, callsite: &'static dyn Callsite) { + let mut lock = LOCKED_CALLSITES.lock().unwrap(); + self.has_locked_callsites.store(true, Ordering::Release); + lock.push(callsite); + } + + /// Push a `DefaultCallsite` to the callsite registry. + /// + /// If we know the callsite being pushed is a `DefaultCallsite`, we can push + /// it to the linked list without having to acquire a lock. + fn push_default(&self, callsite: &'static DefaultCallsite) { + let mut head = self.list_head.load(Ordering::Acquire); + + loop { + callsite.next.store(head, Ordering::Release); + + assert_ne!( + callsite as *const _, head, + "Attempted to register a `DefaultCallsite` that already exists! \ + This will cause an infinite loop when attempting to read from the \ + callsite cache. This is likely a bug! You should only need to call \ + `DefaultCallsite::register` once per `DefaultCallsite`." + ); + + match self.list_head.compare_exchange( + head, + callsite as *const _ as *mut _, + Ordering::AcqRel, + Ordering::Acquire, + ) { + Ok(_) => { + break; + } + Err(current) => head = current, + } + } + } + + /// Invokes the provided closure `f` with each callsite in the registry. + fn for_each(&self, mut f: impl FnMut(&'static dyn Callsite)) { + let mut head = self.list_head.load(Ordering::Acquire); + + while let Some(cs) = unsafe { head.as_ref() } { + f(cs); + + head = cs.next.load(Ordering::Acquire); + } + + if self.has_locked_callsites.load(Ordering::Acquire) { + let locked = LOCKED_CALLSITES.lock().unwrap(); + for &cs in locked.iter() { + f(cs); + } + } + } +} + +pub(crate) fn register_dispatch(dispatch: &Dispatch) { + let dispatchers = DISPATCHERS.register_dispatch(dispatch); + CALLSITES.rebuild_interest(dispatchers); +} + +fn rebuild_callsite_interest( + callsite: &'static dyn Callsite, + dispatchers: &dispatchers::Rebuilder<'_>, +) { + let meta = callsite.metadata(); + + let mut interest = None; + dispatchers.for_each(|dispatch| { + let this_interest = dispatch.register_callsite(meta); + interest = match interest.take() { + None => Some(this_interest), + Some(that_interest) => Some(that_interest.and(this_interest)), + } + }); + + let interest = interest.unwrap_or_else(Interest::never); + callsite.set_interest(interest) +} + +mod private { + /// Don't call this function, it's private. + #[allow(missing_debug_implementations)] + pub struct Private<T>(pub(crate) T); +} + +#[cfg(feature = "std")] +mod dispatchers { + use crate::{dispatcher, lazy::Lazy}; + use std::sync::{ + atomic::{AtomicBool, Ordering}, + RwLock, RwLockReadGuard, RwLockWriteGuard, + }; + + pub(super) struct Dispatchers { + has_just_one: AtomicBool, + } + + static LOCKED_DISPATCHERS: Lazy<RwLock<Vec<dispatcher::Registrar>>> = + Lazy::new(Default::default); + + pub(super) enum Rebuilder<'a> { + JustOne, + Read(RwLockReadGuard<'a, Vec<dispatcher::Registrar>>), + Write(RwLockWriteGuard<'a, Vec<dispatcher::Registrar>>), + } + + impl Dispatchers { + pub(super) const fn new() -> Self { + Self { + has_just_one: AtomicBool::new(true), + } + } + + pub(super) fn rebuilder(&self) -> Rebuilder<'_> { + if self.has_just_one.load(Ordering::SeqCst) { + return Rebuilder::JustOne; + } + Rebuilder::Read(LOCKED_DISPATCHERS.read().unwrap()) + } + + pub(super) fn register_dispatch(&self, dispatch: &dispatcher::Dispatch) -> Rebuilder<'_> { + let mut dispatchers = LOCKED_DISPATCHERS.write().unwrap(); + dispatchers.retain(|d| d.upgrade().is_some()); + dispatchers.push(dispatch.registrar()); + self.has_just_one + .store(dispatchers.len() <= 1, Ordering::SeqCst); + Rebuilder::Write(dispatchers) + } + } + + impl Rebuilder<'_> { + pub(super) fn for_each(&self, mut f: impl FnMut(&dispatcher::Dispatch)) { + let iter = match self { + Rebuilder::JustOne => { + dispatcher::get_default(f); + return; + } + Rebuilder::Read(vec) => vec.iter(), + Rebuilder::Write(vec) => vec.iter(), + }; + iter.filter_map(dispatcher::Registrar::upgrade) + .for_each(|dispatch| f(&dispatch)) + } + } +} + +#[cfg(not(feature = "std"))] +mod dispatchers { + use crate::dispatcher; + + pub(super) struct Dispatchers(()); + pub(super) struct Rebuilder<'a>(Option<&'a dispatcher::Dispatch>); + + impl Dispatchers { + pub(super) const fn new() -> Self { + Self(()) + } + + pub(super) fn rebuilder(&self) -> Rebuilder<'_> { + Rebuilder(None) + } + + pub(super) fn register_dispatch<'dispatch>( + &self, + dispatch: &'dispatch dispatcher::Dispatch, + ) -> Rebuilder<'dispatch> { + // nop; on no_std, there can only ever be one dispatcher + Rebuilder(Some(dispatch)) + } + } + + impl Rebuilder<'_> { + #[inline] + pub(super) fn for_each(&self, mut f: impl FnMut(&dispatcher::Dispatch)) { + if let Some(dispatch) = self.0 { + // we are rebuilding the interest cache because a new dispatcher + // is about to be set. on `no_std`, this should only happen + // once, because the new dispatcher will be the global default. + f(dispatch) + } else { + // otherwise, we are rebuilding the cache because the subscriber + // configuration changed, so use the global default. + // on no_std, there can only ever be one dispatcher + dispatcher::get_default(f) + } + } + } +} diff --git a/vendor/tracing-core/src/dispatcher.rs b/vendor/tracing-core/src/dispatcher.rs new file mode 100644 index 000000000..041722c36 --- /dev/null +++ b/vendor/tracing-core/src/dispatcher.rs @@ -0,0 +1,898 @@ +//! Dispatches trace events to [`Subscriber`]s. +//! +//! The _dispatcher_ is the component of the tracing system which is responsible +//! for forwarding trace data from the instrumentation points that generate it +//! to the subscriber that collects it. +//! +//! # Using the Trace Dispatcher +//! +//! Every thread in a program using `tracing` has a _default subscriber_. When +//! events occur, or spans are created, they are dispatched to the thread's +//! current subscriber. +//! +//! ## Setting the Default Subscriber +//! +//! By default, the current subscriber is an empty implementation that does +//! nothing. To use a subscriber implementation, it must be set as the default. +//! There are two methods for doing so: [`with_default`] and +//! [`set_global_default`]. `with_default` sets the default subscriber for the +//! duration of a scope, while `set_global_default` sets a default subscriber +//! for the entire process. +//! +//! To use either of these functions, we must first wrap our subscriber in a +//! [`Dispatch`], a cloneable, type-erased reference to a subscriber. For +//! example: +//! ```rust +//! # pub struct FooSubscriber; +//! # use tracing_core::{ +//! # dispatcher, Event, Metadata, +//! # span::{Attributes, Id, Record} +//! # }; +//! # impl tracing_core::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { fn new() -> Self { FooSubscriber } } +//! use dispatcher::Dispatch; +//! +//! let my_subscriber = FooSubscriber::new(); +//! let my_dispatch = Dispatch::new(my_subscriber); +//! ``` +//! Then, we can use [`with_default`] to set our `Dispatch` as the default for +//! the duration of a block: +//! ```rust +//! # pub struct FooSubscriber; +//! # use tracing_core::{ +//! # dispatcher, Event, Metadata, +//! # span::{Attributes, Id, Record} +//! # }; +//! # impl tracing_core::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { fn new() -> Self { FooSubscriber } } +//! # let my_subscriber = FooSubscriber::new(); +//! # let my_dispatch = dispatcher::Dispatch::new(my_subscriber); +//! // no default subscriber +//! +//! # #[cfg(feature = "std")] +//! dispatcher::with_default(&my_dispatch, || { +//! // my_subscriber is the default +//! }); +//! +//! // no default subscriber again +//! ``` +//! It's important to note that `with_default` will not propagate the current +//! thread's default subscriber to any threads spawned within the `with_default` +//! block. To propagate the default subscriber to new threads, either use +//! `with_default` from the new thread, or use `set_global_default`. +//! +//! As an alternative to `with_default`, we can use [`set_global_default`] to +//! set a `Dispatch` as the default for all threads, for the lifetime of the +//! program. For example: +//! ```rust +//! # pub struct FooSubscriber; +//! # use tracing_core::{ +//! # dispatcher, Event, Metadata, +//! # span::{Attributes, Id, Record} +//! # }; +//! # impl tracing_core::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { fn new() -> Self { FooSubscriber } } +//! # let my_subscriber = FooSubscriber::new(); +//! # let my_dispatch = dispatcher::Dispatch::new(my_subscriber); +//! // no default subscriber +//! +//! dispatcher::set_global_default(my_dispatch) +//! // `set_global_default` will return an error if the global default +//! // subscriber has already been set. +//! .expect("global default was already set!"); +//! +//! // `my_subscriber` is now the default +//! ``` +//! +//! <pre class="ignore" style="white-space:normal;font:inherit;"> +//! <strong>Note</strong>:the thread-local scoped dispatcher +//! (<a href="#fn.with_default"><code>with_default</code></a>) requires the +//! Rust standard library. <code>no_std</code> users should use +//! <a href="#fn.set_global_default"><code>set_global_default</code></a> +//! instead. +//! </pre> +//! +//! ## Accessing the Default Subscriber +//! +//! A thread's current default subscriber can be accessed using the +//! [`get_default`] function, which executes a closure with a reference to the +//! currently default `Dispatch`. This is used primarily by `tracing` +//! instrumentation. +//! +use crate::{ + callsite, span, + subscriber::{self, NoSubscriber, Subscriber}, + Event, LevelFilter, Metadata, +}; + +use crate::stdlib::{ + any::Any, + fmt, + sync::{ + atomic::{AtomicBool, AtomicUsize, Ordering}, + Arc, + }, +}; + +#[cfg(feature = "std")] +use crate::stdlib::{ + cell::{Cell, RefCell, RefMut}, + error, + sync::Weak, +}; + +/// `Dispatch` trace data to a [`Subscriber`]. +/// +#[derive(Clone)] +pub struct Dispatch { + subscriber: Arc<dyn Subscriber + Send + Sync>, +} + +#[cfg(feature = "std")] +thread_local! { + static CURRENT_STATE: State = State { + default: RefCell::new(None), + can_enter: Cell::new(true), + }; +} + +static EXISTS: AtomicBool = AtomicBool::new(false); +static GLOBAL_INIT: AtomicUsize = AtomicUsize::new(UNINITIALIZED); + +const UNINITIALIZED: usize = 0; +const INITIALIZING: usize = 1; +const INITIALIZED: usize = 2; + +static mut GLOBAL_DISPATCH: Option<Dispatch> = None; + +/// The dispatch state of a thread. +#[cfg(feature = "std")] +struct State { + /// This thread's current default dispatcher. + default: RefCell<Option<Dispatch>>, + /// Whether or not we can currently begin dispatching a trace event. + /// + /// This is set to `false` when functions such as `enter`, `exit`, `event`, + /// and `new_span` are called on this thread's default dispatcher, to + /// prevent further trace events triggered inside those functions from + /// creating an infinite recursion. When we finish handling a dispatch, this + /// is set back to `true`. + can_enter: Cell<bool>, +} + +/// While this guard is active, additional calls to subscriber functions on +/// the default dispatcher will not be able to access the dispatch context. +/// Dropping the guard will allow the dispatch context to be re-entered. +#[cfg(feature = "std")] +struct Entered<'a>(&'a State); + +/// A guard that resets the current default dispatcher to the prior +/// default dispatcher when dropped. +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +#[derive(Debug)] +pub struct DefaultGuard(Option<Dispatch>); + +/// Sets this dispatch as the default for the duration of a closure. +/// +/// The default dispatcher is used when creating a new [span] or +/// [`Event`]. +/// +/// <pre class="ignore" style="white-space:normal;font:inherit;"> +/// <strong>Note</strong>: This function required the Rust standard library. +/// <code>no_std</code> users should use <a href="../fn.set_global_default.html"> +/// <code>set_global_default</code></a> instead. +/// </pre> +/// +/// [span]: super::span +/// [`Subscriber`]: super::subscriber::Subscriber +/// [`Event`]: super::event::Event +/// [`set_global_default`]: super::set_global_default +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub fn with_default<T>(dispatcher: &Dispatch, f: impl FnOnce() -> T) -> T { + // When this guard is dropped, the default dispatcher will be reset to the + // prior default. Using this (rather than simply resetting after calling + // `f`) ensures that we always reset to the prior dispatcher even if `f` + // panics. + let _guard = set_default(dispatcher); + f() +} + +/// Sets the dispatch as the default dispatch for the duration of the lifetime +/// of the returned DefaultGuard +/// +/// <pre class="ignore" style="white-space:normal;font:inherit;"> +/// <strong>Note</strong>: This function required the Rust standard library. +/// <code>no_std</code> users should use <a href="../fn.set_global_default.html"> +/// <code>set_global_default</code></a> instead. +/// </pre> +/// +/// [`set_global_default`]: super::set_global_default +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +#[must_use = "Dropping the guard unregisters the dispatcher."] +pub fn set_default(dispatcher: &Dispatch) -> DefaultGuard { + // When this guard is dropped, the default dispatcher will be reset to the + // prior default. Using this ensures that we always reset to the prior + // dispatcher even if the thread calling this function panics. + State::set_default(dispatcher.clone()) +} + +/// Sets this dispatch as the global default for the duration of the entire program. +/// Will be used as a fallback if no thread-local dispatch has been set in a thread +/// (using `with_default`.) +/// +/// Can only be set once; subsequent attempts to set the global default will fail. +/// Returns `Err` if the global default has already been set. +/// +/// <div class="example-wrap" style="display:inline-block"><pre class="compile_fail" style="white-space:normal;font:inherit;"> +/// <strong>Warning</strong>: In general, libraries should <em>not</em> call +/// <code>set_global_default()</code>! Doing so will cause conflicts when +/// executables that depend on the library try to set the default later. +/// </pre></div> +/// +/// [span]: super::span +/// [`Subscriber`]: super::subscriber::Subscriber +/// [`Event`]: super::event::Event +pub fn set_global_default(dispatcher: Dispatch) -> Result<(), SetGlobalDefaultError> { + // if `compare_exchange` returns Result::Ok(_), then `new` has been set and + // `current`—now the prior value—has been returned in the `Ok()` branch. + if GLOBAL_INIT + .compare_exchange( + UNINITIALIZED, + INITIALIZING, + Ordering::SeqCst, + Ordering::SeqCst, + ) + .is_ok() + { + unsafe { + GLOBAL_DISPATCH = Some(dispatcher); + } + GLOBAL_INIT.store(INITIALIZED, Ordering::SeqCst); + EXISTS.store(true, Ordering::Release); + Ok(()) + } else { + Err(SetGlobalDefaultError { _no_construct: () }) + } +} + +/// Returns true if a `tracing` dispatcher has ever been set. +/// +/// This may be used to completely elide trace points if tracing is not in use +/// at all or has yet to be initialized. +#[doc(hidden)] +#[inline(always)] +pub fn has_been_set() -> bool { + EXISTS.load(Ordering::Relaxed) +} + +/// Returned if setting the global dispatcher fails. +#[derive(Debug)] +pub struct SetGlobalDefaultError { + _no_construct: (), +} + +impl fmt::Display for SetGlobalDefaultError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad("a global default trace dispatcher has already been set") + } +} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl error::Error for SetGlobalDefaultError {} + +/// Executes a closure with a reference to this thread's current [dispatcher]. +/// +/// Note that calls to `get_default` should not be nested; if this function is +/// called while inside of another `get_default`, that closure will be provided +/// with `Dispatch::none` rather than the previously set dispatcher. +/// +/// [dispatcher]: super::dispatcher::Dispatch +#[cfg(feature = "std")] +pub fn get_default<T, F>(mut f: F) -> T +where + F: FnMut(&Dispatch) -> T, +{ + CURRENT_STATE + .try_with(|state| { + if let Some(entered) = state.enter() { + return f(&*entered.current()); + } + + f(&Dispatch::none()) + }) + .unwrap_or_else(|_| f(&Dispatch::none())) +} + +/// Executes a closure with a reference to this thread's current [dispatcher]. +/// +/// Note that calls to `get_default` should not be nested; if this function is +/// called while inside of another `get_default`, that closure will be provided +/// with `Dispatch::none` rather than the previously set dispatcher. +/// +/// [dispatcher]: super::dispatcher::Dispatch +#[cfg(feature = "std")] +#[doc(hidden)] +#[inline(never)] +pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> { + CURRENT_STATE + .try_with(|state| { + let entered = state.enter()?; + Some(f(&*entered.current())) + }) + .ok()? +} + +/// Executes a closure with a reference to the current [dispatcher]. +/// +/// [dispatcher]: super::dispatcher::Dispatch +#[cfg(not(feature = "std"))] +#[doc(hidden)] +pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> { + let dispatch = get_global()?; + Some(f(&dispatch)) +} + +/// Executes a closure with a reference to the current [dispatcher]. +/// +/// [dispatcher]: super::dispatcher::Dispatch +#[cfg(not(feature = "std"))] +pub fn get_default<T, F>(mut f: F) -> T +where + F: FnMut(&Dispatch) -> T, +{ + if let Some(d) = get_global() { + f(d) + } else { + f(&Dispatch::none()) + } +} + +fn get_global() -> Option<&'static Dispatch> { + if GLOBAL_INIT.load(Ordering::SeqCst) != INITIALIZED { + return None; + } + unsafe { + // This is safe given the invariant that setting the global dispatcher + // also sets `GLOBAL_INIT` to `INITIALIZED`. + Some(GLOBAL_DISPATCH.as_ref().expect( + "invariant violated: GLOBAL_DISPATCH must be initialized before GLOBAL_INIT is set", + )) + } +} + +#[cfg(feature = "std")] +pub(crate) struct Registrar(Weak<dyn Subscriber + Send + Sync>); + +impl Dispatch { + /// Returns a new `Dispatch` that discards events and spans. + #[inline] + pub fn none() -> Self { + Dispatch { + subscriber: Arc::new(NoSubscriber::default()), + } + } + + /// Returns a `Dispatch` that forwards to the given [`Subscriber`]. + /// + /// [`Subscriber`]: super::subscriber::Subscriber + pub fn new<S>(subscriber: S) -> Self + where + S: Subscriber + Send + Sync + 'static, + { + let me = Dispatch { + subscriber: Arc::new(subscriber), + }; + callsite::register_dispatch(&me); + me + } + + #[cfg(feature = "std")] + pub(crate) fn registrar(&self) -> Registrar { + Registrar(Arc::downgrade(&self.subscriber)) + } + + /// Registers a new callsite with this subscriber, returning whether or not + /// the subscriber is interested in being notified about the callsite. + /// + /// This calls the [`register_callsite`] function on the [`Subscriber`] + /// that this `Dispatch` forwards to. + /// + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`register_callsite`]: super::subscriber::Subscriber::register_callsite + #[inline] + pub fn register_callsite(&self, metadata: &'static Metadata<'static>) -> subscriber::Interest { + self.subscriber.register_callsite(metadata) + } + + /// Returns the highest [verbosity level][level] that this [`Subscriber`] will + /// enable, or `None`, if the subscriber does not implement level-based + /// filtering or chooses not to implement this method. + /// + /// This calls the [`max_level_hint`] function on the [`Subscriber`] + /// that this `Dispatch` forwards to. + /// + /// [level]: super::Level + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`register_callsite`]: super::subscriber::Subscriber::max_level_hint + // TODO(eliza): consider making this a public API? + #[inline] + pub(crate) fn max_level_hint(&self) -> Option<LevelFilter> { + self.subscriber.max_level_hint() + } + + /// Record the construction of a new span, returning a new [ID] for the + /// span being constructed. + /// + /// This calls the [`new_span`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// [ID]: super::span::Id + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`new_span`]: super::subscriber::Subscriber::new_span + #[inline] + pub fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { + self.subscriber.new_span(span) + } + + /// Record a set of values on a span. + /// + /// This calls the [`record`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`record`]: super::subscriber::Subscriber::record + #[inline] + pub fn record(&self, span: &span::Id, values: &span::Record<'_>) { + self.subscriber.record(span, values) + } + + /// Adds an indication that `span` follows from the span with the id + /// `follows`. + /// + /// This calls the [`record_follows_from`] function on the [`Subscriber`] + /// that this `Dispatch` forwards to. + /// + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`record_follows_from`]: super::subscriber::Subscriber::record_follows_from + #[inline] + pub fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { + self.subscriber.record_follows_from(span, follows) + } + + /// Returns true if a span with the specified [metadata] would be + /// recorded. + /// + /// This calls the [`enabled`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// [metadata]: super::metadata::Metadata + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`enabled`]: super::subscriber::Subscriber::enabled + #[inline] + pub fn enabled(&self, metadata: &Metadata<'_>) -> bool { + self.subscriber.enabled(metadata) + } + + /// Records that an [`Event`] has occurred. + /// + /// This calls the [`event`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// [`Event`]: super::event::Event + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`event`]: super::subscriber::Subscriber::event + #[inline] + pub fn event(&self, event: &Event<'_>) { + if self.subscriber.event_enabled(event) { + self.subscriber.event(event); + } + } + + /// Records that a span has been can_enter. + /// + /// This calls the [`enter`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`enter`]: super::subscriber::Subscriber::enter + pub fn enter(&self, span: &span::Id) { + self.subscriber.enter(span); + } + + /// Records that a span has been exited. + /// + /// This calls the [`exit`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`exit`]: super::subscriber::Subscriber::exit + pub fn exit(&self, span: &span::Id) { + self.subscriber.exit(span); + } + + /// Notifies the subscriber that a [span ID] has been cloned. + /// + /// This function must only be called with span IDs that were returned by + /// this `Dispatch`'s [`new_span`] function. The `tracing` crate upholds + /// this guarantee and any other libraries implementing instrumentation APIs + /// must as well. + /// + /// This calls the [`clone_span`] function on the `Subscriber` that this + /// `Dispatch` forwards to. + /// + /// [span ID]: super::span::Id + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`clone_span`]: super::subscriber::Subscriber::clone_span + /// [`new_span`]: super::subscriber::Subscriber::new_span + #[inline] + pub fn clone_span(&self, id: &span::Id) -> span::Id { + self.subscriber.clone_span(id) + } + + /// Notifies the subscriber that a [span ID] has been dropped. + /// + /// This function must only be called with span IDs that were returned by + /// this `Dispatch`'s [`new_span`] function. The `tracing` crate upholds + /// this guarantee and any other libraries implementing instrumentation APIs + /// must as well. + /// + /// This calls the [`drop_span`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// <pre class="compile_fail" style="white-space:normal;font:inherit;"> + /// <strong>Deprecated</strong>: The <a href="#method.try_close"><code> + /// try_close</code></a> method is functionally identical, but returns + /// <code>true</code> if the span is now closed. It should be used + /// instead of this method. + /// </pre> + /// + /// [span ID]: super::span::Id + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`drop_span`]: super::subscriber::Subscriber::drop_span + /// [`new_span`]: super::subscriber::Subscriber::new_span + /// [`try_close`]: Entered::try_close() + #[inline] + #[deprecated(since = "0.1.2", note = "use `Dispatch::try_close` instead")] + pub fn drop_span(&self, id: span::Id) { + #[allow(deprecated)] + self.subscriber.drop_span(id); + } + + /// Notifies the subscriber that a [span ID] has been dropped, and returns + /// `true` if there are now 0 IDs referring to that span. + /// + /// This function must only be called with span IDs that were returned by + /// this `Dispatch`'s [`new_span`] function. The `tracing` crate upholds + /// this guarantee and any other libraries implementing instrumentation APIs + /// must as well. + /// + /// This calls the [`try_close`] function on the [`Subscriber`] that this + /// `Dispatch` forwards to. + /// + /// [span ID]: super::span::Id + /// [`Subscriber`]: super::subscriber::Subscriber + /// [`try_close`]: super::subscriber::Subscriber::try_close + /// [`new_span`]: super::subscriber::Subscriber::new_span + pub fn try_close(&self, id: span::Id) -> bool { + self.subscriber.try_close(id) + } + + /// Returns a type representing this subscriber's view of the current span. + /// + /// This calls the [`current`] function on the `Subscriber` that this + /// `Dispatch` forwards to. + /// + /// [`current`]: super::subscriber::Subscriber::current_span + #[inline] + pub fn current_span(&self) -> span::Current { + self.subscriber.current_span() + } + + /// Returns `true` if this `Dispatch` forwards to a `Subscriber` of type + /// `T`. + #[inline] + pub fn is<T: Any>(&self) -> bool { + <dyn Subscriber>::is::<T>(&*self.subscriber) + } + + /// Returns some reference to the `Subscriber` this `Dispatch` forwards to + /// if it is of type `T`, or `None` if it isn't. + #[inline] + pub fn downcast_ref<T: Any>(&self) -> Option<&T> { + <dyn Subscriber>::downcast_ref(&*self.subscriber) + } +} + +impl Default for Dispatch { + /// Returns the current default dispatcher + fn default() -> Self { + get_default(|default| default.clone()) + } +} + +impl fmt::Debug for Dispatch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Dispatch") + .field(&format_args!("{:p}", self.subscriber)) + .finish() + } +} + +impl<S> From<S> for Dispatch +where + S: Subscriber + Send + Sync + 'static, +{ + #[inline] + fn from(subscriber: S) -> Self { + Dispatch::new(subscriber) + } +} + +#[cfg(feature = "std")] +impl Registrar { + pub(crate) fn upgrade(&self) -> Option<Dispatch> { + self.0.upgrade().map(|subscriber| Dispatch { subscriber }) + } +} + +// ===== impl State ===== + +#[cfg(feature = "std")] +impl State { + /// Replaces the current default dispatcher on this thread with the provided + /// dispatcher.Any + /// + /// Dropping the returned `ResetGuard` will reset the default dispatcher to + /// the previous value. + #[inline] + fn set_default(new_dispatch: Dispatch) -> DefaultGuard { + let prior = CURRENT_STATE + .try_with(|state| { + state.can_enter.set(true); + state.default.replace(Some(new_dispatch)) + }) + .ok() + .flatten(); + EXISTS.store(true, Ordering::Release); + DefaultGuard(prior) + } + + #[inline] + fn enter(&self) -> Option<Entered<'_>> { + if self.can_enter.replace(false) { + Some(Entered(self)) + } else { + None + } + } +} + +// ===== impl Entered ===== + +#[cfg(feature = "std")] +impl<'a> Entered<'a> { + #[inline] + fn current(&self) -> RefMut<'a, Dispatch> { + let default = self.0.default.borrow_mut(); + RefMut::map(default, |default| { + default.get_or_insert_with(|| get_global().cloned().unwrap_or_else(Dispatch::none)) + }) + } +} + +#[cfg(feature = "std")] +impl<'a> Drop for Entered<'a> { + #[inline] + fn drop(&mut self) { + self.0.can_enter.set(true); + } +} + +// ===== impl DefaultGuard ===== + +#[cfg(feature = "std")] +impl Drop for DefaultGuard { + #[inline] + fn drop(&mut self) { + // Replace the dispatcher and then drop the old one outside + // of the thread-local context. Dropping the dispatch may + // lead to the drop of a subscriber which, in the process, + // could then also attempt to access the same thread local + // state -- causing a clash. + let prev = CURRENT_STATE.try_with(|state| state.default.replace(self.0.take())); + drop(prev) + } +} + +#[cfg(test)] +mod test { + use super::*; + #[cfg(feature = "std")] + use crate::stdlib::sync::atomic::{AtomicUsize, Ordering}; + use crate::{ + callsite::Callsite, + metadata::{Kind, Level, Metadata}, + subscriber::Interest, + }; + + #[test] + fn dispatch_is() { + let dispatcher = Dispatch::new(NoSubscriber::default()); + assert!(dispatcher.is::<NoSubscriber>()); + } + + #[test] + fn dispatch_downcasts() { + let dispatcher = Dispatch::new(NoSubscriber::default()); + assert!(dispatcher.downcast_ref::<NoSubscriber>().is_some()); + } + + struct TestCallsite; + static TEST_CALLSITE: TestCallsite = TestCallsite; + static TEST_META: Metadata<'static> = metadata! { + name: "test", + target: module_path!(), + level: Level::DEBUG, + fields: &[], + callsite: &TEST_CALLSITE, + kind: Kind::EVENT + }; + + impl Callsite for TestCallsite { + fn set_interest(&self, _: Interest) {} + fn metadata(&self) -> &Metadata<'_> { + &TEST_META + } + } + + #[test] + #[cfg(feature = "std")] + fn events_dont_infinite_loop() { + // This test ensures that an event triggered within a subscriber + // won't cause an infinite loop of events. + struct TestSubscriber; + impl Subscriber for TestSubscriber { + fn enabled(&self, _: &Metadata<'_>) -> bool { + true + } + + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(0xAAAA) + } + + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + + fn event(&self, _: &Event<'_>) { + static EVENTS: AtomicUsize = AtomicUsize::new(0); + assert_eq!( + EVENTS.fetch_add(1, Ordering::Relaxed), + 0, + "event method called twice!" + ); + Event::dispatch(&TEST_META, &TEST_META.fields().value_set(&[])) + } + + fn enter(&self, _: &span::Id) {} + + fn exit(&self, _: &span::Id) {} + } + + with_default(&Dispatch::new(TestSubscriber), || { + Event::dispatch(&TEST_META, &TEST_META.fields().value_set(&[])) + }) + } + + #[test] + #[cfg(feature = "std")] + fn spans_dont_infinite_loop() { + // This test ensures that a span created within a subscriber + // won't cause an infinite loop of new spans. + + fn mk_span() { + get_default(|current| { + current.new_span(&span::Attributes::new( + &TEST_META, + &TEST_META.fields().value_set(&[]), + )) + }); + } + + struct TestSubscriber; + impl Subscriber for TestSubscriber { + fn enabled(&self, _: &Metadata<'_>) -> bool { + true + } + + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + static NEW_SPANS: AtomicUsize = AtomicUsize::new(0); + assert_eq!( + NEW_SPANS.fetch_add(1, Ordering::Relaxed), + 0, + "new_span method called twice!" + ); + mk_span(); + span::Id::from_u64(0xAAAA) + } + + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + + fn event(&self, _: &Event<'_>) {} + + fn enter(&self, _: &span::Id) {} + + fn exit(&self, _: &span::Id) {} + } + + with_default(&Dispatch::new(TestSubscriber), mk_span) + } + + #[test] + fn default_no_subscriber() { + let default_dispatcher = Dispatch::default(); + assert!(default_dispatcher.is::<NoSubscriber>()); + } + + #[cfg(feature = "std")] + #[test] + fn default_dispatch() { + struct TestSubscriber; + impl Subscriber for TestSubscriber { + fn enabled(&self, _: &Metadata<'_>) -> bool { + true + } + + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(0xAAAA) + } + + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + + fn event(&self, _: &Event<'_>) {} + + fn enter(&self, _: &span::Id) {} + + fn exit(&self, _: &span::Id) {} + } + let guard = set_default(&Dispatch::new(TestSubscriber)); + let default_dispatcher = Dispatch::default(); + assert!(default_dispatcher.is::<TestSubscriber>()); + + drop(guard); + let default_dispatcher = Dispatch::default(); + assert!(default_dispatcher.is::<NoSubscriber>()); + } +} diff --git a/vendor/tracing-core/src/event.rs b/vendor/tracing-core/src/event.rs new file mode 100644 index 000000000..6e2543762 --- /dev/null +++ b/vendor/tracing-core/src/event.rs @@ -0,0 +1,128 @@ +//! Events represent single points in time during the execution of a program. +use crate::parent::Parent; +use crate::span::Id; +use crate::{field, Metadata}; + +/// `Event`s represent single points in time where something occurred during the +/// execution of a program. +/// +/// An `Event` can be compared to a log record in unstructured logging, but with +/// two key differences: +/// - `Event`s exist _within the context of a [span]_. Unlike log lines, they +/// may be located within the trace tree, allowing visibility into the +/// _temporal_ context in which the event occurred, as well as the source +/// code location. +/// - Like spans, `Event`s have structured key-value data known as _[fields]_, +/// which may include textual message. In general, a majority of the data +/// associated with an event should be in the event's fields rather than in +/// the textual message, as the fields are more structured. +/// +/// [span]: super::span +/// [fields]: super::field +#[derive(Debug)] +pub struct Event<'a> { + fields: &'a field::ValueSet<'a>, + metadata: &'static Metadata<'static>, + parent: Parent, +} + +impl<'a> Event<'a> { + /// Constructs a new `Event` with the specified metadata and set of values, + /// and observes it with the current subscriber. + pub fn dispatch(metadata: &'static Metadata<'static>, fields: &'a field::ValueSet<'_>) { + let event = Event::new(metadata, fields); + crate::dispatcher::get_default(|current| { + current.event(&event); + }); + } + + /// Returns a new `Event` in the current span, with the specified metadata + /// and set of values. + #[inline] + pub fn new(metadata: &'static Metadata<'static>, fields: &'a field::ValueSet<'a>) -> Self { + Event { + fields, + metadata, + parent: Parent::Current, + } + } + + /// Returns a new `Event` as a child of the specified span, with the + /// provided metadata and set of values. + #[inline] + pub fn new_child_of( + parent: impl Into<Option<Id>>, + metadata: &'static Metadata<'static>, + fields: &'a field::ValueSet<'a>, + ) -> Self { + let parent = match parent.into() { + Some(p) => Parent::Explicit(p), + None => Parent::Root, + }; + Event { + fields, + metadata, + parent, + } + } + + /// Constructs a new `Event` with the specified metadata and set of values, + /// and observes it with the current subscriber and an explicit parent. + pub fn child_of( + parent: impl Into<Option<Id>>, + metadata: &'static Metadata<'static>, + fields: &'a field::ValueSet<'_>, + ) { + let event = Self::new_child_of(parent, metadata, fields); + crate::dispatcher::get_default(|current| { + current.event(&event); + }); + } + + /// Visits all the fields on this `Event` with the specified [visitor]. + /// + /// [visitor]: super::field::Visit + #[inline] + pub fn record(&self, visitor: &mut dyn field::Visit) { + self.fields.record(visitor); + } + + /// Returns an iterator over the set of values on this `Event`. + pub fn fields(&self) -> field::Iter { + self.fields.field_set().iter() + } + + /// Returns [metadata] describing this `Event`. + /// + /// [metadata]: super::Metadata + pub fn metadata(&self) -> &'static Metadata<'static> { + self.metadata + } + + /// Returns true if the new event should be a root. + pub fn is_root(&self) -> bool { + matches!(self.parent, Parent::Root) + } + + /// Returns true if the new event's parent should be determined based on the + /// current context. + /// + /// If this is true and the current thread is currently inside a span, then + /// that span should be the new event's parent. Otherwise, if the current + /// thread is _not_ inside a span, then the new event will be the root of its + /// own trace tree. + pub fn is_contextual(&self) -> bool { + matches!(self.parent, Parent::Current) + } + + /// Returns the new event's explicitly-specified parent, if there is one. + /// + /// Otherwise (if the new event is a root or is a child of the current span), + /// returns `None`. + pub fn parent(&self) -> Option<&Id> { + match self.parent { + Parent::Explicit(ref p) => Some(p), + _ => None, + } + } +} diff --git a/vendor/tracing-core/src/field.rs b/vendor/tracing-core/src/field.rs new file mode 100644 index 000000000..c9869ce2c --- /dev/null +++ b/vendor/tracing-core/src/field.rs @@ -0,0 +1,1219 @@ +//! `Span` and `Event` key-value data. +//! +//! Spans and events may be annotated with key-value data, referred to as known +//! as _fields_. These fields consist of a mapping from a key (corresponding to +//! a `&str` but represented internally as an array index) to a [`Value`]. +//! +//! # `Value`s and `Subscriber`s +//! +//! `Subscriber`s consume `Value`s as fields attached to [span]s or [`Event`]s. +//! The set of field keys on a given span or is defined on its [`Metadata`]. +//! When a span is created, it provides [`Attributes`] to the `Subscriber`'s +//! [`new_span`] method, containing any fields whose values were provided when +//! the span was created; and may call the `Subscriber`'s [`record`] method +//! with additional [`Record`]s if values are added for more of its fields. +//! Similarly, the [`Event`] type passed to the subscriber's [`event`] method +//! will contain any fields attached to each event. +//! +//! `tracing` represents values as either one of a set of Rust primitives +//! (`i64`, `u64`, `f64`, `i128`, `u128`, `bool`, and `&str`) or using a +//! `fmt::Display` or `fmt::Debug` implementation. `Subscriber`s are provided +//! these primitive value types as `dyn Value` trait objects. +//! +//! These trait objects can be formatted using `fmt::Debug`, but may also be +//! recorded as typed data by calling the [`Value::record`] method on these +//! trait objects with a _visitor_ implementing the [`Visit`] trait. This trait +//! represents the behavior used to record values of various types. For example, +//! an implementation of `Visit` might record integers by incrementing counters +//! for their field names rather than printing them. +//! +//! +//! # Using `valuable` +//! +//! `tracing`'s [`Value`] trait is intentionally minimalist: it supports only a small +//! number of Rust primitives as typed values, and only permits recording +//! user-defined types with their [`fmt::Debug`] or [`fmt::Display`] +//! implementations. However, there are some cases where it may be useful to record +//! nested values (such as arrays, `Vec`s, or `HashMap`s containing values), or +//! user-defined `struct` and `enum` types without having to format them as +//! unstructured text. +//! +//! To address `Value`'s limitations, `tracing` offers experimental support for +//! the [`valuable`] crate, which provides object-safe inspection of structured +//! values. User-defined types can implement the [`valuable::Valuable`] trait, +//! and be recorded as a `tracing` field by calling their [`as_value`] method. +//! If the [`Subscriber`] also supports the `valuable` crate, it can +//! then visit those types fields as structured values using `valuable`. +//! +//! <pre class="ignore" style="white-space:normal;font:inherit;"> +//! <strong>Note</strong>: <code>valuable</code> support is an +//! <a href = "../index.html#unstable-features">unstable feature</a>. See +//! the documentation on unstable features for details on how to enable it. +//! </pre> +//! +//! For example: +//! ```ignore +//! // Derive `Valuable` for our types: +//! use valuable::Valuable; +//! +//! #[derive(Clone, Debug, Valuable)] +//! struct User { +//! name: String, +//! age: u32, +//! address: Address, +//! } +//! +//! #[derive(Clone, Debug, Valuable)] +//! struct Address { +//! country: String, +//! city: String, +//! street: String, +//! } +//! +//! let user = User { +//! name: "Arwen Undomiel".to_string(), +//! age: 3000, +//! address: Address { +//! country: "Middle Earth".to_string(), +//! city: "Rivendell".to_string(), +//! street: "leafy lane".to_string(), +//! }, +//! }; +//! +//! // Recording `user` as a `valuable::Value` will allow the `tracing` subscriber +//! // to traverse its fields as a nested, typed structure: +//! tracing::info!(current_user = user.as_value()); +//! ``` +//! +//! Alternatively, the [`valuable()`] function may be used to convert a type +//! implementing [`Valuable`] into a `tracing` field value. +//! +//! When the `valuable` feature is enabled, the [`Visit`] trait will include an +//! optional [`record_value`] method. `Visit` implementations that wish to +//! record `valuable` values can implement this method with custom behavior. +//! If a visitor does not implement `record_value`, the [`valuable::Value`] will +//! be forwarded to the visitor's [`record_debug`] method. +//! +//! [`valuable`]: https://crates.io/crates/valuable +//! [`as_value`]: valuable::Valuable::as_value +//! [`Subscriber`]: crate::Subscriber +//! [`record_value`]: Visit::record_value +//! [`record_debug`]: Visit::record_debug +//! +//! [span]: super::span +//! [`Event`]: super::event::Event +//! [`Metadata`]: super::metadata::Metadata +//! [`Attributes`]: super::span::Attributes +//! [`Record`]: super::span::Record +//! [`new_span`]: super::subscriber::Subscriber::new_span +//! [`record`]: super::subscriber::Subscriber::record +//! [`event`]: super::subscriber::Subscriber::event +//! [`Value::record`]: Value::record +use crate::callsite; +use crate::stdlib::{ + borrow::Borrow, + fmt, + hash::{Hash, Hasher}, + num, + ops::Range, + string::String, +}; + +use self::private::ValidLen; + +/// An opaque key allowing _O_(1) access to a field in a `Span`'s key-value +/// data. +/// +/// As keys are defined by the _metadata_ of a span, rather than by an +/// individual instance of a span, a key may be used to access the same field +/// across all instances of a given span with the same metadata. Thus, when a +/// subscriber observes a new span, it need only access a field by name _once_, +/// and use the key for that name for all other accesses. +#[derive(Debug)] +pub struct Field { + i: usize, + fields: FieldSet, +} + +/// An empty field. +/// +/// This can be used to indicate that the value of a field is not currently +/// present but will be recorded later. +/// +/// When a field's value is `Empty`. it will not be recorded. +#[derive(Debug, Eq, PartialEq)] +pub struct Empty; + +/// Describes the fields present on a span. +pub struct FieldSet { + /// The names of each field on the described span. + names: &'static [&'static str], + /// The callsite where the described span originates. + callsite: callsite::Identifier, +} + +/// A set of fields and values for a span. +pub struct ValueSet<'a> { + values: &'a [(&'a Field, Option<&'a (dyn Value + 'a)>)], + fields: &'a FieldSet, +} + +/// An iterator over a set of fields. +#[derive(Debug)] +pub struct Iter { + idxs: Range<usize>, + fields: FieldSet, +} + +/// Visits typed values. +/// +/// An instance of `Visit` ("a visitor") represents the logic necessary to +/// record field values of various types. When an implementor of [`Value`] is +/// [recorded], it calls the appropriate method on the provided visitor to +/// indicate the type that value should be recorded as. +/// +/// When a [`Subscriber`] implementation [records an `Event`] or a +/// [set of `Value`s added to a `Span`], it can pass an `&mut Visit` to the +/// `record` method on the provided [`ValueSet`] or [`Event`]. This visitor +/// will then be used to record all the field-value pairs present on that +/// `Event` or `ValueSet`. +/// +/// # Examples +/// +/// A simple visitor that writes to a string might be implemented like so: +/// ``` +/// # extern crate tracing_core as tracing; +/// use std::fmt::{self, Write}; +/// use tracing::field::{Value, Visit, Field}; +/// pub struct StringVisitor<'a> { +/// string: &'a mut String, +/// } +/// +/// impl<'a> Visit for StringVisitor<'a> { +/// fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { +/// write!(self.string, "{} = {:?}; ", field.name(), value).unwrap(); +/// } +/// } +/// ``` +/// This visitor will format each recorded value using `fmt::Debug`, and +/// append the field name and formatted value to the provided string, +/// regardless of the type of the recorded value. When all the values have +/// been recorded, the `StringVisitor` may be dropped, allowing the string +/// to be printed or stored in some other data structure. +/// +/// The `Visit` trait provides default implementations for `record_i64`, +/// `record_u64`, `record_bool`, `record_str`, and `record_error`, which simply +/// forward the recorded value to `record_debug`. Thus, `record_debug` is the +/// only method which a `Visit` implementation *must* implement. However, +/// visitors may override the default implementations of these functions in +/// order to implement type-specific behavior. +/// +/// Additionally, when a visitor receives a value of a type it does not care +/// about, it is free to ignore those values completely. For example, a +/// visitor which only records numeric data might look like this: +/// +/// ``` +/// # extern crate tracing_core as tracing; +/// # use std::fmt::{self, Write}; +/// # use tracing::field::{Value, Visit, Field}; +/// pub struct SumVisitor { +/// sum: i64, +/// } +/// +/// impl Visit for SumVisitor { +/// fn record_i64(&mut self, _field: &Field, value: i64) { +/// self.sum += value; +/// } +/// +/// fn record_u64(&mut self, _field: &Field, value: u64) { +/// self.sum += value as i64; +/// } +/// +/// fn record_debug(&mut self, _field: &Field, _value: &fmt::Debug) { +/// // Do nothing +/// } +/// } +/// ``` +/// +/// This visitor (which is probably not particularly useful) keeps a running +/// sum of all the numeric values it records, and ignores all other values. A +/// more practical example of recording typed values is presented in +/// `examples/counters.rs`, which demonstrates a very simple metrics system +/// implemented using `tracing`. +/// +/// <div class="example-wrap" style="display:inline-block"> +/// <pre class="ignore" style="white-space:normal;font:inherit;"> +/// <strong>Note</strong>: The <code>record_error</code> trait method is only +/// available when the Rust standard library is present, as it requires the +/// <code>std::error::Error</code> trait. +/// </pre></div> +/// +/// [recorded]: Value::record +/// [`Subscriber`]: super::subscriber::Subscriber +/// [records an `Event`]: super::subscriber::Subscriber::event +/// [set of `Value`s added to a `Span`]: super::subscriber::Subscriber::record +/// [`Event`]: super::event::Event +pub trait Visit { + /// Visits an arbitrary type implementing the [`valuable`] crate's `Valuable` trait. + /// + /// [`valuable`]: https://docs.rs/valuable + #[cfg(all(tracing_unstable, feature = "valuable"))] + #[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] + fn record_value(&mut self, field: &Field, value: valuable::Value<'_>) { + self.record_debug(field, &value) + } + + /// Visit a double-precision floating point value. + fn record_f64(&mut self, field: &Field, value: f64) { + self.record_debug(field, &value) + } + + /// Visit a signed 64-bit integer value. + fn record_i64(&mut self, field: &Field, value: i64) { + self.record_debug(field, &value) + } + + /// Visit an unsigned 64-bit integer value. + fn record_u64(&mut self, field: &Field, value: u64) { + self.record_debug(field, &value) + } + + /// Visit a signed 128-bit integer value. + fn record_i128(&mut self, field: &Field, value: i128) { + self.record_debug(field, &value) + } + + /// Visit an unsigned 128-bit integer value. + fn record_u128(&mut self, field: &Field, value: u128) { + self.record_debug(field, &value) + } + + /// Visit a boolean value. + fn record_bool(&mut self, field: &Field, value: bool) { + self.record_debug(field, &value) + } + + /// Visit a string value. + fn record_str(&mut self, field: &Field, value: &str) { + self.record_debug(field, &value) + } + + /// Records a type implementing `Error`. + /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This is only enabled when the Rust standard library is + /// present. + /// </pre> + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { + self.record_debug(field, &DisplayValue(value)) + } + + /// Visit a value implementing `fmt::Debug`. + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug); +} + +/// A field value of an erased type. +/// +/// Implementors of `Value` may call the appropriate typed recording methods on +/// the [visitor] passed to their `record` method in order to indicate how +/// their data should be recorded. +/// +/// [visitor]: Visit +pub trait Value: crate::sealed::Sealed { + /// Visits this value with the given `Visitor`. + fn record(&self, key: &Field, visitor: &mut dyn Visit); +} + +/// A `Value` which serializes using `fmt::Display`. +/// +/// Uses `record_debug` in the `Value` implementation to +/// avoid an unnecessary evaluation. +#[derive(Clone)] +pub struct DisplayValue<T: fmt::Display>(T); + +/// A `Value` which serializes as a string using `fmt::Debug`. +#[derive(Clone)] +pub struct DebugValue<T: fmt::Debug>(T); + +/// Wraps a type implementing `fmt::Display` as a `Value` that can be +/// recorded using its `Display` implementation. +pub fn display<T>(t: T) -> DisplayValue<T> +where + T: fmt::Display, +{ + DisplayValue(t) +} + +/// Wraps a type implementing `fmt::Debug` as a `Value` that can be +/// recorded using its `Debug` implementation. +pub fn debug<T>(t: T) -> DebugValue<T> +where + T: fmt::Debug, +{ + DebugValue(t) +} + +/// Wraps a type implementing [`Valuable`] as a `Value` that +/// can be recorded using its `Valuable` implementation. +/// +/// [`Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html +#[cfg(all(tracing_unstable, feature = "valuable"))] +#[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] +pub fn valuable<T>(t: &T) -> valuable::Value<'_> +where + T: valuable::Valuable, +{ + t.as_value() +} + +// ===== impl Visit ===== + +impl<'a, 'b> Visit for fmt::DebugStruct<'a, 'b> { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + self.field(field.name(), value); + } +} + +impl<'a, 'b> Visit for fmt::DebugMap<'a, 'b> { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + self.entry(&format_args!("{}", field), value); + } +} + +impl<F> Visit for F +where + F: FnMut(&Field, &dyn fmt::Debug), +{ + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + (self)(field, value) + } +} + +// ===== impl Value ===== + +macro_rules! impl_values { + ( $( $record:ident( $( $whatever:tt)+ ) ),+ ) => { + $( + impl_value!{ $record( $( $whatever )+ ) } + )+ + } +} + +macro_rules! ty_to_nonzero { + (u8) => { + NonZeroU8 + }; + (u16) => { + NonZeroU16 + }; + (u32) => { + NonZeroU32 + }; + (u64) => { + NonZeroU64 + }; + (u128) => { + NonZeroU128 + }; + (usize) => { + NonZeroUsize + }; + (i8) => { + NonZeroI8 + }; + (i16) => { + NonZeroI16 + }; + (i32) => { + NonZeroI32 + }; + (i64) => { + NonZeroI64 + }; + (i128) => { + NonZeroI128 + }; + (isize) => { + NonZeroIsize + }; +} + +macro_rules! impl_one_value { + (f32, $op:expr, $record:ident) => { + impl_one_value!(normal, f32, $op, $record); + }; + (f64, $op:expr, $record:ident) => { + impl_one_value!(normal, f64, $op, $record); + }; + (bool, $op:expr, $record:ident) => { + impl_one_value!(normal, bool, $op, $record); + }; + ($value_ty:tt, $op:expr, $record:ident) => { + impl_one_value!(normal, $value_ty, $op, $record); + impl_one_value!(nonzero, $value_ty, $op, $record); + }; + (normal, $value_ty:tt, $op:expr, $record:ident) => { + impl $crate::sealed::Sealed for $value_ty {} + impl $crate::field::Value for $value_ty { + fn record(&self, key: &$crate::field::Field, visitor: &mut dyn $crate::field::Visit) { + visitor.$record(key, $op(*self)) + } + } + }; + (nonzero, $value_ty:tt, $op:expr, $record:ident) => { + // This `use num::*;` is reported as unused because it gets emitted + // for every single invocation of this macro, so there are multiple `use`s. + // All but the first are useless indeed. + // We need this import because we can't write a path where one part is + // the `ty_to_nonzero!($value_ty)` invocation. + #[allow(clippy::useless_attribute, unused)] + use num::*; + impl $crate::sealed::Sealed for ty_to_nonzero!($value_ty) {} + impl $crate::field::Value for ty_to_nonzero!($value_ty) { + fn record(&self, key: &$crate::field::Field, visitor: &mut dyn $crate::field::Visit) { + visitor.$record(key, $op(self.get())) + } + } + }; +} + +macro_rules! impl_value { + ( $record:ident( $( $value_ty:tt ),+ ) ) => { + $( + impl_one_value!($value_ty, |this: $value_ty| this, $record); + )+ + }; + ( $record:ident( $( $value_ty:tt ),+ as $as_ty:ty) ) => { + $( + impl_one_value!($value_ty, |this: $value_ty| this as $as_ty, $record); + )+ + }; +} + +// ===== impl Value ===== + +impl_values! { + record_u64(u64), + record_u64(usize, u32, u16, u8 as u64), + record_i64(i64), + record_i64(isize, i32, i16, i8 as i64), + record_u128(u128), + record_i128(i128), + record_bool(bool), + record_f64(f64, f32 as f64) +} + +impl<T: crate::sealed::Sealed> crate::sealed::Sealed for Wrapping<T> {} +impl<T: crate::field::Value> crate::field::Value for Wrapping<T> { + fn record(&self, key: &crate::field::Field, visitor: &mut dyn crate::field::Visit) { + self.0.record(key, visitor) + } +} + +impl crate::sealed::Sealed for str {} + +impl Value for str { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_str(key, self) + } +} + +#[cfg(feature = "std")] +impl crate::sealed::Sealed for dyn std::error::Error + 'static {} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl Value for dyn std::error::Error + 'static { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_error(key, self) + } +} + +#[cfg(feature = "std")] +impl crate::sealed::Sealed for dyn std::error::Error + Send + 'static {} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl Value for dyn std::error::Error + Send + 'static { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + (self as &dyn std::error::Error).record(key, visitor) + } +} + +#[cfg(feature = "std")] +impl crate::sealed::Sealed for dyn std::error::Error + Sync + 'static {} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl Value for dyn std::error::Error + Sync + 'static { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + (self as &dyn std::error::Error).record(key, visitor) + } +} + +#[cfg(feature = "std")] +impl crate::sealed::Sealed for dyn std::error::Error + Send + Sync + 'static {} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl Value for dyn std::error::Error + Send + Sync + 'static { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + (self as &dyn std::error::Error).record(key, visitor) + } +} + +impl<'a, T: ?Sized> crate::sealed::Sealed for &'a T where T: Value + crate::sealed::Sealed + 'a {} + +impl<'a, T: ?Sized> Value for &'a T +where + T: Value + 'a, +{ + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + (*self).record(key, visitor) + } +} + +impl<'a, T: ?Sized> crate::sealed::Sealed for &'a mut T where T: Value + crate::sealed::Sealed + 'a {} + +impl<'a, T: ?Sized> Value for &'a mut T +where + T: Value + 'a, +{ + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + // Don't use `(*self).record(key, visitor)`, otherwise would + // cause stack overflow due to `unconditional_recursion`. + T::record(self, key, visitor) + } +} + +impl<'a> crate::sealed::Sealed for fmt::Arguments<'a> {} + +impl<'a> Value for fmt::Arguments<'a> { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_debug(key, self) + } +} + +impl<T: ?Sized> crate::sealed::Sealed for crate::stdlib::boxed::Box<T> where T: Value {} + +impl<T: ?Sized> Value for crate::stdlib::boxed::Box<T> +where + T: Value, +{ + #[inline] + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + self.as_ref().record(key, visitor) + } +} + +impl crate::sealed::Sealed for String {} +impl Value for String { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_str(key, self.as_str()) + } +} + +impl fmt::Debug for dyn Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // We are only going to be recording the field value, so we don't + // actually care about the field name here. + struct NullCallsite; + static NULL_CALLSITE: NullCallsite = NullCallsite; + impl crate::callsite::Callsite for NullCallsite { + fn set_interest(&self, _: crate::subscriber::Interest) { + unreachable!("you somehow managed to register the null callsite?") + } + + fn metadata(&self) -> &crate::Metadata<'_> { + unreachable!("you somehow managed to access the null callsite?") + } + } + + static FIELD: Field = Field { + i: 0, + fields: FieldSet::new(&[], crate::identify_callsite!(&NULL_CALLSITE)), + }; + + let mut res = Ok(()); + self.record(&FIELD, &mut |_: &Field, val: &dyn fmt::Debug| { + res = write!(f, "{:?}", val); + }); + res + } +} + +impl fmt::Display for dyn Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self, f) + } +} + +// ===== impl DisplayValue ===== + +impl<T: fmt::Display> crate::sealed::Sealed for DisplayValue<T> {} + +impl<T> Value for DisplayValue<T> +where + T: fmt::Display, +{ + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_debug(key, self) + } +} + +impl<T: fmt::Display> fmt::Debug for DisplayValue<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl<T: fmt::Display> fmt::Display for DisplayValue<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +// ===== impl DebugValue ===== + +impl<T: fmt::Debug> crate::sealed::Sealed for DebugValue<T> {} + +impl<T> Value for DebugValue<T> +where + T: fmt::Debug, +{ + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_debug(key, &self.0) + } +} + +impl<T: fmt::Debug> fmt::Debug for DebugValue<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +// ===== impl ValuableValue ===== + +#[cfg(all(tracing_unstable, feature = "valuable"))] +impl crate::sealed::Sealed for valuable::Value<'_> {} + +#[cfg(all(tracing_unstable, feature = "valuable"))] +#[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] +impl Value for valuable::Value<'_> { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_value(key, *self) + } +} + +#[cfg(all(tracing_unstable, feature = "valuable"))] +impl crate::sealed::Sealed for &'_ dyn valuable::Valuable {} + +#[cfg(all(tracing_unstable, feature = "valuable"))] +#[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] +impl Value for &'_ dyn valuable::Valuable { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_value(key, self.as_value()) + } +} + +impl crate::sealed::Sealed for Empty {} +impl Value for Empty { + #[inline] + fn record(&self, _: &Field, _: &mut dyn Visit) {} +} + +impl<T: Value> crate::sealed::Sealed for Option<T> {} + +impl<T: Value> Value for Option<T> { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + if let Some(v) = &self { + v.record(key, visitor) + } + } +} + +// ===== impl Field ===== + +impl Field { + /// Returns an [`Identifier`] that uniquely identifies the [`Callsite`] + /// which defines this field. + /// + /// [`Identifier`]: super::callsite::Identifier + /// [`Callsite`]: super::callsite::Callsite + #[inline] + pub fn callsite(&self) -> callsite::Identifier { + self.fields.callsite() + } + + /// Returns a string representing the name of the field. + pub fn name(&self) -> &'static str { + self.fields.names[self.i] + } +} + +impl fmt::Display for Field { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad(self.name()) + } +} + +impl AsRef<str> for Field { + fn as_ref(&self) -> &str { + self.name() + } +} + +impl PartialEq for Field { + fn eq(&self, other: &Self) -> bool { + self.callsite() == other.callsite() && self.i == other.i + } +} + +impl Eq for Field {} + +impl Hash for Field { + fn hash<H>(&self, state: &mut H) + where + H: Hasher, + { + self.callsite().hash(state); + self.i.hash(state); + } +} + +impl Clone for Field { + fn clone(&self) -> Self { + Field { + i: self.i, + fields: FieldSet { + names: self.fields.names, + callsite: self.fields.callsite(), + }, + } + } +} + +// ===== impl FieldSet ===== + +impl FieldSet { + /// Constructs a new `FieldSet` with the given array of field names and callsite. + pub const fn new(names: &'static [&'static str], callsite: callsite::Identifier) -> Self { + Self { names, callsite } + } + + /// Returns an [`Identifier`] that uniquely identifies the [`Callsite`] + /// which defines this set of fields.. + /// + /// [`Identifier`]: super::callsite::Identifier + /// [`Callsite`]: super::callsite::Callsite + pub(crate) fn callsite(&self) -> callsite::Identifier { + callsite::Identifier(self.callsite.0) + } + + /// Returns the [`Field`] named `name`, or `None` if no such field exists. + /// + /// [`Field`]: super::Field + pub fn field<Q: ?Sized>(&self, name: &Q) -> Option<Field> + where + Q: Borrow<str>, + { + let name = &name.borrow(); + self.names.iter().position(|f| f == name).map(|i| Field { + i, + fields: FieldSet { + names: self.names, + callsite: self.callsite(), + }, + }) + } + + /// Returns `true` if `self` contains the given `field`. + /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: If <code>field</code> shares a name with a field + /// in this <code>FieldSet</code>, but was created by a <code>FieldSet</code> + /// with a different callsite, this <code>FieldSet</code> does <em>not</em> + /// contain it. This is so that if two separate span callsites define a field + /// named "foo", the <code>Field</code> corresponding to "foo" for each + /// of those callsites are not equivalent. + /// </pre></div> + pub fn contains(&self, field: &Field) -> bool { + field.callsite() == self.callsite() && field.i <= self.len() + } + + /// Returns an iterator over the `Field`s in this `FieldSet`. + pub fn iter(&self) -> Iter { + let idxs = 0..self.len(); + Iter { + idxs, + fields: FieldSet { + names: self.names, + callsite: self.callsite(), + }, + } + } + + /// Returns a new `ValueSet` with entries for this `FieldSet`'s values. + /// + /// Note that a `ValueSet` may not be constructed with arrays of over 32 + /// elements. + #[doc(hidden)] + pub fn value_set<'v, V>(&'v self, values: &'v V) -> ValueSet<'v> + where + V: ValidLen<'v>, + { + ValueSet { + fields: self, + values: values.borrow(), + } + } + + /// Returns the number of fields in this `FieldSet`. + #[inline] + pub fn len(&self) -> usize { + self.names.len() + } + + /// Returns whether or not this `FieldSet` has fields. + #[inline] + pub fn is_empty(&self) -> bool { + self.names.is_empty() + } +} + +impl<'a> IntoIterator for &'a FieldSet { + type IntoIter = Iter; + type Item = Field; + #[inline] + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +impl fmt::Debug for FieldSet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FieldSet") + .field("names", &self.names) + .field("callsite", &self.callsite) + .finish() + } +} + +impl fmt::Display for FieldSet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_set() + .entries(self.names.iter().map(display)) + .finish() + } +} + +// ===== impl Iter ===== + +impl Iterator for Iter { + type Item = Field; + fn next(&mut self) -> Option<Field> { + let i = self.idxs.next()?; + Some(Field { + i, + fields: FieldSet { + names: self.fields.names, + callsite: self.fields.callsite(), + }, + }) + } +} + +// ===== impl ValueSet ===== + +impl<'a> ValueSet<'a> { + /// Returns an [`Identifier`] that uniquely identifies the [`Callsite`] + /// defining the fields this `ValueSet` refers to. + /// + /// [`Identifier`]: super::callsite::Identifier + /// [`Callsite`]: super::callsite::Callsite + #[inline] + pub fn callsite(&self) -> callsite::Identifier { + self.fields.callsite() + } + + /// Visits all the fields in this `ValueSet` with the provided [visitor]. + /// + /// [visitor]: Visit + pub fn record(&self, visitor: &mut dyn Visit) { + let my_callsite = self.callsite(); + for (field, value) in self.values { + if field.callsite() != my_callsite { + continue; + } + if let Some(value) = value { + value.record(field, visitor); + } + } + } + + /// Returns the number of fields in this `ValueSet` that would be visited + /// by a given [visitor] to the [`ValueSet::record()`] method. + /// + /// [visitor]: Visit + /// [`ValueSet::record()`]: ValueSet::record() + pub fn len(&self) -> usize { + let my_callsite = self.callsite(); + self.values + .iter() + .filter(|(field, _)| field.callsite() == my_callsite) + .count() + } + + /// Returns `true` if this `ValueSet` contains a value for the given `Field`. + pub(crate) fn contains(&self, field: &Field) -> bool { + field.callsite() == self.callsite() + && self + .values + .iter() + .any(|(key, val)| *key == field && val.is_some()) + } + + /// Returns true if this `ValueSet` contains _no_ values. + pub fn is_empty(&self) -> bool { + let my_callsite = self.callsite(); + self.values + .iter() + .all(|(key, val)| val.is_none() || key.callsite() != my_callsite) + } + + pub(crate) fn field_set(&self) -> &FieldSet { + self.fields + } +} + +impl<'a> fmt::Debug for ValueSet<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.values + .iter() + .fold(&mut f.debug_struct("ValueSet"), |dbg, (key, v)| { + if let Some(val) = v { + val.record(key, dbg); + } + dbg + }) + .field("callsite", &self.callsite()) + .finish() + } +} + +impl<'a> fmt::Display for ValueSet<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.values + .iter() + .fold(&mut f.debug_map(), |dbg, (key, v)| { + if let Some(val) = v { + val.record(key, dbg); + } + dbg + }) + .finish() + } +} + +// ===== impl ValidLen ===== + +mod private { + use super::*; + + /// Marker trait implemented by arrays which are of valid length to + /// construct a `ValueSet`. + /// + /// `ValueSet`s may only be constructed from arrays containing 32 or fewer + /// elements, to ensure the array is small enough to always be allocated on the + /// stack. This trait is only implemented by arrays of an appropriate length, + /// ensuring that the correct size arrays are used at compile-time. + pub trait ValidLen<'a>: Borrow<[(&'a Field, Option<&'a (dyn Value + 'a)>)]> {} +} + +macro_rules! impl_valid_len { + ( $( $len:tt ),+ ) => { + $( + impl<'a> private::ValidLen<'a> for + [(&'a Field, Option<&'a (dyn Value + 'a)>); $len] {} + )+ + } +} + +impl_valid_len! { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 +} + +#[cfg(test)] +mod test { + use super::*; + use crate::metadata::{Kind, Level, Metadata}; + use crate::stdlib::{borrow::ToOwned, string::String}; + + struct TestCallsite1; + static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1; + static TEST_META_1: Metadata<'static> = metadata! { + name: "field_test1", + target: module_path!(), + level: Level::INFO, + fields: &["foo", "bar", "baz"], + callsite: &TEST_CALLSITE_1, + kind: Kind::SPAN, + }; + + impl crate::callsite::Callsite for TestCallsite1 { + fn set_interest(&self, _: crate::subscriber::Interest) { + unimplemented!() + } + + fn metadata(&self) -> &Metadata<'_> { + &TEST_META_1 + } + } + + struct TestCallsite2; + static TEST_CALLSITE_2: TestCallsite2 = TestCallsite2; + static TEST_META_2: Metadata<'static> = metadata! { + name: "field_test2", + target: module_path!(), + level: Level::INFO, + fields: &["foo", "bar", "baz"], + callsite: &TEST_CALLSITE_2, + kind: Kind::SPAN, + }; + + impl crate::callsite::Callsite for TestCallsite2 { + fn set_interest(&self, _: crate::subscriber::Interest) { + unimplemented!() + } + + fn metadata(&self) -> &Metadata<'_> { + &TEST_META_2 + } + } + + #[test] + fn value_set_with_no_values_is_empty() { + let fields = TEST_META_1.fields(); + let values = &[ + (&fields.field("foo").unwrap(), None), + (&fields.field("bar").unwrap(), None), + (&fields.field("baz").unwrap(), None), + ]; + let valueset = fields.value_set(values); + assert!(valueset.is_empty()); + } + + #[test] + fn empty_value_set_is_empty() { + let fields = TEST_META_1.fields(); + let valueset = fields.value_set(&[]); + assert!(valueset.is_empty()); + } + + #[test] + fn value_sets_with_fields_from_other_callsites_are_empty() { + let fields = TEST_META_1.fields(); + let values = &[ + (&fields.field("foo").unwrap(), Some(&1 as &dyn Value)), + (&fields.field("bar").unwrap(), Some(&2 as &dyn Value)), + (&fields.field("baz").unwrap(), Some(&3 as &dyn Value)), + ]; + let valueset = TEST_META_2.fields().value_set(values); + assert!(valueset.is_empty()) + } + + #[test] + fn sparse_value_sets_are_not_empty() { + let fields = TEST_META_1.fields(); + let values = &[ + (&fields.field("foo").unwrap(), None), + (&fields.field("bar").unwrap(), Some(&57 as &dyn Value)), + (&fields.field("baz").unwrap(), None), + ]; + let valueset = fields.value_set(values); + assert!(!valueset.is_empty()); + } + + #[test] + fn fields_from_other_callsets_are_skipped() { + let fields = TEST_META_1.fields(); + let values = &[ + (&fields.field("foo").unwrap(), None), + ( + &TEST_META_2.fields().field("bar").unwrap(), + Some(&57 as &dyn Value), + ), + (&fields.field("baz").unwrap(), None), + ]; + + struct MyVisitor; + impl Visit for MyVisitor { + fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) { + assert_eq!(field.callsite(), TEST_META_1.callsite()) + } + } + let valueset = fields.value_set(values); + valueset.record(&mut MyVisitor); + } + + #[test] + fn empty_fields_are_skipped() { + let fields = TEST_META_1.fields(); + let values = &[ + (&fields.field("foo").unwrap(), Some(&Empty as &dyn Value)), + (&fields.field("bar").unwrap(), Some(&57 as &dyn Value)), + (&fields.field("baz").unwrap(), Some(&Empty as &dyn Value)), + ]; + + struct MyVisitor; + impl Visit for MyVisitor { + fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) { + assert_eq!(field.name(), "bar") + } + } + let valueset = fields.value_set(values); + valueset.record(&mut MyVisitor); + } + + #[test] + fn record_debug_fn() { + let fields = TEST_META_1.fields(); + let values = &[ + (&fields.field("foo").unwrap(), Some(&1 as &dyn Value)), + (&fields.field("bar").unwrap(), Some(&2 as &dyn Value)), + (&fields.field("baz").unwrap(), Some(&3 as &dyn Value)), + ]; + let valueset = fields.value_set(values); + let mut result = String::new(); + valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| { + use crate::stdlib::fmt::Write; + write!(&mut result, "{:?}", value).unwrap(); + }); + assert_eq!(result, "123".to_owned()); + } + + #[test] + #[cfg(feature = "std")] + fn record_error() { + let fields = TEST_META_1.fields(); + let err: Box<dyn std::error::Error + Send + Sync + 'static> = + std::io::Error::new(std::io::ErrorKind::Other, "lol").into(); + let values = &[ + (&fields.field("foo").unwrap(), Some(&err as &dyn Value)), + (&fields.field("bar").unwrap(), Some(&Empty as &dyn Value)), + (&fields.field("baz").unwrap(), Some(&Empty as &dyn Value)), + ]; + let valueset = fields.value_set(values); + let mut result = String::new(); + valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| { + use core::fmt::Write; + write!(&mut result, "{:?}", value).unwrap(); + }); + assert_eq!(result, format!("{}", err)); + } +} diff --git a/vendor/tracing-core/src/lazy.rs b/vendor/tracing-core/src/lazy.rs new file mode 100644 index 000000000..4f004e636 --- /dev/null +++ b/vendor/tracing-core/src/lazy.rs @@ -0,0 +1,76 @@ +#[cfg(feature = "std")] +pub(crate) use once_cell::sync::Lazy; + +#[cfg(not(feature = "std"))] +pub(crate) use self::spin::Lazy; + +#[cfg(not(feature = "std"))] +mod spin { + //! This is the `once_cell::sync::Lazy` type, but modified to use our + //! `spin::Once` type rather than `OnceCell`. This is used to replace + //! `once_cell::sync::Lazy` on `no-std` builds. + use crate::spin::Once; + use core::{cell::Cell, fmt, ops::Deref}; + + /// Re-implementation of `once_cell::sync::Lazy` on top of `spin::Once` + /// rather than `OnceCell`. + /// + /// This is used when the standard library is disabled. + pub(crate) struct Lazy<T, F = fn() -> T> { + cell: Once<T>, + init: Cell<Option<F>>, + } + + impl<T: fmt::Debug, F> fmt::Debug for Lazy<T, F> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Lazy") + .field("cell", &self.cell) + .field("init", &"..") + .finish() + } + } + + // We never create a `&F` from a `&Lazy<T, F>` so it is fine to not impl + // `Sync` for `F`. We do create a `&mut Option<F>` in `force`, but this is + // properly synchronized, so it only happens once so it also does not + // contribute to this impl. + unsafe impl<T, F: Send> Sync for Lazy<T, F> where Once<T>: Sync {} + // auto-derived `Send` impl is OK. + + impl<T, F> Lazy<T, F> { + /// Creates a new lazy value with the given initializing function. + pub(crate) const fn new(init: F) -> Lazy<T, F> { + Lazy { + cell: Once::new(), + init: Cell::new(Some(init)), + } + } + } + + impl<T, F: FnOnce() -> T> Lazy<T, F> { + /// Forces the evaluation of this lazy value and returns a reference to + /// the result. + /// + /// This is equivalent to the `Deref` impl, but is explicit. + pub(crate) fn force(this: &Lazy<T, F>) -> &T { + this.cell.call_once(|| match this.init.take() { + Some(f) => f(), + None => panic!("Lazy instance has previously been poisoned"), + }) + } + } + + impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> { + type Target = T; + fn deref(&self) -> &T { + Lazy::force(self) + } + } + + impl<T: Default> Default for Lazy<T> { + /// Creates a new lazy value using `Default` as the initializing function. + fn default() -> Lazy<T> { + Lazy::new(T::default) + } + } +} diff --git a/vendor/tracing-core/src/lib.rs b/vendor/tracing-core/src/lib.rs new file mode 100644 index 000000000..c1f87b22f --- /dev/null +++ b/vendor/tracing-core/src/lib.rs @@ -0,0 +1,295 @@ +//! Core primitives for `tracing`. +//! +//! [`tracing`] is a framework for instrumenting Rust programs to collect +//! structured, event-based diagnostic information. This crate defines the core +//! primitives of `tracing`. +//! +//! This crate provides: +//! +//! * [`span::Id`] identifies a span within the execution of a program. +//! +//! * [`Event`] represents a single event within a trace. +//! +//! * [`Subscriber`], the trait implemented to collect trace data. +//! +//! * [`Metadata`] and [`Callsite`] provide information describing spans and +//! `Event`s. +//! +//! * [`Field`], [`FieldSet`], [`Value`], and [`ValueSet`] represent the +//! structured data attached to a span. +//! +//! * [`Dispatch`] allows spans and events to be dispatched to `Subscriber`s. +//! +//! In addition, it defines the global callsite registry and per-thread current +//! dispatcher which other components of the tracing system rely on. +//! +//! *Compiler support: [requires `rustc` 1.49+][msrv]* +//! +//! [msrv]: #supported-rust-versions +//! +//! ## Usage +//! +//! Application authors will typically not use this crate directly. Instead, +//! they will use the [`tracing`] crate, which provides a much more +//! fully-featured API. However, this crate's API will change very infrequently, +//! so it may be used when dependencies must be very stable. +//! +//! `Subscriber` implementations may depend on `tracing-core` rather than +//! `tracing`, as the additional APIs provided by `tracing` are primarily useful +//! for instrumenting libraries and applications, and are generally not +//! necessary for `Subscriber` implementations. +//! +//! The [`tokio-rs/tracing`] repository contains less stable crates designed to +//! be used with the `tracing` ecosystem. It includes a collection of +//! `Subscriber` implementations, as well as utility and adapter crates. +//! +//! ## Crate Feature Flags +//! +//! The following crate [feature flags] are available: +//! +//! * `std`: Depend on the Rust standard library (enabled by default). +//! +//! `no_std` users may disable this feature with `default-features = false`: +//! +//! ```toml +//! [dependencies] +//! tracing-core = { version = "0.1.22", default-features = false } +//! ``` +//! +//! **Note**:`tracing-core`'s `no_std` support requires `liballoc`. +//! +//! ### Unstable Features +//! +//! These feature flags enable **unstable** features. The public API may break in 0.1.x +//! releases. To enable these features, the `--cfg tracing_unstable` must be passed to +//! `rustc` when compiling. +//! +//! The following unstable feature flags are currently available: +//! +//! * `valuable`: Enables support for recording [field values] using the +//! [`valuable`] crate. +//! +//! #### Enabling Unstable Features +//! +//! The easiest way to set the `tracing_unstable` cfg is to use the `RUSTFLAGS` +//! env variable when running `cargo` commands: +//! +//! ```shell +//! RUSTFLAGS="--cfg tracing_unstable" cargo build +//! ``` +//! Alternatively, the following can be added to the `.cargo/config` file in a +//! project to automatically enable the cfg flag for that project: +//! +//! ```toml +//! [build] +//! rustflags = ["--cfg", "tracing_unstable"] +//! ``` +//! +//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section +//! [field values]: crate::field +//! [`valuable`]: https://crates.io/crates/valuable +//! +//! ## Supported Rust Versions +//! +//! Tracing is built against the latest stable release. The minimum supported +//! version is 1.49. 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. +//! +//! +//! [`span::Id`]: span::Id +//! [`Event`]: event::Event +//! [`Subscriber`]: subscriber::Subscriber +//! [`Metadata`]: metadata::Metadata +//! [`Callsite`]: callsite::Callsite +//! [`Field`]: field::Field +//! [`FieldSet`]: field::FieldSet +//! [`Value`]: field::Value +//! [`ValueSet`]: field::ValueSet +//! [`Dispatch`]: dispatcher::Dispatch +//! [`tokio-rs/tracing`]: https://github.com/tokio-rs/tracing +//! [`tracing`]: https://crates.io/crates/tracing +#![doc(html_root_url = "https://docs.rs/tracing-core/0.1.22")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png", + issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" +)] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::broken_intra_doc_links))] +#![warn( + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + unreachable_pub, + bad_style, + const_err, + dead_code, + improper_ctypes, + non_shorthand_field_patterns, + no_mangle_generic_items, + overflowing_literals, + path_statements, + patterns_in_fns_without_body, + private_in_public, + unconditional_recursion, + unused, + unused_allocation, + unused_comparisons, + unused_parens, + while_true +)] +#[cfg(not(feature = "std"))] +extern crate alloc; + +/// Statically constructs an [`Identifier`] for the provided [`Callsite`]. +/// +/// This may be used in contexts such as static initializers. +/// +/// For example: +/// ```rust +/// use tracing_core::{callsite, identify_callsite}; +/// # use tracing_core::{Metadata, subscriber::Interest}; +/// # fn main() { +/// pub struct MyCallsite { +/// // ... +/// } +/// impl callsite::Callsite for MyCallsite { +/// # fn set_interest(&self, _: Interest) { unimplemented!() } +/// # fn metadata(&self) -> &Metadata { unimplemented!() } +/// // ... +/// } +/// +/// static CALLSITE: MyCallsite = MyCallsite { +/// // ... +/// }; +/// +/// static CALLSITE_ID: callsite::Identifier = identify_callsite!(&CALLSITE); +/// # } +/// ``` +/// +/// [`Identifier`]: callsite::Identifier +/// [`Callsite`]: callsite::Callsite +#[macro_export] +macro_rules! identify_callsite { + ($callsite:expr) => { + $crate::callsite::Identifier($callsite) + }; +} + +/// Statically constructs new span [metadata]. +/// +/// /// For example: +/// ```rust +/// # use tracing_core::{callsite::Callsite, subscriber::Interest}; +/// use tracing_core::metadata; +/// use tracing_core::metadata::{Kind, Level, Metadata}; +/// # fn main() { +/// # pub struct MyCallsite { } +/// # impl Callsite for MyCallsite { +/// # fn set_interest(&self, _: Interest) { unimplemented!() } +/// # fn metadata(&self) -> &Metadata { unimplemented!() } +/// # } +/// # +/// static FOO_CALLSITE: MyCallsite = MyCallsite { +/// // ... +/// }; +/// +/// static FOO_METADATA: Metadata = metadata!{ +/// name: "foo", +/// target: module_path!(), +/// level: Level::DEBUG, +/// fields: &["bar", "baz"], +/// callsite: &FOO_CALLSITE, +/// kind: Kind::SPAN, +/// }; +/// # } +/// ``` +/// +/// [metadata]: metadata::Metadata +/// [`Metadata::new`]: metadata::Metadata::new +#[macro_export] +macro_rules! metadata { + ( + name: $name:expr, + target: $target:expr, + level: $level:expr, + fields: $fields:expr, + callsite: $callsite:expr, + kind: $kind:expr + ) => { + $crate::metadata! { + name: $name, + target: $target, + level: $level, + fields: $fields, + callsite: $callsite, + kind: $kind, + } + }; + ( + name: $name:expr, + target: $target:expr, + level: $level:expr, + fields: $fields:expr, + callsite: $callsite:expr, + kind: $kind:expr, + ) => { + $crate::metadata::Metadata::new( + $name, + $target, + $level, + Some(file!()), + Some(line!()), + Some(module_path!()), + $crate::field::FieldSet::new($fields, $crate::identify_callsite!($callsite)), + $kind, + ) + }; +} + +pub(crate) mod lazy; + +// Trimmed-down vendored version of spin 0.5.2 (0387621) +// Dependency of no_std lazy_static, not required in a std build +#[cfg(not(feature = "std"))] +pub(crate) mod spin; + +#[cfg(not(feature = "std"))] +#[doc(hidden)] +pub type Once = self::spin::Once<()>; + +#[cfg(feature = "std")] +pub use stdlib::sync::Once; + +pub mod callsite; +pub mod dispatcher; +pub mod event; +pub mod field; +pub mod metadata; +mod parent; +pub mod span; +pub(crate) mod stdlib; +pub mod subscriber; + +#[doc(inline)] +pub use self::{ + callsite::Callsite, + dispatcher::Dispatch, + event::Event, + field::Field, + metadata::{Level, LevelFilter, Metadata}, + subscriber::Subscriber, +}; + +pub use self::{metadata::Kind, subscriber::Interest}; + +mod sealed { + pub trait Sealed {} +} diff --git a/vendor/tracing-core/src/metadata.rs b/vendor/tracing-core/src/metadata.rs new file mode 100644 index 000000000..47b9388a4 --- /dev/null +++ b/vendor/tracing-core/src/metadata.rs @@ -0,0 +1,1061 @@ +//! Metadata describing trace data. +use super::{callsite, field}; +use crate::stdlib::{ + cmp, fmt, + str::FromStr, + sync::atomic::{AtomicUsize, Ordering}, +}; + +/// Metadata describing a [span] or [event]. +/// +/// All spans and events have the following metadata: +/// - A [name], represented as a static string. +/// - A [target], a string that categorizes part of the system where the span +/// or event occurred. The `tracing` macros default to using the module +/// path where the span or event originated as the target, but it may be +/// overridden. +/// - A [verbosity level]. This determines how verbose a given span or event +/// is, and allows enabling or disabling more verbose diagnostics +/// situationally. See the documentation for the [`Level`] type for details. +/// - The names of the [fields] defined by the span or event. +/// - Whether the metadata corresponds to a span or event. +/// +/// In addition, the following optional metadata describing the source code +/// location where the span or event originated _may_ be provided: +/// - The [file name] +/// - The [line number] +/// - The [module path] +/// +/// Metadata is used by [`Subscriber`]s when filtering spans and events, and it +/// may also be used as part of their data payload. +/// +/// When created by the `event!` or `span!` macro, the metadata describing a +/// particular event or span is constructed statically and exists as a single +/// static instance. Thus, the overhead of creating the metadata is +/// _significantly_ lower than that of creating the actual span. Therefore, +/// filtering is based on metadata, rather than on the constructed span. +/// +/// <pre class="ignore" style="white-space:normal;font:inherit;"> +/// <strong>Note</strong>: Although instances of <code>Metadata</code> +/// cannot be compared directly, they provide a method +/// <a href="struct.Metadata.html#method.id"><code>id</code></a>, returning +/// an opaque <a href="../callsite/struct.Identifier.html">callsite +/// identifier</a>which uniquely identifies the callsite where the metadata +/// originated. This can be used to determine if two <code>Metadata</code> +/// correspond to the same callsite. +/// </pre> +/// +/// [span]: super::span +/// [event]: super::event +/// [name]: Metadata::name() +/// [target]: Metadata::target() +/// [fields]: Metadata::fields() +/// [verbosity level]: Metadata::level() +/// [file name]: Metadata::file() +/// [line number]: Metadata::line() +/// [module path]: Metadata::module_path() +/// [`Subscriber`]: super::subscriber::Subscriber +/// [`id`]: Metadata::id +/// [callsite identifier]: super::callsite::Identifier +pub struct Metadata<'a> { + /// The name of the span described by this metadata. + name: &'static str, + + /// The part of the system that the span that this metadata describes + /// occurred in. + 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. + module_path: Option<&'a str>, + + /// The name of the source code file where the span occurred, or `None` if + /// this could not be determined. + file: Option<&'a str>, + + /// The line number in the source code file where the span occurred, or + /// `None` if this could not be determined. + line: Option<u32>, + + /// The names of the key-value fields attached to the described span or + /// event. + fields: field::FieldSet, + + /// The kind of the callsite. + kind: Kind, +} + +/// Indicates whether the callsite is a span or event. +#[derive(Clone, Eq, PartialEq)] +pub struct Kind(u8); + +/// Describes the level of verbosity of a span or event. +/// +/// # Comparing Levels +/// +/// `Level` implements the [`PartialOrd`] and [`Ord`] traits, allowing two +/// `Level`s to be compared to determine which is considered more or less +/// verbose. Levels which are more verbose are considered "greater than" levels +/// which are less verbose, with [`Level::ERROR`] considered the lowest, and +/// [`Level::TRACE`] considered the highest. +/// +/// For example: +/// ``` +/// use tracing_core::Level; +/// +/// assert!(Level::TRACE > Level::DEBUG); +/// assert!(Level::ERROR < Level::WARN); +/// assert!(Level::INFO <= Level::DEBUG); +/// assert_eq!(Level::TRACE, Level::TRACE); +/// ``` +/// +/// # Filtering +/// +/// `Level`s are typically used to implement filtering that determines which +/// spans and events are enabled. Depending on the use case, more or less +/// verbose diagnostics may be desired. For example, when running in +/// development, [`DEBUG`]-level traces may be enabled by default. When running in +/// production, only [`INFO`]-level and lower traces might be enabled. Libraries +/// may include very verbose diagnostics at the [`DEBUG`] and/or [`TRACE`] levels. +/// Applications using those libraries typically chose to ignore those traces. However, when +/// debugging an issue involving said libraries, it may be useful to temporarily +/// enable the more verbose traces. +/// +/// The [`LevelFilter`] type is provided to enable filtering traces by +/// verbosity. `Level`s can be compared against [`LevelFilter`]s, and +/// [`LevelFilter`] has a variant for each `Level`, which compares analogously +/// to that level. In addition, [`LevelFilter`] adds a [`LevelFilter::OFF`] +/// variant, which is considered "less verbose" than every other `Level`. This is +/// intended to allow filters to completely disable tracing in a particular context. +/// +/// For example: +/// ``` +/// use tracing_core::{Level, LevelFilter}; +/// +/// assert!(LevelFilter::OFF < Level::TRACE); +/// assert!(LevelFilter::TRACE > Level::DEBUG); +/// assert!(LevelFilter::ERROR < Level::WARN); +/// assert!(LevelFilter::INFO <= Level::DEBUG); +/// assert!(LevelFilter::INFO >= Level::INFO); +/// ``` +/// +/// ## Examples +/// +/// Below is a simple example of how a [`Subscriber`] could implement filtering through +/// a [`LevelFilter`]. When a span or event is recorded, the [`Subscriber::enabled`] method +/// compares the span or event's `Level` against the configured [`LevelFilter`]. +/// The optional [`Subscriber::max_level_hint`] method can also be implemented to allow spans +/// and events above a maximum verbosity level to be skipped more efficiently, +/// often improving performance in short-lived programs. +/// +/// ``` +/// use tracing_core::{span, Event, Level, LevelFilter, Subscriber, Metadata}; +/// # use tracing_core::span::{Id, Record, Current}; +/// +/// #[derive(Debug)] +/// pub struct MySubscriber { +/// /// The most verbose level that this subscriber will enable. +/// max_level: LevelFilter, +/// +/// // ... +/// } +/// +/// impl MySubscriber { +/// /// Returns a new `MySubscriber` which will record spans and events up to +/// /// `max_level`. +/// pub fn with_max_level(max_level: LevelFilter) -> Self { +/// Self { +/// max_level, +/// // ... +/// } +/// } +/// } +/// impl Subscriber for MySubscriber { +/// fn enabled(&self, meta: &Metadata<'_>) -> bool { +/// // A span or event is enabled if it is at or below the configured +/// // maximum level. +/// meta.level() <= &self.max_level +/// } +/// +/// // This optional method returns the most verbose level that this +/// // subscriber will enable. Although implementing this method is not +/// // *required*, it permits additional optimizations when it is provided, +/// // allowing spans and events above the max level to be skipped +/// // more efficiently. +/// fn max_level_hint(&self) -> Option<LevelFilter> { +/// Some(self.max_level) +/// } +/// +/// // Implement the rest of the subscriber... +/// fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { +/// // ... +/// # drop(span); Id::from_u64(1) +/// } + +/// fn event(&self, event: &Event<'_>) { +/// // ... +/// # drop(event); +/// } +/// +/// // ... +/// # fn enter(&self, _: &Id) {} +/// # fn exit(&self, _: &Id) {} +/// # fn record(&self, _: &Id, _: &Record<'_>) {} +/// # fn record_follows_from(&self, _: &Id, _: &Id) {} +/// } +/// ``` +/// +/// It is worth noting that the `tracing-subscriber` crate provides [additional +/// APIs][envfilter] for performing more sophisticated filtering, such as +/// enabling different levels based on which module or crate a span or event is +/// recorded in. +/// +/// [`DEBUG`]: Level::DEBUG +/// [`INFO`]: Level::INFO +/// [`TRACE`]: Level::TRACE +/// [`Subscriber::enabled`]: crate::subscriber::Subscriber::enabled +/// [`Subscriber::max_level_hint`]: crate::subscriber::Subscriber::max_level_hint +/// [`Subscriber`]: crate::subscriber::Subscriber +/// [envfilter]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub struct Level(LevelInner); + +/// A filter comparable to a verbosity [`Level`]. +/// +/// If a [`Level`] is considered less than a `LevelFilter`, it should be +/// considered enabled; if greater than or equal to the `LevelFilter`, +/// that level is disabled. See [`LevelFilter::current`] for more +/// details. +/// +/// Note that this is essentially identical to the `Level` type, but with the +/// addition of an [`OFF`] level that completely disables all trace +/// instrumentation. +/// +/// See the documentation for the [`Level`] type to see how `Level`s +/// and `LevelFilter`s interact. +/// +/// [`OFF`]: LevelFilter::OFF +#[repr(transparent)] +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub struct LevelFilter(Option<Level>); + +/// Indicates that a string could not be parsed to a valid level. +#[derive(Clone, Debug)] +pub struct ParseLevelFilterError(()); + +static MAX_LEVEL: AtomicUsize = AtomicUsize::new(LevelFilter::OFF_USIZE); + +// ===== impl Metadata ===== + +impl<'a> Metadata<'a> { + /// Construct new metadata for a span or event, with a name, target, level, field + /// names, and optional source code location. + pub const fn new( + name: &'static str, + target: &'a str, + level: Level, + file: Option<&'a str>, + line: Option<u32>, + module_path: Option<&'a str>, + fields: field::FieldSet, + kind: Kind, + ) -> Self { + Metadata { + name, + target, + level, + module_path, + file, + line, + fields, + kind, + } + } + + /// Returns the names of the fields on the described span or event. + pub fn fields(&self) -> &field::FieldSet { + &self.fields + } + + /// Returns the level of verbosity of the described span or event. + pub fn level(&self) -> &Level { + &self.level + } + + /// Returns the name of the span. + pub fn name(&self) -> &'static str { + self.name + } + + /// Returns a string describing the part of the system where the span or + /// event that this metadata describes occurred. + /// + /// Typically, this is the module path, but alternate targets may be set + /// when spans or events are constructed. + pub fn target(&self) -> &'a str { + self.target + } + + /// Returns the path to the Rust module where the span occurred, or + /// `None` if the module path is unknown. + pub fn module_path(&self) -> Option<&'a str> { + self.module_path + } + + /// Returns the name of the source code file where the span + /// occurred, or `None` if the file is unknown + pub fn file(&self) -> Option<&'a str> { + self.file + } + + /// Returns the line number in the source code file where the span + /// occurred, or `None` if the line number is unknown. + pub fn line(&self) -> Option<u32> { + self.line + } + + /// Returns an opaque `Identifier` that uniquely identifies the callsite + /// this `Metadata` originated from. + #[inline] + pub fn callsite(&self) -> callsite::Identifier { + self.fields.callsite() + } + + /// Returns true if the callsite kind is `Event`. + pub fn is_event(&self) -> bool { + self.kind.is_event() + } + + /// Return true if the callsite kind is `Span`. + pub fn is_span(&self) -> bool { + self.kind.is_span() + } +} + +impl<'a> fmt::Debug for Metadata<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut meta = f.debug_struct("Metadata"); + meta.field("name", &self.name) + .field("target", &self.target) + .field("level", &self.level); + + if let Some(path) = self.module_path() { + meta.field("module_path", &path); + } + + match (self.file(), self.line()) { + (Some(file), Some(line)) => { + meta.field("location", &format_args!("{}:{}", file, line)); + } + (Some(file), None) => { + meta.field("file", &format_args!("{}", file)); + } + + // Note: a line num with no file is a kind of weird case that _probably_ never occurs... + (None, Some(line)) => { + meta.field("line", &line); + } + (None, None) => {} + }; + + meta.field("fields", &format_args!("{}", self.fields)) + .field("callsite", &self.callsite()) + .field("kind", &self.kind) + .finish() + } +} + +impl Kind { + const EVENT_BIT: u8 = 1 << 0; + const SPAN_BIT: u8 = 1 << 1; + const HINT_BIT: u8 = 1 << 2; + + /// `Event` callsite + pub const EVENT: Kind = Kind(Self::EVENT_BIT); + + /// `Span` callsite + pub const SPAN: Kind = Kind(Self::SPAN_BIT); + + /// `enabled!` callsite. [`Subscriber`][`crate::subscriber::Subscriber`]s can assume + /// this `Kind` means they will never recieve a + /// full event with this [`Metadata`]. + pub const HINT: Kind = Kind(Self::HINT_BIT); + + /// Return true if the callsite kind is `Span` + pub fn is_span(&self) -> bool { + self.0 & Self::SPAN_BIT == Self::SPAN_BIT + } + + /// Return true if the callsite kind is `Event` + pub fn is_event(&self) -> bool { + self.0 & Self::EVENT_BIT == Self::EVENT_BIT + } + + /// Return true if the callsite kind is `Hint` + pub fn is_hint(&self) -> bool { + self.0 & Self::HINT_BIT == Self::HINT_BIT + } + + /// Sets that this `Kind` is a [hint](Self::HINT). + /// + /// This can be called on [`SPAN`](Self::SPAN) and [`EVENT`](Self::EVENT) + /// kinds to construct a hint callsite that also counts as a span or event. + pub const fn hint(self) -> Self { + Self(self.0 | Self::HINT_BIT) + } +} + +impl fmt::Debug for Kind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("Kind(")?; + let mut has_bits = false; + let mut write_bit = |name: &str| { + if has_bits { + f.write_str(" | ")?; + } + f.write_str(name)?; + has_bits = true; + Ok(()) + }; + + if self.is_event() { + write_bit("EVENT")?; + } + + if self.is_span() { + write_bit("SPAN")?; + } + + if self.is_hint() { + write_bit("HINT")?; + } + + // if none of the expected bits were set, something is messed up, so + // just print the bits for debugging purposes + if !has_bits { + write!(f, "{:#b}", self.0)?; + } + + f.write_str(")") + } +} + +// ===== impl Level ===== + +impl Level { + /// The "error" level. + /// + /// Designates very serious errors. + pub const ERROR: Level = Level(LevelInner::Error); + /// The "warn" level. + /// + /// Designates hazardous situations. + pub const WARN: Level = Level(LevelInner::Warn); + /// The "info" level. + /// + /// Designates useful information. + pub const INFO: Level = Level(LevelInner::Info); + /// The "debug" level. + /// + /// Designates lower priority information. + pub const DEBUG: Level = Level(LevelInner::Debug); + /// The "trace" level. + /// + /// Designates very low priority, often extremely verbose, information. + pub const TRACE: Level = Level(LevelInner::Trace); + + /// Returns the string representation of the `Level`. + /// + /// This returns the same string as the `fmt::Display` implementation. + pub fn as_str(&self) -> &'static str { + match *self { + Level::TRACE => "TRACE", + Level::DEBUG => "DEBUG", + Level::INFO => "INFO", + Level::WARN => "WARN", + Level::ERROR => "ERROR", + } + } +} + +impl fmt::Display for Level { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + Level::TRACE => f.pad("TRACE"), + Level::DEBUG => f.pad("DEBUG"), + Level::INFO => f.pad("INFO"), + Level::WARN => f.pad("WARN"), + Level::ERROR => f.pad("ERROR"), + } + } +} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl crate::stdlib::error::Error for ParseLevelError {} + +impl FromStr for Level { + type Err = ParseLevelError; + fn from_str(s: &str) -> Result<Self, ParseLevelError> { + s.parse::<usize>() + .map_err(|_| ParseLevelError { _p: () }) + .and_then(|num| match num { + 1 => Ok(Level::ERROR), + 2 => Ok(Level::WARN), + 3 => Ok(Level::INFO), + 4 => Ok(Level::DEBUG), + 5 => Ok(Level::TRACE), + _ => Err(ParseLevelError { _p: () }), + }) + .or_else(|_| match s { + s if s.eq_ignore_ascii_case("error") => Ok(Level::ERROR), + s if s.eq_ignore_ascii_case("warn") => Ok(Level::WARN), + s if s.eq_ignore_ascii_case("info") => Ok(Level::INFO), + s if s.eq_ignore_ascii_case("debug") => Ok(Level::DEBUG), + s if s.eq_ignore_ascii_case("trace") => Ok(Level::TRACE), + _ => Err(ParseLevelError { _p: () }), + }) + } +} + +#[repr(usize)] +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +enum LevelInner { + /// The "trace" level. + /// + /// Designates very low priority, often extremely verbose, information. + Trace = 0, + /// The "debug" level. + /// + /// Designates lower priority information. + Debug = 1, + /// The "info" level. + /// + /// Designates useful information. + Info = 2, + /// The "warn" level. + /// + /// Designates hazardous situations. + Warn = 3, + /// The "error" level. + /// + /// Designates very serious errors. + Error = 4, +} + +// === impl LevelFilter === + +impl From<Level> for LevelFilter { + #[inline] + fn from(level: Level) -> Self { + Self::from_level(level) + } +} + +impl From<Option<Level>> for LevelFilter { + #[inline] + fn from(level: Option<Level>) -> Self { + Self(level) + } +} + +impl From<LevelFilter> for Option<Level> { + #[inline] + fn from(filter: LevelFilter) -> Self { + filter.into_level() + } +} + +impl LevelFilter { + /// The "off" level. + /// + /// Designates that trace instrumentation should be completely disabled. + pub const OFF: LevelFilter = LevelFilter(None); + /// The "error" level. + /// + /// Designates very serious errors. + pub const ERROR: LevelFilter = LevelFilter::from_level(Level::ERROR); + /// The "warn" level. + /// + /// Designates hazardous situations. + pub const WARN: LevelFilter = LevelFilter::from_level(Level::WARN); + /// The "info" level. + /// + /// Designates useful information. + pub const INFO: LevelFilter = LevelFilter::from_level(Level::INFO); + /// The "debug" level. + /// + /// Designates lower priority information. + pub const DEBUG: LevelFilter = LevelFilter::from_level(Level::DEBUG); + /// The "trace" level. + /// + /// Designates very low priority, often extremely verbose, information. + pub const TRACE: LevelFilter = LevelFilter(Some(Level::TRACE)); + + /// Returns a `LevelFilter` that enables spans and events with verbosity up + /// to and including `level`. + pub const fn from_level(level: Level) -> Self { + Self(Some(level)) + } + + /// Returns the most verbose [`Level`] that this filter accepts, or `None` + /// if it is [`OFF`]. + /// + /// [`OFF`]: LevelFilter::OFF + pub const fn into_level(self) -> Option<Level> { + self.0 + } + + // These consts are necessary because `as` casts are not allowed as + // match patterns. + const ERROR_USIZE: usize = LevelInner::Error as usize; + const WARN_USIZE: usize = LevelInner::Warn as usize; + const INFO_USIZE: usize = LevelInner::Info as usize; + const DEBUG_USIZE: usize = LevelInner::Debug as usize; + const TRACE_USIZE: usize = LevelInner::Trace as usize; + // Using the value of the last variant + 1 ensures that we match the value + // for `Option::None` as selected by the niche optimization for + // `LevelFilter`. If this is the case, converting a `usize` value into a + // `LevelFilter` (in `LevelFilter::current`) will be an identity conversion, + // rather than generating a lookup table. + const OFF_USIZE: usize = LevelInner::Error as usize + 1; + + /// Returns a `LevelFilter` that matches the most verbose [`Level`] that any + /// currently active [`Subscriber`] will enable. + /// + /// User code should treat this as a *hint*. If a given span or event has a + /// level *higher* than the returned `LevelFilter`, it will not be enabled. + /// However, if the level is less than or equal to this value, the span or + /// event is *not* guaranteed to be enabled; the subscriber will still + /// filter each callsite individually. + /// + /// Therefore, comparing a given span or event's level to the returned + /// `LevelFilter` **can** be used for determining if something is + /// *disabled*, but **should not** be used for determining if something is + /// *enabled*. + /// + /// [`Level`]: super::Level + /// [`Subscriber`]: super::Subscriber + #[inline(always)] + pub fn current() -> Self { + match MAX_LEVEL.load(Ordering::Relaxed) { + Self::ERROR_USIZE => Self::ERROR, + Self::WARN_USIZE => Self::WARN, + Self::INFO_USIZE => Self::INFO, + Self::DEBUG_USIZE => Self::DEBUG, + Self::TRACE_USIZE => Self::TRACE, + Self::OFF_USIZE => Self::OFF, + #[cfg(debug_assertions)] + unknown => unreachable!( + "/!\\ `LevelFilter` representation seems to have changed! /!\\ \n\ + This is a bug (and it's pretty bad). Please contact the `tracing` \ + maintainers. Thank you and I'm sorry.\n \ + The offending repr was: {:?}", + unknown, + ), + #[cfg(not(debug_assertions))] + _ => unsafe { + // Using `unreachable_unchecked` here (rather than + // `unreachable!()`) is necessary to ensure that rustc generates + // an identity conversion from integer -> discriminant, rather + // than generating a lookup table. We want to ensure this + // function is a single `mov` instruction (on x86) if at all + // possible, because it is called *every* time a span/event + // callsite is hit; and it is (potentially) the only code in the + // hottest path for skipping a majority of callsites when level + // filtering is in use. + // + // safety: This branch is only truly unreachable if we guarantee + // that no values other than the possible enum discriminants + // will *ever* be present. The `AtomicUsize` is initialized to + // the `OFF` value. It is only set by the `set_max` function, + // which takes a `LevelFilter` as a parameter. This restricts + // the inputs to `set_max` to the set of valid discriminants. + // Therefore, **as long as `MAX_VALUE` is only ever set by + // `set_max`**, this is safe. + crate::stdlib::hint::unreachable_unchecked() + }, + } + } + + pub(crate) fn set_max(LevelFilter(level): LevelFilter) { + let val = match level { + Some(Level(level)) => level as usize, + None => Self::OFF_USIZE, + }; + + // using an AcqRel swap ensures an ordered relationship of writes to the + // max level. + MAX_LEVEL.swap(val, Ordering::AcqRel); + } +} + +impl fmt::Display for LevelFilter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + LevelFilter::OFF => f.pad("off"), + LevelFilter::ERROR => f.pad("error"), + LevelFilter::WARN => f.pad("warn"), + LevelFilter::INFO => f.pad("info"), + LevelFilter::DEBUG => f.pad("debug"), + LevelFilter::TRACE => f.pad("trace"), + } + } +} + +impl fmt::Debug for LevelFilter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + LevelFilter::OFF => f.pad("LevelFilter::OFF"), + LevelFilter::ERROR => f.pad("LevelFilter::ERROR"), + LevelFilter::WARN => f.pad("LevelFilter::WARN"), + LevelFilter::INFO => f.pad("LevelFilter::INFO"), + LevelFilter::DEBUG => f.pad("LevelFilter::DEBUG"), + LevelFilter::TRACE => f.pad("LevelFilter::TRACE"), + } + } +} + +impl FromStr for LevelFilter { + type Err = ParseLevelFilterError; + fn from_str(from: &str) -> Result<Self, Self::Err> { + from.parse::<usize>() + .ok() + .and_then(|num| match num { + 0 => Some(LevelFilter::OFF), + 1 => Some(LevelFilter::ERROR), + 2 => Some(LevelFilter::WARN), + 3 => Some(LevelFilter::INFO), + 4 => Some(LevelFilter::DEBUG), + 5 => Some(LevelFilter::TRACE), + _ => None, + }) + .or_else(|| match from { + "" => Some(LevelFilter::ERROR), + s if s.eq_ignore_ascii_case("error") => Some(LevelFilter::ERROR), + s if s.eq_ignore_ascii_case("warn") => Some(LevelFilter::WARN), + s if s.eq_ignore_ascii_case("info") => Some(LevelFilter::INFO), + s if s.eq_ignore_ascii_case("debug") => Some(LevelFilter::DEBUG), + s if s.eq_ignore_ascii_case("trace") => Some(LevelFilter::TRACE), + s if s.eq_ignore_ascii_case("off") => Some(LevelFilter::OFF), + _ => None, + }) + .ok_or(ParseLevelFilterError(())) + } +} + +/// Returned if parsing a `Level` fails. +#[derive(Debug)] +pub struct ParseLevelError { + _p: (), +} + +impl fmt::Display for ParseLevelError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad( + "error parsing level: expected one of \"error\", \"warn\", \ + \"info\", \"debug\", \"trace\", or a number 1-5", + ) + } +} + +impl fmt::Display for ParseLevelFilterError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad( + "error parsing level filter: expected one of \"off\", \"error\", \ + \"warn\", \"info\", \"debug\", \"trace\", or a number 0-5", + ) + } +} + +#[cfg(feature = "std")] +impl std::error::Error for ParseLevelFilterError {} + +// ==== Level and LevelFilter comparisons ==== + +// /!\ BIG, IMPORTANT WARNING /!\ +// Do NOT mess with these implementations! They are hand-written for a reason! +// +// Since comparing `Level`s and `LevelFilter`s happens in a *very* hot path +// (potentially, every time a span or event macro is hit, regardless of whether +// or not is enabled), we *need* to ensure that these comparisons are as fast as +// possible. Therefore, we have some requirements: +// +// 1. We want to do our best to ensure that rustc will generate integer-integer +// comparisons wherever possible. +// +// The derived `Ord`/`PartialOrd` impls for `LevelFilter` will not do this, +// because `LevelFilter`s are represented by `Option<Level>`, rather than as +// a separate `#[repr(usize)]` enum. This was (unfortunately) necessary for +// backwards-compatibility reasons, as the `tracing` crate's original +// version of `LevelFilter` defined `const fn` conversions between `Level`s +// and `LevelFilter`, so we're stuck with the `Option<Level>` repr. +// Therefore, we need hand-written `PartialOrd` impls that cast both sides of +// the comparison to `usize`s, to force the compiler to generate integer +// compares. +// +// 2. The hottest `Level`/`LevelFilter` comparison, the one that happens every +// time a callsite is hit, occurs *within the `tracing` crate's macros*. +// This means that the comparison is happening *inside* a crate that +// *depends* on `tracing-core`, not in `tracing-core` itself. The compiler +// will only inline function calls across crate boundaries if the called +// function is annotated with an `#[inline]` attribute, and we *definitely* +// want the comparison functions to be inlined: as previously mentioned, they +// should compile down to a single integer comparison on release builds, and +// it seems really sad to push an entire stack frame to call a function +// consisting of one `cmp` instruction! +// +// Therefore, we need to ensure that all the comparison methods have +// `#[inline]` or `#[inline(always)]` attributes. It's not sufficient to just +// add the attribute to `partial_cmp` in a manual implementation of the +// trait, since it's the comparison operators (`lt`, `le`, `gt`, and `ge`) +// that will actually be *used*, and the default implementation of *those* +// methods, which calls `partial_cmp`, does not have an inline annotation. +// +// 3. We need the comparisons to be inverted. The discriminants for the +// `LevelInner` enum are assigned in "backwards" order, with `TRACE` having +// the *lowest* value. However, we want `TRACE` to compare greater-than all +// other levels. +// +// Why are the numeric values inverted? In order to ensure that `LevelFilter` +// (which, as previously mentioned, *has* to be internally represented by an +// `Option<Level>`) compiles down to a single integer value. This is +// necessary for storing the global max in an `AtomicUsize`, and for ensuring +// that we use fast integer-integer comparisons, as mentioned previously. In +// order to ensure this, we exploit the niche optimization. The niche +// optimization for `Option<{enum with a numeric repr}>` will choose +// `(HIGHEST_DISCRIMINANT_VALUE + 1)` as the representation for `None`. +// Therefore, the integer representation of `LevelFilter::OFF` (which is +// `None`) will be the number 5. `OFF` must compare higher than every other +// level in order for it to filter as expected. Since we want to use a single +// `cmp` instruction, we can't special-case the integer value of `OFF` to +// compare higher, as that will generate more code. Instead, we need it to be +// on one end of the enum, with `ERROR` on the opposite end, so we assign the +// value 0 to `ERROR`. +// +// This *does* mean that when parsing `LevelFilter`s or `Level`s from +// `String`s, the integer values are inverted, but that doesn't happen in a +// hot path. +// +// Note that we manually invert the comparisons by swapping the left-hand and +// right-hand side. Using `Ordering::reverse` generates significantly worse +// code (per Matt Godbolt's Compiler Explorer). +// +// Anyway, that's a brief history of why this code is the way it is. Don't +// change it unless you know what you're doing. + +impl PartialEq<LevelFilter> for Level { + #[inline(always)] + fn eq(&self, other: &LevelFilter) -> bool { + self.0 as usize == filter_as_usize(&other.0) + } +} + +impl PartialOrd for Level { + #[inline(always)] + fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> { + Some(self.cmp(other)) + } + + #[inline(always)] + fn lt(&self, other: &Level) -> bool { + (other.0 as usize) < (self.0 as usize) + } + + #[inline(always)] + fn le(&self, other: &Level) -> bool { + (other.0 as usize) <= (self.0 as usize) + } + + #[inline(always)] + fn gt(&self, other: &Level) -> bool { + (other.0 as usize) > (self.0 as usize) + } + + #[inline(always)] + fn ge(&self, other: &Level) -> bool { + (other.0 as usize) >= (self.0 as usize) + } +} + +impl Ord for Level { + #[inline(always)] + fn cmp(&self, other: &Self) -> cmp::Ordering { + (other.0 as usize).cmp(&(self.0 as usize)) + } +} + +impl PartialOrd<LevelFilter> for Level { + #[inline(always)] + fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> { + Some(filter_as_usize(&other.0).cmp(&(self.0 as usize))) + } + + #[inline(always)] + fn lt(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) < (self.0 as usize) + } + + #[inline(always)] + fn le(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) <= (self.0 as usize) + } + + #[inline(always)] + fn gt(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) > (self.0 as usize) + } + + #[inline(always)] + fn ge(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) >= (self.0 as usize) + } +} + +#[inline(always)] +fn filter_as_usize(x: &Option<Level>) -> usize { + match x { + Some(Level(f)) => *f as usize, + None => LevelFilter::OFF_USIZE, + } +} + +impl PartialEq<Level> for LevelFilter { + #[inline(always)] + fn eq(&self, other: &Level) -> bool { + filter_as_usize(&self.0) == other.0 as usize + } +} + +impl PartialOrd for LevelFilter { + #[inline(always)] + fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> { + Some(self.cmp(other)) + } + + #[inline(always)] + fn lt(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) < filter_as_usize(&self.0) + } + + #[inline(always)] + fn le(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) <= filter_as_usize(&self.0) + } + + #[inline(always)] + fn gt(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) > filter_as_usize(&self.0) + } + + #[inline(always)] + fn ge(&self, other: &LevelFilter) -> bool { + filter_as_usize(&other.0) >= filter_as_usize(&self.0) + } +} + +impl Ord for LevelFilter { + #[inline(always)] + fn cmp(&self, other: &Self) -> cmp::Ordering { + filter_as_usize(&other.0).cmp(&filter_as_usize(&self.0)) + } +} + +impl PartialOrd<Level> for LevelFilter { + #[inline(always)] + fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> { + Some((other.0 as usize).cmp(&filter_as_usize(&self.0))) + } + + #[inline(always)] + fn lt(&self, other: &Level) -> bool { + (other.0 as usize) < filter_as_usize(&self.0) + } + + #[inline(always)] + fn le(&self, other: &Level) -> bool { + (other.0 as usize) <= filter_as_usize(&self.0) + } + + #[inline(always)] + fn gt(&self, other: &Level) -> bool { + (other.0 as usize) > filter_as_usize(&self.0) + } + + #[inline(always)] + fn ge(&self, other: &Level) -> bool { + (other.0 as usize) >= filter_as_usize(&self.0) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::stdlib::mem; + + #[test] + fn level_from_str() { + assert_eq!("error".parse::<Level>().unwrap(), Level::ERROR); + assert_eq!("4".parse::<Level>().unwrap(), Level::DEBUG); + assert!("0".parse::<Level>().is_err()) + } + + #[test] + fn filter_level_conversion() { + let mapping = [ + (LevelFilter::OFF, None), + (LevelFilter::ERROR, Some(Level::ERROR)), + (LevelFilter::WARN, Some(Level::WARN)), + (LevelFilter::INFO, Some(Level::INFO)), + (LevelFilter::DEBUG, Some(Level::DEBUG)), + (LevelFilter::TRACE, Some(Level::TRACE)), + ]; + for (filter, level) in mapping.iter() { + assert_eq!(filter.into_level(), *level); + match level { + Some(level) => { + let actual: LevelFilter = (*level).into(); + assert_eq!(actual, *filter); + } + None => { + let actual: LevelFilter = None.into(); + assert_eq!(actual, *filter); + } + } + } + } + + #[test] + fn level_filter_is_usize_sized() { + assert_eq!( + mem::size_of::<LevelFilter>(), + mem::size_of::<usize>(), + "`LevelFilter` is no longer `usize`-sized! global MAX_LEVEL may now be invalid!" + ) + } + + #[test] + fn level_filter_reprs() { + let mapping = [ + (LevelFilter::OFF, LevelInner::Error as usize + 1), + (LevelFilter::ERROR, LevelInner::Error as usize), + (LevelFilter::WARN, LevelInner::Warn as usize), + (LevelFilter::INFO, LevelInner::Info as usize), + (LevelFilter::DEBUG, LevelInner::Debug as usize), + (LevelFilter::TRACE, LevelInner::Trace as usize), + ]; + for &(filter, expected) in &mapping { + let repr = unsafe { + // safety: The entire purpose of this test is to assert that the + // actual repr matches what we expect it to be --- we're testing + // that *other* unsafe code is sound using the transmuted value. + // We're not going to do anything with it that might be unsound. + mem::transmute::<LevelFilter, usize>(filter) + }; + assert_eq!(expected, repr, "repr changed for {:?}", filter) + } + } +} diff --git a/vendor/tracing-core/src/parent.rs b/vendor/tracing-core/src/parent.rs new file mode 100644 index 000000000..cb34b376c --- /dev/null +++ b/vendor/tracing-core/src/parent.rs @@ -0,0 +1,11 @@ +use crate::span::Id; + +#[derive(Debug)] +pub(crate) enum Parent { + /// The new span will be a root span. + Root, + /// The new span will be rooted in the current span. + Current, + /// The new span has an explicitly-specified parent. + Explicit(Id), +} diff --git a/vendor/tracing-core/src/span.rs b/vendor/tracing-core/src/span.rs new file mode 100644 index 000000000..44738b290 --- /dev/null +++ b/vendor/tracing-core/src/span.rs @@ -0,0 +1,341 @@ +//! Spans represent periods of time in the execution of a program. +use crate::field::FieldSet; +use crate::parent::Parent; +use crate::stdlib::num::NonZeroU64; +use crate::{field, Metadata}; + +/// Identifies a span within the context of a subscriber. +/// +/// They are generated by [`Subscriber`]s for each span as it is created, by +/// the [`new_span`] trait method. See the documentation for that method for +/// more information on span ID generation. +/// +/// [`Subscriber`]: super::subscriber::Subscriber +/// [`new_span`]: super::subscriber::Subscriber::new_span +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct Id(NonZeroU64); + +/// Attributes provided to a `Subscriber` describing a new span when it is +/// created. +#[derive(Debug)] +pub struct Attributes<'a> { + metadata: &'static Metadata<'static>, + values: &'a field::ValueSet<'a>, + parent: Parent, +} + +/// A set of fields recorded by a span. +#[derive(Debug)] +pub struct Record<'a> { + values: &'a field::ValueSet<'a>, +} + +/// Indicates what [the `Subscriber` considers] the "current" span. +/// +/// As subscribers may not track a notion of a current span, this has three +/// possible states: +/// - "unknown", indicating that the subscriber does not track a current span, +/// - "none", indicating that the current context is known to not be in a span, +/// - "some", with the current span's [`Id`] and [`Metadata`]. +/// +/// [the `Subscriber` considers]: super::subscriber::Subscriber::current_span +/// [`Metadata`]: super::metadata::Metadata +#[derive(Debug)] +pub struct Current { + inner: CurrentInner, +} + +#[derive(Debug)] +enum CurrentInner { + Current { + id: Id, + metadata: &'static Metadata<'static>, + }, + None, + Unknown, +} + +// ===== impl Span ===== + +impl Id { + /// Constructs a new span ID from the given `u64`. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: Span IDs must be greater than zero. + /// </pre> + /// + /// # Panics + /// - If the provided `u64` is 0. + pub fn from_u64(u: u64) -> Self { + Id(NonZeroU64::new(u).expect("span IDs must be > 0")) + } + + /// Constructs a new span ID from the given `NonZeroU64`. + /// + /// Unlike [`Id::from_u64`](Id::from_u64()), this will never panic. + #[inline] + pub const fn from_non_zero_u64(id: NonZeroU64) -> Self { + Id(id) + } + + // Allow `into` by-ref since we don't want to impl Copy for Id + #[allow(clippy::wrong_self_convention)] + /// Returns the span's ID as a `u64`. + pub fn into_u64(&self) -> u64 { + self.0.get() + } + + // Allow `into` by-ref since we don't want to impl Copy for Id + #[allow(clippy::wrong_self_convention)] + /// Returns the span's ID as a `NonZeroU64`. + #[inline] + pub const fn into_non_zero_u64(&self) -> NonZeroU64 { + self.0 + } +} + +impl<'a> From<&'a Id> for Option<Id> { + fn from(id: &'a Id) -> Self { + Some(id.clone()) + } +} + +// ===== impl Attributes ===== + +impl<'a> Attributes<'a> { + /// Returns `Attributes` describing a new child span of the current span, + /// with the provided metadata and values. + pub fn new(metadata: &'static Metadata<'static>, values: &'a field::ValueSet<'a>) -> Self { + Attributes { + metadata, + values, + parent: Parent::Current, + } + } + + /// Returns `Attributes` describing a new span at the root of its own trace + /// tree, with the provided metadata and values. + pub fn new_root(metadata: &'static Metadata<'static>, values: &'a field::ValueSet<'a>) -> Self { + Attributes { + metadata, + values, + parent: Parent::Root, + } + } + + /// Returns `Attributes` describing a new child span of the specified + /// parent span, with the provided metadata and values. + pub fn child_of( + parent: Id, + metadata: &'static Metadata<'static>, + values: &'a field::ValueSet<'a>, + ) -> Self { + Attributes { + metadata, + values, + parent: Parent::Explicit(parent), + } + } + + /// Returns a reference to the new span's metadata. + pub fn metadata(&self) -> &'static Metadata<'static> { + self.metadata + } + + /// Returns a reference to a `ValueSet` containing any values the new span + /// was created with. + pub fn values(&self) -> &field::ValueSet<'a> { + self.values + } + + /// Returns true if the new span should be a root. + pub fn is_root(&self) -> bool { + matches!(self.parent, Parent::Root) + } + + /// Returns true if the new span's parent should be determined based on the + /// current context. + /// + /// If this is true and the current thread is currently inside a span, then + /// that span should be the new span's parent. Otherwise, if the current + /// thread is _not_ inside a span, then the new span will be the root of its + /// own trace tree. + pub fn is_contextual(&self) -> bool { + matches!(self.parent, Parent::Current) + } + + /// Returns the new span's explicitly-specified parent, if there is one. + /// + /// Otherwise (if the new span is a root or is a child of the current span), + /// returns `None`. + pub fn parent(&self) -> Option<&Id> { + match self.parent { + Parent::Explicit(ref p) => Some(p), + _ => None, + } + } + + /// Records all the fields in this set of `Attributes` with the provided + /// [Visitor]. + /// + /// [visitor]: super::field::Visit + pub fn record(&self, visitor: &mut dyn field::Visit) { + self.values.record(visitor) + } + + /// Returns `true` if this set of `Attributes` contains a value for the + /// given `Field`. + pub fn contains(&self, field: &field::Field) -> bool { + self.values.contains(field) + } + + /// Returns true if this set of `Attributes` contains _no_ values. + pub fn is_empty(&self) -> bool { + self.values.is_empty() + } + + /// Returns the set of all [fields] defined by this span's [`Metadata`]. + /// + /// Note that the [`FieldSet`] returned by this method includes *all* the + /// fields declared by this span, not just those with values that are recorded + /// as part of this set of `Attributes`. Other fields with values not present in + /// this `Attributes`' value set may [record] values later. + /// + /// [fields]: crate::field + /// [record]: Attributes::record() + /// [`Metadata`]: crate::metadata::Metadata + /// [`FieldSet`]: crate::field::FieldSet + pub fn fields(&self) -> &FieldSet { + self.values.field_set() + } +} + +// ===== impl Record ===== + +impl<'a> Record<'a> { + /// Constructs a new `Record` from a `ValueSet`. + pub fn new(values: &'a field::ValueSet<'a>) -> Self { + Self { values } + } + + /// Records all the fields in this `Record` with the provided [Visitor]. + /// + /// [visitor]: super::field::Visit + pub fn record(&self, visitor: &mut dyn field::Visit) { + self.values.record(visitor) + } + + /// Returns the number of fields that would be visited from this `Record` + /// when [`Record::record()`] is called + /// + /// [`Record::record()`]: Record::record() + pub fn len(&self) -> usize { + self.values.len() + } + + /// Returns `true` if this `Record` contains a value for the given `Field`. + pub fn contains(&self, field: &field::Field) -> bool { + self.values.contains(field) + } + + /// Returns true if this `Record` contains _no_ values. + pub fn is_empty(&self) -> bool { + self.values.is_empty() + } +} + +// ===== impl Current ===== + +impl Current { + /// Constructs a new `Current` that indicates the current context is a span + /// with the given `metadata` and `metadata`. + pub fn new(id: Id, metadata: &'static Metadata<'static>) -> Self { + Self { + inner: CurrentInner::Current { id, metadata }, + } + } + + /// Constructs a new `Current` that indicates the current context is *not* + /// in a span. + pub fn none() -> Self { + Self { + inner: CurrentInner::None, + } + } + + /// Constructs a new `Current` that indicates the `Subscriber` does not + /// track a current span. + pub(crate) fn unknown() -> Self { + Self { + inner: CurrentInner::Unknown, + } + } + + /// Returns `true` if the `Subscriber` that constructed this `Current` tracks a + /// current span. + /// + /// If this returns `true` and [`id`], [`metadata`], or [`into_inner`] + /// return `None`, that indicates that we are currently known to *not* be + /// inside a span. If this returns `false`, those methods will also return + /// `None`, but in this case, that is because the subscriber does not keep + /// track of the currently-entered span. + /// + /// [`id`]: Current::id() + /// [`metadata`]: Current::metadata() + /// [`into_inner`]: Current::into_inner() + pub fn is_known(&self) -> bool { + !matches!(self.inner, CurrentInner::Unknown) + } + + /// Consumes `self` and returns the span `Id` and `Metadata` of the current + /// span, if one exists and is known. + pub fn into_inner(self) -> Option<(Id, &'static Metadata<'static>)> { + match self.inner { + CurrentInner::Current { id, metadata } => Some((id, metadata)), + _ => None, + } + } + + /// Borrows the `Id` of the current span, if one exists and is known. + pub fn id(&self) -> Option<&Id> { + match self.inner { + CurrentInner::Current { ref id, .. } => Some(id), + _ => None, + } + } + + /// Borrows the `Metadata` of the current span, if one exists and is known. + pub fn metadata(&self) -> Option<&'static Metadata<'static>> { + match self.inner { + CurrentInner::Current { metadata, .. } => Some(metadata), + _ => None, + } + } +} + +impl<'a> From<&'a Current> for Option<&'a Id> { + fn from(cur: &'a Current) -> Self { + cur.id() + } +} + +impl<'a> From<&'a Current> for Option<Id> { + fn from(cur: &'a Current) -> Self { + cur.id().cloned() + } +} + +impl From<Current> for Option<Id> { + fn from(cur: Current) -> Self { + match cur.inner { + CurrentInner::Current { id, .. } => Some(id), + _ => None, + } + } +} + +impl<'a> From<&'a Current> for Option<&'static Metadata<'static>> { + fn from(cur: &'a Current) -> Self { + cur.metadata() + } +} diff --git a/vendor/tracing-core/src/spin/LICENSE b/vendor/tracing-core/src/spin/LICENSE new file mode 100644 index 000000000..84d5f4d7a --- /dev/null +++ b/vendor/tracing-core/src/spin/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Mathijs van de Nes + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/tracing-core/src/spin/mod.rs b/vendor/tracing-core/src/spin/mod.rs new file mode 100644 index 000000000..148b192b3 --- /dev/null +++ b/vendor/tracing-core/src/spin/mod.rs @@ -0,0 +1,7 @@ +//! Synchronization primitives based on spinning + +pub(crate) use mutex::*; +pub(crate) use once::Once; + +mod mutex; +mod once; diff --git a/vendor/tracing-core/src/spin/mutex.rs b/vendor/tracing-core/src/spin/mutex.rs new file mode 100644 index 000000000..c261a6191 --- /dev/null +++ b/vendor/tracing-core/src/spin/mutex.rs @@ -0,0 +1,118 @@ +use core::cell::UnsafeCell; +use core::default::Default; +use core::fmt; +use core::hint; +use core::marker::Sync; +use core::ops::{Deref, DerefMut, Drop}; +use core::option::Option::{self, None, Some}; +use core::sync::atomic::{AtomicBool, Ordering}; + +/// This type provides MUTual EXclusion based on spinning. +pub(crate) struct Mutex<T: ?Sized> { + lock: AtomicBool, + data: UnsafeCell<T>, +} + +/// A guard to which the protected data can be accessed +/// +/// When the guard falls out of scope it will release the lock. +#[derive(Debug)] +pub(crate) struct MutexGuard<'a, T: ?Sized> { + lock: &'a AtomicBool, + data: &'a mut T, +} + +// Same unsafe impls as `std::sync::Mutex` +unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {} +unsafe impl<T: ?Sized + Send> Send for Mutex<T> {} + +impl<T> Mutex<T> { + /// Creates a new spinlock wrapping the supplied data. + pub(crate) const fn new(user_data: T) -> Mutex<T> { + Mutex { + lock: AtomicBool::new(false), + data: UnsafeCell::new(user_data), + } + } +} + +impl<T: ?Sized> Mutex<T> { + fn obtain_lock(&self) { + while self + .lock + .compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed) + .is_err() + { + // Wait until the lock looks unlocked before retrying + while self.lock.load(Ordering::Relaxed) { + hint::spin_loop(); + } + } + } + + /// Locks the spinlock and returns a guard. + /// + /// The returned value may be dereferenced for data access + /// and the lock will be dropped when the guard falls out of scope. + pub(crate) fn lock(&self) -> MutexGuard<'_, T> { + self.obtain_lock(); + MutexGuard { + lock: &self.lock, + data: unsafe { &mut *self.data.get() }, + } + } + + /// Tries to lock the mutex. If it is already locked, it will return None. Otherwise it returns + /// a guard within Some. + pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { + if self + .lock + .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) + .is_ok() + { + Some(MutexGuard { + lock: &self.lock, + data: unsafe { &mut *self.data.get() }, + }) + } else { + None + } + } +} + +impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.try_lock() { + Some(guard) => write!(f, "Mutex {{ data: ") + .and_then(|()| (&*guard).fmt(f)) + .and_then(|()| write!(f, "}}")), + None => write!(f, "Mutex {{ <locked> }}"), + } + } +} + +impl<T: ?Sized + Default> Default for Mutex<T> { + fn default() -> Mutex<T> { + Mutex::new(Default::default()) + } +} + +impl<'a, T: ?Sized> Deref for MutexGuard<'a, T> { + type Target = T; + fn deref<'b>(&'b self) -> &'b T { + &*self.data + } +} + +impl<'a, T: ?Sized> DerefMut for MutexGuard<'a, T> { + fn deref_mut<'b>(&'b mut self) -> &'b mut T { + &mut *self.data + } +} + +impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> { + /// The dropping of the MutexGuard will release the lock it was created from. + fn drop(&mut self) { + self.lock.store(false, Ordering::Release); + } +} diff --git a/vendor/tracing-core/src/spin/once.rs b/vendor/tracing-core/src/spin/once.rs new file mode 100644 index 000000000..27c99e56e --- /dev/null +++ b/vendor/tracing-core/src/spin/once.rs @@ -0,0 +1,158 @@ +use core::cell::UnsafeCell; +use core::fmt; +use core::hint::spin_loop; +use core::sync::atomic::{AtomicUsize, Ordering}; + +/// A synchronization primitive which can be used to run a one-time global +/// initialization. Unlike its std equivalent, this is generalized so that the +/// closure returns a value and it is stored. Once therefore acts something like +/// a future, too. +pub struct Once<T> { + state: AtomicUsize, + data: UnsafeCell<Option<T>>, // TODO remove option and use mem::uninitialized +} + +impl<T: fmt::Debug> fmt::Debug for Once<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.r#try() { + Some(s) => write!(f, "Once {{ data: ") + .and_then(|()| s.fmt(f)) + .and_then(|()| write!(f, "}}")), + None => write!(f, "Once {{ <uninitialized> }}"), + } + } +} + +// Same unsafe impls as `std::sync::RwLock`, because this also allows for +// concurrent reads. +unsafe impl<T: Send + Sync> Sync for Once<T> {} +unsafe impl<T: Send> Send for Once<T> {} + +// Four states that a Once can be in, encoded into the lower bits of `state` in +// the Once structure. +const INCOMPLETE: usize = 0x0; +const RUNNING: usize = 0x1; +const COMPLETE: usize = 0x2; +const PANICKED: usize = 0x3; + +use core::hint::unreachable_unchecked as unreachable; + +impl<T> Once<T> { + /// Initialization constant of `Once`. + pub const INIT: Self = Once { + state: AtomicUsize::new(INCOMPLETE), + data: UnsafeCell::new(None), + }; + + /// Creates a new `Once` value. + pub const fn new() -> Once<T> { + Self::INIT + } + + fn force_get<'a>(&'a self) -> &'a T { + match unsafe { &*self.data.get() }.as_ref() { + None => unsafe { unreachable() }, + Some(p) => p, + } + } + + /// Performs an initialization routine once and only once. The given closure + /// will be executed if this is the first time `call_once` has been called, + /// and otherwise the routine will *not* be invoked. + /// + /// This method will block the calling thread if another initialization + /// routine is currently running. + /// + /// When this function returns, it is guaranteed that some initialization + /// has run and completed (it may not be the closure specified). The + /// returned pointer will point to the result from the closure that was + /// run. + pub fn call_once<'a, F>(&'a self, builder: F) -> &'a T + where + F: FnOnce() -> T, + { + let mut status = self.state.load(Ordering::SeqCst); + + if status == INCOMPLETE { + status = match self.state.compare_exchange( + INCOMPLETE, + RUNNING, + Ordering::SeqCst, + Ordering::SeqCst, + ) { + Ok(status) => { + debug_assert_eq!( + status, INCOMPLETE, + "if compare_exchange succeeded, previous status must be incomplete", + ); + // We init + // We use a guard (Finish) to catch panics caused by builder + let mut finish = Finish { + state: &self.state, + panicked: true, + }; + unsafe { *self.data.get() = Some(builder()) }; + finish.panicked = false; + + self.state.store(COMPLETE, Ordering::SeqCst); + + // This next line is strictly an optimization + return self.force_get(); + } + Err(status) => status, + } + } + + loop { + match status { + INCOMPLETE => unreachable!(), + RUNNING => { + // We spin + spin_loop(); + status = self.state.load(Ordering::SeqCst) + } + PANICKED => panic!("Once has panicked"), + COMPLETE => return self.force_get(), + _ => unsafe { unreachable() }, + } + } + } + + /// Returns a pointer iff the `Once` was previously initialized + pub fn r#try<'a>(&'a self) -> Option<&'a T> { + match self.state.load(Ordering::SeqCst) { + COMPLETE => Some(self.force_get()), + _ => None, + } + } + + /// Like try, but will spin if the `Once` is in the process of being + /// initialized + pub fn wait<'a>(&'a self) -> Option<&'a T> { + loop { + match self.state.load(Ordering::SeqCst) { + INCOMPLETE => return None, + + RUNNING => { + spin_loop() // We spin + } + COMPLETE => return Some(self.force_get()), + PANICKED => panic!("Once has panicked"), + _ => unsafe { unreachable() }, + } + } + } +} + +struct Finish<'a> { + state: &'a AtomicUsize, + panicked: bool, +} + +impl<'a> Drop for Finish<'a> { + fn drop(&mut self) { + if self.panicked { + self.state.store(PANICKED, Ordering::SeqCst); + } + } +} diff --git a/vendor/tracing-core/src/stdlib.rs b/vendor/tracing-core/src/stdlib.rs new file mode 100644 index 000000000..741549519 --- /dev/null +++ b/vendor/tracing-core/src/stdlib.rs @@ -0,0 +1,78 @@ +//! Re-exports either the Rust `std` library or `core` and `alloc` when `std` is +//! disabled. +//! +//! `crate::stdlib::...` should be used rather than `std::` when adding code that +//! will be available with the standard library disabled. +//! +//! Note that this module is called `stdlib` rather than `std`, as Rust 1.34.0 +//! does not permit redefining the name `stdlib` (although this works on the +//! latest stable Rust). +#[cfg(feature = "std")] +pub(crate) use std::*; + +#[cfg(not(feature = "std"))] +pub(crate) use self::no_std::*; + +#[cfg(not(feature = "std"))] +mod no_std { + // We pre-emptively export everything from libcore/liballoc, (even modules + // we aren't using currently) to make adding new code easier. Therefore, + // some of these imports will be unused. + #![allow(unused_imports)] + + pub(crate) use core::{ + any, array, ascii, cell, char, clone, cmp, convert, default, f32, f64, ffi, future, hash, + hint, i128, i16, i8, isize, iter, marker, mem, num, ops, option, pin, ptr, result, task, + time, u128, u16, u32, u8, usize, + }; + + pub(crate) use alloc::{boxed, collections, rc, string, vec}; + + pub(crate) mod borrow { + pub(crate) use alloc::borrow::*; + pub(crate) use core::borrow::*; + } + + pub(crate) mod fmt { + pub(crate) use alloc::fmt::*; + pub(crate) use core::fmt::*; + } + + pub(crate) mod slice { + pub(crate) use alloc::slice::*; + pub(crate) use core::slice::*; + } + + pub(crate) mod str { + pub(crate) use alloc::str::*; + pub(crate) use core::str::*; + } + + pub(crate) mod sync { + pub(crate) use crate::spin::MutexGuard; + pub(crate) use alloc::sync::*; + pub(crate) use core::sync::*; + + /// This wraps `spin::Mutex` to return a `Result`, so that it can be + /// used with code written against `std::sync::Mutex`. + /// + /// Since `spin::Mutex` doesn't support poisoning, the `Result` returned + /// by `lock` will always be `Ok`. + #[derive(Debug, Default)] + pub(crate) struct Mutex<T> { + inner: crate::spin::Mutex<T>, + } + + impl<T> Mutex<T> { + // pub(crate) fn new(data: T) -> Self { + // Self { + // inner: crate::spin::Mutex::new(data), + // } + // } + + pub(crate) fn lock(&self) -> Result<MutexGuard<'_, T>, ()> { + Ok(self.inner.lock()) + } + } + } +} diff --git a/vendor/tracing-core/src/subscriber.rs b/vendor/tracing-core/src/subscriber.rs new file mode 100644 index 000000000..a6f0834e2 --- /dev/null +++ b/vendor/tracing-core/src/subscriber.rs @@ -0,0 +1,849 @@ +//! Subscribers collect and record trace data. +use crate::{span, Event, LevelFilter, Metadata}; + +use crate::stdlib::{ + any::{Any, TypeId}, + boxed::Box, + sync::Arc, +}; + +/// Trait representing the functions required to collect trace data. +/// +/// Crates that provide implementations of methods for collecting or recording +/// trace data should implement the `Subscriber` interface. This trait is +/// intended to represent fundamental primitives for collecting trace events and +/// spans — other libraries may offer utility functions and types to make +/// subscriber implementations more modular or improve the ergonomics of writing +/// subscribers. +/// +/// A subscriber is responsible for the following: +/// - Registering new spans as they are created, and providing them with span +/// IDs. Implicitly, this means the subscriber may determine the strategy for +/// determining span equality. +/// - Recording the attachment of field values and follows-from annotations to +/// spans. +/// - Filtering spans and events, and determining when those filters must be +/// invalidated. +/// - Observing spans as they are entered, exited, and closed, and events as +/// they occur. +/// +/// When a span is entered or exited, the subscriber is provided only with the +/// [ID] with which it tagged that span when it was created. This means +/// that it is up to the subscriber to determine whether and how span _data_ — +/// the fields and metadata describing the span — should be stored. The +/// [`new_span`] function is called when a new span is created, and at that +/// point, the subscriber _may_ choose to store the associated data if it will +/// be referenced again. However, if the data has already been recorded and will +/// not be needed by the implementations of `enter` and `exit`, the subscriber +/// may freely discard that data without allocating space to store it. +/// +/// ## Overriding default impls +/// +/// Some trait methods on `Subscriber` have default implementations, either in +/// order to reduce the surface area of implementing `Subscriber`, or for +/// backward-compatibility reasons. However, many subscribers will likely want +/// to override these default implementations. +/// +/// The following methods are likely of interest: +/// +/// - [`register_callsite`] is called once for each callsite from which a span +/// event may originate, and returns an [`Interest`] value describing whether or +/// not the subscriber wishes to see events or spans from that callsite. By +/// default, it calls [`enabled`], and returns `Interest::always()` if +/// `enabled` returns true, or `Interest::never()` if enabled returns false. +/// However, if the subscriber's interest can change dynamically at runtime, +/// it may want to override this function to return `Interest::sometimes()`. +/// Additionally, subscribers which wish to perform a behaviour once for each +/// callsite, such as allocating storage for data related to that callsite, +/// can perform it in `register_callsite`. +/// +/// See also the [documentation on the callsite registry][cs-reg] for details +/// on [`register_callsite`]. +/// +/// - [`event_enabled`] is called once before every call to the [`event`] +/// method. This can be used to implement filtering on events once their field +/// values are known, but before any processing is done in the `event` method. +/// - [`clone_span`] is called every time a span ID is cloned, and [`try_close`] +/// is called when a span ID is dropped. By default, these functions do +/// nothing. However, they can be used to implement reference counting for +/// spans, allowing subscribers to free storage for span data and to determine +/// when a span has _closed_ permanently (rather than being exited). +/// Subscribers which store per-span data or which need to track span closures +/// should override these functions together. +/// +/// [ID]: super::span::Id +/// [`new_span`]: Subscriber::new_span +/// [`register_callsite`]: Subscriber::register_callsite +/// [`enabled`]: Subscriber::enabled +/// [`clone_span`]: Subscriber::clone_span +/// [`try_close`]: Subscriber::try_close +/// [cs-reg]: crate::callsite#registering-callsites +/// [`event`]: Subscriber::event +/// [`event_enabled`]: Subscriber::event_enabled +pub trait Subscriber: 'static { + // === Span registry methods ============================================== + + /// Registers a new [callsite] with this subscriber, returning whether or not + /// the subscriber is interested in being notified about the callsite. + /// + /// By default, this function assumes that the subscriber's [filter] + /// represents an unchanging view of its interest in the callsite. However, + /// if this is not the case, subscribers may override this function to + /// indicate different interests, or to implement behaviour that should run + /// once for every callsite. + /// + /// This function is guaranteed to be called at least once per callsite on + /// every active subscriber. The subscriber may store the keys to fields it + /// cares about in order to reduce the cost of accessing fields by name, + /// preallocate storage for that callsite, or perform any other actions it + /// wishes to perform once for each callsite. + /// + /// The subscriber should then return an [`Interest`], indicating + /// whether it is interested in being notified about that callsite in the + /// future. This may be `Always` indicating that the subscriber always + /// wishes to be notified about the callsite, and its filter need not be + /// re-evaluated; `Sometimes`, indicating that the subscriber may sometimes + /// care about the callsite but not always (such as when sampling), or + /// `Never`, indicating that the subscriber never wishes to be notified about + /// that callsite. If all active subscribers return `Never`, a callsite will + /// never be enabled unless a new subscriber expresses interest in it. + /// + /// `Subscriber`s which require their filters to be run every time an event + /// occurs or a span is entered/exited should return `Interest::sometimes`. + /// If a subscriber returns `Interest::sometimes`, then its [`enabled`] method + /// will be called every time an event or span is created from that callsite. + /// + /// For example, suppose a sampling subscriber is implemented by + /// incrementing a counter every time `enabled` is called and only returning + /// `true` when the counter is divisible by a specified sampling rate. If + /// that subscriber returns `Interest::always` from `register_callsite`, then + /// the filter will not be re-evaluated once it has been applied to a given + /// set of metadata. Thus, the counter will not be incremented, and the span + /// or event that corresponds to the metadata will never be `enabled`. + /// + /// `Subscriber`s that need to change their filters occasionally should call + /// [`rebuild_interest_cache`] to re-evaluate `register_callsite` for all + /// callsites. + /// + /// Similarly, if a `Subscriber` has a filtering strategy that can be + /// changed dynamically at runtime, it would need to re-evaluate that filter + /// if the cached results have changed. + /// + /// A subscriber which manages fanout to multiple other subscribers + /// should proxy this decision to all of its child subscribers, + /// returning `Interest::never` only if _all_ such children return + /// `Interest::never`. If the set of subscribers to which spans are + /// broadcast may change dynamically, the subscriber should also never + /// return `Interest::Never`, as a new subscriber may be added that _is_ + /// interested. + /// + /// See the [documentation on the callsite registry][cs-reg] for more + /// details on how and when the `register_callsite` method is called. + /// + /// # Notes + /// This function may be called again when a new subscriber is created or + /// when the registry is invalidated. + /// + /// If a subscriber returns `Interest::never` for a particular callsite, it + /// _may_ still see spans and events originating from that callsite, if + /// another subscriber expressed interest in it. + /// + /// [callsite]: crate::callsite + /// [filter]: Self::enabled + /// [metadata]: super::metadata::Metadata + /// [`enabled`]: Subscriber::enabled() + /// [`rebuild_interest_cache`]: super::callsite::rebuild_interest_cache + /// [cs-reg]: crate::callsite#registering-callsites + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + if self.enabled(metadata) { + Interest::always() + } else { + Interest::never() + } + } + + /// Returns true if a span or event with the specified [metadata] would be + /// recorded. + /// + /// By default, it is assumed that this filter needs only be evaluated once + /// for each callsite, so it is called by [`register_callsite`] when each + /// callsite is registered. The result is used to determine if the subscriber + /// is always [interested] or never interested in that callsite. This is intended + /// primarily as an optimization, so that expensive filters (such as those + /// involving string search, et cetera) need not be re-evaluated. + /// + /// However, if the subscriber's interest in a particular span or event may + /// change, or depends on contexts only determined dynamically at runtime, + /// then the `register_callsite` method should be overridden to return + /// [`Interest::sometimes`]. In that case, this function will be called every + /// time that span or event occurs. + /// + /// [metadata]: super::metadata::Metadata + /// [interested]: Interest + /// [`Interest::sometimes`]: Interest::sometimes + /// [`register_callsite`]: Subscriber::register_callsite() + fn enabled(&self, metadata: &Metadata<'_>) -> bool; + + /// Returns the highest [verbosity level][level] that this `Subscriber` will + /// enable, or `None`, if the subscriber does not implement level-based + /// filtering or chooses not to implement this method. + /// + /// If this method returns a [`Level`][level], it will be used as a hint to + /// determine the most verbose level that will be enabled. This will allow + /// spans and events which are more verbose than that level to be skipped + /// more efficiently. Subscribers which perform filtering are strongly + /// encouraged to provide an implementation of this method. + /// + /// If the maximum level the subscriber will enable can change over the + /// course of its lifetime, it is free to return a different value from + /// multiple invocations of this method. However, note that changes in the + /// maximum level will **only** be reflected after the callsite [`Interest`] + /// cache is rebuilt, by calling the [`callsite::rebuild_interest_cache`][rebuild] + /// function. Therefore, if the subscriber will change the value returned by + /// this method, it is responsible for ensuring that + /// [`rebuild_interest_cache`][rebuild] is called after the value of the max + /// level changes. + /// + /// [level]: super::Level + /// [rebuild]: super::callsite::rebuild_interest_cache + fn max_level_hint(&self) -> Option<LevelFilter> { + None + } + + /// Visit the construction of a new span, returning a new [span ID] for the + /// span being constructed. + /// + /// The provided [`Attributes`] contains any field values that were provided + /// when the span was created. The subscriber may pass a [visitor] to the + /// `Attributes`' [`record` method] to record these values. + /// + /// IDs are used to uniquely identify spans and events within the context of a + /// subscriber, so span equality will be based on the returned ID. Thus, if + /// the subscriber wishes for all spans with the same metadata to be + /// considered equal, it should return the same ID every time it is given a + /// particular set of metadata. Similarly, if it wishes for two separate + /// instances of a span with the same metadata to *not* be equal, it should + /// return a distinct ID every time this function is called, regardless of + /// the metadata. + /// + /// Note that the subscriber is free to assign span IDs based on whatever + /// scheme it sees fit. Any guarantees about uniqueness, ordering, or ID + /// reuse are left up to the subscriber implementation to determine. + /// + /// [span ID]: super::span::Id + /// [`Attributes`]: super::span::Attributes + /// [visitor]: super::field::Visit + /// [`record` method]: super::span::Attributes::record + fn new_span(&self, span: &span::Attributes<'_>) -> span::Id; + + // === Notification methods =============================================== + + /// Record a set of values on a span. + /// + /// This method will be invoked when value is recorded on a span. + /// Recording multiple values for the same field is possible, + /// but the actual behaviour is defined by the subscriber implementation. + /// + /// Keep in mind that a span might not provide a value + /// for each field it declares. + /// + /// The subscriber is expected to provide a [visitor] to the `Record`'s + /// [`record` method] in order to record the added values. + /// + /// # Example + /// "foo = 3" will be recorded when [`record`] is called on the + /// `Attributes` passed to `new_span`. + /// Since values are not provided for the `bar` and `baz` fields, + /// the span's `Metadata` will indicate that it _has_ those fields, + /// but values for them won't be recorded at this time. + /// + /// ```rust,ignore + /// # use tracing::span; + /// + /// let mut span = span!("my_span", foo = 3, bar, baz); + /// + /// // `Subscriber::record` will be called with a `Record` + /// // containing "bar = false" + /// span.record("bar", &false); + /// + /// // `Subscriber::record` will be called with a `Record` + /// // containing "baz = "a string"" + /// span.record("baz", &"a string"); + /// ``` + /// + /// [visitor]: super::field::Visit + /// [`record`]: super::span::Attributes::record + /// [`record` method]: super::span::Record::record + fn record(&self, span: &span::Id, values: &span::Record<'_>); + + /// Adds an indication that `span` follows from the span with the id + /// `follows`. + /// + /// This relationship differs somewhat from the parent-child relationship: a + /// span may have any number of prior spans, rather than a single one; and + /// spans are not considered to be executing _inside_ of the spans they + /// follow from. This means that a span may close even if subsequent spans + /// that follow from it are still open, and time spent inside of a + /// subsequent span should not be included in the time its precedents were + /// executing. This is used to model causal relationships such as when a + /// single future spawns several related background tasks, et cetera. + /// + /// If the subscriber has spans corresponding to the given IDs, it should + /// record this relationship in whatever way it deems necessary. Otherwise, + /// if one or both of the given span IDs do not correspond to spans that the + /// subscriber knows about, or if a cyclical relationship would be created + /// (i.e., some span _a_ which proceeds some other span _b_ may not also + /// follow from _b_), it may silently do nothing. + fn record_follows_from(&self, span: &span::Id, follows: &span::Id); + + /// Determine if an [`Event`] should be recorded. + /// + /// By default, this returns `true` and `Subscriber`s can filter events in + /// [`event`][Self::event] without any penalty. However, when `event` is + /// more complicated, this can be used to determine if `event` should be + /// called at all, separating out the decision from the processing. + fn event_enabled(&self, event: &Event<'_>) -> bool { + let _ = event; + true + } + + /// Records that an [`Event`] has occurred. + /// + /// This method will be invoked when an Event is constructed by + /// the `Event`'s [`dispatch` method]. For example, this happens internally + /// when an event macro from `tracing` is called. + /// + /// The key difference between this method and `record` is that `record` is + /// called when a value is recorded for a field defined by a span, + /// while `event` is called when a new event occurs. + /// + /// The provided `Event` struct contains any field values attached to the + /// event. The subscriber may pass a [visitor] to the `Event`'s + /// [`record` method] to record these values. + /// + /// [`Event`]: super::event::Event + /// [visitor]: super::field::Visit + /// [`record` method]: super::event::Event::record + /// [`dispatch` method]: super::event::Event::dispatch + fn event(&self, event: &Event<'_>); + + /// Records that a span has been entered. + /// + /// When entering a span, this method is called to notify the subscriber + /// that the span has been entered. The subscriber is provided with the + /// [span ID] of the entered span, and should update any internal state + /// tracking the current span accordingly. + /// + /// [span ID]: super::span::Id + fn enter(&self, span: &span::Id); + + /// Records that a span has been exited. + /// + /// When exiting a span, this method is called to notify the subscriber + /// that the span has been exited. The subscriber is provided with the + /// [span ID] of the exited span, and should update any internal state + /// tracking the current span accordingly. + /// + /// Exiting a span does not imply that the span will not be re-entered. + /// + /// [span ID]: super::span::Id + fn exit(&self, span: &span::Id); + + /// Notifies the subscriber that a [span ID] has been cloned. + /// + /// This function is guaranteed to only be called with span IDs that were + /// returned by this subscriber's `new_span` function. + /// + /// Note that the default implementation of this function this is just the + /// identity function, passing through the identifier. However, it can be + /// used in conjunction with [`try_close`] to track the number of handles + /// capable of `enter`ing a span. When all the handles have been dropped + /// (i.e., `try_close` has been called one more time than `clone_span` for a + /// given ID), the subscriber may assume that the span will not be entered + /// again. It is then free to deallocate storage for data associated with + /// that span, write data from that span to IO, and so on. + /// + /// For more unsafe situations, however, if `id` is itself a pointer of some + /// kind this can be used as a hook to "clone" the pointer, depending on + /// what that means for the specified pointer. + /// + /// [span ID]: super::span::Id + /// [`try_close`]: Subscriber::try_close + fn clone_span(&self, id: &span::Id) -> span::Id { + id.clone() + } + + /// **This method is deprecated.** + /// + /// Using `drop_span` may result in subscribers composed using + /// `tracing-subscriber` crate's `Layer` trait from observing close events. + /// Use [`try_close`] instead. + /// + /// The default implementation of this function does nothing. + /// + /// [`try_close`]: Subscriber::try_close + #[deprecated(since = "0.1.2", note = "use `Subscriber::try_close` instead")] + fn drop_span(&self, _id: span::Id) {} + + /// Notifies the subscriber that a [span ID] has been dropped, and returns + /// `true` if there are now 0 IDs that refer to that span. + /// + /// Higher-level libraries providing functionality for composing multiple + /// subscriber implementations may use this return value to notify any + /// "layered" subscribers that this subscriber considers the span closed. + /// + /// The default implementation of this method calls the subscriber's + /// [`drop_span`] method and returns `false`. This means that, unless the + /// subscriber overrides the default implementation, close notifications + /// will never be sent to any layered subscribers. In general, if the + /// subscriber tracks reference counts, this method should be implemented, + /// rather than `drop_span`. + /// + /// This function is guaranteed to only be called with span IDs that were + /// returned by this subscriber's `new_span` function. + /// + /// It's guaranteed that if this function has been called once more than the + /// number of times `clone_span` was called with the same `id`, then no more + /// handles that can enter the span with that `id` exist. This means that it + /// can be used in conjunction with [`clone_span`] to track the number of + /// handles capable of `enter`ing a span. When all the handles have been + /// dropped (i.e., `try_close` has been called one more time than + /// `clone_span` for a given ID), the subscriber may assume that the span + /// will not be entered again, and should return `true`. It is then free to + /// deallocate storage for data associated with that span, write data from + /// that span to IO, and so on. + /// + /// **Note**: since this function is called when spans are dropped, + /// implementations should ensure that they are unwind-safe. Panicking from + /// inside of a `try_close` function may cause a double panic, if the span + /// was dropped due to a thread unwinding. + /// + /// [span ID]: super::span::Id + /// [`clone_span`]: Subscriber::clone_span + /// [`drop_span`]: Subscriber::drop_span + fn try_close(&self, id: span::Id) -> bool { + #[allow(deprecated)] + self.drop_span(id); + false + } + + /// Returns a type representing this subscriber's view of the current span. + /// + /// If subscribers track a current span, they should override this function + /// to return [`Current::new`] if the thread from which this method is + /// called is inside a span, or [`Current::none`] if the thread is not + /// inside a span. + /// + /// By default, this returns a value indicating that the subscriber + /// does **not** track what span is current. If the subscriber does not + /// implement a current span, it should not override this method. + /// + /// [`Current::new`]: super::span::Current#tymethod.new + /// [`Current::none`]: super::span::Current#tymethod.none + fn current_span(&self) -> span::Current { + span::Current::unknown() + } + + // === Downcasting methods ================================================ + + /// If `self` is the same type as the provided `TypeId`, returns an untyped + /// `*const` pointer to that type. Otherwise, returns `None`. + /// + /// If you wish to downcast a `Subscriber`, it is strongly advised to use + /// the safe API provided by [`downcast_ref`] instead. + /// + /// This API is required for `downcast_raw` to be a trait method; a method + /// signature like [`downcast_ref`] (with a generic type parameter) is not + /// object-safe, and thus cannot be a trait method for `Subscriber`. This + /// means that if we only exposed `downcast_ref`, `Subscriber` + /// implementations could not override the downcasting behavior + /// + /// This method may be overridden by "fan out" or "chained" subscriber + /// implementations which consist of multiple composed types. Such + /// subscribers might allow `downcast_raw` by returning references to those + /// component if they contain components with the given `TypeId`. + /// + /// # Safety + /// + /// The [`downcast_ref`] method expects that the pointer returned by + /// `downcast_raw` is non-null and points to a valid instance of the type + /// with the provided `TypeId`. Failure to ensure this will result in + /// undefined behaviour, so implementing `downcast_raw` is unsafe. + /// + /// [`downcast_ref`]: #method.downcast_ref + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + if id == TypeId::of::<Self>() { + Some(self as *const Self as *const ()) + } else { + None + } + } +} + +impl dyn Subscriber { + /// Returns `true` if this `Subscriber` is the same type as `T`. + pub fn is<T: Any>(&self) -> bool { + self.downcast_ref::<T>().is_some() + } + + /// Returns some reference to this `Subscriber` value if it is of type `T`, + /// or `None` if it isn't. + pub fn downcast_ref<T: Any>(&self) -> Option<&T> { + unsafe { + let raw = self.downcast_raw(TypeId::of::<T>())?; + if raw.is_null() { + None + } else { + Some(&*(raw as *const _)) + } + } + } +} + +impl dyn Subscriber + Send { + /// Returns `true` if this [`Subscriber`] is the same type as `T`. + pub fn is<T: Any>(&self) -> bool { + self.downcast_ref::<T>().is_some() + } + + /// Returns some reference to this [`Subscriber`] value if it is of type `T`, + /// or `None` if it isn't. + pub fn downcast_ref<T: Any>(&self) -> Option<&T> { + unsafe { + let raw = self.downcast_raw(TypeId::of::<T>())?; + if raw.is_null() { + None + } else { + Some(&*(raw as *const _)) + } + } + } +} + +impl dyn Subscriber + Sync { + /// Returns `true` if this [`Subscriber`] is the same type as `T`. + pub fn is<T: Any>(&self) -> bool { + self.downcast_ref::<T>().is_some() + } + + /// Returns some reference to this `[`Subscriber`] value if it is of type `T`, + /// or `None` if it isn't. + pub fn downcast_ref<T: Any>(&self) -> Option<&T> { + unsafe { + let raw = self.downcast_raw(TypeId::of::<T>())?; + if raw.is_null() { + None + } else { + Some(&*(raw as *const _)) + } + } + } +} + +impl dyn Subscriber + Send + Sync { + /// Returns `true` if this [`Subscriber`] is the same type as `T`. + pub fn is<T: Any>(&self) -> bool { + self.downcast_ref::<T>().is_some() + } + + /// Returns some reference to this [`Subscriber`] value if it is of type `T`, + /// or `None` if it isn't. + pub fn downcast_ref<T: Any>(&self) -> Option<&T> { + unsafe { + let raw = self.downcast_raw(TypeId::of::<T>())?; + if raw.is_null() { + None + } else { + Some(&*(raw as *const _)) + } + } + } +} + +/// Indicates a [`Subscriber`]'s interest in a particular callsite. +/// +/// `Subscriber`s return an `Interest` from their [`register_callsite`] methods +/// in order to determine whether that span should be enabled or disabled. +/// +/// [`Subscriber`]: super::Subscriber +/// [`register_callsite`]: super::Subscriber::register_callsite +#[derive(Clone, Debug)] +pub struct Interest(InterestKind); + +#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +enum InterestKind { + Never = 0, + Sometimes = 1, + Always = 2, +} + +impl Interest { + /// Returns an `Interest` indicating that the subscriber is never interested + /// in being notified about a callsite. + /// + /// If all active subscribers are `never()` interested in a callsite, it will + /// be completely disabled unless a new subscriber becomes active. + #[inline] + pub fn never() -> Self { + Interest(InterestKind::Never) + } + + /// Returns an `Interest` indicating the subscriber is sometimes interested + /// in being notified about a callsite. + /// + /// If all active subscribers are `sometimes` or `never` interested in a + /// callsite, the currently active subscriber will be asked to filter that + /// callsite every time it creates a span. This will be the case until a new + /// subscriber expresses that it is `always` interested in the callsite. + #[inline] + pub fn sometimes() -> Self { + Interest(InterestKind::Sometimes) + } + + /// Returns an `Interest` indicating the subscriber is always interested in + /// being notified about a callsite. + /// + /// If any subscriber expresses that it is `always()` interested in a given + /// callsite, then the callsite will always be enabled. + #[inline] + pub fn always() -> Self { + Interest(InterestKind::Always) + } + + /// Returns `true` if the subscriber is never interested in being notified + /// about this callsite. + #[inline] + pub fn is_never(&self) -> bool { + matches!(self.0, InterestKind::Never) + } + + /// Returns `true` if the subscriber is sometimes interested in being notified + /// about this callsite. + #[inline] + pub fn is_sometimes(&self) -> bool { + matches!(self.0, InterestKind::Sometimes) + } + + /// Returns `true` if the subscriber is always interested in being notified + /// about this callsite. + #[inline] + pub fn is_always(&self) -> bool { + matches!(self.0, InterestKind::Always) + } + + /// Returns the common interest between these two Interests. + /// + /// If both interests are the same, this propagates that interest. + /// Otherwise, if they differ, the result must always be + /// `Interest::sometimes` --- if the two subscribers differ in opinion, we + /// will have to ask the current subscriber what it thinks, no matter what. + pub(crate) fn and(self, rhs: Interest) -> Self { + if self.0 == rhs.0 { + self + } else { + Interest::sometimes() + } + } +} + +/// A no-op [`Subscriber`]. +/// +/// [`NoSubscriber`] implements the [`Subscriber`] trait by never being enabled, +/// never being interested in any callsite, and dropping all spans and events. +#[derive(Copy, Clone, Debug, Default)] +pub struct NoSubscriber(()); + +impl Subscriber for NoSubscriber { + #[inline] + fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest { + Interest::never() + } + + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(0xDEAD) + } + + fn event(&self, _event: &Event<'_>) {} + + fn record(&self, _span: &span::Id, _values: &span::Record<'_>) {} + + fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {} + + #[inline] + fn enabled(&self, _metadata: &Metadata<'_>) -> bool { + false + } + + fn enter(&self, _span: &span::Id) {} + fn exit(&self, _span: &span::Id) {} +} + +impl<S> Subscriber for Box<S> +where + S: Subscriber + ?Sized, +{ + #[inline] + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.as_ref().register_callsite(metadata) + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + self.as_ref().enabled(metadata) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + self.as_ref().max_level_hint() + } + + #[inline] + fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { + self.as_ref().new_span(span) + } + + #[inline] + fn record(&self, span: &span::Id, values: &span::Record<'_>) { + self.as_ref().record(span, values) + } + + #[inline] + fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { + self.as_ref().record_follows_from(span, follows) + } + + #[inline] + fn event_enabled(&self, event: &Event<'_>) -> bool { + self.as_ref().event_enabled(event) + } + + #[inline] + fn event(&self, event: &Event<'_>) { + self.as_ref().event(event) + } + + #[inline] + fn enter(&self, span: &span::Id) { + self.as_ref().enter(span) + } + + #[inline] + fn exit(&self, span: &span::Id) { + self.as_ref().exit(span) + } + + #[inline] + fn clone_span(&self, id: &span::Id) -> span::Id { + self.as_ref().clone_span(id) + } + + #[inline] + fn try_close(&self, id: span::Id) -> bool { + self.as_ref().try_close(id) + } + + #[inline] + #[allow(deprecated)] + fn drop_span(&self, id: span::Id) { + self.as_ref().try_close(id); + } + + #[inline] + fn current_span(&self) -> span::Current { + self.as_ref().current_span() + } + + #[inline] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + if id == TypeId::of::<Self>() { + return Some(self as *const Self as *const _); + } + + self.as_ref().downcast_raw(id) + } +} + +impl<S> Subscriber for Arc<S> +where + S: Subscriber + ?Sized, +{ + #[inline] + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.as_ref().register_callsite(metadata) + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + self.as_ref().enabled(metadata) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + self.as_ref().max_level_hint() + } + + #[inline] + fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { + self.as_ref().new_span(span) + } + + #[inline] + fn record(&self, span: &span::Id, values: &span::Record<'_>) { + self.as_ref().record(span, values) + } + + #[inline] + fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { + self.as_ref().record_follows_from(span, follows) + } + + #[inline] + fn event_enabled(&self, event: &Event<'_>) -> bool { + self.as_ref().event_enabled(event) + } + + #[inline] + fn event(&self, event: &Event<'_>) { + self.as_ref().event(event) + } + + #[inline] + fn enter(&self, span: &span::Id) { + self.as_ref().enter(span) + } + + #[inline] + fn exit(&self, span: &span::Id) { + self.as_ref().exit(span) + } + + #[inline] + fn clone_span(&self, id: &span::Id) -> span::Id { + self.as_ref().clone_span(id) + } + + #[inline] + fn try_close(&self, id: span::Id) -> bool { + self.as_ref().try_close(id) + } + + #[inline] + #[allow(deprecated)] + fn drop_span(&self, id: span::Id) { + self.as_ref().try_close(id); + } + + #[inline] + fn current_span(&self) -> span::Current { + self.as_ref().current_span() + } + + #[inline] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + if id == TypeId::of::<Self>() { + return Some(self as *const Self as *const _); + } + + self.as_ref().downcast_raw(id) + } +} diff --git a/vendor/tracing-core/tests/common/mod.rs b/vendor/tracing-core/tests/common/mod.rs new file mode 100644 index 000000000..3420f0b89 --- /dev/null +++ b/vendor/tracing-core/tests/common/mod.rs @@ -0,0 +1,30 @@ +use tracing_core::{metadata::Metadata, span, subscriber::Subscriber, Event}; + +pub struct TestSubscriberA; +impl Subscriber for TestSubscriberA { + fn enabled(&self, _: &Metadata<'_>) -> bool { + true + } + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(1) + } + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + fn event(&self, _: &Event<'_>) {} + fn enter(&self, _: &span::Id) {} + fn exit(&self, _: &span::Id) {} +} +pub struct TestSubscriberB; +impl Subscriber for TestSubscriberB { + fn enabled(&self, _: &Metadata<'_>) -> bool { + true + } + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(1) + } + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + fn event(&self, _: &Event<'_>) {} + fn enter(&self, _: &span::Id) {} + fn exit(&self, _: &span::Id) {} +} diff --git a/vendor/tracing-core/tests/dispatch.rs b/vendor/tracing-core/tests/dispatch.rs new file mode 100644 index 000000000..3820692a8 --- /dev/null +++ b/vendor/tracing-core/tests/dispatch.rs @@ -0,0 +1,56 @@ +#![cfg(feature = "std")] +mod common; + +use common::*; +use tracing_core::dispatcher::*; + +#[test] +fn set_default_dispatch() { + set_global_default(Dispatch::new(TestSubscriberA)).expect("global dispatch set failed"); + get_default(|current| { + assert!( + current.is::<TestSubscriberA>(), + "global dispatch get failed" + ) + }); + + let guard = set_default(&Dispatch::new(TestSubscriberB)); + get_default(|current| assert!(current.is::<TestSubscriberB>(), "set_default get failed")); + + // Drop the guard, setting the dispatch back to the global dispatch + drop(guard); + + get_default(|current| { + assert!( + current.is::<TestSubscriberA>(), + "global dispatch get failed" + ) + }); +} + +#[test] +fn nested_set_default() { + let _guard = set_default(&Dispatch::new(TestSubscriberA)); + get_default(|current| { + assert!( + current.is::<TestSubscriberA>(), + "set_default for outer subscriber failed" + ) + }); + + let inner_guard = set_default(&Dispatch::new(TestSubscriberB)); + get_default(|current| { + assert!( + current.is::<TestSubscriberB>(), + "set_default inner subscriber failed" + ) + }); + + drop(inner_guard); + get_default(|current| { + assert!( + current.is::<TestSubscriberA>(), + "set_default outer subscriber failed" + ) + }); +} diff --git a/vendor/tracing-core/tests/global_dispatch.rs b/vendor/tracing-core/tests/global_dispatch.rs new file mode 100644 index 000000000..d430ac618 --- /dev/null +++ b/vendor/tracing-core/tests/global_dispatch.rs @@ -0,0 +1,34 @@ +mod common; + +use common::*; +use tracing_core::dispatcher::*; +#[test] +fn global_dispatch() { + set_global_default(Dispatch::new(TestSubscriberA)).expect("global dispatch set failed"); + get_default(|current| { + assert!( + current.is::<TestSubscriberA>(), + "global dispatch get failed" + ) + }); + + #[cfg(feature = "std")] + with_default(&Dispatch::new(TestSubscriberB), || { + get_default(|current| { + assert!( + current.is::<TestSubscriberB>(), + "thread-local override of global dispatch failed" + ) + }); + }); + + get_default(|current| { + assert!( + current.is::<TestSubscriberA>(), + "reset to global override failed" + ) + }); + + set_global_default(Dispatch::new(TestSubscriberA)) + .expect_err("double global dispatch set succeeded"); +} diff --git a/vendor/tracing-core/tests/macros.rs b/vendor/tracing-core/tests/macros.rs new file mode 100644 index 000000000..ee9007eee --- /dev/null +++ b/vendor/tracing-core/tests/macros.rs @@ -0,0 +1,48 @@ +use tracing_core::{ + callsite::Callsite, + metadata, + metadata::{Kind, Level, Metadata}, + subscriber::Interest, +}; + +#[test] +fn metadata_macro_api() { + // This test should catch any inadvertent breaking changes + // caused by changes to the macro. + struct TestCallsite; + + impl Callsite for TestCallsite { + fn set_interest(&self, _: Interest) { + unimplemented!("test") + } + fn metadata(&self) -> &Metadata<'_> { + unimplemented!("test") + } + } + + static CALLSITE: TestCallsite = TestCallsite; + let _metadata = metadata! { + name: "test_metadata", + target: "test_target", + level: Level::DEBUG, + fields: &["foo", "bar", "baz"], + callsite: &CALLSITE, + kind: Kind::SPAN, + }; + let _metadata = metadata! { + name: "test_metadata", + target: "test_target", + level: Level::TRACE, + fields: &[], + callsite: &CALLSITE, + kind: Kind::EVENT, + }; + let _metadata = metadata! { + name: "test_metadata", + target: "test_target", + level: Level::INFO, + fields: &[], + callsite: &CALLSITE, + kind: Kind::EVENT + }; +} diff --git a/vendor/tracing-log/.cargo-checksum.json b/vendor/tracing-log/.cargo-checksum.json new file mode 100644 index 000000000..9d068bc0e --- /dev/null +++ b/vendor/tracing-log/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"d7cc8e83d800946b622f3392cbb5245d57dde70ef1b544fe82260371a90b39ec","Cargo.toml":"3e7cfde6270347998b817355e0b9bb7a59cfb458477f8a91045a15b5081900c7","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"f69a91df2fb2eb0b8a381acf6dc7d4fe611aece9a2ce9389743a00c6b1ca9dc9","benches/logging.rs":"d4d0ceb4bf0fcf0c139c48bece64cad22c04b0c83fa2754983b5d4f141c817b5","src/env_logger.rs":"853d961ef431fcad924d07b90e4fc9f73413ed45e7bd7b9aaba95a1fdc28349a","src/interest_cache.rs":"590437659a6f10f2ba53b640dbe2eb090c3fc7b364c2b7d8b6178d1a849454c2","src/lib.rs":"7f1b89dc2fbd8f4d8bc81fe5818fb175f44b4d91d789e5bf67f3463f46864d8d","src/log_tracer.rs":"679c0a84896598439bcb8a01bc49ae58148039df642f43c70fd665b9c687a05a","src/trace_logger.rs":"b1d58046a76bdad604072c7f969e19e02f90324d6881504403b7debc965845a6","tests/log_tracer.rs":"2d4725822b19c47c0cbcfaf2f9af3f939f55b4b0d9b3a2c5584af2b77453854b","tests/reexport_log_crate.rs":"1eb4672784ecda75743494b0662d90fd48b1252b44b68afa898fa0bec3699683"},"package":"78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922"}
\ No newline at end of file diff --git a/vendor/tracing-log/CHANGELOG.md b/vendor/tracing-log/CHANGELOG.md new file mode 100644 index 000000000..2ae9f0e6d --- /dev/null +++ b/vendor/tracing-log/CHANGELOG.md @@ -0,0 +1,66 @@ +# 0.1.3 (April 21st, 2022) + +### Added + +- **log-tracer**: Added `LogTracer::with_interest_cache` to enable a limited + form of per-record `Interest` caching for `log` records ([#1636]) + +### Changed + +- Updated minimum supported Rust version (MSRV) to Rust 1.49.0 ([#1913]) + +### Fixed + +- **log-tracer**: Fixed `LogTracer` not honoring `tracing` max level filters + ([#1543]) +- Broken links in documentation ([#2068], [#2077]) + +Thanks to @Millione, @teozkr, @koute, @Folyd, and @ben0x539 for contributing to +this release! + +[#1636]: https://github.com/tokio-rs/tracing/pulls/1636 +[#1913]: https://github.com/tokio-rs/tracing/pulls/1913 +[#1543]: https://github.com/tokio-rs/tracing/pulls/1543 +[#2068]: https://github.com/tokio-rs/tracing/pulls/2068 +[#2077]: https://github.com/tokio-rs/tracing/pulls/2077 + +# 0.1.2 (February 19th, 2020) + +### Added + +- Re-export the `log` crate so that users can ensure consistent versions ([#602]) +- `AsLog` implementation for `tracing::LevelFilter` ([#1248]) +- `AsTrace` implementation for `log::LevelFilter` ([#1248]) + +### Fixed + +- **log-tracer**: Fixed `Log::enabled` implementation for `LogTracer` not + calling `Subscriber::enabled` ([#1254]) +- **log-tracer**: Fixed `Log::enabled` implementation for `LogTracer` not + checking the max level hint ([#1247]) +- Several documentation fixes ([#483], [#485], [#537], [#595], [#941], [#981]) + +[#483]: https://github.com/tokio-rs/tracing/pulls/483 +[#485]: https://github.com/tokio-rs/tracing/pulls/485 +[#537]: https://github.com/tokio-rs/tracing/pulls/537 +[#595]: https://github.com/tokio-rs/tracing/pulls/595 +[#605]: https://github.com/tokio-rs/tracing/pulls/604 +[#941]: https://github.com/tokio-rs/tracing/pulls/941 +[#1247]: https://github.com/tokio-rs/tracing/pulls/1247 +[#1248]: https://github.com/tokio-rs/tracing/pulls/1248 +[#1254]: https://github.com/tokio-rs/tracing/pulls/1254 + +# 0.1.1 (October 29, 2019) + +### Deprecated + +- `TraceLogger` (use `tracing`'s "log" and "log-always" feature flags instead) + +### Fixed + +- Issues with `log/std` feature flag (#406) +- Minor documentation issues (#405, #408) + +# 0.1.0 (September 3, 2019) + +- Initial release diff --git a/vendor/tracing-log/Cargo.toml b/vendor/tracing-log/Cargo.toml new file mode 100644 index 000000000..4243c3413 --- /dev/null +++ b/vendor/tracing-log/Cargo.toml @@ -0,0 +1,89 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +rust-version = "1.49.0" +name = "tracing-log" +version = "0.1.3" +authors = ["Tokio Contributors <team@tokio.rs>"] +description = """ +Provides compatibility between `tracing` and the `log` crate. +""" +homepage = "https://tokio.rs" +readme = "README.md" +keywords = [ + "logging", + "tracing", + "log", +] +categories = [ + "development-tools::debugging", + "asynchronous", +] +license = "MIT" +repository = "https://github.com/tokio-rs/tracing" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = [ + "--cfg", + "docsrs", +] + +[[bench]] +name = "logging" +harness = false + +[dependencies.ahash] +version = "0.7.4" +optional = true + +[dependencies.env_logger] +version = "0.7" +optional = true + +[dependencies.lazy_static] +version = "1.3.0" + +[dependencies.log] +version = "0.4" + +[dependencies.lru] +version = "0.7.0" +optional = true + +[dependencies.tracing-core] +version = "0.1.17" + +[dev-dependencies.criterion] +version = "0.3" +default_features = false + +[dev-dependencies.tracing] +version = "0.1" + +[features] +default = [ + "log-tracer", + "trace-logger", + "std", +] +interest-cache = [ + "lru", + "ahash", +] +log-tracer = [] +std = ["log/std"] +trace-logger = [] + +[badges.maintenance] +status = "actively-maintained" diff --git a/vendor/tracing-log/LICENSE b/vendor/tracing-log/LICENSE new file mode 100644 index 000000000..cdb28b4b5 --- /dev/null +++ b/vendor/tracing-log/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2019 Tokio Contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/tracing-log/README.md b/vendor/tracing-log/README.md new file mode 100644 index 000000000..1cfee3681 --- /dev/null +++ b/vendor/tracing-log/README.md @@ -0,0 +1,85 @@ +![Tracing — Structured, application-level diagnostics][splash] + +[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg + +# tracing-log + +[`log`] compatibility for [`tracing`]. + +[![Crates.io][crates-badge]][crates-url] +[![Documentation][docs-badge]][docs-url] +[![Documentation (master)][docs-master-badge]][docs-master-url] +[![MIT licensed][mit-badge]][mit-url] +[![Build Status][actions-badge]][actions-url] +[![Discord chat][discord-badge]][discord-url] +![maintenance status][maint-badge] + +[Documentation][docs-url] | [Chat (discord)][discord-url] + + +[crates-badge]: https://img.shields.io/crates/v/tracing-log.svg +[crates-url]: https://crates.io/crates/tracing-log +[docs-badge]: https://docs.rs/tracing-log/badge.svg +[docs-url]: https://docs.rs/tracing-log +[docs-master-badge]: https://img.shields.io/badge/docs-master-blue +[docs-master-url]: https://tracing-rs.netlify.com/tracing_log +[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg +[mit-url]: LICENSE +[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg +[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI +[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white +[discord-url]: https://discord.gg/EeF3cQw +[maint-badge]: https://img.shields.io/badge/maintenance-experimental-blue.svg + +## Overview + +[`tracing`] is a framework for instrumenting Rust programs with context-aware, +structured, event-based diagnostic information. This crate provides +compatibility layers for using `tracing` alongside the logging facade provided +by the [`log`] crate. + +This crate provides: + +- [`AsTrace`] and [`AsLog`] traits for converting between `tracing` and `log` types. +- [`LogTracer`], a [`log::Log`] implementation that consumes [`log::Record`]s + and outputs them as [`tracing::Event`]s. +- An [`env_logger`] module, with helpers for using the [`env_logger` crate] + with `tracing` (optional, enabled by the `env_logger` feature). + +[`tracing`]: https://crates.io/crates/tracing +[`log`]: https://crates.io/crates/log +[`AsTrace`]: https://docs.rs/tracing-log/latest/tracing_log/trait.AsTrace.html +[`AsLog`]: https://docs.rs/tracing-log/latest/tracing_log/trait.AsLog.html +[`LogTracer`]: https://docs.rs/tracing-log/latest/tracing_log/struct.LogTracer.html +[`log::Log`]: https://docs.rs/log/latest/log/trait.Log.html +[`log::Record`]: https://docs.rs/log/latest/log/struct.Record.html +[`tracing::Subscriber`]: https://docs.rs/tracing/latest/tracing/trait.Subscriber.html +[`tracing::Event`]: https://docs.rs/tracing/latest/tracing/struct.Event.html + +*Compiler support: [requires `rustc` 1.49+][msrv]* + +[msrv]: #supported-rust-versions + +## Supported Rust Versions + +Tracing is built against the latest stable release. The minimum supported +version is 1.49. 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. + +## License + +This project is licensed under the [MIT license](LICENSE). + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in Tracing by you, shall be licensed as MIT, without any additional +terms or conditions. diff --git a/vendor/tracing-log/benches/logging.rs b/vendor/tracing-log/benches/logging.rs new file mode 100644 index 000000000..c72e3edf0 --- /dev/null +++ b/vendor/tracing-log/benches/logging.rs @@ -0,0 +1,91 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use log::trace; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use tracing_subscriber::{EnvFilter, FmtSubscriber}; + +// This creates a bunch of threads and makes sure they start executing +// a given callback almost exactly at the same time. +fn run_on_many_threads<F, R>(thread_count: usize, callback: F) -> Vec<R> +where + F: Fn() -> R + 'static + Send + Clone, + R: Send + 'static, +{ + let started_count = Arc::new(AtomicUsize::new(0)); + let barrier = Arc::new(AtomicBool::new(false)); + #[allow(clippy::needless_collect)] + let threads: Vec<_> = (0..thread_count) + .map(|_| { + let started_count = started_count.clone(); + let barrier = barrier.clone(); + let callback = callback.clone(); + + std::thread::spawn(move || { + started_count.fetch_add(1, Ordering::SeqCst); + while !barrier.load(Ordering::SeqCst) { + std::thread::yield_now(); + } + + callback() + }) + }) + .collect(); + + while started_count.load(Ordering::SeqCst) != thread_count { + std::thread::yield_now(); + } + barrier.store(true, Ordering::SeqCst); + + threads + .into_iter() + .map(|handle| handle.join()) + .collect::<Result<Vec<R>, _>>() + .unwrap() +} + +fn bench_logger(c: &mut Criterion) { + let env_filter = EnvFilter::default() + .add_directive("info".parse().unwrap()) + .add_directive("ws=off".parse().unwrap()) + .add_directive("yamux=off".parse().unwrap()) + .add_directive("regalloc=off".parse().unwrap()) + .add_directive("cranelift_codegen=off".parse().unwrap()) + .add_directive("cranelift_wasm=warn".parse().unwrap()) + .add_directive("hyper=warn".parse().unwrap()) + .add_directive("dummy=trace".parse().unwrap()); + + let builder = tracing_log::LogTracer::builder().with_max_level(log::LevelFilter::Trace); + + #[cfg(feature = "interest-cache")] + let builder = builder.with_interest_cache(tracing_log::InterestCacheConfig::default()); + + builder.init().unwrap(); + + let builder = FmtSubscriber::builder() + .with_env_filter(env_filter) + .with_filter_reloading(); + + let subscriber = builder.finish(); + tracing::subscriber::set_global_default(subscriber).unwrap(); + + const THREAD_COUNT: usize = 8; + + c.bench_function("log_from_multiple_threads", |b| { + b.iter_custom(|count| { + let durations = run_on_many_threads(THREAD_COUNT, move || { + let start = Instant::now(); + for _ in 0..count { + trace!("A dummy log!"); + } + start.elapsed() + }); + + let total_time: Duration = durations.into_iter().sum(); + Duration::from_nanos((total_time.as_nanos() / THREAD_COUNT as u128) as u64) + }) + }); +} + +criterion_group!(benches, bench_logger); +criterion_main!(benches); diff --git a/vendor/tracing-log/src/env_logger.rs b/vendor/tracing-log/src/env_logger.rs new file mode 100644 index 000000000..74922d1dc --- /dev/null +++ b/vendor/tracing-log/src/env_logger.rs @@ -0,0 +1,49 @@ +//! Utilities for configuring the `env_logger` crate to emit `tracing` events. + +/// Extension trait to configure an `env_logger::Builder` to emit traces. +pub trait BuilderExt: crate::sealed::Sealed { + /// Configure the built `env_logger::Logger` to emit `tracing` events for + /// all consumed `log` records, rather than printing them to standard out. + /// + /// Note that this replaces any previously configured formatting. + fn emit_traces(&mut self) -> &mut Self; +} + +impl crate::sealed::Sealed for env_logger::Builder {} + +impl BuilderExt for env_logger::Builder { + fn emit_traces(&mut self) -> &mut Self { + self.format(|_, record| super::format_trace(record)) + } +} + +/// Attempts to initialize the global logger with an env logger configured to +/// emit `tracing` events. +/// +/// This should be called early in the execution of a Rust program. Any log +/// events that occur before initialization will be ignored. +/// +/// # Errors +/// +/// This function will fail if it is called more than once, or if another +/// library has already initialized a global logger. +pub fn try_init() -> Result<(), log::SetLoggerError> { + env_logger::Builder::from_default_env() + .emit_traces() + .try_init() +} + +/// Initializes the global logger with an env logger configured to +/// emit `tracing` events. +/// +/// This should be called early in the execution of a Rust program. Any log +/// events that occur before initialization will be ignored. +/// +/// # Panics +/// +/// This function will panic if it is called more than once, or if another +/// library has already initialized a global logger. +pub fn init() { + try_init() + .expect("tracing_log::env_logger::init should not be called after logger initialized"); +} diff --git a/vendor/tracing-log/src/interest_cache.rs b/vendor/tracing-log/src/interest_cache.rs new file mode 100644 index 000000000..fb3da875e --- /dev/null +++ b/vendor/tracing-log/src/interest_cache.rs @@ -0,0 +1,542 @@ +use ahash::AHasher; +use log::{Level, Metadata}; +use lru::LruCache; +use std::cell::RefCell; +use std::hash::Hasher; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Mutex; + +/// The interest cache configuration. +#[derive(Debug)] +pub struct InterestCacheConfig { + min_verbosity: Level, + lru_cache_size: usize, +} + +impl Default for InterestCacheConfig { + fn default() -> Self { + InterestCacheConfig { + min_verbosity: Level::Debug, + lru_cache_size: 1024, + } + } +} + +impl InterestCacheConfig { + fn disabled() -> Self { + Self { + lru_cache_size: 0, + ..Self::default() + } + } +} + +impl InterestCacheConfig { + /// Sets the minimum logging verbosity for which the cache will apply. + /// + /// The interest for logs with a lower verbosity than specified here + /// will not be cached. + /// + /// It should be set to the lowest verbosity level for which the majority + /// of the logs in your application are usually *disabled*. + /// + /// In normal circumstances with typical logger usage patterns + /// you shouldn't ever have to change this. + /// + /// By default this is set to `Debug`. + pub fn with_min_verbosity(mut self, level: Level) -> Self { + self.min_verbosity = level; + self + } + + /// Sets the number of entries in the LRU cache used to cache interests + /// for `log` records. + /// + /// The bigger the cache, the more unlikely it will be for the interest + /// in a given callsite to be recalculated, at the expense of extra + /// memory usage per every thread which tries to log events. + /// + /// Every unique [level] + [target] pair consumes a single slot + /// in the cache. Entries will be added to the cache until its size + /// reaches the value configured here, and from then on it will evict + /// the least recently seen level + target pair when adding a new entry. + /// + /// The ideal value to set here widely depends on how much exactly + /// you're logging, and how diverse the targets are to which you are logging. + /// + /// If your application spends a significant amount of time filtering logs + /// which are *not* getting printed out then increasing this value will most + /// likely help. + /// + /// Setting this to zero will disable the cache. + /// + /// By default this is set to 1024. + /// + /// [level]: log::Metadata::level + /// [target]: log::Metadata::target + pub fn with_lru_cache_size(mut self, size: usize) -> Self { + self.lru_cache_size = size; + self + } +} + +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +struct Key { + target_address: usize, + level_and_length: usize, +} + +struct State { + min_verbosity: Level, + epoch: usize, + cache: LruCache<Key, u64, ahash::RandomState>, +} + +impl State { + fn new(epoch: usize, config: &InterestCacheConfig) -> Self { + State { + epoch, + min_verbosity: config.min_verbosity, + cache: LruCache::new(config.lru_cache_size), + } + } +} + +// When the logger's filters are reconfigured the interest cache in core is cleared, +// and we also want to get notified when that happens so that we can clear our cache too. +// +// So what we do here is to register a dummy callsite with the core, just so that we can be +// notified when that happens. It doesn't really matter how exactly our dummy callsite looks +// like and whether subscribers will actually be interested in it, since nothing will actually +// be logged from it. + +static INTEREST_CACHE_EPOCH: AtomicUsize = AtomicUsize::new(0); + +fn interest_cache_epoch() -> usize { + INTEREST_CACHE_EPOCH.load(Ordering::Relaxed) +} + +struct SentinelCallsite; + +impl tracing_core::Callsite for SentinelCallsite { + fn set_interest(&self, _: tracing_core::subscriber::Interest) { + INTEREST_CACHE_EPOCH.fetch_add(1, Ordering::SeqCst); + } + + fn metadata(&self) -> &tracing_core::Metadata<'_> { + &SENTINEL_METADATA + } +} + +static SENTINEL_CALLSITE: SentinelCallsite = SentinelCallsite; +static SENTINEL_METADATA: tracing_core::Metadata<'static> = tracing_core::Metadata::new( + "log interest cache", + "log", + tracing_core::Level::ERROR, + None, + None, + None, + tracing_core::field::FieldSet::new(&[], tracing_core::identify_callsite!(&SENTINEL_CALLSITE)), + tracing_core::metadata::Kind::EVENT, +); + +lazy_static::lazy_static! { + static ref CONFIG: Mutex<InterestCacheConfig> = { + tracing_core::callsite::register(&SENTINEL_CALLSITE); + Mutex::new(InterestCacheConfig::disabled()) + }; +} + +thread_local! { + static STATE: RefCell<State> = { + let config = CONFIG.lock().unwrap(); + RefCell::new(State::new(interest_cache_epoch(), &config)) + }; +} + +pub(crate) fn configure(new_config: Option<InterestCacheConfig>) { + *CONFIG.lock().unwrap() = new_config.unwrap_or_else(InterestCacheConfig::disabled); + INTEREST_CACHE_EPOCH.fetch_add(1, Ordering::SeqCst); +} + +pub(crate) fn try_cache(metadata: &Metadata<'_>, callback: impl FnOnce() -> bool) -> bool { + STATE.with(|state| { + let mut state = state.borrow_mut(); + + // If the interest cache in core was rebuilt we need to reset the cache here too. + let epoch = interest_cache_epoch(); + if epoch != state.epoch { + *state = State::new(epoch, &CONFIG.lock().unwrap()); + } + + let level = metadata.level(); + if state.cache.cap() == 0 || level < state.min_verbosity { + return callback(); + } + + let target = metadata.target(); + + let mut hasher = AHasher::default(); + hasher.write(target.as_bytes()); + + const HASH_MASK: u64 = !1; + const INTEREST_MASK: u64 = 1; + + // We mask out the least significant bit of the hash since we'll use + // that space to save the interest. + // + // Since we use a good hashing function the loss of only a single bit + // won't really affect us negatively. + let target_hash = hasher.finish() & HASH_MASK; + + // Since log targets are usually static strings we just use the address of the pointer + // as the key for our cache. + // + // We want each level to be cached separately so we also use the level as key, and since + // some linkers at certain optimization levels deduplicate strings if their prefix matches + // (e.g. "ham" and "hamster" might actually have the same address in memory) we also use the length. + let key = Key { + target_address: target.as_ptr() as usize, + // For extra efficiency we pack both the level and the length into a single field. + // The `level` can be between 1 and 5, so it can take at most 3 bits of space. + level_and_length: level as usize | target.len().wrapping_shl(3), + }; + + if let Some(&cached) = state.cache.get(&key) { + // And here we make sure that the target actually matches. + // + // This is just a hash of the target string, so theoretically we're not guaranteed + // that it won't collide, however in practice it shouldn't matter as it is quite + // unlikely that the target string's address and its length and the level and + // the hash will *all* be equal at the same time. + // + // We could of course actually store the whole target string in our cache, + // but we really want to avoid doing that as the necessary memory allocations + // would completely tank our performance, especially in cases where the cache's + // size is too small so it needs to regularly replace entries. + if cached & HASH_MASK == target_hash { + return (cached & INTEREST_MASK) != 0; + } + + // Realistically we should never land here, unless someone is using a non-static + // target string with the same length and level, or is very lucky and found a hash + // collision for the cache's key. + } + + let interest = callback(); + state.cache.put(key, target_hash | interest as u64); + + interest + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn lock_for_test() -> impl Drop { + // We need to make sure only one test runs at a time. + + lazy_static::lazy_static! { + static ref LOCK: Mutex<()> = Mutex::new(()); + } + + match LOCK.lock() { + Ok(guard) => guard, + Err(poison) => poison.into_inner(), + } + } + + #[test] + fn test_when_disabled_the_callback_is_always_called() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::disabled(); + + std::thread::spawn(|| { + let metadata = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy") + .build(); + let mut count = 0; + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 1); + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 2); + }) + .join() + .unwrap(); + } + + #[test] + fn test_when_enabled_the_callback_is_called_only_once_for_a_high_enough_verbosity() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default().with_min_verbosity(Level::Debug); + + std::thread::spawn(|| { + let metadata = log::MetadataBuilder::new() + .level(Level::Debug) + .target("dummy") + .build(); + let mut count = 0; + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 1); + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 1); + }) + .join() + .unwrap(); + } + + #[test] + fn test_when_core_interest_cache_is_rebuilt_this_cache_is_also_flushed() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default().with_min_verbosity(Level::Debug); + + std::thread::spawn(|| { + let metadata = log::MetadataBuilder::new() + .level(Level::Debug) + .target("dummy") + .build(); + { + let mut count = 0; + try_cache(&metadata, || { + count += 1; + true + }); + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 1); + } + tracing_core::callsite::rebuild_interest_cache(); + { + let mut count = 0; + try_cache(&metadata, || { + count += 1; + true + }); + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 1); + } + }) + .join() + .unwrap(); + } + + #[test] + fn test_when_enabled_the_callback_is_always_called_for_a_low_enough_verbosity() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default().with_min_verbosity(Level::Debug); + + std::thread::spawn(|| { + let metadata = log::MetadataBuilder::new() + .level(Level::Info) + .target("dummy") + .build(); + let mut count = 0; + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 1); + try_cache(&metadata, || { + count += 1; + true + }); + assert_eq!(count, 2); + }) + .join() + .unwrap(); + } + + #[test] + fn test_different_log_levels_are_cached_separately() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default().with_min_verbosity(Level::Debug); + + std::thread::spawn(|| { + let metadata_debug = log::MetadataBuilder::new() + .level(Level::Debug) + .target("dummy") + .build(); + let metadata_trace = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy") + .build(); + let mut count_debug = 0; + let mut count_trace = 0; + try_cache(&metadata_debug, || { + count_debug += 1; + true + }); + try_cache(&metadata_trace, || { + count_trace += 1; + true + }); + try_cache(&metadata_debug, || { + count_debug += 1; + true + }); + try_cache(&metadata_trace, || { + count_trace += 1; + true + }); + assert_eq!(count_debug, 1); + assert_eq!(count_trace, 1); + }) + .join() + .unwrap(); + } + + #[test] + fn test_different_log_targets_are_cached_separately() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default().with_min_verbosity(Level::Debug); + + std::thread::spawn(|| { + let metadata_1 = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy_1") + .build(); + let metadata_2 = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy_2") + .build(); + let mut count_1 = 0; + let mut count_2 = 0; + try_cache(&metadata_1, || { + count_1 += 1; + true + }); + try_cache(&metadata_2, || { + count_2 += 1; + true + }); + try_cache(&metadata_1, || { + count_1 += 1; + true + }); + try_cache(&metadata_2, || { + count_2 += 1; + true + }); + assert_eq!(count_1, 1); + assert_eq!(count_2, 1); + }) + .join() + .unwrap(); + } + + #[test] + fn test_when_cache_runs_out_of_space_the_callback_is_called_again() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default() + .with_min_verbosity(Level::Debug) + .with_lru_cache_size(1); + + std::thread::spawn(|| { + let metadata_1 = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy_1") + .build(); + let metadata_2 = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy_2") + .build(); + let mut count = 0; + try_cache(&metadata_1, || { + count += 1; + true + }); + try_cache(&metadata_1, || { + count += 1; + true + }); + assert_eq!(count, 1); + try_cache(&metadata_2, || true); + try_cache(&metadata_1, || { + count += 1; + true + }); + assert_eq!(count, 2); + }) + .join() + .unwrap(); + } + + #[test] + fn test_cache_returns_previously_computed_value() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default().with_min_verbosity(Level::Debug); + + std::thread::spawn(|| { + let metadata_1 = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy_1") + .build(); + let metadata_2 = log::MetadataBuilder::new() + .level(Level::Trace) + .target("dummy_2") + .build(); + try_cache(&metadata_1, || true); + assert_eq!(try_cache(&metadata_1, || { unreachable!() }), true); + try_cache(&metadata_2, || false); + assert_eq!(try_cache(&metadata_2, || { unreachable!() }), false); + }) + .join() + .unwrap(); + } + + #[test] + fn test_cache_handles_non_static_target_string() { + let _lock = lock_for_test(); + + *CONFIG.lock().unwrap() = InterestCacheConfig::default().with_min_verbosity(Level::Debug); + + std::thread::spawn(|| { + let mut target = *b"dummy_1"; + let metadata_1 = log::MetadataBuilder::new() + .level(Level::Trace) + .target(std::str::from_utf8(&target).unwrap()) + .build(); + + try_cache(&metadata_1, || true); + assert_eq!(try_cache(&metadata_1, || { unreachable!() }), true); + + *target.last_mut().unwrap() = b'2'; + let metadata_2 = log::MetadataBuilder::new() + .level(Level::Trace) + .target(std::str::from_utf8(&target).unwrap()) + .build(); + + try_cache(&metadata_2, || false); + assert_eq!(try_cache(&metadata_2, || { unreachable!() }), false); + }) + .join() + .unwrap(); + } +} diff --git a/vendor/tracing-log/src/lib.rs b/vendor/tracing-log/src/lib.rs new file mode 100644 index 000000000..09e9114a4 --- /dev/null +++ b/vendor/tracing-log/src/lib.rs @@ -0,0 +1,628 @@ +//! Adapters for connecting unstructured log records from the `log` crate into +//! the `tracing` ecosystem. +//! +//! # Overview +//! +//! [`tracing`] is a framework for instrumenting Rust programs with context-aware, +//! structured, event-based diagnostic information. This crate provides +//! compatibility layers for using `tracing` alongside the logging facade provided +//! by the [`log`] crate. +//! +//! This crate provides: +//! +//! - [`AsTrace`] and [`AsLog`] traits for converting between `tracing` and `log` types. +//! - [`LogTracer`], a [`log::Log`] implementation that consumes [`log::Record`]s +//! and outputs them as [`tracing::Event`]. +//! - An [`env_logger`] module, with helpers for using the [`env_logger` crate] +//! with `tracing` (optional, enabled by the `env-logger` feature). +//! +//! *Compiler support: [requires `rustc` 1.49+][msrv]* +//! +//! [msrv]: #supported-rust-versions +//! +//! # Usage +//! +//! ## Convert log records to tracing `Event`s +//! +//! To convert [`log::Record`]s as [`tracing::Event`]s, set `LogTracer` as the default +//! logger by calling its [`init`] or [`init_with_filter`] methods. +//! +//! ```rust +//! # use std::error::Error; +//! use tracing_log::LogTracer; +//! use log; +//! +//! # fn main() -> Result<(), Box<Error>> { +//! LogTracer::init()?; +//! +//! // will be available for Subscribers as a tracing Event +//! log::trace!("an example trace log"); +//! # Ok(()) +//! # } +//! ``` +//! +//! This conversion does not convert unstructured data in log records (such as +//! values passed as format arguments to the `log!` macro) to structured +//! `tracing` fields. However, it *does* attach these new events to to the +//! span that was currently executing when the record was logged. This is the +//! primary use-case for this library: making it possible to locate the log +//! records emitted by dependencies which use `log` within the context of a +//! trace. +//! +//! ## Convert tracing `Event`s to logs +//! +//! Enabling the ["log" and "log-always" feature flags][flags] on the `tracing` +//! crate will cause all `tracing` spans and events to emit `log::Record`s as +//! they occur. +//! +//! ## Caution: Mixing both conversions +//! +//! 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). +//! +//! 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]. +//! * `interest-cache`: makes it possible to configure an interest cache for +//! logs emitted through the `log` crate (see [`Builder::with_interest_cache`]); requires `std` +//! +//! ## Supported Rust Versions +//! +//! Tracing is built against the latest stable release. The minimum supported +//! version is 1.49. 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. +//! +//! [`init`]: LogTracer::init +//! [`init_with_filter`]: LogTracer::init_with_filter +//! [`tracing`]: https://crates.io/crates/tracing +//! [`env_logger` crate]: https://crates.io/crates/env-logger +//! [`tracing::Subscriber`]: https://docs.rs/tracing/latest/tracing/trait.Subscriber.html +//! [`Subscriber`]: https://docs.rs/tracing/latest/tracing/trait.Subscriber.html +//! [`tracing::Event`]: https://docs.rs/tracing/latest/tracing/struct.Event.html +//! [flags]: https://docs.rs/tracing/latest/tracing/#crate-feature-flags +//! [`Builder::with_interest_cache`]: log_tracer::Builder::with_interest_cache +#![doc(html_root_url = "https://docs.rs/tracing-log/0.1.3")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png", + issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" +)] +#![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::broken_intra_doc_links))] +#![warn( + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + unreachable_pub, + bad_style, + const_err, + dead_code, + improper_ctypes, + non_shorthand_field_patterns, + no_mangle_generic_items, + overflowing_literals, + path_statements, + patterns_in_fns_without_body, + private_in_public, + unconditional_recursion, + unused, + unused_allocation, + unused_comparisons, + unused_parens, + while_true +)] +use lazy_static::lazy_static; + +use std::{fmt, io}; + +use tracing_core::{ + callsite::{self, Callsite}, + dispatcher, + field::{self, Field, Visit}, + identify_callsite, + metadata::{Kind, Level}, + subscriber, Event, Metadata, +}; + +#[cfg(feature = "log-tracer")] +#[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; + +pub use log; + +#[cfg(all(feature = "interest-cache", feature = "log-tracer", feature = "std"))] +mod interest_cache; + +#[cfg(all(feature = "interest-cache", feature = "log-tracer", feature = "std"))] +#[cfg_attr( + docsrs, + doc(cfg(all(feature = "interest-cache", feature = "log-tracer", feature = "std"))) +)] +pub use crate::interest_cache::InterestCacheConfig; + +/// Format a log record as a trace event in the current span. +pub fn format_trace(record: &log::Record<'_>) -> io::Result<()> { + dispatch_record(record); + Ok(()) +} + +// XXX(eliza): this is factored out so that we don't have to deal with the pub +// function `format_trace`'s `Result` return type...maybe we should get rid of +// that in 0.2... +pub(crate) fn dispatch_record(record: &log::Record<'_>) { + dispatcher::get_default(|dispatch| { + let filter_meta = record.as_trace(); + if !dispatch.enabled(&filter_meta) { + return; + } + + let (_, keys, meta) = loglevel_to_cs(record.level()); + + let log_module = record.module_path(); + let log_file = record.file(); + let log_line = record.line(); + + let module = log_module.as_ref().map(|s| s as &dyn field::Value); + let file = log_file.as_ref().map(|s| s as &dyn field::Value); + let line = log_line.as_ref().map(|s| s as &dyn field::Value); + + dispatch.event(&Event::new( + meta, + &meta.fields().value_set(&[ + (&keys.message, Some(record.args() as &dyn field::Value)), + (&keys.target, Some(&record.target())), + (&keys.module, module), + (&keys.file, file), + (&keys.line, line), + ]), + )); + }); +} + +/// Trait implemented for `tracing` types that can be converted to a `log` +/// equivalent. +pub trait AsLog: crate::sealed::Sealed { + /// The `log` type that this type can be converted into. + type Log; + /// Returns the `log` equivalent of `self`. + fn as_log(&self) -> Self::Log; +} + +/// Trait implemented for `log` types that can be converted to a `tracing` +/// equivalent. +pub trait AsTrace: crate::sealed::Sealed { + /// The `tracing` type that this type can be converted into. + type Trace; + /// Returns the `tracing` equivalent of `self`. + fn as_trace(&self) -> Self::Trace; +} + +impl<'a> crate::sealed::Sealed for Metadata<'a> {} + +impl<'a> AsLog for Metadata<'a> { + type Log = log::Metadata<'a>; + fn as_log(&self) -> Self::Log { + log::Metadata::builder() + .level(self.level().as_log()) + .target(self.target()) + .build() + } +} +impl<'a> crate::sealed::Sealed for log::Metadata<'a> {} + +impl<'a> AsTrace for log::Metadata<'a> { + type Trace = Metadata<'a>; + fn as_trace(&self) -> Self::Trace { + let cs_id = identify_callsite!(loglevel_to_cs(self.level()).0); + Metadata::new( + "log record", + self.target(), + self.level().as_trace(), + None, + None, + None, + field::FieldSet::new(FIELD_NAMES, cs_id), + Kind::EVENT, + ) + } +} + +struct Fields { + message: field::Field, + target: field::Field, + module: field::Field, + file: field::Field, + line: field::Field, +} + +static FIELD_NAMES: &[&str] = &[ + "message", + "log.target", + "log.module_path", + "log.file", + "log.line", +]; + +impl Fields { + fn new(cs: &'static dyn Callsite) -> Self { + let fieldset = cs.metadata().fields(); + let message = fieldset.field("message").unwrap(); + let target = fieldset.field("log.target").unwrap(); + let module = fieldset.field("log.module_path").unwrap(); + let file = fieldset.field("log.file").unwrap(); + let line = fieldset.field("log.line").unwrap(); + Fields { + message, + target, + module, + file, + line, + } + } +} + +macro_rules! log_cs { + ($level:expr, $cs:ident, $meta:ident, $ty:ident) => { + struct $ty; + static $cs: $ty = $ty; + static $meta: Metadata<'static> = Metadata::new( + "log event", + "log", + $level, + None, + None, + None, + field::FieldSet::new(FIELD_NAMES, identify_callsite!(&$cs)), + Kind::EVENT, + ); + + impl callsite::Callsite for $ty { + fn set_interest(&self, _: subscriber::Interest) {} + fn metadata(&self) -> &'static Metadata<'static> { + &$meta + } + } + }; +} + +log_cs!( + tracing_core::Level::TRACE, + TRACE_CS, + TRACE_META, + TraceCallsite +); +log_cs!( + tracing_core::Level::DEBUG, + DEBUG_CS, + DEBUG_META, + DebugCallsite +); +log_cs!(tracing_core::Level::INFO, INFO_CS, INFO_META, InfoCallsite); +log_cs!(tracing_core::Level::WARN, WARN_CS, WARN_META, WarnCallsite); +log_cs!( + tracing_core::Level::ERROR, + ERROR_CS, + ERROR_META, + ErrorCallsite +); + +lazy_static! { + static ref TRACE_FIELDS: Fields = Fields::new(&TRACE_CS); + static ref DEBUG_FIELDS: Fields = Fields::new(&DEBUG_CS); + static ref INFO_FIELDS: Fields = Fields::new(&INFO_CS); + static ref WARN_FIELDS: Fields = Fields::new(&WARN_CS); + static ref ERROR_FIELDS: Fields = Fields::new(&ERROR_CS); +} + +fn level_to_cs(level: Level) -> (&'static dyn Callsite, &'static Fields) { + match level { + Level::TRACE => (&TRACE_CS, &*TRACE_FIELDS), + Level::DEBUG => (&DEBUG_CS, &*DEBUG_FIELDS), + Level::INFO => (&INFO_CS, &*INFO_FIELDS), + Level::WARN => (&WARN_CS, &*WARN_FIELDS), + Level::ERROR => (&ERROR_CS, &*ERROR_FIELDS), + } +} + +fn loglevel_to_cs( + level: log::Level, +) -> ( + &'static dyn Callsite, + &'static Fields, + &'static Metadata<'static>, +) { + match level { + log::Level::Trace => (&TRACE_CS, &*TRACE_FIELDS, &TRACE_META), + log::Level::Debug => (&DEBUG_CS, &*DEBUG_FIELDS, &DEBUG_META), + log::Level::Info => (&INFO_CS, &*INFO_FIELDS, &INFO_META), + log::Level::Warn => (&WARN_CS, &*WARN_FIELDS, &WARN_META), + log::Level::Error => (&ERROR_CS, &*ERROR_FIELDS, &ERROR_META), + } +} + +impl<'a> crate::sealed::Sealed for log::Record<'a> {} + +impl<'a> AsTrace for log::Record<'a> { + type Trace = Metadata<'a>; + fn as_trace(&self) -> Self::Trace { + let cs_id = identify_callsite!(loglevel_to_cs(self.level()).0); + Metadata::new( + "log record", + self.target(), + self.level().as_trace(), + self.file(), + self.line(), + self.module_path(), + field::FieldSet::new(FIELD_NAMES, cs_id), + Kind::EVENT, + ) + } +} + +impl crate::sealed::Sealed for tracing_core::Level {} + +impl AsLog for tracing_core::Level { + type Log = log::Level; + fn as_log(&self) -> log::Level { + match *self { + tracing_core::Level::ERROR => log::Level::Error, + tracing_core::Level::WARN => log::Level::Warn, + tracing_core::Level::INFO => log::Level::Info, + tracing_core::Level::DEBUG => log::Level::Debug, + tracing_core::Level::TRACE => log::Level::Trace, + } + } +} + +impl crate::sealed::Sealed for log::Level {} + +impl AsTrace for log::Level { + type Trace = tracing_core::Level; + #[inline] + fn as_trace(&self) -> tracing_core::Level { + match self { + log::Level::Error => tracing_core::Level::ERROR, + log::Level::Warn => tracing_core::Level::WARN, + log::Level::Info => tracing_core::Level::INFO, + log::Level::Debug => tracing_core::Level::DEBUG, + log::Level::Trace => tracing_core::Level::TRACE, + } + } +} + +impl crate::sealed::Sealed for log::LevelFilter {} + +impl AsTrace for log::LevelFilter { + type Trace = tracing_core::LevelFilter; + #[inline] + fn as_trace(&self) -> tracing_core::LevelFilter { + match self { + log::LevelFilter::Off => tracing_core::LevelFilter::OFF, + log::LevelFilter::Error => tracing_core::LevelFilter::ERROR, + log::LevelFilter::Warn => tracing_core::LevelFilter::WARN, + log::LevelFilter::Info => tracing_core::LevelFilter::INFO, + log::LevelFilter::Debug => tracing_core::LevelFilter::DEBUG, + log::LevelFilter::Trace => tracing_core::LevelFilter::TRACE, + } + } +} + +impl crate::sealed::Sealed for tracing_core::LevelFilter {} + +impl AsLog for tracing_core::LevelFilter { + type Log = log::LevelFilter; + #[inline] + fn as_log(&self) -> Self::Log { + match *self { + tracing_core::LevelFilter::OFF => log::LevelFilter::Off, + tracing_core::LevelFilter::ERROR => log::LevelFilter::Error, + tracing_core::LevelFilter::WARN => log::LevelFilter::Warn, + tracing_core::LevelFilter::INFO => log::LevelFilter::Info, + tracing_core::LevelFilter::DEBUG => log::LevelFilter::Debug, + tracing_core::LevelFilter::TRACE => log::LevelFilter::Trace, + } + } +} +/// Extends log `Event`s to provide complete `Metadata`. +/// +/// In `tracing-log`, an `Event` produced by a log (through [`AsTrace`]) has an hard coded +/// "log" target and no `file`, `line`, or `module_path` attributes. This happens because `Event` +/// requires its `Metadata` to be `'static`, while [`log::Record`]s provide them with a generic +/// lifetime. +/// +/// However, these values are stored in the `Event`'s fields and +/// the [`normalized_metadata`] method allows to build a new `Metadata` +/// that only lives as long as its source `Event`, but provides complete +/// data. +/// +/// It can typically be used by `Subscriber`s when processing an `Event`, +/// to allow accessing its complete metadata in a consistent way, +/// regardless of the source of its source. +/// +/// [`normalized_metadata`]: NormalizeEvent#normalized_metadata +pub trait NormalizeEvent<'a>: crate::sealed::Sealed { + /// If this `Event` comes from a `log`, this method provides a new + /// normalized `Metadata` which has all available attributes + /// from the original log, including `file`, `line`, `module_path` + /// and `target`. + /// Returns `None` is the `Event` is not issued from a `log`. + fn normalized_metadata(&'a self) -> Option<Metadata<'a>>; + /// Returns whether this `Event` represents a log (from the `log` crate) + fn is_log(&self) -> bool; +} + +impl<'a> crate::sealed::Sealed for Event<'a> {} + +impl<'a> NormalizeEvent<'a> for Event<'a> { + fn normalized_metadata(&'a self) -> Option<Metadata<'a>> { + let original = self.metadata(); + if self.is_log() { + let mut fields = LogVisitor::new_for(self, level_to_cs(*original.level()).1); + self.record(&mut fields); + + Some(Metadata::new( + "log event", + fields.target.unwrap_or("log"), + *original.level(), + fields.file, + fields.line.map(|l| l as u32), + fields.module_path, + field::FieldSet::new(&["message"], original.callsite()), + Kind::EVENT, + )) + } else { + None + } + } + + fn is_log(&self) -> bool { + self.metadata().callsite() == identify_callsite!(level_to_cs(*self.metadata().level()).0) + } +} + +struct LogVisitor<'a> { + target: Option<&'a str>, + module_path: Option<&'a str>, + file: Option<&'a str>, + line: Option<u64>, + fields: &'static Fields, +} + +impl<'a> LogVisitor<'a> { + // We don't actually _use_ the provided event argument; it is simply to + // ensure that the `LogVisitor` does not outlive the event whose fields it + // is visiting, so that the reference casts in `record_str` are safe. + fn new_for(_event: &'a Event<'a>, fields: &'static Fields) -> Self { + Self { + target: None, + module_path: None, + file: None, + line: None, + fields, + } + } +} + +impl<'a> Visit for LogVisitor<'a> { + fn record_debug(&mut self, _field: &Field, _value: &dyn fmt::Debug) {} + + fn record_u64(&mut self, field: &Field, value: u64) { + if field == &self.fields.line { + self.line = Some(value); + } + } + + fn record_str(&mut self, field: &Field, value: &str) { + unsafe { + // The `Visit` API erases the string slice's lifetime. However, we + // know it is part of the `Event` struct with a lifetime of `'a`. If + // (and only if!) this `LogVisitor` was constructed with the same + // lifetime parameter `'a` as the event in question, it's safe to + // cast these string slices to the `'a` lifetime. + if field == &self.fields.file { + self.file = Some(&*(value as *const _)); + } else if field == &self.fields.target { + self.target = Some(&*(value as *const _)); + } else if field == &self.fields.module { + self.module_path = Some(&*(value as *const _)); + } + } + } +} + +mod sealed { + pub trait Sealed {} +} + +#[cfg(test)] +mod test { + use super::*; + + fn test_callsite(level: log::Level) { + let record = log::Record::builder() + .args(format_args!("Error!")) + .level(level) + .target("myApp") + .file(Some("server.rs")) + .line(Some(144)) + .module_path(Some("server")) + .build(); + + let meta = record.as_trace(); + let (cs, _keys, _) = loglevel_to_cs(record.level()); + let cs_meta = cs.metadata(); + assert_eq!( + meta.callsite(), + cs_meta.callsite(), + "actual: {:#?}\nexpected: {:#?}", + meta, + cs_meta + ); + assert_eq!(meta.level(), &level.as_trace()); + } + + #[test] + fn error_callsite_is_correct() { + test_callsite(log::Level::Error); + } + + #[test] + fn warn_callsite_is_correct() { + test_callsite(log::Level::Warn); + } + + #[test] + fn info_callsite_is_correct() { + test_callsite(log::Level::Info); + } + + #[test] + fn debug_callsite_is_correct() { + test_callsite(log::Level::Debug); + } + + #[test] + fn trace_callsite_is_correct() { + test_callsite(log::Level::Trace); + } +} diff --git a/vendor/tracing-log/src/log_tracer.rs b/vendor/tracing-log/src/log_tracer.rs new file mode 100644 index 000000000..6b24dab10 --- /dev/null +++ b/vendor/tracing-log/src/log_tracer.rs @@ -0,0 +1,307 @@ +//! An adapter for converting [`log`] records into `tracing` `Event`s. +//! +//! This module provides the [`LogTracer`] type which implements `log`'s [logger +//! interface] by recording log records as `tracing` `Event`s. This is intended for +//! use in conjunction with a `tracing` `Subscriber` to consume events from +//! dependencies that emit [`log`] records within a trace context. +//! +//! # Usage +//! +//! To create and initialize a `LogTracer` with the default configurations, use: +//! +//! * [`init`] if you want to convert all logs, regardless of log level, +//! allowing the tracing `Subscriber` to perform any filtering +//! * [`init_with_filter`] to convert all logs up to a specified log level +//! +//! In addition, a [builder] is available for cases where more advanced +//! configuration is required. In particular, the builder can be used to [ignore +//! log records][ignore] emitted by particular crates. This is useful in cases +//! such as when a crate emits both `tracing` diagnostics _and_ log records by +//! default. +//! +//! [logger interface]: log::Log +//! [`init`]: LogTracer.html#method.init +//! [`init_with_filter`]: LogTracer.html#method.init_with_filter +//! [builder]: LogTracer::builder() +//! [ignore]: Builder::ignore_crate() +use crate::AsTrace; +pub use log::SetLoggerError; +use tracing_core::dispatcher; + +/// A simple "logger" that converts all log records into `tracing` `Event`s. +#[derive(Debug)] +pub struct LogTracer { + ignore_crates: Box<[String]>, +} + +/// Configures a new `LogTracer`. +#[derive(Debug)] +pub struct Builder { + ignore_crates: Vec<String>, + filter: log::LevelFilter, + #[cfg(all(feature = "interest-cache", feature = "std"))] + interest_cache_config: Option<crate::InterestCacheConfig>, +} + +// ===== impl LogTracer ===== + +impl LogTracer { + /// Returns a builder that allows customizing a `LogTracer` and setting it + /// the default logger. + /// + /// For example: + /// ```rust + /// # use std::error::Error; + /// use tracing_log::LogTracer; + /// use log; + /// + /// # fn main() -> Result<(), Box<Error>> { + /// LogTracer::builder() + /// .ignore_crate("foo") // suppose the `foo` crate is using `tracing`'s log feature + /// .with_max_level(log::LevelFilter::Info) + /// .init()?; + /// + /// // will be available for Subscribers as a tracing Event + /// log::info!("an example info log"); + /// # Ok(()) + /// # } + /// ``` + pub fn builder() -> Builder { + Builder::default() + } + + /// Creates a new `LogTracer` that can then be used as a logger for the `log` crate. + /// + /// It is generally simpler to use the [`init`] or [`init_with_filter`] methods + /// which will create the `LogTracer` and set it as the global logger. + /// + /// Logger setup without the initialization methods can be done with: + /// + /// ```rust + /// # use std::error::Error; + /// use tracing_log::LogTracer; + /// use log; + /// + /// # fn main() -> Result<(), Box<Error>> { + /// let logger = LogTracer::new(); + /// log::set_boxed_logger(Box::new(logger))?; + /// log::set_max_level(log::LevelFilter::Trace); + /// + /// // will be available for Subscribers as a tracing Event + /// log::trace!("an example trace log"); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`init`]: LogTracer::init() + /// [`init_with_filter`]: .#method.init_with_filter + pub fn new() -> Self { + Self { + ignore_crates: Vec::new().into_boxed_slice(), + } + } + + /// Sets up `LogTracer` as global logger for the `log` crate, + /// with the given level as max level filter. + /// + /// Setting a global logger can only be done once. + /// + /// The [`builder`] function can be used to customize the `LogTracer` before + /// initializing it. + /// + /// [`builder`]: LogTracer::builder() + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + pub fn init_with_filter(level: log::LevelFilter) -> Result<(), SetLoggerError> { + Self::builder().with_max_level(level).init() + } + + /// Sets a `LogTracer` as the global logger for the `log` crate. + /// + /// Setting a global logger can only be done once. + /// + /// ```rust + /// # use std::error::Error; + /// use tracing_log::LogTracer; + /// use log; + /// + /// # fn main() -> Result<(), Box<Error>> { + /// LogTracer::init()?; + /// + /// // will be available for Subscribers as a tracing Event + /// log::trace!("an example trace log"); + /// # Ok(()) + /// # } + /// ``` + /// + /// This will forward all logs to `tracing` and lets the current `Subscriber` + /// determine if they are enabled. + /// + /// The [`builder`] function can be used to customize the `LogTracer` before + /// initializing it. + /// + /// If you know in advance you want to filter some log levels, + /// use [`builder`] or [`init_with_filter`] instead. + /// + /// [`init_with_filter`]: LogTracer::init_with_filter() + /// [`builder`]: LogTracer::builder() + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + pub fn init() -> Result<(), SetLoggerError> { + Self::builder().init() + } +} + +impl Default for LogTracer { + fn default() -> Self { + Self::new() + } +} + +#[cfg(all(feature = "interest-cache", feature = "std"))] +use crate::interest_cache::try_cache as try_cache_interest; + +#[cfg(not(all(feature = "interest-cache", feature = "std")))] +fn try_cache_interest(_: &log::Metadata<'_>, callback: impl FnOnce() -> bool) -> bool { + callback() +} + +impl log::Log for LogTracer { + fn enabled(&self, metadata: &log::Metadata<'_>) -> bool { + // First, check the log record against the current max level enabled by + // the current `tracing` subscriber. + if metadata.level().as_trace() > tracing_core::LevelFilter::current() { + // If the log record's level is above that, disable it. + return false; + } + + // Okay, it wasn't disabled by the max level — do we have any specific + // modules to ignore? + if !self.ignore_crates.is_empty() { + // If we are ignoring certain module paths, ensure that the metadata + // does not start with one of those paths. + let target = metadata.target(); + for ignored in &self.ignore_crates[..] { + if target.starts_with(ignored) { + return false; + } + } + } + + try_cache_interest(metadata, || { + // Finally, check if the current `tracing` dispatcher cares about this. + dispatcher::get_default(|dispatch| dispatch.enabled(&metadata.as_trace())) + }) + } + + fn log(&self, record: &log::Record<'_>) { + if self.enabled(record.metadata()) { + crate::dispatch_record(record); + } + } + + fn flush(&self) {} +} + +// ===== impl Builder ===== + +impl Builder { + /// Returns a new `Builder` to construct a [`LogTracer`]. + /// + pub fn new() -> Self { + Self::default() + } + + /// Sets a global maximum level for `log` records. + /// + /// Log records whose level is more verbose than the provided level will be + /// disabled. + /// + /// By default, all `log` records will be enabled. + pub fn with_max_level(self, filter: impl Into<log::LevelFilter>) -> Self { + let filter = filter.into(); + Self { filter, ..self } + } + + /// Configures the `LogTracer` to ignore all log records whose target + /// starts with the given string. + /// + /// This should be used when a crate enables the `tracing/log` feature to + /// emit log records for tracing events. Otherwise, those events will be + /// recorded twice. + pub fn ignore_crate(mut self, name: impl Into<String>) -> Self { + self.ignore_crates.push(name.into()); + self + } + + /// Configures the `LogTracer` to ignore all log records whose target + /// starts with any of the given the given strings. + /// + /// This should be used when a crate enables the `tracing/log` feature to + /// emit log records for tracing events. Otherwise, those events will be + /// recorded twice. + pub fn ignore_all<I>(self, crates: impl IntoIterator<Item = I>) -> Self + where + I: Into<String>, + { + crates.into_iter().fold(self, Self::ignore_crate) + } + + /// Configures the `LogTracer` to either disable or enable the interest cache. + /// + /// When enabled, a per-thread LRU cache will be used to cache whenever the logger + /// is interested in a given [level] + [target] pair for records generated through + /// the `log` crate. + /// + /// When no `trace!` logs are enabled the logger is able to cheaply filter + /// them out just by comparing their log level to the globally specified + /// maximum, and immediately reject them. When *any* other `trace!` log is + /// enabled (even one which doesn't actually exist!) the logger has to run + /// its full filtering machinery on each and every `trace!` log, which can + /// potentially be very expensive. + /// + /// Enabling this cache is useful in such situations to improve performance. + /// + /// You most likely do not want to enabled this if you have registered any dynamic + /// filters on your logger and you want them to be run every time. + /// + /// This is disabled by default. + /// + /// [level]: log::Metadata::level + /// [target]: log::Metadata::target + #[cfg(all(feature = "interest-cache", feature = "std"))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "interest-cache", feature = "std"))))] + pub fn with_interest_cache(mut self, config: crate::InterestCacheConfig) -> Self { + self.interest_cache_config = Some(config); + self + } + + /// Constructs a new `LogTracer` with the provided configuration and sets it + /// as the default logger. + /// + /// Setting a global logger can only be done once. + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + #[allow(unused_mut)] + pub fn init(mut self) -> Result<(), SetLoggerError> { + #[cfg(all(feature = "interest-cache", feature = "std"))] + crate::interest_cache::configure(self.interest_cache_config.take()); + + let ignore_crates = self.ignore_crates.into_boxed_slice(); + let logger = Box::new(LogTracer { ignore_crates }); + log::set_boxed_logger(logger)?; + log::set_max_level(self.filter); + Ok(()) + } +} + +impl Default for Builder { + fn default() -> Self { + Self { + ignore_crates: Vec::new(), + filter: log::LevelFilter::max(), + #[cfg(all(feature = "interest-cache", feature = "std"))] + interest_cache_config: None, + } + } +} diff --git a/vendor/tracing-log/src/trace_logger.rs b/vendor/tracing-log/src/trace_logger.rs new file mode 100644 index 000000000..88a52f752 --- /dev/null +++ b/vendor/tracing-log/src/trace_logger.rs @@ -0,0 +1,458 @@ +//! A `tracing` [`Subscriber`] that uses the [`log`] crate as a backend for +//! formatting `tracing` spans and events. +//! +//! When a [`TraceLogger`] is set as the current subscriber, it will record +//! traces by emitting [`log::Record`]s that can be collected by a logger. +//! +//! **Note**: This API has been deprecated since version 0.1.1. In order to emit +//! `tracing` events as `log` records, the ["log" and "log-always" feature +//! flags][flags] on the `tracing` crate should be used instead. +//! +//! [`log`]: log +//! [`Subscriber`]: https://docs.rs/tracing/0.1.7/tracing/subscriber/trait.Subscriber.html +//! [`log::Record`]:log::Record +//! [flags]: https://docs.rs/tracing/latest/tracing/#crate-feature-flags +#![deprecated( + since = "0.1.1", + note = "use the `tracing` crate's \"log\" feature flag instead" +)] +use crate::AsLog; +use std::{ + cell::RefCell, + collections::HashMap, + fmt::{self, Write}, + sync::{ + atomic::{AtomicUsize, Ordering}, + Mutex, + }, +}; +use tracing_core::{ + field, + span::{self, Id}, + Event, Metadata, Subscriber, +}; + +/// A `tracing` [`Subscriber`] implementation that logs all recorded +/// trace events. +/// +/// **Note**: This API has been deprecated since version 0.1.1. In order to emit +/// `tracing` events as `log` records, the ["log" and "log-always" feature +/// flags][flags] on the `tracing` crate should be used instead. +/// +/// [`Subscriber`]: https://docs.rs/tracing/0.1.7/tracing/subscriber/trait.Subscriber.html +/// [flags]: https://docs.rs/tracing/latest/tracing/#crate-feature-flags +pub struct TraceLogger { + settings: Builder, + spans: Mutex<HashMap<Id, SpanLineBuilder>>, + next_id: AtomicUsize, +} + +thread_local! { + static CURRENT: RefCell<Vec<Id>> = RefCell::new(Vec::new()); +} +/// Configures and constructs a [`TraceLogger`]. +/// +#[derive(Debug)] +pub struct Builder { + log_span_closes: bool, + log_enters: bool, + log_exits: bool, + log_ids: bool, + parent_fields: bool, + log_parent: bool, +} + +// ===== impl TraceLogger ===== + +impl TraceLogger { + /// Returns a new `TraceLogger` with the default configuration. + pub fn new() -> Self { + Self::builder().finish() + } + + /// Returns a `Builder` for configuring a `TraceLogger`. + pub fn builder() -> Builder { + Default::default() + } + + fn from_builder(settings: Builder) -> Self { + Self { + settings, + ..Default::default() + } + } + + fn next_id(&self) -> Id { + Id::from_u64(self.next_id.fetch_add(1, Ordering::SeqCst) as u64) + } +} + +// ===== impl Builder ===== + +impl Builder { + /// Configures whether or not the [`TraceLogger`] being constructed will log + /// when a span closes. + /// + pub fn with_span_closes(self, log_span_closes: bool) -> Self { + Self { + log_span_closes, + ..self + } + } + + /// Configures whether or not the [`TraceLogger`] being constructed will + /// include the fields of parent spans when formatting events. + /// + pub fn with_parent_fields(self, parent_fields: bool) -> Self { + Self { + parent_fields, + ..self + } + } + + /// Configures whether or not the [`TraceLogger`] being constructed will log + /// when a span is entered. + /// + /// If this is set to false, fields from the current span will still be + /// recorded as context, but the actual entry will not create a log record. + /// + pub fn with_span_entry(self, log_enters: bool) -> Self { + Self { log_enters, ..self } + } + + /// Configures whether or not the [`TraceLogger`] being constructed will log + /// when a span is exited. + /// + pub fn with_span_exits(self, log_exits: bool) -> Self { + Self { log_exits, ..self } + } + + /// Configures whether or not the [`TraceLogger`] being constructed will + /// include span IDs when formatting log output. + /// + pub fn with_ids(self, log_ids: bool) -> Self { + Self { log_ids, ..self } + } + + /// Configures whether or not the [`TraceLogger`] being constructed will + /// include the names of parent spans as context when formatting events. + /// + pub fn with_parent_names(self, log_parent: bool) -> Self { + Self { log_parent, ..self } + } + + /// Complete the builder, returning a configured [`TraceLogger`]. + /// + pub fn finish(self) -> TraceLogger { + TraceLogger::from_builder(self) + } +} + +impl Default for Builder { + fn default() -> Self { + Builder { + log_span_closes: false, + parent_fields: true, + log_exits: false, + log_ids: false, + log_parent: true, + log_enters: false, + } + } +} + +impl Default for TraceLogger { + fn default() -> Self { + TraceLogger { + settings: Default::default(), + spans: Default::default(), + next_id: AtomicUsize::new(1), + } + } +} + +#[derive(Debug)] +struct SpanLineBuilder { + parent: Option<Id>, + ref_count: usize, + fields: String, + file: Option<String>, + line: Option<u32>, + module_path: Option<String>, + target: String, + level: log::Level, + name: &'static str, +} + +impl SpanLineBuilder { + fn new(parent: Option<Id>, meta: &Metadata<'_>, fields: String) -> Self { + Self { + parent, + ref_count: 1, + fields, + file: meta.file().map(String::from), + line: meta.line(), + module_path: meta.module_path().map(String::from), + target: String::from(meta.target()), + level: meta.level().as_log(), + name: meta.name(), + } + } + + fn log_meta(&self) -> log::Metadata<'_> { + log::MetadataBuilder::new() + .level(self.level) + .target(self.target.as_ref()) + .build() + } + + fn finish(self) { + let log_meta = self.log_meta(); + let logger = log::logger(); + if logger.enabled(&log_meta) { + logger.log( + &log::Record::builder() + .metadata(log_meta) + .target(self.target.as_ref()) + .module_path(self.module_path.as_ref().map(String::as_ref)) + .file(self.file.as_ref().map(String::as_ref)) + .line(self.line) + .args(format_args!("close {}; {}", self.name, self.fields)) + .build(), + ); + } + } +} + +impl field::Visit for SpanLineBuilder { + fn record_debug(&mut self, field: &field::Field, value: &dyn fmt::Debug) { + write!(self.fields, " {}={:?};", field.name(), value) + .expect("write to string should never fail") + } +} + +impl Subscriber for TraceLogger { + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + log::logger().enabled(&metadata.as_log()) + } + + fn new_span(&self, attrs: &span::Attributes<'_>) -> Id { + let id = self.next_id(); + let mut spans = self.spans.lock().unwrap(); + let mut fields = String::new(); + let parent = self.current_id(); + if self.settings.parent_fields { + let mut next_parent = parent.as_ref(); + while let Some(parent) = next_parent.and_then(|p| spans.get(p)) { + write!(&mut fields, "{}", parent.fields).expect("write to string cannot fail"); + next_parent = parent.parent.as_ref(); + } + } + let mut span = SpanLineBuilder::new(parent, attrs.metadata(), fields); + attrs.record(&mut span); + spans.insert(id.clone(), span); + id + } + + fn record(&self, span: &Id, values: &span::Record<'_>) { + let mut spans = self.spans.lock().unwrap(); + if let Some(span) = spans.get_mut(span) { + values.record(span); + } + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + // TODO: this should eventually track the relationship? + log::logger().log( + &log::Record::builder() + .level(log::Level::Trace) + .args(format_args!("span {:?} follows_from={:?};", span, follows)) + .build(), + ); + } + + fn enter(&self, id: &Id) { + let _ = CURRENT.try_with(|current| { + let mut current = current.borrow_mut(); + if current.contains(id) { + // Ignore duplicate enters. + return; + } + current.push(id.clone()); + }); + let spans = self.spans.lock().unwrap(); + if self.settings.log_enters { + if let Some(span) = spans.get(id) { + let log_meta = span.log_meta(); + let logger = log::logger(); + if logger.enabled(&log_meta) { + let current_id = self.current_id(); + let current_fields = current_id + .as_ref() + .and_then(|id| spans.get(id)) + .map(|span| span.fields.as_ref()) + .unwrap_or(""); + if self.settings.log_ids { + logger.log( + &log::Record::builder() + .metadata(log_meta) + .target(span.target.as_ref()) + .module_path(span.module_path.as_ref().map(String::as_ref)) + .file(span.file.as_ref().map(String::as_ref)) + .line(span.line) + .args(format_args!( + "enter {}; in={:?}; {}", + span.name, current_id, current_fields + )) + .build(), + ); + } else { + logger.log( + &log::Record::builder() + .metadata(log_meta) + .target(span.target.as_ref()) + .module_path(span.module_path.as_ref().map(String::as_ref)) + .file(span.file.as_ref().map(String::as_ref)) + .line(span.line) + .args(format_args!("enter {}; {}", span.name, current_fields)) + .build(), + ); + } + } + } + } + } + + fn exit(&self, id: &Id) { + let _ = CURRENT.try_with(|current| { + let mut current = current.borrow_mut(); + if current.last() == Some(id) { + current.pop() + } else { + None + } + }); + if self.settings.log_exits { + let spans = self.spans.lock().unwrap(); + if let Some(span) = spans.get(id) { + let log_meta = span.log_meta(); + let logger = log::logger(); + if logger.enabled(&log_meta) { + logger.log( + &log::Record::builder() + .metadata(log_meta) + .target(span.target.as_ref()) + .module_path(span.module_path.as_ref().map(String::as_ref)) + .file(span.file.as_ref().map(String::as_ref)) + .line(span.line) + .args(format_args!("exit {}", span.name)) + .build(), + ); + } + } + } + } + + fn event(&self, event: &Event<'_>) { + let meta = event.metadata(); + let log_meta = meta.as_log(); + let logger = log::logger(); + if logger.enabled(&log_meta) { + let spans = self.spans.lock().unwrap(); + let current = self.current_id().and_then(|id| spans.get(&id)); + let (current_fields, parent) = current + .map(|span| { + let fields = span.fields.as_ref(); + let parent = if self.settings.log_parent { + Some(span.name) + } else { + None + }; + (fields, parent) + }) + .unwrap_or(("", None)); + logger.log( + &log::Record::builder() + .metadata(log_meta) + .target(meta.target()) + .module_path(meta.module_path().as_ref().cloned()) + .file(meta.file().as_ref().cloned()) + .line(meta.line()) + .args(format_args!( + "{}{}{}{}", + parent.unwrap_or(""), + if parent.is_some() { ": " } else { "" }, + LogEvent(event), + current_fields, + )) + .build(), + ); + } + } + + fn clone_span(&self, id: &Id) -> Id { + let mut spans = self.spans.lock().unwrap(); + if let Some(span) = spans.get_mut(id) { + span.ref_count += 1; + } + id.clone() + } + + fn try_close(&self, id: Id) -> bool { + let mut spans = self.spans.lock().unwrap(); + if spans.contains_key(&id) { + if spans.get(&id).unwrap().ref_count == 1 { + let span = spans.remove(&id).unwrap(); + if self.settings.log_span_closes { + span.finish(); + } + return true; + } else { + spans.get_mut(&id).unwrap().ref_count -= 1; + } + } + false + } +} + +impl TraceLogger { + #[inline] + fn current_id(&self) -> Option<Id> { + CURRENT + .try_with(|current| current.borrow().last().map(|span| self.clone_span(span))) + .ok()? + } +} + +struct LogEvent<'a>(&'a Event<'a>); + +impl<'a> fmt::Display for LogEvent<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut has_logged = false; + let mut format_fields = |field: &field::Field, value: &dyn fmt::Debug| { + let name = field.name(); + let leading = if has_logged { " " } else { "" }; + // TODO: handle fmt error? + let _ = if name == "message" { + write!(f, "{}{:?};", leading, value) + } else { + write!(f, "{}{}={:?};", leading, name, value) + }; + has_logged = true; + }; + + self.0.record(&mut format_fields); + Ok(()) + } +} + +impl fmt::Debug for TraceLogger { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("TraceLogger") + .field("settings", &self.settings) + .field("spans", &self.spans) + .field("current", &self.current_id()) + .field("next_id", &self.next_id) + .finish() + } +} diff --git a/vendor/tracing-log/tests/log_tracer.rs b/vendor/tracing-log/tests/log_tracer.rs new file mode 100644 index 000000000..a1bee93b6 --- /dev/null +++ b/vendor/tracing-log/tests/log_tracer.rs @@ -0,0 +1,123 @@ +use std::sync::{Arc, Mutex}; +use tracing::subscriber::with_default; +use tracing_core::span::{Attributes, Record}; +use tracing_core::{span, Event, Level, LevelFilter, Metadata, Subscriber}; +use tracing_log::{LogTracer, NormalizeEvent}; + +struct State { + last_normalized_metadata: Mutex<(bool, Option<OwnedMetadata>)>, +} + +#[derive(PartialEq, Debug)] +struct OwnedMetadata { + name: String, + target: String, + level: Level, + module_path: Option<String>, + file: Option<String>, + line: Option<u32>, +} + +struct TestSubscriber(Arc<State>); + +impl Subscriber for TestSubscriber { + fn enabled(&self, meta: &Metadata<'_>) -> bool { + dbg!(meta); + true + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + Some(LevelFilter::from_level(Level::INFO)) + } + + fn new_span(&self, _span: &Attributes<'_>) -> span::Id { + span::Id::from_u64(42) + } + + fn record(&self, _span: &span::Id, _values: &Record<'_>) {} + + fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {} + + fn event(&self, event: &Event<'_>) { + dbg!(event); + *self.0.last_normalized_metadata.lock().unwrap() = ( + event.is_log(), + event.normalized_metadata().map(|normalized| OwnedMetadata { + name: normalized.name().to_string(), + target: normalized.target().to_string(), + level: *normalized.level(), + module_path: normalized.module_path().map(String::from), + file: normalized.file().map(String::from), + line: normalized.line(), + }), + ) + } + + fn enter(&self, _span: &span::Id) {} + + fn exit(&self, _span: &span::Id) {} +} + +#[test] +fn normalized_metadata() { + LogTracer::init().unwrap(); + let me = Arc::new(State { + last_normalized_metadata: Mutex::new((false, None)), + }); + let state = me.clone(); + + with_default(TestSubscriber(me), || { + log::info!("expected info log"); + log::debug!("unexpected debug log"); + let log = log::Record::builder() + .args(format_args!("Error!")) + .level(log::Level::Info) + .build(); + log::logger().log(&log); + last( + &state, + true, + Some(OwnedMetadata { + name: "log event".to_string(), + target: "".to_string(), + level: Level::INFO, + module_path: None, + file: None, + line: None, + }), + ); + + let log = log::Record::builder() + .args(format_args!("Error!")) + .level(log::Level::Info) + .target("log_tracer_target") + .file(Some("server.rs")) + .line(Some(144)) + .module_path(Some("log_tracer")) + .build(); + log::logger().log(&log); + last( + &state, + true, + Some(OwnedMetadata { + name: "log event".to_string(), + target: "log_tracer_target".to_string(), + level: Level::INFO, + module_path: Some("log_tracer".to_string()), + file: Some("server.rs".to_string()), + line: Some(144), + }), + ); + + tracing::info!("test with a tracing info"); + last(&state, false, None); + }) +} + +fn last(state: &State, should_be_log: bool, expected: Option<OwnedMetadata>) { + let lock = state.last_normalized_metadata.lock().unwrap(); + let (is_log, metadata) = &*lock; + dbg!(&metadata); + assert_eq!(dbg!(*is_log), should_be_log); + assert_eq!(metadata.as_ref(), expected.as_ref()); +} diff --git a/vendor/tracing-log/tests/reexport_log_crate.rs b/vendor/tracing-log/tests/reexport_log_crate.rs new file mode 100644 index 000000000..1540941f2 --- /dev/null +++ b/vendor/tracing-log/tests/reexport_log_crate.rs @@ -0,0 +1,10 @@ +use tracing_log::{log::LevelFilter, LogTracer}; + +/// This test makes sure we can access `log::LevelFilter` through the `tracing_log` crate and don't +/// have to depend on `log` separately. +/// +/// See https://github.com/tokio-rs/tracing/issues/552. +#[test] +fn can_initialize_log_tracer_with_level() { + LogTracer::init_with_filter(LevelFilter::Error).unwrap(); +} diff --git a/vendor/tracing-subscriber/.cargo-checksum.json b/vendor/tracing-subscriber/.cargo-checksum.json new file mode 100644 index 000000000..509cf43fc --- /dev/null +++ b/vendor/tracing-subscriber/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"29fe135a995cb47126440183c4d42f842ad08ec0b3796a701f28cc83991b73b8","Cargo.toml":"62d5ba5d1eef80f27e41b45b3d9bde666143ab488b39e2f675774855142e0022","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"5ea57d24ff4451b10feb2689f1046d8d43bf6af4ee60285e676dfd1a586c65db","benches/enter.rs":"4a94a04e2abd07950ef2f0b646f4dcdf4ff00abf6396edb5a53c8b41b7691b1a","benches/filter.rs":"6374005ffa47fa19880bb95e3e37406f40ea72a02c5136f4d5eb4c663d452b18","benches/filter_log.rs":"612716bdf9a188093e84d014a4847f18157f148f7d64e54150cd5c91ac709a8a","benches/fmt.rs":"5a0ff37967ffef3a221eebb78855d031e2e883a8a67528c8e794cc6f16cbee8a","benches/support/mod.rs":"72bef51154da9c9b3d81300195c1929a818858fa4b4fc2aa07b49ca586f4cd39","src/field/debug.rs":"4ab50198a0b042d92fefa77b5cac0aef7ba6936149fa555f4b6e2036dcd7f2d7","src/field/delimited.rs":"5e7967637dc3181c097637dcb2a95f35db16583b5fc293b30211db5779ab21ab","src/field/display.rs":"da8cfcb22a39f451f075e2c3a9ce5193c6afe19853cdbd643239657cac5b7e47","src/field/mod.rs":"cb8ab273159f42fc8ebe71c82acc63c962e546328fc4aa9fd5948ce996ef9e05","src/filter/directive.rs":"6341c3a1c8b6d33171647964c35816317c81b03bb098b493f1f1a22222f6ce84","src/filter/env/builder.rs":"8f527b1b0e3d9c572046235e0304146a574fe114b757af90a1c07851a07ba2cf","src/filter/env/directive.rs":"ecd2a7ffb882869f8ea9b0398f5af58ce1797a216b9dc9086c21363d1d500e77","src/filter/env/field.rs":"e1e32a2fc39884c9a5df1d5047128e43f1d0720c0b9daa6bf1e08ca9bcc5f537","src/filter/env/mod.rs":"8403df3f061a1c266b6ab6b30b03c6eb32c1c9354037a2d1eeb36817932e6ea5","src/filter/filter_fn.rs":"0debbc4a4b4d2a57b2a06905017ac908bf34b0a64aaf961535fbf6f4d5a700a9","src/filter/layer_filters/combinator.rs":"abf90b8d7a21486b28f20d4b648395b2b95a35b526bbdaba63bd2c0181ae9e87","src/filter/layer_filters/mod.rs":"2046a860b21751791dae61d20219b3d59049b84fb289ea1fca7796d214c5643c","src/filter/level.rs":"cc449757aac47caaf19dd5ba4d74c8efbcd7531fcd6c13da0c5f6fdda12cc9ca","src/filter/mod.rs":"8ebfd0dc92415ff27ec552f20919e598842a87186f13f120449053a96e1e3307","src/filter/targets.rs":"eaa11d620017269770da5b2b97302b3beb81187242912260ca98e6d60ea77780","src/fmt/fmt_layer.rs":"5042d3e2eba5f68e9041c94b52f26ab17b84f68c54d16e4c0edd9d3a3699f727","src/fmt/format/json.rs":"554985ed40f7c59787aae87626144241ca973929e33979c54f821b673b71fec9","src/fmt/format/mod.rs":"a5f6b879802d353192235cf85aa8653afa3e8ead09955e631a39415324ce42d2","src/fmt/format/pretty.rs":"30fac9a916f3b9f572efa3b650c6dab7d86a57ce903cdbba6c6c0a52064cf238","src/fmt/mod.rs":"d5274d1f8106dc7497d2be7c6d874aeca15b9ca0fd4e0c6775d3774e75b30099","src/fmt/time/datetime.rs":"778d4604d800e46b940087394e7b72750738b554e02aea523fa9820ab0768c08","src/fmt/time/mod.rs":"30c97a9d3abd099f52c4c91c7b5f0d29ed9d54d80d1718c6fb74bfd664589de1","src/fmt/time/time_crate.rs":"1bfd59516a583e396afc1770250aa8c06b52f6162a6e7b2cadb860b7eebd9d76","src/fmt/writer.rs":"1931125cf14019b66b42a9ff95e5794f344697f799c021b4a7d48fd7eb7b58f4","src/layer/context.rs":"77137d8b2810c9059ce7838c3b665748bcb9765487d6103b92596e08b0e9e84b","src/layer/layered.rs":"945e42296ce3e86aa405eb31efdef4346cdfd84111f36700cc73e473e3ce5b28","src/layer/mod.rs":"b4febff42ea5ae12b92b8c73b7c691d9486e671446da99669fc5348314721cc9","src/layer/tests.rs":"3e974d627c4bc4269cfa10c82c890e596c9d46af8e6bc03c6c117bde1237e948","src/lib.rs":"bbe402c3ff042550b265976a688a0e3a95ac9ece77f290992e93fd2f51643f5c","src/macros.rs":"e184bffc6b5999c48e365ad08343dca764a5fb711b789beb26bd1d5f1d767726","src/prelude.rs":"088635def33be9a4c4b6ed934dc22540c555e27d62f7625a43aa9c0e525ca467","src/registry/extensions.rs":"0418b39287bbc06cc95b8cecd6a25aa808b8e04714d842340ff75db458cafe5b","src/registry/mod.rs":"76627b056ce39d006708a6273b6418d001b688f016f58aa546e7821d1ef7f3bb","src/registry/sharded.rs":"aaac353632b1bbb804048b40cdfce9877688efa0679ae42ab36a095fd3e3877f","src/registry/stack.rs":"9ef333d6a8a28a064e80ff1e376dbb07bc597009010ec332b2dc3ab435d737c2","src/reload.rs":"478f7612e19fa606cfa16f2791e426267256fe5799c3918b57209d3ebfa70406","src/sync.rs":"7f78f3de5b618a999be0e61f936a233975e7769f1ebb55a0e48c3d199e9c45e3","src/util.rs":"55b4e9d63112f9d5a12a287273a9b1212741058384332d3edc024168cacfd627","tests/cached_layer_filters_dont_break_other_layers.rs":"b2084542a014abeff821b30b2b8c21e32bfdcffae53ce5335fb588f557fa4244","tests/duplicate_spans.rs":"3bf35184fb7d1dc5f33e5098820febbec37ef3ccd06b693d11b5585affb60ff4","tests/env_filter/main.rs":"b2d89ee7aaf94f0563e4e5b025cf43186ec61657b763b7c0ae010ff548635251","tests/env_filter/per_layer.rs":"19e9998922f24ec368fcbcda406f43a95335551c4c1669b509bbfc1ef216432a","tests/event_enabling.rs":"15e301a8ff6c74c454547dad15a47b5f11fc54e539162191f21462b6d5080830","tests/field_filter.rs":"fb8735801ba7ecabb421ca361bd1c846841aee63eecbdd665f9544a1cec70f67","tests/filter_log.rs":"086f1e708a2e7389024d7e36d963947909d94c1975db92f4fc425b5cba2af533","tests/fmt_max_level_hint.rs":"d4c6d6f976ae41ab8052fa610a7337ad7150802cbd5634cb30fc45c1f215cfcd","tests/hinted_layer_filters_dont_break_other_layers.rs":"d5ba9cfb6784cf59f007e673ad549dc722d109f6b3d4a69f6aa11b25ca10b469","tests/layer_filter_interests_are_cached.rs":"d036d1c4bc3754e94ebfdda9c841f4858ccec40aba0720f3fbf26c817bfe5a83","tests/layer_filters/boxed.rs":"04db459721a26d6502a2b3fbe42154c5a451021a9374a18c017d10971f44e0c0","tests/layer_filters/combinators.rs":"cdbfaa37fa5b0439ec2ae8028601d22120ff2a42867a2af8a3b27fc58e70cb6c","tests/layer_filters/downcast_raw.rs":"9b90ead571543cbe14e89b4fe637360d9baf3069f6f656ed3bdf65e7318648f1","tests/layer_filters/filter_scopes.rs":"02611bc58d0d8a67a127eca8cab1b2d9a9901bd2c8a8daad41adf6089b28aee0","tests/layer_filters/main.rs":"fe0ed219b79bcbb2ece4c224ee5bf05662fe3b23a224f746b273afdb1abc0fbd","tests/layer_filters/targets.rs":"138e3f9ddd68571d94c5aff9d54ee2fbc5f44724c6ee42477a411740ccb79ee6","tests/layer_filters/trees.rs":"4df7b5cf12da44a9255c56e5b80e2b0cf84820230ba916f324c67bc3ee4e4605","tests/layer_filters/vec.rs":"eaf2e7fe0a76633cc02bc729513202a5fb169e2bdb5a8042d8c7bd1f7092691d","tests/multiple_layer_filter_interests_cached.rs":"1ea195f03e58d715228ec1b604f85bda2fc82812d05b2f6370d5edd34a035f32","tests/option.rs":"071af56d21e1438b112c7698790328f4797d2d4dd4c7e4857ea8d4825f1fac89","tests/registry_max_level_hint.rs":"ba386d32b8d13832d7009163241c3d0723488c0393d85647eb9368776251e4fc","tests/registry_with_subscriber.rs":"61a545e1bf3f75efd0dd18c20bb93e8a1f2e0158b342179a94228c4cbd5bb9cc","tests/reload.rs":"9a1b0bcb838a0f11beb528db7e578f12cc26e1592236dd7c2fa849271025e72c","tests/same_len_filters.rs":"eceb745f7f5b6c8737c1860a58e2cf98a048fc486dee4379e94485f41c92c925","tests/support.rs":"d5d8ae7a143bda971e24dcba01137be0efea957d732b43502fd845c3bc952f8b","tests/unhinted_layer_filters_dont_break_other_layers.rs":"519cfef4977e511af938546d4208c645a28248c8ed8666daf180f0ad32f0a261","tests/utils.rs":"2c37d9f39010767190f72cb2b3faa3131985764aa547027197108299a9a6bb9e","tests/vec.rs":"d1176f3e1b0954129792a28282b95084d417143b0cc4e35887b95cee3c675392","tests/vec_subscriber_filter_interests_cached.rs":"115a0f097cd649c570eabe74f82791bbe15b2de32a2eef403575661798aadd82"},"package":"3a713421342a5a666b7577783721d3117f1b69a393df803ee17bb73b1e122a59"}
\ No newline at end of file diff --git a/vendor/tracing-subscriber/CHANGELOG.md b/vendor/tracing-subscriber/CHANGELOG.md new file mode 100644 index 000000000..35747566e --- /dev/null +++ b/vendor/tracing-subscriber/CHANGELOG.md @@ -0,0 +1,1212 @@ +# 0.3.14 (Jul 1, 2022) + +This release fixes multiple filtering bugs in the `Layer` implementations for +`Option<impl Layer>` and `Vec<impl Layer>`. + +### Fixed + +- **layer**: `Layer::event_enabled` implementation for `Option<impl Layer<S>>` + returning `false` when the `Option` is `None`, disabling all events globally + ([#2193]) +- **layer**: `Layer::max_level_hint` implementation for `Option<impl Layer<S>>` + incorrectly disabling max level filtering when the option is `None` ([#2195]) +- **layer**: `Layer::max_level_hint` implementation for `Vec<impl Layer<S>>` + returning `LevelFilter::ERROR` rather than `LevelFilter::OFF` when the `Vec` + is empty ([#2195]) + +Thanks to @CAD97 and @guswynn for contributing to this release! + +[#2193]: https://github.com/tokio-rs/tracing/pull/2193 +[#2195]: https://github.com/tokio-rs/tracing/pull/2195 + +# 0.3.13 (Jun 30, 2022) (YANKED) + +This release of `tracing-subscriber` fixes a compilation failure due to an +incorrect `tracing-core` dependency that was introduced in v0.3.12. + +### Changed + +- **tracing_core**: Updated minimum dependency version to 0.1.28 ([#2190]) + +[#2190]: https://github.com/tokio-rs/tracing/pull/2190 + +# 0.3.12 (Jun 29, 2022) (YANKED) + +This release of `tracing-subscriber` adds a new `Layer::event_enabled` method, +which allows `Layer`s to filter events *after* their field values are recorded; +a `Filter` implementation for `reload::Layer`, to make using `reload` with +per-layer filtering more ergonomic, and additional inherent method downcasting +APIs for the `Layered` type. In addition, it includes dependency updates, and +minor fixes for documentation and feature flagging. + +### Added + +- **layer**: `Layer::event_enabled` method, which can be implemented to filter + events based on their field values ([#2008]) +- **reload**: `Filter` implementation for `reload::Layer` ([#2159]) +- **layer**: `Layered::downcast_ref` and `Layered::is` inherent methods + ([#2160]) + +### Changed + +- **parking_lot**: Updated dependency on `parking_lot` to 0.13.0 ([#2143]) +- Replaced `lazy_static` dependency with `once_cell` ([#2147]) + +### Fixed + +- Don't enable `tracing-core` features by default ([#2107]) +- Several documentation link and typo fixes ([#2064], [#2068], #[2077], [#2161], + [#1088]) + +Thanks to @ben0x539, @jamesmunns, @georgemp, @james7132, @jswrenn, @CAD97, and +@guswynn for contributing to this release! + +[#2008]: https://github.com/tokio-rs/tracing/pull/2008 +[#2159]: https://github.com/tokio-rs/tracing/pull/2159 +[#2160]: https://github.com/tokio-rs/tracing/pull/2160 +[#2143]: https://github.com/tokio-rs/tracing/pull/2143 +[#2107]: https://github.com/tokio-rs/tracing/pull/2107 +[#2064]: https://github.com/tokio-rs/tracing/pull/2064 +[#2068]: https://github.com/tokio-rs/tracing/pull/2068 +[#2077]: https://github.com/tokio-rs/tracing/pull/2077 +[#2161]: https://github.com/tokio-rs/tracing/pull/2161 +[#1088]: https://github.com/tokio-rs/tracing/pull/1088 + +# 0.3.11 (Apr 9, 2022) + +This is a bugfix release for the `Filter` implementation for `EnvFilter` added +in [v0.3.10]. + +### Fixed + +- **env-filter**: Added missing `Filter::on_record` callback to `EnvFilter`'s + `Filter` impl ([#2058]) +- **env-filter**: Fixed method resolution issues when calling `EnvFilter` + methods with both the `Filter` and `Layer` traits in scope ([#2057]) +- **env-filter**: Fixed `EnvFilter::builder().parse()` and other parsing methods + returning an error when parsing an empty string ([#2052]) + +Thanks to new contributor @Ma124 for contributing to this release! + +[v0.3.10]: https://github.com/tokio-rs/tracing/releases/tag/tracing-subscriber-0.3.10 +[#2058]: https://github.com/tokio-rs/tracing/pull/2058 +[#2057]: https://github.com/tokio-rs/tracing/pull/2057 +[#2052]: https://github.com/tokio-rs/tracing/pull/2052 + +# 0.3.10 (Apr 1, 2022) + +This release adds several new features, including a `Filter` implementation and +new builder API for `EnvFilter`, support for using a `Vec<L> where L: Layer` as +a `Layer`, and a number of smaller API improvements to make working with dynamic +and reloadable layers easier. + +### Added + +- **registry**: Implement `Filter` for `EnvFilter`, allowing it to be used with + per-layer filtering ([#1983]) +- **registry**: `Filter::on_new_span`, `Filter::on_enter`, + `Filter::on_exit`, `Filter::on_close` and `Filter::on_record` callbacks to + allow `Filter`s to track span states internally ([#1973], [#2017], [#2031]) +- **registry**: `Filtered::filter` and `Filtered::filter_mut` accessors + ([#1959]) +- **registry**: `Filtered::inner` and `Filtered::inner_mut` accessors to borrow + the wrapped `Layer` ([#2034]) +- **layer**: Implement `Layer` for `Vec<L: Layer>`, to allow composing together + a dynamically sized list of `Layer`s ([#2027]) +- **layer**: `Layer::boxed` method to make type-erasing `Layer`s easier + ([#2026]) +- **fmt**: `fmt::Layer::writer` and `fmt::Layer::writer_mut` accessors ([#2034]) +- **fmt**: `fmt::Layer::set_ansi` method to allow changing the ANSI formatting + configuration at runtime ([#2034]) +- **env-filter**: `EnvFilter::builder` to configure a new `EnvFilter` prior to + parsing it ([#2035]) +- Several documentation fixes and improvements ([#1972], [#1971], [#2023], + [#2023]) + +### Fixed + +- **fmt**: `fmt::Layer`'s auto traits no longer depend on the `Subscriber` type + parameter's auto traits ([#2025]) +- **env-filter**: Fixed missing help text when the `ansi` feature is disabled + ([#2029]) + +Thanks to new contributors @TimoFreiberg and @wagenet, as well as @CAD97 for +contributing to this release! + +[#1983]: https://github.com/tokio-rs/tracing/pull/1983 +[#1973]: https://github.com/tokio-rs/tracing/pull/1973 +[#2017]: https://github.com/tokio-rs/tracing/pull/2017 +[#2031]: https://github.com/tokio-rs/tracing/pull/2031 +[#1959]: https://github.com/tokio-rs/tracing/pull/1959 +[#2034]: https://github.com/tokio-rs/tracing/pull/2034 +[#2027]: https://github.com/tokio-rs/tracing/pull/2027 +[#2026]: https://github.com/tokio-rs/tracing/pull/2026 +[#2035]: https://github.com/tokio-rs/tracing/pull/2035 +[#1972]: https://github.com/tokio-rs/tracing/pull/1972 +[#1971]: https://github.com/tokio-rs/tracing/pull/1971 +[#2023]: https://github.com/tokio-rs/tracing/pull/2023 +[#2025]: https://github.com/tokio-rs/tracing/pull/2025 +[#2029]: https://github.com/tokio-rs/tracing/pull/2029 + +# 0.3.9 (Feb 17, 2022) + +This release updates the minimum supported Rust version (MSRV) to 1.49.0, and +updates the (optional) dependency on `parking_lot` to v0.12. + +### Changed + +- Updated minimum supported Rust version (MSRV) to 1.49.0 ([#1913]) +- `parking_lot`: updated to v0.12 ([008339d]) + +### Added + +- **fmt**: Documentation improvements ([#1926], [#1927]) + +[#1913]: https://github.com/tokio-rs/tracing/pull/1913 +[#1926]: https://github.com/tokio-rs/tracing/pull/1926 +[#1927]: https://github.com/tokio-rs/tracing/pull/1927 +[008339d]: https://github.com/tokio-rs/tracing/commit/008339d1e8750ffe7b4634fc7789bda0c522424f + +# 0.3.8 (Feb 4, 2022) + +This release adds *experimental* support for recording structured field +values using the [`valuable`] crate to the `format::Json` formatter. In +particular, user-defined types which are recorded using their +[`valuable::Valuable`] implementations will be serialized as JSON objects, +rather than using their `fmt::Debug` representation. See [this blog post][post] +for details on `valuable`. + +Note that `valuable` support currently requires `--cfg tracing_unstable`. See +the documentation for details. + +Additionally, this release includes a number of other smaller API improvements. + +### Added + +- **json**: Experimental support for recording [`valuable`] values as structured + JSON ([#1862], [#1901]) +- **filter**: `Targets::would_enable` method for testing if a `Targets` filter + would enable a given target ([#1903]) +- **fmt**: `map_event_format`, `map_fmt_fields`, and `map_writer` methods to + `fmt::Layer` and `fmt::SubscriberBuilder` ([#1871]) + +### Changed + +- `tracing-core`: updated to [0.1.22][core-0.1.22] + +### Fixed + +- Set `smallvec` minimal version to 1.2.0, to fix compilation errors with `-Z + minimal-versions` ([#1890]) +- Minor documentation fixes ([#1902], [#1893]) + +Thanks to @guswynn, @glts, and @lilyball for contributing to this release! + +[`valuable`]: https://crates.io/crates/valuable +[`valuable::Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html +[post]: https://tokio.rs/blog/2021-05-valuable +[core-0.1.22]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.22 +[#1862]: https://github.com/tokio-rs/tracing/pull/1862 +[#1901]: https://github.com/tokio-rs/tracing/pull/1901 +[#1903]: https://github.com/tokio-rs/tracing/pull/1903 +[#1871]: https://github.com/tokio-rs/tracing/pull/1871 +[#1890]: https://github.com/tokio-rs/tracing/pull/1890 +[#1902]: https://github.com/tokio-rs/tracing/pull/1902 +[#1893]: https://github.com/tokio-rs/tracing/pull/1893 + +# 0.3.7 (Jan 25, 2022) + +This release adds combinators for combining filters. + +Additionally, this release also updates the `thread-local` crate to v1.1.4, +fixing warnings for the security advisory [RUSTSEC-2022-0006]. Note that +previous versions of `tracing-subscriber` did not use any of the `thread-local` +crate's APIs effected by the vulnerability. However, updating the version fixes +warnings emitted by `cargo audit` and similar tools. + +### Added + +- **filter**: Added combinators for combining filters ([#1578]) + +### Fixed + +- **registry**: Updated `thread-local` to v1.1.4 ([#1858]) + +Thanks to new contributor @matze for contributing to this release! + +[RUSTSEC-2022-0006]: https://rustsec.org/advisories/RUSTSEC-2022-0006 +[#1578]: https://github.com/tokio-rs/tracing/pull/1578 +[#1858]: https://github.com/tokio-rs/tracing/pull/1858 + +# 0.3.6 (Jan 14, 2022) + +This release adds configuration options to `tracing_subscriber::fmt` to log +source code locations for events. +### Added + +- **fmt**: Added `with_file` and `with_line_number` + configuration methods to `fmt::Format`, `fmt::SubscriberBuilder`, and + `fmt::Layer` ([#1773]) + +### Fixed + +- **fmt**: Removed incorrect leading comma from span fields with the `Pretty` + formatter ([#1833]) + +### Deprecated + +- **fmt**: Deprecated `Pretty::with_source_location`, as it can now be replaced + by the more general `Format`, `SubscriberBuilder`, and `Layer` methods + ([#1773]) + +Thanks to new contributor @renecouto for contributing to this release! + +[#1773]: https://github.com/tokio-rs/tracing/pull/1773 +[#1833]: https://github.com/tokio-rs/tracing/pull/1833 + +# 0.3.5 (Dec 29, 2021) + +This release re-enables `RUST_LOG` filtering in `tracing_subscriber::fmt`'s +default initialization methods, and adds an `OffsetLocalTime` formatter for +using local timestamps with the `time` crate. + +### Added + +- **fmt**: Added `OffsetLocalTime` formatter to `fmt::time` for formatting local + timestamps with a fixed offset ([#1772]) + +### Fixed + +- **fmt**: Added a `Targets` filter to `fmt::init()` and `fmt::try_init()` when + the "env-filter" feature is disabled, so that `RUST_LOG` is still honored + ([#1781]) + +Thanks to @marienz and @ishitatsuyuki for contributing to this release! + +[#1772]: https://github.com/tokio-rs/tracing/pull/1772 +[#1781]: https://github.com/tokio-rs/tracing/pull/1781 + +# 0.3.4 (Dec 23, 2021) + +This release contains bugfixes for the `fmt` module, as well as documentation +improvements. + +### Fixed + +- **fmt**: Fixed `fmt` not emitting log lines when timestamp formatting fails + ([#1689]) +- **fmt**: Fixed double space before thread IDs with `Pretty` formatter + ([#1778]) +- Several documentation improvements ([#1608], [#1699], [#1701]) + +[#1689]: https://github.com/tokio-rs/tracing/pull/1689 +[#1778]: https://github.com/tokio-rs/tracing/pull/1778 +[#1608]: https://github.com/tokio-rs/tracing/pull/1608 +[#1699]: https://github.com/tokio-rs/tracing/pull/1699 +[#1701]: https://github.com/tokio-rs/tracing/pull/1701 + +Thanks to new contributors @Swatinem and @rukai for contributing to this +release! + +# 0.3.3 (Nov 29, 2021) + +This release fixes a pair of regressions in `tracing-subscriber`'s `fmt` module. + +### Fixed + +- **fmt**: Fixed missing event fields with `Compact` formatter ([#1755]) +- **fmt**: Fixed `PrettyFields` formatter (and thus `format::Pretty` event + formatter) ignoring the `fmt::Layer`'s ANSI color code configuration ([#1747]) + +[#1755]: https://github.com/tokio-rs/tracing/pull/1755 +[#1747]: https://github.com/tokio-rs/tracing/pull/1747 + +# 0.3.2 (Nov 19, 2021) + +### Fixed + +- **fmt**: Fixed `MakeWriter` filtering not working with `BoxMakeWriter` + ([#1694]) + +### Added + +- **fmt**: `Writer::has_ansi_escapes` method to check if an output supports ANSI + terminal formatting escape codes ([#1696]) +- **fmt**: Added additional ANSI terminal formatting to field formatters when + supported ([#1702]) +- **fmt**: Added `FmtContext::span_scope`, `FmtContext::event_scope`, and + `FmtContext::parent_span` methods for accessing the current span and its scope + when formatting an event ([#1728]) +- **fmt**: Improved documentation on implementing event formatters ([#1727]) + +[#1694]: https://github.com/tokio-rs/tracing/pull/1694 +[#1696]: https://github.com/tokio-rs/tracing/pull/1696 +[#1702]: https://github.com/tokio-rs/tracing/pull/1702 +[#1728]: https://github.com/tokio-rs/tracing/pull/1728 +[#1727]: https://github.com/tokio-rs/tracing/pull/1727 +# 0.3.1 (Oct 25, 2021) + +This release fixes a few issues related to feature flagging. + +### Fixed + +- **time**: Compilation error when enabling the "time" feature flag without also + enabling the "local-time" feature flag ([#1685]) +- **registry**: Unused method warnings when the "std" feature is enabled but the + "registry" feature is disabled ([#1686]) + +[#1685]: https://github.com/tokio-rs/tracing/pull/1685 +[#1686]: https://github.com/tokio-rs/tracing/pull/1686 + +# 0.3.0 (Oct 22, 2021) + +This is a breaking release of `tracing-subscriber`. The primary breaking change +in this release is the removal of the dependency on the [`chrono` crate], due to +[RUSTSEC-2020-0159]. To replace `chrono`, support is added for formatting +timestamps using the [`time` crate] instead. + +In addition, this release includes a number of other breaking API changes, such +as adding (limited) support for `#![no_std]` targets, removing previously +deprecated APIs, and more. + +### Breaking Changes + +- Removed APIs deprecated in the v0.2.x release series. +- Renamed `Layer::new_span` to `Layer::on_new_span` ([#1674]) +- Removed `Layer` impl for `Arc<L: Layer<S>>` and `Arc<dyn Layer<S> + ...>` + ([#1649]) +- Replaced the [`chrono` crate] with the [`time` crate] for timestamp formatting, to + resolve [RUSTSEC-2020-0159] ([#1646]) +- Removed `json` and `env-filter` from default features. They must now be + enabled explictly ([#1647]). This means that `RUST_LOG`-based filters _will not_ + work unless the `env-filter` feature is enabled. +- Changed `FormatEvent::format_event` and `FormatFields::format_fields` + trait methods to take a `Writer` type, rather than a `&mut dyn fmt::Write` + trait object ([#1661]) +- Changed the signature of the `MakeWriter` trait by adding a lifetime parameter + ([#781]) + +### Changed + +- **layer**: Renamed `Layer::new_span` to `Layer::on_new_span` ([#1674]) +- **fmt**: Changed `FormatEvent::format_event` and `FormatFields::format_fields` + trait methods to take a `Writer` type, rather than a `&mut dyn fmt::Write` + trait object ([#1661]) +- **json**, **env-filter**: `json` and `env-filter` feature flags are no longer + enabled by default ([#1647]) +### Removed + +- Removed deprecated `CurrentSpan` type ([#1320]) +- **registry**: Removed deprecated `SpanRef::parents` iterator, replaced by + `SpanRef::scope` in [#1431] ([#1648)]) +- **layer**: Removed deprecated `Context::scope` iterator, replaced by + `Context::span_scope` and `Context::event_scope` in [#1431] and [#1434] + ([#1648)]) +- **layer**: Removed `Layer` impl for `Arc<L: Layer<S>>` and + `Arc<dyn Layer<S> + ...>`. These interfere with per-layer filtering. ([#1649]) +- **fmt**: Removed deprecated `LayerBuilder` type ([#1673]) +- **fmt**: Removed `fmt::Layer::on_event` (renamed to `fmt::Layer::fmt_event`) + ([#1673]) +- **fmt**, **chrono**: Removed the `chrono` feature flag and APIs for using the + [`chrono` crate] for timestamp formatting ([#1646]) +### Added + +- **fmt**, **time**: `LocalTime` and `UtcTime` types for formatting timestamps + using the [`time` crate] ([#1646]) +- **fmt**: Added a lifetime parameter to the `MakeWriter` trait, allowing it to + return a borrowed writer. This enables implementations of `MakeWriter` for + types such as `Mutex<T: io::Write>` and `std::fs::File`. ([#781]) +- **env-filter**: Documentation improvements ([#1637]) +- Support for some APIs on `#![no_std]` targets, by disabling the `std` feature + flag ([#1660]) + +Thanks to @Folyd and @nmathewson for contributing to this release! + +[#1320]: https://github.com/tokio-rs/tracing/pull/1320 +[#1673]: https://github.com/tokio-rs/tracing/pull/1673 +[#1674]: https://github.com/tokio-rs/tracing/pull/1674 +[#1646]: https://github.com/tokio-rs/tracing/pull/1646 +[#1647]: https://github.com/tokio-rs/tracing/pull/1647 +[#1648]: https://github.com/tokio-rs/tracing/pull/1648 +[#1649]: https://github.com/tokio-rs/tracing/pull/1649 +[#1660]: https://github.com/tokio-rs/tracing/pull/1660 +[#1661]: https://github.com/tokio-rs/tracing/pull/1661 +[#1431]: https://github.com/tokio-rs/tracing/pull/1431 +[#1434]: https://github.com/tokio-rs/tracing/pull/1434 +[#781]: https://github.com/tokio-rs/tracing/pull/781 + +[`chrono` crate]: https://crates.io/crates/chrono +[`time` crate]: https://crates.io/crates/time +[RUSTSEC-2020-0159]: https://rustsec.org/advisories/RUSTSEC-2020-0159.html + +# 0.2.25 (October 5, 2021) + +This release fixes an issue where a `Layer` implementation's custom +`downcast_raw` implementation was lost when wrapping that layer with a per-layer +filter. + +### Fixed + +- **registry**: Forward `Filtered::downcast_raw` to wrapped `Layer` ([#1619]) + +### Added + +- Documentation improvements ([#1596], [#1601]) + +Thanks to @bryanburgers for contributing to this release! + +[#1619]: https://github.com/tokio-rs/tracing/pull/1619 +[#1601]: https://github.com/tokio-rs/tracing/pull/1601 +[#1596]: https://github.com/tokio-rs/tracing/pull/1596 + +# 0.2.24 (September 19, 2021) + +This release contains a number of bug fixes, including a fix for +`tracing-subscriber` failing to compile on the minimum supported Rust version of +1.42.0. It also adds `IntoIterator` implementations for the `Targets` type. + +### Fixed + +- Fixed compilation on Rust 1.42.0 ([#1580], [#1581]) +- **registry**: Ensure per-layer filter `enabled` state is cleared when a global + filter short-circuits filter evaluation ([#1575]) +- **layer**: Fixed `Layer::on_layer` not being called for `Box`ed `Layer`s, + which broke per-layer filtering ([#1576]) + +### Added + +- **filter**: Added `Targets::iter`, returning an iterator over the set of + target-level pairs enabled by a `Targets` filter ([#1574]) +- **filter**: Added `IntoIterator` implementations for `Targets` and `&Targets` + ([#1574]) + +Thanks to new contributor @connec for contributing to this release! + +[#1580]: https://github.com/tokio-rs/tracing/pull/1580 +[#1581]: https://github.com/tokio-rs/tracing/pull/1581 +[#1575]: https://github.com/tokio-rs/tracing/pull/1575 +[#1576]: https://github.com/tokio-rs/tracing/pull/1576 +[#1574]: https://github.com/tokio-rs/tracing/pull/1574 + +# 0.2.23 (September 16, 2021) + +This release fixes a few bugs in the per-layer filtering API added in v0.2.21. + +### Fixed + +- **env-filter**: Fixed excessive `EnvFilter` memory use ([#1568]) +- **filter**: Fixed a panic that may occur in debug mode when using per-layer + filters together with global filters ([#1569]) +- Fixed incorrect documentation formatting ([#1572]) + +[#1568]: https://github.com/tokio-rs/tracing/pull/1568 +[#1569]: https://github.com/tokio-rs/tracing/pull/1569 +[#1572]: https://github.com/tokio-rs/tracing/pull/1572 + +# 0.2.22 (September 13, 2021) + +This fixes a regression where the `filter::ParseError` type was accidentally +renamed. + +### Fixed + +- **filter**: Fix `filter::ParseError` accidentally being renamed to + `filter::DirectiveParseError` ([#1558]) + +[#1558]: https://github.com/tokio-rs/tracing/pull/1558 + +# 0.2.21 (September 12, 2021) + +This release introduces the [`Filter`] trait, a new API for [per-layer +filtering][plf]. This allows controlling which spans and events are recorded by +various layers individually, rather than globally. + +In addition, it adds a new [`Targets`] filter, which provides a lighter-weight +version of the filtering provided by [`EnvFilter`], as well as other smaller API +improvements and fixes. + +### Deprecated + +- **registry**: `SpanRef::parent_id`, which cannot properly support per-layer + filtering. Use `.parent().map(SpanRef::id)` instead. ([#1523]) + +### Fixed + +- **layer** `Context` methods that are provided when the `Subscriber` implements + `LookupSpan` no longer require the "registry" feature flag ([#1525]) +- **layer** `fmt::Debug` implementation for `Layered` no longer requires the `S` + type parameter to implement `Debug` ([#1528]) + +### Added + +- **registry**: `Filter` trait, `Filtered` type, `Layer::with_filter` method, + and other APIs for per-layer filtering ([#1523]) +- **filter**: `FilterFn` and `DynFilterFn` types that implement global (`Layer`) + and per-layer (`Filter`) filtering for closures and function pointers + ([#1523]) +- **filter**: `Targets` filter, which implements a lighter-weight form of + `EnvFilter`-like filtering ([#1550]) +- **env-filter**: Added support for filtering on floating-point values ([#1507]) +- **layer**: `Layer::on_layer` callback, called when layering the `Layer` onto a +`Subscriber` ([#1523]) +- **layer**: `Layer` implementations for `Box<L>` and `Arc<L>` where `L: Layer` + ([#1536]) +- **layer**: `Layer` implementations for `Box<dyn Layer<S> + Send + Sync + 'static>` + and `Arc<dyn Layer<S> + Send + Sync + 'static>` ([#1536]) +- A number of small documentation fixes and improvements ([#1553], [#1544], + [#1539], [#1524]) + +Special thanks to new contributors @jsgf and @maxburke for contributing to this +release! + +[`Filter`]: https://docs.rs/tracing-subscriber/0.2.21/tracing_subscriber/layer/trait.Filter.html +[plf]: https://docs.rs/tracing-subscriber/0.2.21/tracing_subscriber/layer/index.html#per-layer-filtering +[`Targets`]: https://docs.rs/tracing-subscriber/0.2.21/tracing_subscriber/filter/struct.Targets.html +[`EnvFilter`]: https://docs.rs/tracing-subscriber/0.2.21/tracing_subscriber/filter/struct.EnvFilter.html +[#1507]: https://github.com/tokio-rs/tracing/pull/1507 +[#1523]: https://github.com/tokio-rs/tracing/pull/1523 +[#1524]: https://github.com/tokio-rs/tracing/pull/1524 +[#1525]: https://github.com/tokio-rs/tracing/pull/1525 +[#1528]: https://github.com/tokio-rs/tracing/pull/1528 +[#1539]: https://github.com/tokio-rs/tracing/pull/1539 +[#1544]: https://github.com/tokio-rs/tracing/pull/1544 +[#1550]: https://github.com/tokio-rs/tracing/pull/1550 +[#1553]: https://github.com/tokio-rs/tracing/pull/1553 + +# 0.2.20 (August 17, 2021) + +### Fixed + +- **fmt**: Fixed `fmt` printing only the first `source` for errors with a chain + of sources ([#1460]) +- **fmt**: Fixed missing space between level and event in the `Pretty` formatter + ([#1498]) +- **json**: Fixed `Json` formatter not honoring `without_time` and `with_level` + configurations ([#1463]) + +### Added + +- **registry**: Improved panic message when cloning a span whose ID doesn't + exist, to aid in debugging issues with multiple subscribers ([#1483]) +- **registry**: Improved documentation on span ID generation ([#1453]) + +[#1460]: https://github.com/tokio-rs/tracing/pull/1460 +[#1483]: https://github.com/tokio-rs/tracing/pull/1483 +[#1463]: https://github.com/tokio-rs/tracing/pull/1463 +[#1453]: https://github.com/tokio-rs/tracing/pull/1453 +[#1498]: https://github.com/tokio-rs/tracing/pull/1498 + +Thanks to new contributors @joshtriplett and @lerouxrgd, and returning +contributor @teozkr, for contributing to this release! + +# 0.2.19 (June 25, 2021) + +### Deprecated + +- **registry**: `SpanRef::parents`, `SpanRef::from_root`, and `Context::scope` + iterators, which are replaced by new `SpanRef::scope` and `Scope::from_root` + iterators ([#1413]) + +### Added + +- **registry**: `SpanRef::scope` method, which returns a leaf-to-root `Iterator` + including the leaf span ([#1413]) +- **registry**: `Scope::from_root` method, which reverses the `scope` iterator + to iterate root-to-leaf ([#1413]) +- **registry**: `Context::event_span` method, which looks up the parent span of + an event ([#1434]) +- **registry**: `Context::event_scope` method, returning a `Scope` iterator over + the span scope of an event ([#1434]) +- **fmt**: `MakeWriter::make_writer_for` method, which allows returning a + different writer based on a span or event's metadata ([#1141]) +- **fmt**: `MakeWriterExt` trait, with `with_max_level`, `with_min_level`, + `with_filter`, `and`, and `or_else` combinators ([#1274]) +- **fmt**: `MakeWriter` implementation for `Arc<W> where &W: io::Write` + ([#1274]) + +Thanks to @teozkr and @Folyd for contributing to this release! + +[#1413]: https://github.com/tokio-rs/tracing/pull/1413 +[#1434]: https://github.com/tokio-rs/tracing/pull/1434 +[#1141]: https://github.com/tokio-rs/tracing/pull/1141 +[#1274]: https://github.com/tokio-rs/tracing/pull/1274 + +# 0.2.18 (April 30, 2021) + +### Deprecated + +- Deprecated the `CurrentSpan` type, which is inefficient and largely superseded + by the `registry` API ([#1321]) + +### Fixed + +- **json**: Invalid JSON emitted for events in spans with no fields ([#1333]) +- **json**: Missing span data for synthesized new span, exit, and close events + ([#1334]) +- **fmt**: Extra space before log lines when timestamps are disabled ([#1355]) + +### Added + +- **env-filter**: Support for filters on spans whose names contain any + characters other than `{` and `]` ([#1368]) + +Thanks to @Folyd, and new contributors @akinnane and @aym-v for contributing to +this release! + +[#1321]: https://github.com/tokio-rs/tracing/pull/1321 +[#1333]: https://github.com/tokio-rs/tracing/pull/1333 +[#1334]: https://github.com/tokio-rs/tracing/pull/1334 +[#1355]: https://github.com/tokio-rs/tracing/pull/1355 +[#1368]: https://github.com/tokio-rs/tracing/pull/1368 + +# 0.2.17 (March 12, 2021) + +### Fixed + +- **fmt**: `Pretty` formatter now honors `with_ansi(false)` to disable ANSI + terminal formatting ([#1240]) +- **fmt**: Fixed extra padding when using `Pretty` formatter ([#1275]) +- **chrono**: Removed extra trailing space with `ChronoLocal` time formatter + ([#1103]) + +### Added + +- **fmt**: Added `FmtContext::current_span()` method, returning the current span + ([#1290]) +- **fmt**: `FmtSpan` variants may now be combined using the `|` operator for + more granular control over what span events are generated ([#1277]) + +Thanks to new contributors @cratelyn, @dignati, and @zicklag, as well as @Folyd, +@matklad, and @najamelan, for contributing to this release! + +[#1240]: https://github.com/tokio-rs/tracing/pull/1240 +[#1275]: https://github.com/tokio-rs/tracing/pull/1275 +[#1103]: https://github.com/tokio-rs/tracing/pull/1103 +[#1290]: https://github.com/tokio-rs/tracing/pull/1290 +[#1277]: https://github.com/tokio-rs/tracing/pull/1277 + +# 0.2.16 (February 19, 2021) + +### Fixed + +- **env-filter**: Fixed directives where the level is in mixed case (such as + `Info`) failing to parse ([#1126]) +- **fmt**: Fixed `fmt::Subscriber` not providing a max-level hint ([#1251]) +- `tracing-subscriber` no longer enables `tracing` and `tracing-core`'s default + features ([#1144]) + +### Changed + +- **chrono**: Updated `chrono` dependency to 0.4.16 ([#1189]) +- **log**: Updated `tracing-log` dependency to 0.1.2 + +Thanks to @salewski, @taiki-e, @davidpdrsn and @markdingram for contributing to +this release! + +[#1126]: https://github.com/tokio-rs/tracing/pull/1126 +[#1251]: https://github.com/tokio-rs/tracing/pull/1251 +[#1144]: https://github.com/tokio-rs/tracing/pull/1144 +[#1189]: https://github.com/tokio-rs/tracing/pull/1189 + +# 0.2.15 (November 2, 2020) + +### Fixed + +- **fmt**: Fixed wrong lifetime parameters on `FormatFields` impl for + `FmtContext` ([#1082]) + +### Added + +- **fmt**: `format::Pretty`, an aesthetically pleasing, human-readable event + formatter for local development and user-facing CLIs ([#1080]) +- **fmt**: `FmtContext::field_format`, which returns the subscriber's field + formatter ([#1082]) + +[#1082]: https://github.com/tokio-rs/tracing/pull/1082 +[#1080]: https://github.com/tokio-rs/tracing/pull/1080 + +# 0.2.14 (October 22, 2020) + +### Fixed + +- **registry**: Fixed `Registry::new` allocating an excessively large amount of + memory, most of which would never be used ([#1064]) + +### Changed + +- **registry**: Improved `new_span` performance by reusing `HashMap` allocations + for `Extensions` ([#1064]) +- **registry**: Significantly improved the performance of `Registry::enter` and + `Registry::exit` ([#1058]) + +[#1064]: https://github.com/tokio-rs/tracing/pull/1064 +[#1058]: https://github.com/tokio-rs/tracing/pull/1058 + +# 0.2.13 (October 7, 2020) + +### Changed + +- Updated `tracing-core` to 0.1.17 ([#992]) + +### Added + +- **env-filter**: Added support for filtering on targets which contain dashes + ([#1014]) +- **env-filter**: Added a warning when creating an `EnvFilter` that contains + directives that would enable a level disabled by the `tracing` crate's + `static_max_level` features ([#1021]) + +Thanks to @jyn514 and @bkchr for contributing to this release! + +[#992]: https://github.com/tokio-rs/tracing/pull/992 +[#1014]: https://github.com/tokio-rs/tracing/pull/1014 +[#1021]: https://github.com/tokio-rs/tracing/pull/1021 + +# 0.2.12 (September 11, 2020) + +### Fixed + +- **env-filter**: Fixed a regression where `Option<Level>` lost its + `Into<LevelFilter>` impl ([#966]) +- **env-filter**: Fixed `EnvFilter` enabling spans that should not be enabled + when multiple subscribers are in use ([#927]) + +### Changed + +- **json**: `format::Json` now outputs fields in a more readable order ([#892]) +- Updated `tracing-core` dependency to 0.1.16 + +### Added + +- **fmt**: Add `BoxMakeWriter` for erasing the type of a `MakeWriter` + implementation ([#958]) +- **fmt**: Add `TestWriter` `MakeWriter` implementation to support libtest + output capturing ([#938]) +- **layer**: Add `Layer` impl for `Option<T> where T: Layer` ([#910]) +- **env-filter**: Add `From<Level>` impl for `Directive` ([#918]) +- Multiple documentation fixes and improvements + +Thanks to @Pothulapati, @samrg472, @bryanburgers, @keetonian, and @SriRamanujam +for contributing to this release! + +[#927]: https://github.com/tokio-rs/tracing/pull/927 +[#966]: https://github.com/tokio-rs/tracing/pull/966 +[#958]: https://github.com/tokio-rs/tracing/pull/958 +[#892]: https://github.com/tokio-rs/tracing/pull/892 +[#938]: https://github.com/tokio-rs/tracing/pull/938 +[#910]: https://github.com/tokio-rs/tracing/pull/910 +[#918]: https://github.com/tokio-rs/tracing/pull/918 + +# 0.2.11 (August 10, 2020) + +### Fixed + +- **env-filter**: Incorrect max level hint when filters involving span field + values are in use (#907) +- **registry**: Fixed inconsistent span stacks when multiple registries are in + use on the same thread (#901) + +### Changed + +- **env-filter**: `regex` dependency enables fewer unused feature flags (#899) + +Thanks to @bdonlan and @jeromegn for contributing to this release! + +# 0.2.10 (July 31, 2020) + +### Fixed + +- **docs**: Incorrect formatting (#862) + +### Changed + +- **filter**: `LevelFilter` is now a re-export of the + `tracing_core::LevelFilter` type, it can now be used interchangably with the + versions in `tracing` and `tracing-core` (#853) +- **filter**: Significant performance improvements when comparing `LevelFilter`s + and `Level`s (#853) +- Updated the minimum `tracing-core` dependency to 0.1.12 (#853) + +### Added + +- **filter**: `LevelFilter` and `EnvFilter` now participate in `tracing-core`'s + max level hinting, improving performance significantly in some use cases where + levels are disabled globally (#853) + +# 0.2.9 (July 23, 2020) + +### Fixed + +- **fmt**: Fixed compilation failure on MSRV when the `chrono` feature is + disabled (#844) + +### Added + +- **fmt**: Span lookup methods defined by `layer::Context` are now also provided + by `FmtContext` (#834) + +# 0.2.8 (July 17, 2020) + +### Changed + +- **fmt**: When the `chrono` dependency is enabled, the `SystemTime` timestamp + formatter now emits human-readable timestamps rather than using `SystemTime`'s + `fmt::Debug`implementation (`chrono` is still required for customized + timestamp formatting) (#807) +- **ansi**: Updated `ansi_term` dependency to 0.12 (#816) + +### Added + +- **json**: `with_span_list` method to configure the JSON formatter to include a + list of all spans in the current trace in formatting events (similarly to the + text formatter) (#741) +- **json**: `with_current_span` method to configure the JSON formatter to include + a field for the _current_ span (the leaf of the trace) in formatted events + (#741) +- **fmt**: `with_thread_names` and `with_thread_ids` methods to configure + `fmt::Subscriber`s and `fmt::Layer`s to include the thread name and/or thread ID + of the current thread when formatting events (#818) + +Thanks to new contributors @mockersf, @keetonian, and @Pothulapati for +contributing to this release! + +# 0.2.7 (July 1, 2020) + +### Changed + +- **parking_lot**: Updated the optional `parking_lot` dependency to accept the + latest `parking_lot` version (#774) + +### Fixed + +- **fmt**: Fixed events with explicitly overridden parent spans being formatted + as though they were children of the current span (#767) + +### Added + +- **fmt**: Added the option to print synthesized events when spans are created, + entered, exited, and closed, including span durations (#761) +- Documentation clarification and improvement (#762, #769) + +Thanks to @rkuhn, @greenwoodcm, and @Ralith for contributing to this release! + +# 0.2.6 (June 19, 2020) + +### Fixed + +- **fmt**: Fixed an issue in the JSON formatter where using `Span::record` would + result in malformed spans (#709) + +# 0.2.5 (April 21, 2020) + +### Changed + +- **fmt**: Bump sharded-slab dependency (#679) + +### Fixed + +- **fmt**: remove trailing space in `ChronoUtc` `format_time` (#677) + +# 0.2.4 (April 6, 2020) + +This release includes several API ergonomics improvements, including shorthand +constructors for many types, and an extension trait for initializing subscribers +using method-chaining style. Additionally, several bugs in less commonly used +`fmt` APIs were fixed. + +### Added + +- **fmt**: Shorthand free functions for constructing most types in `fmt` + (including `tracing_subscriber::fmt()` to return a `SubscriberBuilder`, + `tracing_subscriber::fmt::layer()` to return a format `Layer`, etc) (#660) +- **registry**: Shorthand free function `tracing_subscriber::registry()` to + construct a new registry (#660) +- Added `SubscriberInitExt` extension trait for more ergonomic subscriber + initialization (#660) + +### Changed + +- **fmt**: Moved `LayerBuilder` methods to `Layer` (#655) + +### Deprecated + +- **fmt**: `LayerBuilder`, as `Layer` now implements all builder methods (#655) + +### Fixed + +- **fmt**: Fixed `Compact` formatter not omitting levels with + `with_level(false)` (#657) +- **fmt**: Fixed `fmt::Layer` duplicating the fields for a new span if another + layer has already formatted its fields (#634) +- **fmt**: Added missing space when using `record` to add new fields to a span + that already has fields (#659) +- Updated outdated documentation (#647) + + +# 0.2.3 (March 5, 2020) + +### Fixed + +- **env-filter**: Regression where filter directives were selected in the order + they were listed, rather than most specific first (#624) + +# 0.2.2 (February 27, 2020) + +### Added + +- **fmt**: Added `flatten_event` to `SubscriberBuilder` (#599) +- **fmt**: Added `with_level` to `SubscriberBuilder` (#594) + +# 0.2.1 (February 13, 2020) + +### Changed + +- **filter**: `EnvFilter` directive selection now behaves correctly (i.e. like + `env_logger`) (#583) + +### Fixed + +- **filter**: Fixed `EnvFilter` incorrectly allowing less-specific filter + directives to enable events that are disabled by more-specific filters (#583) +- **filter**: Multiple significant `EnvFilter` performance improvements, + especially when filtering events generated by `log` records (#578, #583) +- **filter**: Replaced `BTreeMap` with `Vec` in `DirectiveSet`, improving + iteration performance significantly with typical numbers of filter directives + (#580) + +A big thank-you to @samschlegel for lots of help with `EnvFilter` performance +tuning in this release! + +# 0.2.0 (February 4, 2020) + +### Breaking Changes + +- **fmt**: Renamed `Context` to `FmtContext` (#420, #425) +- **fmt**: Renamed `Builder` to `SubscriberBuilder` (#420) +- **filter**: Removed `Filter`. Use `EnvFilter` instead (#434) + +### Added + +- **registry**: `Registry`, a `Subscriber` implementation that `Layer`s can use + as a high-performance, in-memory span store. (#420, #425, #432, #433, #435) +- **registry**: Added `LookupSpan` trait, implemented by `Subscriber`s to expose + stored span data to `Layer`s (#420) +- **fmt**: Added `fmt::Layer`, to allow composing log formatting with other `Layer`s +- **fmt**: Added support for JSON field and event formatting (#377, #415) +- **filter**: Documentation for filtering directives (#554) + +### Changed + +- **fmt**: Renamed `Context` to `FmtContext` (#420, #425) (BREAKING) +- **fmt**: Renamed `Builder` to `SubscriberBuilder` (#420) (BREAKING) +- **fmt**: Reimplemented `fmt::Subscriber` in terms of the `Registry` + and `Layer`s (#420) + +### Removed + +- **filter**: Removed `Filter`. Use `EnvFilter` instead (#434) (BREAKING) + +### Fixed + +- **fmt**: Fixed memory leaks in the slab used to store per-span data + (3c35048) +- **fmt**: `fmt::SubscriberBuilder::init` not setting up `log` compatibility + (#489) +- **fmt**: Spans closed by a child span closing not also closing _their_ + parents (#514) +- **Layer**: Fixed `Layered` subscribers failing to downcast to their own type + (#549) +- **Layer**: Fixed `Layer::downcast_ref` returning invalid references (#454) + +# 0.2.0-alpha.6 (February 3, 2020) + +### Fixed + +- **fmt**: Fixed empty `{}` printed after spans with no fields (f079f2d) +- **fmt**: Fixed inconsistent formatting when ANSI colors are disabled (506a482) +- **fmt**: Fixed mis-aligned levels when ANSI colors are disabled (eba1adb) +- Fixed warnings on nightly Rust compilers (#558) + +# 0.2.0-alpha.5 (January 31, 2020) + +### Added + +- **env_filter**: Documentation for filtering directives (#554) +- **registry**, **env_filter**: Updated `smallvec` dependency to 0.1 (#543) + +### Fixed + +- **registry**: Fixed a memory leak in the slab used to store per-span data + (3c35048) +- **Layer**: Fixed `Layered` subscribers failing to downcast to their own type + (#549) +- **fmt**: Fixed a panic when multiple layers insert `FormattedFields` + extensions from the same formatter type (1c3bb70) +- **fmt**: Fixed `fmt::Layer::on_record` inserting a new `FormattedFields` when + formatted fields for a span already exist (1c3bb70) + +# 0.2.0-alpha.4 (January 11, 2020) + +### Fixed + +- **registry**: Removed inadvertently committed `dbg!` macros (#533) + +# 0.2.0-alpha.3 (January 10, 2020) + +### Added + +- **fmt**: Public `FormattedFields::new` constructor (#478) +- **fmt**: Added examples to `fmt::Layer` documentation (#510) +- Documentation now shows what feature flags are required by each API item (#525) + +### Fixed + +- **fmt**: Missing space between timestamp and level (#480) +- **fmt**: Incorrect formatting with `with_target(false)` (#481) +- **fmt**: `fmt::SubscriberBuilder::init` not setting up `log` compatibility + (#489) +- **registry**: Spans exited out of order not being closed properly on exit + (#509) +- **registry**: Memory leak when spans are closed by a child span closing (#514) +- **registry**: Spans closed by a child span closing not also closing _their_ + parents (#514) +- Compilation errors with `no-default-features` (#499, #500) + +# 0.2.0-alpha.2 (December 8, 2019) + +### Added + +- `LookupSpans` implementation for `Layered` (#448) +- `SpanRef::from_root` to iterate over a span's parents from the root (#460) +- `Context::scope`, to iterate over the current context from the root (#460) +- `Context::lookup_current`, which returns a `SpanRef` to the current + span's data (#460) + +### Changed + +- Lifetimes on some new `Context` methods to be less restrictive (#460) + +### Fixed + +- `Layer::downcast_ref` returning invalid references (#454) +- Compilation failure on 32-bit platforms (#462) +- Compilation failure with ANSI formatters (#438) + +# 0.2.0-alpha.1 (November 18, 2019) + +### Added + +- `Registry`, a reusable span store that `Layer`s can use a + high-performance, in-memory store. (#420, #425, #432, #433, #435) +- Reimplemented `fmt::Subscriber` in terms of the `Registry` + and `Layer`s (#420) +- Add benchmarks for fmt subscriber (#421) +- Add support for JSON field and event formatting (#377, #415) + +### Changed + +- **BREAKING**: Change `fmt::format::FormatFields` and + `fmt::format::FormatEvent` to accept a mandatory `FmtContext`. These + `FormatFields` and `FormatEvent` will likely see additional breaking + changes in subsequent alpha. (#420, #425) +- **BREAKING**: Removed `Filter`. Use `EnvFilter` instead (#434) + +### Contributers + +Thanks to all the contributers to this release! + +- @pimeys for #377 and #415 + +# 0.1.6 (October 29, 2019) + +### Added + +- Add `init` and `try_init` functions to `FmtSubscriber` (#385) +- Add `ChronoUtc` and `ChronoLocal` timers, RFC 3339 support (#387) +- Add `tracing::subscriber::set_default` which sets the default + subscriber and returns a drop guard. This drop guard will reset the + dispatch on drop (#388). + +### Fixed + +- Fix default level for `EnvFilter`. Setting `RUST_LOG=target` + previously only the `ERROR` level, while it should enable everything. + `tracing-subscriber` now defaults to `TRACE` if no level is specified + (#401) +- Fix `tracing-log` feature flag for init + try_init. The feature flag + `tracing_log` was used instead of the correct `tracing-log`. As a + result, both `tracing-log` and `tracing_log` needed to be specified in + order to initialize the global logger. Only `tracing-log` needs to be + specified now (#400). + +### Contributers + +Thanks to all the contributers to this release! + +- @emschwartz for #385, #387, #400 and #401 +- @bIgBV for #388 + +# 0.1.5 (October 7, 2019) + +### Fixed + +- Spans not being closed properly when `FmtSubscriber::current_span` is used + (#371) + +# 0.1.4 (September 26, 2019) + +### Fixed + +- Spans entered twice on the same thread sometimes being completely exited when + the more deeply-nested entry is exited (#361) +- Setting `with_ansi(false)` on `FmtSubscriber` not disabling ANSI color + formatting for timestamps (#354) +- Incorrect reference counting in `FmtSubscriber` that could cause spans to not + be closed when all references are dropped (#366) + +# 0.1.3 (September 16, 2019) + +### Fixed + +- `Layered` subscribers not properly forwarding calls to `current_span` + (#350) + +# 0.1.2 (September 12, 2019) + +### Fixed + +- `EnvFilter` ignoring directives with targets that are the same number of + characters (#333) +- `EnvFilter` failing to properly apply filter directives to events generated + from `log` records by`tracing-log` (#344) + +### Changed + +- Renamed `Filter` to `EnvFilter`, deprecated `Filter` (#339) +- Renamed "filter" feature flag to "env-filter", deprecated "filter" (#339) +- `FmtSubscriber` now defaults to enabling only the `INFO` level and above when + a max level filter or `EnvFilter` is not set (#336) +- Made `parking_lot` dependency an opt-in feature flag (#348) + +### Added + +- `EnvFilter::add_directive` to add new directives to filters after they are + constructed (#334) +- `fmt::Builder::with_max_level` to set a global level filter for a + `FmtSubscriber` without requiring the use of `EnvFilter` (#336) +- `Layer` implementation for `LevelFilter` (#336) +- `EnvFilter` now implements `fmt::Display` (#329) + +### Removed + +- Removed dependency on `crossbeam-util` (#348) + +# 0.1.1 (September 4, 2019) + +### Fixed + +- Potential double panic in `CurrentSpan` (#325) + +# 0.1.0 (September 3, 2019) + +- Initial release diff --git a/vendor/tracing-subscriber/Cargo.toml b/vendor/tracing-subscriber/Cargo.toml new file mode 100644 index 000000000..1d15642a2 --- /dev/null +++ b/vendor/tracing-subscriber/Cargo.toml @@ -0,0 +1,232 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +rust-version = "1.49.0" +name = "tracing-subscriber" +version = "0.3.14" +authors = [ + "Eliza Weisman <eliza@buoyant.io>", + "David Barsky <me@davidbarsky.com>", + "Tokio Contributors <team@tokio.rs>", +] +description = """ +Utilities for implementing and composing `tracing` subscribers. +""" +homepage = "https://tokio.rs" +readme = "README.md" +keywords = [ + "logging", + "tracing", + "metrics", + "subscriber", +] +categories = [ + "development-tools::debugging", + "development-tools::profiling", + "asynchronous", +] +license = "MIT" +repository = "https://github.com/tokio-rs/tracing" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = [ + "--cfg", + "docsrs", +] + +[[bench]] +name = "filter" +harness = false + +[[bench]] +name = "filter_log" +harness = false + +[[bench]] +name = "fmt" +harness = false + +[[bench]] +name = "enter" +harness = false + +[dependencies.ansi_term] +version = "0.12" +optional = true + +[dependencies.matchers] +version = "0.1.0" +optional = true + +[dependencies.once_cell] +version = "1.12" +optional = true + +[dependencies.parking_lot] +version = "0.12" +optional = true + +[dependencies.regex] +version = "1" +features = ["std"] +optional = true +default-features = false + +[dependencies.serde] +version = "1.0" +optional = true + +[dependencies.serde_json] +version = "1.0" +optional = true + +[dependencies.sharded-slab] +version = "0.1.0" +optional = true + +[dependencies.smallvec] +version = "1.2.0" +optional = true + +[dependencies.thread_local] +version = "1.1.4" +optional = true + +[dependencies.time] +version = "0.3" +features = ["formatting"] +optional = true + +[dependencies.tracing] +version = "0.1" +optional = true +default-features = false + +[dependencies.tracing-core] +version = "0.1.28" +default-features = false + +[dependencies.tracing-log] +version = "0.1.2" +features = [ + "log-tracer", + "std", +] +optional = true +default-features = false + +[dependencies.tracing-serde] +version = "0.1.3" +optional = true + +[dev-dependencies.criterion] +version = "0.3" +default_features = false + +[dev-dependencies.log] +version = "0.4" + +[dev-dependencies.regex] +version = "1" +features = ["std"] +default-features = false + +[dev-dependencies.time] +version = "0.3" +features = [ + "formatting", + "macros", +] + +[dev-dependencies.tokio] +version = "1" +features = [ + "rt", + "macros", +] + +[dev-dependencies.tracing] +version = "0.1" + +[dev-dependencies.tracing-futures] +version = "0.2" +features = [ + "std-future", + "std", +] +default-features = false + +[dev-dependencies.tracing-log] +version = "0.1.2" + +[features] +alloc = [] +ansi = [ + "fmt", + "ansi_term", +] +default = [ + "smallvec", + "fmt", + "ansi", + "tracing-log", + "std", +] +env-filter = [ + "matchers", + "regex", + "once_cell", + "tracing", + "std", + "thread_local", +] +fmt = [ + "registry", + "std", +] +json = [ + "tracing-serde", + "serde", + "serde_json", +] +local-time = ["time/local-offset"] +registry = [ + "sharded-slab", + "thread_local", + "std", +] +std = [ + "alloc", + "tracing-core/std", +] +valuable = [ + "tracing-core/valuable", + "valuable_crate", + "valuable-serde", + "tracing-serde/valuable", +] + +[target."cfg(tracing_unstable)".dependencies.valuable-serde] +version = "0.1.0" +optional = true +default_features = false + +[target."cfg(tracing_unstable)".dependencies.valuable_crate] +version = "0.1.0" +optional = true +default_features = false +package = "valuable" + +[badges.maintenance] +status = "experimental" diff --git a/vendor/tracing-subscriber/LICENSE b/vendor/tracing-subscriber/LICENSE new file mode 100644 index 000000000..cdb28b4b5 --- /dev/null +++ b/vendor/tracing-subscriber/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2019 Tokio Contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/tracing-subscriber/README.md b/vendor/tracing-subscriber/README.md new file mode 100644 index 000000000..95242b37e --- /dev/null +++ b/vendor/tracing-subscriber/README.md @@ -0,0 +1,61 @@ +![Tracing — Structured, application-level diagnostics][splash] + +[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg + +# tracing-subscriber + +Utilities for implementing and composing [`tracing`][tracing] subscribers. + +[![Crates.io][crates-badge]][crates-url] +[![Documentation][docs-badge]][docs-url] +[![Documentation (master)][docs-master-badge]][docs-master-url] +[![MIT licensed][mit-badge]][mit-url] +[![Build Status][actions-badge]][actions-url] +[![Discord chat][discord-badge]][discord-url] +![maintenance status][maint-badge] + +[Documentation][docs-url] | [Chat][discord-url] + +[tracing]: https://github.com/tokio-rs/tracing/tree/master/tracing +[tracing-fmt]: https://github.com/tokio-rs/tracing/tree/master/tracing-subscriber +[crates-badge]: https://img.shields.io/crates/v/tracing-subscriber.svg +[crates-url]: https://crates.io/crates/tracing-subscriber +[docs-badge]: https://docs.rs/tracing-subscriber/badge.svg +[docs-url]: https://docs.rs/tracing-subscriber/0.3.14 +[docs-master-badge]: https://img.shields.io/badge/docs-master-blue +[docs-master-url]: https://tracing-rs.netlify.com/tracing_subscriber +[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg +[mit-url]: LICENSE +[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg +[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI +[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white +[discord-url]: https://discord.gg/EeF3cQw +[maint-badge]: https://img.shields.io/badge/maintenance-experimental-blue.svg + +*Compiler support: [requires `rustc` 1.49+][msrv]* + +[msrv]: #supported-rust-versions + +## Supported Rust Versions + +Tracing is built against the latest stable release. The minimum supported +version is 1.49. 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. + +## License + +This project is licensed under the [MIT license](LICENSE). + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in Tracing by you, shall be licensed as MIT, without any additional +terms or conditions. diff --git a/vendor/tracing-subscriber/benches/enter.rs b/vendor/tracing-subscriber/benches/enter.rs new file mode 100644 index 000000000..49c6e730a --- /dev/null +++ b/vendor/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/vendor/tracing-subscriber/benches/filter.rs b/vendor/tracing-subscriber/benches/filter.rs new file mode 100644 index 000000000..91ab9c91d --- /dev/null +++ b/vendor/tracing-subscriber/benches/filter.rs @@ -0,0 +1,308 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use std::time::Duration; +use tracing::{dispatcher::Dispatch, span, Event, Id, Metadata}; +use tracing_subscriber::{prelude::*, EnvFilter}; + +mod support; +use support::MultithreadedBench; + +/// A subscriber that is enabled but otherwise does nothing. +struct EnabledSubscriber; + +impl tracing::Subscriber for EnabledSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let _ = span; + Id::from_u64(0xDEAD_FACE) + } + + fn event(&self, event: &Event<'_>) { + let _ = event; + } + + fn record(&self, span: &Id, values: &span::Record<'_>) { + let _ = (span, values); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +fn bench_static(c: &mut Criterion) { + let mut group = c.benchmark_group("static"); + + group.bench_function("baseline_single_threaded", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + tracing::info!(target: "static_filter", "hi"); + tracing::debug!(target: "static_filter", "hi"); + tracing::warn!(target: "static_filter", "hi"); + tracing::trace!(target: "foo", "hi"); + }) + }); + }); + group.bench_function("single_threaded", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info!(target: "static_filter", "hi"); + tracing::debug!(target: "static_filter", "hi"); + tracing::warn!(target: "static_filter", "hi"); + tracing::trace!(target: "foo", "hi"); + }) + }); + }); + group.bench_function("enabled_one", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("enabled_many", |b| { + let filter = "foo=debug,bar=trace,baz=error,quux=warn,static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_level_one", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::debug!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_level_many", |b| { + let filter = "foo=debug,bar=info,baz=error,quux=warn,static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::trace!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_one", |b| { + let filter = "foo=info".parse::<EnvFilter>().expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_many", |b| { + let filter = "foo=debug,bar=trace,baz=error,quux=warn,whibble=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("baseline_multithreaded", |b| { + let dispatch = Dispatch::new(EnabledSubscriber); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + tracing::info!(target: "static_filter", "hi"); + }) + .thread(|| { + tracing::debug!(target: "static_filter", "hi"); + }) + .thread(|| { + tracing::warn!(target: "static_filter", "hi"); + }) + .thread(|| { + tracing::warn!(target: "foo", "hi"); + }) + .run(); + total += elapsed; + } + total + }) + }); + group.bench_function("multithreaded", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + let dispatch = Dispatch::new(EnabledSubscriber.with(filter)); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + tracing::info!(target: "static_filter", "hi"); + }) + .thread(|| { + tracing::debug!(target: "static_filter", "hi"); + }) + .thread(|| { + tracing::warn!(target: "static_filter", "hi"); + }) + .thread(|| { + tracing::warn!(target: "foo", "hi"); + }) + .run(); + total += elapsed; + } + total + }); + }); + group.finish(); +} + +fn bench_dynamic(c: &mut Criterion) { + let mut group = c.benchmark_group("dynamic"); + + group.bench_function("baseline_single_threaded", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + tracing::info_span!("foo").in_scope(|| { + tracing::info!("hi"); + tracing::debug!("hi"); + }); + tracing::info_span!("bar").in_scope(|| { + tracing::warn!("hi"); + }); + tracing::trace!("hi"); + }) + }); + }); + group.bench_function("single_threaded", |b| { + let filter = "[foo]=trace".parse::<EnvFilter>().expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info_span!("foo").in_scope(|| { + tracing::info!("hi"); + tracing::debug!("hi"); + }); + tracing::info_span!("bar").in_scope(|| { + tracing::warn!("hi"); + }); + tracing::trace!("hi"); + }) + }); + }); + group.bench_function("baseline_multithreaded", |b| { + let dispatch = Dispatch::new(EnabledSubscriber); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + tracing::info!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + tracing::debug!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("bar"); + let _ = span.enter(); + tracing::debug!("hi"); + }) + .thread(|| { + tracing::trace!("hi"); + }) + .run(); + total += elapsed; + } + total + }) + }); + group.bench_function("multithreaded", |b| { + let filter = "[foo]=trace".parse::<EnvFilter>().expect("should parse"); + let dispatch = Dispatch::new(EnabledSubscriber.with(filter)); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + tracing::info!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + tracing::debug!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("bar"); + let _ = span.enter(); + tracing::debug!("hi"); + }) + .thread(|| { + tracing::trace!("hi"); + }) + .run(); + total += elapsed; + } + total + }) + }); + + group.finish(); +} + +fn bench_mixed(c: &mut Criterion) { + let mut group = c.benchmark_group("mixed"); + group.bench_function("disabled", |b| { + let filter = "[foo]=trace,bar[quux]=debug,[{baz}]=debug,asdf=warn,wibble=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_by_level", |b| { + let filter = "[foo]=info,bar[quux]=debug,asdf=warn,static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::trace!(target: "static_filter", "hi"); + }) + }); + }); +} + +criterion_group!(benches, bench_static, bench_dynamic, bench_mixed); +criterion_main!(benches); diff --git a/vendor/tracing-subscriber/benches/filter_log.rs b/vendor/tracing-subscriber/benches/filter_log.rs new file mode 100644 index 000000000..4dcf3b4ec --- /dev/null +++ b/vendor/tracing-subscriber/benches/filter_log.rs @@ -0,0 +1,315 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use std::time::Duration; +use tracing::{dispatcher::Dispatch, span, Event, Id, Metadata}; +use tracing_subscriber::{prelude::*, EnvFilter}; + +mod support; +use support::MultithreadedBench; + +/// A subscriber that is enabled but otherwise does nothing. +struct EnabledSubscriber; + +impl tracing::Subscriber for EnabledSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let _ = span; + Id::from_u64(0xDEAD_FACE) + } + + fn event(&self, event: &Event<'_>) { + let _ = event; + } + + fn record(&self, span: &Id, values: &span::Record<'_>) { + let _ = (span, values); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +fn bench_static(c: &mut Criterion) { + let _ = tracing_log::LogTracer::init(); + + let mut group = c.benchmark_group("log/static"); + + group.bench_function("baseline_single_threaded", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + log::info!(target: "static_filter", "hi"); + log::debug!(target: "static_filter", "hi"); + log::warn!(target: "static_filter", "hi"); + log::trace!(target: "foo", "hi"); + }) + }); + }); + group.bench_function("single_threaded", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::info!(target: "static_filter", "hi"); + log::debug!(target: "static_filter", "hi"); + log::warn!(target: "static_filter", "hi"); + log::trace!(target: "foo", "hi"); + }) + }); + }); + group.bench_function("enabled_one", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("enabled_many", |b| { + let filter = "foo=debug,bar=trace,baz=error,quux=warn,static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_level_one", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::debug!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_level_many", |b| { + let filter = "foo=debug,bar=info,baz=error,quux=warn,static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::trace!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_one", |b| { + let filter = "foo=info".parse::<EnvFilter>().expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_many", |b| { + let filter = "foo=debug,bar=trace,baz=error,quux=warn,whibble=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("baseline_multithreaded", |b| { + let dispatch = Dispatch::new(EnabledSubscriber); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + log::info!(target: "static_filter", "hi"); + }) + .thread(|| { + log::debug!(target: "static_filter", "hi"); + }) + .thread(|| { + log::warn!(target: "static_filter", "hi"); + }) + .thread(|| { + log::warn!(target: "foo", "hi"); + }) + .run(); + total += elapsed; + } + total + }) + }); + group.bench_function("multithreaded", |b| { + let filter = "static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + let dispatch = Dispatch::new(EnabledSubscriber.with(filter)); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + log::info!(target: "static_filter", "hi"); + }) + .thread(|| { + log::debug!(target: "static_filter", "hi"); + }) + .thread(|| { + log::warn!(target: "static_filter", "hi"); + }) + .thread(|| { + log::warn!(target: "foo", "hi"); + }) + .run(); + total += elapsed; + } + total + }); + }); + group.finish(); +} + +fn bench_dynamic(c: &mut Criterion) { + let _ = tracing_log::LogTracer::init(); + + let mut group = c.benchmark_group("log/dynamic"); + + group.bench_function("baseline_single_threaded", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + tracing::info_span!("foo").in_scope(|| { + log::info!("hi"); + log::debug!("hi"); + }); + tracing::info_span!("bar").in_scope(|| { + log::warn!("hi"); + }); + log::trace!("hi"); + }) + }); + }); + group.bench_function("single_threaded", |b| { + let filter = "[foo]=trace".parse::<EnvFilter>().expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + tracing::info_span!("foo").in_scope(|| { + log::info!("hi"); + log::debug!("hi"); + }); + tracing::info_span!("bar").in_scope(|| { + log::warn!("hi"); + }); + log::trace!("hi"); + }) + }); + }); + group.bench_function("baseline_multithreaded", |b| { + let dispatch = Dispatch::new(EnabledSubscriber); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + log::info!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + log::debug!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("bar"); + let _ = span.enter(); + log::debug!("hi"); + }) + .thread(|| { + log::trace!("hi"); + }) + .run(); + total += elapsed; + } + total + }) + }); + group.bench_function("multithreaded", |b| { + let filter = "[foo]=trace".parse::<EnvFilter>().expect("should parse"); + let dispatch = Dispatch::new(EnabledSubscriber.with(filter)); + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + log::info!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("foo"); + let _ = span.enter(); + log::debug!("hi"); + }) + .thread(|| { + let span = tracing::info_span!("bar"); + let _ = span.enter(); + log::debug!("hi"); + }) + .thread(|| { + log::trace!("hi"); + }) + .run(); + total += elapsed; + } + total + }) + }); + + group.finish(); +} + +fn bench_mixed(c: &mut Criterion) { + let _ = tracing_log::LogTracer::init(); + + let mut group = c.benchmark_group("log/mixed"); + + group.bench_function("disabled", |b| { + let filter = "[foo]=trace,bar[quux]=debug,[{baz}]=debug,asdf=warn,wibble=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::info!(target: "static_filter", "hi"); + }) + }); + }); + group.bench_function("disabled_by_level", |b| { + let filter = "[foo]=info,bar[quux]=debug,asdf=warn,static_filter=info" + .parse::<EnvFilter>() + .expect("should parse"); + tracing::subscriber::with_default(EnabledSubscriber.with(filter), || { + b.iter(|| { + log::trace!(target: "static_filter", "hi"); + }) + }); + }); +} + +criterion_group!(benches, bench_static, bench_dynamic, bench_mixed); +criterion_main!(benches); diff --git a/vendor/tracing-subscriber/benches/fmt.rs b/vendor/tracing-subscriber/benches/fmt.rs new file mode 100644 index 000000000..a039e66d4 --- /dev/null +++ b/vendor/tracing-subscriber/benches/fmt.rs @@ -0,0 +1,331 @@ +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; +use std::{io, time::Duration}; + +mod support; +use support::MultithreadedBench; + +/// A fake writer that doesn't actually do anything. +/// +/// We want to measure the subscriber's overhead, *not* the performance of +/// stdout/file writers. Using a no-op Write implementation lets us only measure +/// the subscriber's overhead. +struct NoWriter; + +impl io::Write for NoWriter { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + Ok(buf.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl NoWriter { + fn new() -> Self { + Self + } +} + +fn bench_new_span(c: &mut Criterion) { + bench_thrpt(c, "new_span", |group, i| { + group.bench_with_input(BenchmarkId::new("single_thread", i), i, |b, &i| { + tracing::dispatcher::with_default(&mk_dispatch(), || { + b.iter(|| { + for n in 0..i { + let _span = tracing::info_span!("span", n); + } + }) + }); + }); + group.bench_with_input(BenchmarkId::new("multithreaded", i), i, |b, &i| { + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + let dispatch = mk_dispatch(); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(move || { + for n in 0..i { + let _span = tracing::info_span!("span", n); + } + }) + .thread(move || { + for n in 0..i { + let _span = tracing::info_span!("span", n); + } + }) + .thread(move || { + for n in 0..i { + let _span = tracing::info_span!("span", n); + } + }) + .thread(move || { + for n in 0..i { + let _span = tracing::info_span!("span", n); + } + }) + .run(); + total += elapsed; + } + total + }) + }); + }); +} + +type Group<'a> = criterion::BenchmarkGroup<'a, criterion::measurement::WallTime>; +fn bench_thrpt(c: &mut Criterion, name: &'static str, mut f: impl FnMut(&mut Group<'_>, &usize)) { + const N_SPANS: &[usize] = &[1, 10, 50]; + + let mut group = c.benchmark_group(name); + for spans in N_SPANS { + group.throughput(Throughput::Elements(*spans as u64)); + f(&mut group, spans); + } + group.finish(); +} + +fn mk_dispatch() -> tracing::Dispatch { + let subscriber = tracing_subscriber::FmtSubscriber::builder() + .with_writer(NoWriter::new) + .finish(); + tracing::Dispatch::new(subscriber) +} + +fn bench_event(c: &mut Criterion) { + bench_thrpt(c, "event", |group, i| { + group.bench_with_input(BenchmarkId::new("root/single_threaded", i), i, |b, &i| { + let dispatch = mk_dispatch(); + tracing::dispatcher::with_default(&dispatch, || { + b.iter(|| { + for n in 0..i { + tracing::info!(n); + } + }) + }); + }); + group.bench_with_input(BenchmarkId::new("root/multithreaded", i), i, |b, &i| { + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + let dispatch = mk_dispatch(); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread(move || { + for n in 0..i { + tracing::info!(n); + } + }) + .thread(move || { + for n in 0..i { + tracing::info!(n); + } + }) + .thread(move || { + for n in 0..i { + tracing::info!(n); + } + }) + .thread(move || { + for n in 0..i { + tracing::info!(n); + } + }) + .run(); + total += elapsed; + } + total + }) + }); + group.bench_with_input( + BenchmarkId::new("unique_parent/single_threaded", i), + i, + |b, &i| { + tracing::dispatcher::with_default(&mk_dispatch(), || { + let span = tracing::info_span!("unique_parent", foo = false); + let _guard = span.enter(); + b.iter(|| { + for n in 0..i { + tracing::info!(n); + } + }) + }); + }, + ); + group.bench_with_input( + BenchmarkId::new("unique_parent/multithreaded", i), + i, + |b, &i| { + b.iter_custom(|iters| { + let mut total = Duration::from_secs(0); + let dispatch = mk_dispatch(); + for _ in 0..iters { + let bench = MultithreadedBench::new(dispatch.clone()); + let elapsed = bench + .thread_with_setup(move |start| { + let span = tracing::info_span!("unique_parent", foo = false); + let _guard = span.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }) + .thread_with_setup(move |start| { + let span = tracing::info_span!("unique_parent", foo = false); + let _guard = span.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }) + .thread_with_setup(move |start| { + let span = tracing::info_span!("unique_parent", foo = false); + let _guard = span.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }) + .thread_with_setup(move |start| { + let span = tracing::info_span!("unique_parent", foo = false); + let _guard = span.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }) + .run(); + total += elapsed; + } + total + }) + }, + ); + group.bench_with_input( + BenchmarkId::new("shared_parent/multithreaded", i), + i, + |b, &i| { + b.iter_custom(|iters| { + let dispatch = mk_dispatch(); + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let parent = tracing::dispatcher::with_default(&dispatch, || { + tracing::info_span!("shared_parent", foo = "hello world") + }); + let bench = MultithreadedBench::new(dispatch.clone()); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + for n in 0..i { + tracing::info!(n); + } + }); + let elapsed = bench.run(); + total += elapsed; + } + total + }) + }, + ); + group.bench_with_input( + BenchmarkId::new("multi-parent/multithreaded", i), + i, + |b, &i| { + b.iter_custom(|iters| { + let dispatch = mk_dispatch(); + let mut total = Duration::from_secs(0); + for _ in 0..iters { + let parent = tracing::dispatcher::with_default(&dispatch, || { + tracing::info_span!("multiparent", foo = "hello world") + }); + let bench = MultithreadedBench::new(dispatch.clone()); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + let mut span = tracing::info_span!("parent"); + for n in 0..i { + let s = tracing::info_span!(parent: &span, "parent2", n, i); + s.in_scope(|| { + tracing::info!(n); + }); + span = s; + } + }); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + let mut span = tracing::info_span!("parent"); + for n in 0..i { + let s = tracing::info_span!(parent: &span, "parent2", n, i); + s.in_scope(|| { + tracing::info!(n); + }); + span = s; + } + }); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + let mut span = tracing::info_span!("parent"); + for n in 0..i { + let s = tracing::info_span!(parent: &span, "parent2", n, i); + s.in_scope(|| { + tracing::info!(n); + }); + span = s; + } + }); + let parent2 = parent.clone(); + bench.thread_with_setup(move |start| { + let _guard = parent2.enter(); + start.wait(); + let mut span = tracing::info_span!("parent"); + for n in 0..i { + let s = tracing::info_span!(parent: &span, "parent2", n, i); + s.in_scope(|| { + tracing::info!(n); + }); + span = s; + } + }); + let elapsed = bench.run(); + total += elapsed; + } + total + }) + }, + ); + }); +} + +criterion_group!(benches, bench_new_span, bench_event); +criterion_main!(benches); diff --git a/vendor/tracing-subscriber/benches/support/mod.rs b/vendor/tracing-subscriber/benches/support/mod.rs new file mode 100644 index 000000000..25e9e7e22 --- /dev/null +++ b/vendor/tracing-subscriber/benches/support/mod.rs @@ -0,0 +1,49 @@ +use std::{ + sync::{Arc, Barrier}, + thread, + time::{Duration, Instant}, +}; +use tracing::dispatcher::Dispatch; + +#[derive(Clone)] +pub(super) struct MultithreadedBench { + start: Arc<Barrier>, + end: Arc<Barrier>, + dispatch: Dispatch, +} + +impl MultithreadedBench { + pub(super) fn new(dispatch: Dispatch) -> Self { + Self { + start: Arc::new(Barrier::new(5)), + end: Arc::new(Barrier::new(5)), + dispatch, + } + } + + pub(super) fn thread(&self, f: impl FnOnce() + Send + 'static) -> &Self { + self.thread_with_setup(|start| { + start.wait(); + f() + }) + } + + pub(super) fn thread_with_setup(&self, f: impl FnOnce(&Barrier) + Send + 'static) -> &Self { + let this = self.clone(); + thread::spawn(move || { + let dispatch = this.dispatch.clone(); + tracing::dispatcher::with_default(&dispatch, move || { + f(&*this.start); + this.end.wait(); + }) + }); + self + } + + pub(super) fn run(&self) -> Duration { + self.start.wait(); + let t0 = Instant::now(); + self.end.wait(); + t0.elapsed() + } +} diff --git a/vendor/tracing-subscriber/src/field/debug.rs b/vendor/tracing-subscriber/src/field/debug.rs new file mode 100644 index 000000000..cc67d29fe --- /dev/null +++ b/vendor/tracing-subscriber/src/field/debug.rs @@ -0,0 +1,111 @@ +//! `MakeVisitor` wrappers for working with `fmt::Debug` fields. +use super::{MakeVisitor, VisitFmt, VisitOutput}; +use tracing_core::field::{Field, Visit}; + +use core::fmt; + +/// A visitor wrapper that ensures any `fmt::Debug` fields are formatted using +/// the alternate (`:#`) formatter. +#[derive(Debug, Clone)] +pub struct Alt<V>(V); + +// TODO(eliza): When `error` as a primitive type is stable, add a +// `DisplayErrors` wrapper... + +// === impl Alt === +// +impl<V> Alt<V> { + /// Wraps the provided visitor so that any `fmt::Debug` fields are formatted + /// using the alternative (`:#`) formatter. + pub fn new(inner: V) -> Self { + Alt(inner) + } +} + +impl<T, V> MakeVisitor<T> for Alt<V> +where + V: MakeVisitor<T>, +{ + type Visitor = Alt<V::Visitor>; + + #[inline] + fn make_visitor(&self, target: T) -> Self::Visitor { + Alt(self.0.make_visitor(target)) + } +} + +impl<V> Visit for Alt<V> +where + V: Visit, +{ + #[inline] + fn record_f64(&mut self, field: &Field, value: f64) { + self.0.record_f64(field, value) + } + + #[inline] + fn record_i64(&mut self, field: &Field, value: i64) { + self.0.record_i64(field, value) + } + + #[inline] + fn record_u64(&mut self, field: &Field, value: u64) { + self.0.record_u64(field, value) + } + + #[inline] + fn record_bool(&mut self, field: &Field, value: bool) { + self.0.record_bool(field, value) + } + + /// Visit a string value. + fn record_str(&mut self, field: &Field, value: &str) { + self.0.record_str(field, value) + } + + // TODO(eliza): add RecordError when stable + // fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { + // self.record_debug(field, &format_args!("{}", value)) + // } + + #[inline] + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + self.0.record_debug(field, &format_args!("{:#?}", value)) + } +} + +impl<V, O> VisitOutput<O> for Alt<V> +where + V: VisitOutput<O>, +{ + #[inline] + fn finish(self) -> O { + self.0.finish() + } +} + +feature! { + #![feature = "std"] + use super::VisitWrite; + use std::io; + + impl<V> VisitWrite for Alt<V> + where + V: VisitWrite, + { + #[inline] + fn writer(&mut self) -> &mut dyn io::Write { + self.0.writer() + } + } +} + +impl<V> VisitFmt for Alt<V> +where + V: VisitFmt, +{ + #[inline] + fn writer(&mut self) -> &mut dyn fmt::Write { + self.0.writer() + } +} diff --git a/vendor/tracing-subscriber/src/field/delimited.rs b/vendor/tracing-subscriber/src/field/delimited.rs new file mode 100644 index 000000000..98634cea9 --- /dev/null +++ b/vendor/tracing-subscriber/src/field/delimited.rs @@ -0,0 +1,184 @@ +//! A `MakeVisitor` wrapper that separates formatted fields with a delimiter. +use super::{MakeVisitor, VisitFmt, VisitOutput}; + +use core::fmt; +use tracing_core::field::{Field, Visit}; + +/// A `MakeVisitor` wrapper that wraps a visitor that writes formatted output so +/// that a delimiter is inserted between writing formatted field values. +#[derive(Debug, Clone)] +pub struct Delimited<D, V> { + delimiter: D, + inner: V, +} + +/// A visitor wrapper that inserts a delimiter after the wrapped visitor formats +/// a field value. +#[derive(Debug)] +pub struct VisitDelimited<D, V> { + delimiter: D, + seen: bool, + inner: V, + err: fmt::Result, +} + +// === impl Delimited === + +impl<D, V, T> MakeVisitor<T> for Delimited<D, V> +where + D: AsRef<str> + Clone, + V: MakeVisitor<T>, + V::Visitor: VisitFmt, +{ + type Visitor = VisitDelimited<D, V::Visitor>; + fn make_visitor(&self, target: T) -> Self::Visitor { + let inner = self.inner.make_visitor(target); + VisitDelimited::new(self.delimiter.clone(), inner) + } +} + +impl<D, V> Delimited<D, V> { + /// Returns a new [`MakeVisitor`] implementation that wraps `inner` so that + /// it will format each visited field separated by the provided `delimiter`. + /// + /// [`MakeVisitor`]: super::MakeVisitor + pub fn new(delimiter: D, inner: V) -> Self { + Self { delimiter, inner } + } +} + +// === impl VisitDelimited === + +impl<D, V> VisitDelimited<D, V> { + /// Returns a new [`Visit`] implementation that wraps `inner` so that + /// each formatted field is separated by the provided `delimiter`. + /// + /// [`Visit`]: tracing_core::field::Visit + pub fn new(delimiter: D, inner: V) -> Self { + Self { + delimiter, + inner, + seen: false, + err: Ok(()), + } + } + + fn delimit(&mut self) + where + V: VisitFmt, + D: AsRef<str>, + { + if self.err.is_err() { + return; + } + + if self.seen { + self.err = self.inner.writer().write_str(self.delimiter.as_ref()); + } + + self.seen = true; + } +} + +impl<D, V> Visit for VisitDelimited<D, V> +where + V: VisitFmt, + D: AsRef<str>, +{ + fn record_i64(&mut self, field: &Field, value: i64) { + self.delimit(); + self.inner.record_i64(field, value); + } + + fn record_u64(&mut self, field: &Field, value: u64) { + self.delimit(); + self.inner.record_u64(field, value); + } + + fn record_bool(&mut self, field: &Field, value: bool) { + self.delimit(); + self.inner.record_bool(field, value); + } + + fn record_str(&mut self, field: &Field, value: &str) { + self.delimit(); + self.inner.record_str(field, value); + } + + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + self.delimit(); + self.inner.record_debug(field, value); + } +} + +impl<D, V> VisitOutput<fmt::Result> for VisitDelimited<D, V> +where + V: VisitFmt, + D: AsRef<str>, +{ + fn finish(self) -> fmt::Result { + self.err?; + self.inner.finish() + } +} + +impl<D, V> VisitFmt for VisitDelimited<D, V> +where + V: VisitFmt, + D: AsRef<str>, +{ + fn writer(&mut self) -> &mut dyn fmt::Write { + self.inner.writer() + } +} + +#[cfg(test)] +#[cfg(all(test, feature = "alloc"))] +mod test { + use super::*; + use crate::field::test_util::*; + + #[test] + fn delimited_visitor() { + let mut s = String::new(); + let visitor = DebugVisitor::new(&mut s); + let mut visitor = VisitDelimited::new(", ", visitor); + + TestAttrs1::with(|attrs| attrs.record(&mut visitor)); + visitor.finish().unwrap(); + + assert_eq!( + s.as_str(), + "question=\"life, the universe, and everything\", tricky=true, can_you_do_it=true" + ); + } + + #[test] + fn delimited_new_visitor() { + let make = Delimited::new("; ", MakeDebug); + + TestAttrs1::with(|attrs| { + let mut s = String::new(); + { + let mut v = make.make_visitor(&mut s); + attrs.record(&mut v); + } + assert_eq!( + s.as_str(), + "question=\"life, the universe, and everything\"; tricky=true; can_you_do_it=true" + ); + }); + + TestAttrs2::with(|attrs| { + let mut s = String::new(); + { + let mut v = make.make_visitor(&mut s); + attrs.record(&mut v); + } + assert_eq!( + s.as_str(), + "question=None; question.answer=42; tricky=true; can_you_do_it=false" + ); + }); + } +} diff --git a/vendor/tracing-subscriber/src/field/display.rs b/vendor/tracing-subscriber/src/field/display.rs new file mode 100644 index 000000000..78a039ce1 --- /dev/null +++ b/vendor/tracing-subscriber/src/field/display.rs @@ -0,0 +1,117 @@ +//! `MakeVisitor` wrappers for working with `fmt::Display` fields. +use super::{MakeVisitor, VisitFmt, VisitOutput}; +use tracing_core::field::{Field, Visit}; + +use core::fmt; + +/// A visitor wrapper that ensures any strings named "message" are formatted +/// using `fmt::Display` +#[derive(Debug, Clone)] +pub struct Messages<V>(V); + +// TODO(eliza): When `error` as a primitive type is stable, add a +// `DisplayErrors` wrapper... + +// === impl Messages === +// +impl<V> Messages<V> { + /// Returns a new [`MakeVisitor`] implementation that will wrap `inner` so + /// that any strings named `message` are formatted using `fmt::Display`. + /// + /// [`MakeVisitor`]: super::MakeVisitor + pub fn new(inner: V) -> Self { + Messages(inner) + } +} + +impl<T, V> MakeVisitor<T> for Messages<V> +where + V: MakeVisitor<T>, +{ + type Visitor = Messages<V::Visitor>; + + #[inline] + fn make_visitor(&self, target: T) -> Self::Visitor { + Messages(self.0.make_visitor(target)) + } +} + +impl<V> Visit for Messages<V> +where + V: Visit, +{ + #[inline] + fn record_f64(&mut self, field: &Field, value: f64) { + self.0.record_f64(field, value) + } + + #[inline] + fn record_i64(&mut self, field: &Field, value: i64) { + self.0.record_i64(field, value) + } + + #[inline] + fn record_u64(&mut self, field: &Field, value: u64) { + self.0.record_u64(field, value) + } + + #[inline] + fn record_bool(&mut self, field: &Field, value: bool) { + self.0.record_bool(field, value) + } + + /// Visit a string value. + fn record_str(&mut self, field: &Field, value: &str) { + if field.name() == "message" { + self.0.record_debug(field, &format_args!("{}", value)) + } else { + self.0.record_str(field, value) + } + } + + // TODO(eliza): add RecordError when stable + // fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { + // self.record_debug(field, &format_args!("{}", value)) + // } + + #[inline] + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + self.0.record_debug(field, value) + } +} + +impl<V, O> VisitOutput<O> for Messages<V> +where + V: VisitOutput<O>, +{ + #[inline] + fn finish(self) -> O { + self.0.finish() + } +} + +feature! { + #![feature = "std"] + use super::VisitWrite; + use std::io; + + impl<V> VisitWrite for Messages<V> + where + V: VisitWrite, + { + #[inline] + fn writer(&mut self) -> &mut dyn io::Write { + self.0.writer() + } + } +} + +impl<V> VisitFmt for Messages<V> +where + V: VisitFmt, +{ + #[inline] + fn writer(&mut self) -> &mut dyn fmt::Write { + self.0.writer() + } +} diff --git a/vendor/tracing-subscriber/src/field/mod.rs b/vendor/tracing-subscriber/src/field/mod.rs new file mode 100644 index 000000000..5dfddb362 --- /dev/null +++ b/vendor/tracing-subscriber/src/field/mod.rs @@ -0,0 +1,366 @@ +//! Utilities for working with [fields] and [field visitors]. +//! +//! [fields]: tracing_core::field +//! [field visitors]: tracing_core::field::Visit +use core::{fmt, marker::PhantomData}; +pub use tracing_core::field::Visit; +use tracing_core::{ + span::{Attributes, Record}, + Event, +}; +pub mod debug; +pub mod delimited; +pub mod display; + +/// Creates new [visitors]. +/// +/// A type implementing `MakeVisitor` represents a composable factory for types +/// implementing the [`Visit` trait][visitors]. The `MakeVisitor` trait defines +/// a single function, `make_visitor`, which takes in a `T`-typed `target` and +/// returns a type implementing `Visit` configured for that target. A target may +/// be a string, output stream, or data structure that the visitor will record +/// data to, configuration variables that determine the visitor's behavior, or +/// `()` when no input is required to produce a visitor. +/// +/// [visitors]: tracing_core::field::Visit +pub trait MakeVisitor<T> { + /// The visitor type produced by this `MakeVisitor`. + type Visitor: Visit; + + /// Make a new visitor for the provided `target`. + fn make_visitor(&self, target: T) -> Self::Visitor; +} + +/// A [visitor] that produces output once it has visited a set of fields. +/// +/// [visitor]: tracing_core::field::Visit +pub trait VisitOutput<Out>: Visit { + /// Completes the visitor, returning any output. + /// + /// This is called once a full set of fields has been visited. + fn finish(self) -> Out; + + /// Visit a set of fields, and return the output of finishing the visitor + /// once the fields have been visited. + fn visit<R>(mut self, fields: &R) -> Out + where + R: RecordFields, + Self: Sized, + { + fields.record(&mut self); + self.finish() + } +} + +/// Extension trait implemented by types which can be recorded by a [visitor]. +/// +/// This allows writing code that is generic over `tracing_core`'s +/// [`span::Attributes`][attr], [`span::Record`][rec], and [`Event`][event] +/// types. These types all provide inherent `record` methods that allow a +/// visitor to record their fields, but there is no common trait representing this. +/// +/// With `RecordFields`, we can write code like this: +/// ``` +/// use tracing_core::field::Visit; +/// # use tracing_core::field::Field; +/// use tracing_subscriber::field::RecordFields; +/// +/// struct MyVisitor { +/// // ... +/// } +/// # impl MyVisitor { fn new() -> Self { Self{} } } +/// impl Visit for MyVisitor { +/// // ... +/// # fn record_debug(&mut self, _: &Field, _: &dyn std::fmt::Debug) {} +/// } +/// +/// fn record_with_my_visitor<R>(r: R) +/// where +/// R: RecordFields, +/// { +/// let mut visitor = MyVisitor::new(); +/// r.record(&mut visitor); +/// } +/// ``` +/// [visitor]: tracing_core::field::Visit +/// [attr]: tracing_core::span::Attributes +/// [rec]: tracing_core::span::Record +/// [event]: tracing_core::event::Event +pub trait RecordFields: crate::sealed::Sealed<RecordFieldsMarker> { + /// Record all the fields in `self` with the provided `visitor`. + fn record(&self, visitor: &mut dyn Visit); +} + +/// Extension trait implemented for all `MakeVisitor` implementations that +/// produce a visitor implementing `VisitOutput`. +pub trait MakeOutput<T, Out> +where + Self: MakeVisitor<T> + crate::sealed::Sealed<(T, Out)>, + Self::Visitor: VisitOutput<Out>, +{ + /// Visits all fields in `fields` with a new visitor constructed from + /// `target`. + fn visit_with<F>(&self, target: T, fields: &F) -> Out + where + F: RecordFields, + { + self.make_visitor(target).visit(fields) + } +} + +feature! { + #![feature = "std"] + use std::io; + + /// Extension trait implemented by visitors to indicate that they write to an + /// `io::Write` instance, and allow access to that writer. + pub trait VisitWrite: VisitOutput<Result<(), io::Error>> { + /// Returns the writer that this visitor writes to. + fn writer(&mut self) -> &mut dyn io::Write; + } +} + +/// Extension trait implemented by visitors to indicate that they write to a +/// `fmt::Write` instance, and allow access to that writer. +pub trait VisitFmt: VisitOutput<fmt::Result> { + /// Returns the formatter that this visitor writes to. + fn writer(&mut self) -> &mut dyn fmt::Write; +} + +/// Extension trait providing `MakeVisitor` combinators. +pub trait MakeExt<T> +where + Self: MakeVisitor<T> + Sized, + Self: crate::sealed::Sealed<MakeExtMarker<T>>, +{ + /// Wraps `self` so that any `fmt::Debug` fields are recorded using the + /// alternate formatter (`{:#?}`). + fn debug_alt(self) -> debug::Alt<Self> { + debug::Alt::new(self) + } + + /// Wraps `self` so that any string fields named "message" are recorded + /// using `fmt::Display`. + fn display_messages(self) -> display::Messages<Self> { + display::Messages::new(self) + } + + /// Wraps `self` so that when fields are formatted to a writer, they are + /// separated by the provided `delimiter`. + fn delimited<D>(self, delimiter: D) -> delimited::Delimited<D, Self> + where + D: AsRef<str> + Clone, + Self::Visitor: VisitFmt, + { + delimited::Delimited::new(delimiter, self) + } +} + +// === impl RecordFields === + +impl<'a> crate::sealed::Sealed<RecordFieldsMarker> for Event<'a> {} +impl<'a> RecordFields for Event<'a> { + fn record(&self, visitor: &mut dyn Visit) { + Event::record(self, visitor) + } +} + +impl<'a> crate::sealed::Sealed<RecordFieldsMarker> for Attributes<'a> {} +impl<'a> RecordFields for Attributes<'a> { + fn record(&self, visitor: &mut dyn Visit) { + Attributes::record(self, visitor) + } +} + +impl<'a> crate::sealed::Sealed<RecordFieldsMarker> for Record<'a> {} +impl<'a> RecordFields for Record<'a> { + fn record(&self, visitor: &mut dyn Visit) { + Record::record(self, visitor) + } +} + +impl<'a, F> crate::sealed::Sealed<RecordFieldsMarker> for &'a F where F: RecordFields {} +impl<'a, F> RecordFields for &'a F +where + F: RecordFields, +{ + fn record(&self, visitor: &mut dyn Visit) { + F::record(*self, visitor) + } +} + +// === blanket impls === + +impl<T, V, F> MakeVisitor<T> for F +where + F: Fn(T) -> V, + V: Visit, +{ + type Visitor = V; + fn make_visitor(&self, target: T) -> Self::Visitor { + (self)(target) + } +} + +impl<T, Out, M> crate::sealed::Sealed<(T, Out)> for M +where + M: MakeVisitor<T>, + M::Visitor: VisitOutput<Out>, +{ +} + +impl<T, Out, M> MakeOutput<T, Out> for M +where + M: MakeVisitor<T>, + M::Visitor: VisitOutput<Out>, +{ +} + +impl<T, M> crate::sealed::Sealed<MakeExtMarker<T>> for M where M: MakeVisitor<T> + Sized {} + +impl<T, M> MakeExt<T> for M +where + M: MakeVisitor<T> + Sized, + M: crate::sealed::Sealed<MakeExtMarker<T>>, +{ +} + +#[derive(Debug)] +#[doc(hidden)] +pub struct MakeExtMarker<T> { + _p: PhantomData<T>, +} + +#[derive(Debug)] +#[doc(hidden)] +pub struct RecordFieldsMarker { + _p: (), +} + +#[cfg(all(test, feature = "alloc"))] +#[macro_use] +pub(in crate::field) mod test_util { + use super::*; + pub(in crate::field) use alloc::string::String; + use tracing_core::{ + callsite::Callsite, + field::{Field, Value}, + metadata::{Kind, Level, Metadata}, + }; + + pub(crate) struct TestAttrs1; + pub(crate) struct TestAttrs2; + + impl TestAttrs1 { + pub(crate) fn with<T>(f: impl FnOnce(Attributes<'_>) -> T) -> T { + let fieldset = TEST_META_1.fields(); + let values = &[ + ( + &fieldset.field("question").unwrap(), + Some(&"life, the universe, and everything" as &dyn Value), + ), + (&fieldset.field("question.answer").unwrap(), None), + ( + &fieldset.field("tricky").unwrap(), + Some(&true as &dyn Value), + ), + ( + &fieldset.field("can_you_do_it").unwrap(), + Some(&true as &dyn Value), + ), + ]; + let valueset = fieldset.value_set(values); + let attrs = tracing_core::span::Attributes::new(&TEST_META_1, &valueset); + f(attrs) + } + } + + impl TestAttrs2 { + pub(crate) fn with<T>(f: impl FnOnce(Attributes<'_>) -> T) -> T { + let fieldset = TEST_META_1.fields(); + let none = tracing_core::field::debug(&Option::<&str>::None); + let values = &[ + ( + &fieldset.field("question").unwrap(), + Some(&none as &dyn Value), + ), + ( + &fieldset.field("question.answer").unwrap(), + Some(&42 as &dyn Value), + ), + ( + &fieldset.field("tricky").unwrap(), + Some(&true as &dyn Value), + ), + ( + &fieldset.field("can_you_do_it").unwrap(), + Some(&false as &dyn Value), + ), + ]; + let valueset = fieldset.value_set(values); + let attrs = tracing_core::span::Attributes::new(&TEST_META_1, &valueset); + f(attrs) + } + } + + struct TestCallsite1; + static TEST_CALLSITE_1: &'static dyn Callsite = &TestCallsite1; + static TEST_META_1: Metadata<'static> = tracing_core::metadata! { + name: "field_test1", + target: module_path!(), + level: Level::INFO, + fields: &["question", "question.answer", "tricky", "can_you_do_it"], + callsite: TEST_CALLSITE_1, + kind: Kind::SPAN, + }; + + impl Callsite for TestCallsite1 { + fn set_interest(&self, _: tracing_core::subscriber::Interest) { + unimplemented!() + } + + fn metadata(&self) -> &Metadata<'_> { + &TEST_META_1 + } + } + + pub(crate) struct MakeDebug; + pub(crate) struct DebugVisitor<'a> { + writer: &'a mut dyn fmt::Write, + err: fmt::Result, + } + + impl<'a> DebugVisitor<'a> { + pub(crate) fn new(writer: &'a mut dyn fmt::Write) -> Self { + Self { + writer, + err: Ok(()), + } + } + } + + impl<'a> Visit for DebugVisitor<'a> { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + write!(self.writer, "{}={:?}", field, value).unwrap(); + } + } + + impl<'a> VisitOutput<fmt::Result> for DebugVisitor<'a> { + fn finish(self) -> fmt::Result { + self.err + } + } + + impl<'a> VisitFmt for DebugVisitor<'a> { + fn writer(&mut self) -> &mut dyn fmt::Write { + self.writer + } + } + + impl<'a> MakeVisitor<&'a mut dyn fmt::Write> for MakeDebug { + type Visitor = DebugVisitor<'a>; + fn make_visitor(&self, w: &'a mut dyn fmt::Write) -> DebugVisitor<'a> { + DebugVisitor::new(w) + } + } +} diff --git a/vendor/tracing-subscriber/src/filter/directive.rs b/vendor/tracing-subscriber/src/filter/directive.rs new file mode 100644 index 000000000..2ae3f0f24 --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/directive.rs @@ -0,0 +1,456 @@ +use crate::filter::level::{self, LevelFilter}; +#[cfg(not(feature = "smallvec"))] +use alloc::vec; +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; + +use core::{cmp::Ordering, fmt, iter::FromIterator, slice, str::FromStr}; +use tracing_core::{Level, Metadata}; +/// Indicates that a string could not be parsed as a filtering directive. +#[derive(Debug)] +pub struct ParseError { + kind: ParseErrorKind, +} + +/// A directive which will statically enable or disable a given callsite. +/// +/// Unlike a dynamic directive, this can be cached by the callsite. +#[derive(Debug, PartialEq, Eq, Clone)] +pub(crate) struct StaticDirective { + pub(in crate::filter) target: Option<String>, + pub(in crate::filter) field_names: Vec<String>, + pub(in crate::filter) level: LevelFilter, +} + +#[cfg(feature = "smallvec")] +pub(crate) type FilterVec<T> = smallvec::SmallVec<[T; 8]>; +#[cfg(not(feature = "smallvec"))] +pub(crate) type FilterVec<T> = Vec<T>; + +#[derive(Debug, PartialEq, Clone)] +pub(in crate::filter) struct DirectiveSet<T> { + directives: FilterVec<T>, + pub(in crate::filter) max_level: LevelFilter, +} + +pub(in crate::filter) trait Match { + fn cares_about(&self, meta: &Metadata<'_>) -> bool; + fn level(&self) -> &LevelFilter; +} + +#[derive(Debug)] +enum ParseErrorKind { + #[cfg(feature = "std")] + Field(Box<dyn std::error::Error + Send + Sync>), + Level(level::ParseError), + Other(Option<&'static str>), +} + +// === impl DirectiveSet === + +impl<T> DirectiveSet<T> { + #[cfg(feature = "env-filter")] + pub(crate) fn is_empty(&self) -> bool { + self.directives.is_empty() + } + + pub(crate) fn iter(&self) -> slice::Iter<'_, T> { + self.directives.iter() + } +} + +impl<T: Ord> Default for DirectiveSet<T> { + fn default() -> Self { + Self { + directives: FilterVec::new(), + max_level: LevelFilter::OFF, + } + } +} + +impl<T: Match + Ord> DirectiveSet<T> { + pub(crate) fn directives(&self) -> impl Iterator<Item = &T> { + self.directives.iter() + } + + pub(crate) fn directives_for<'a>( + &'a self, + metadata: &'a Metadata<'a>, + ) -> impl Iterator<Item = &'a T> + 'a { + self.directives().filter(move |d| d.cares_about(metadata)) + } + + pub(crate) fn add(&mut self, directive: T) { + // does this directive enable a more verbose level than the current + // max? if so, update the max level. + let level = *directive.level(); + if level > self.max_level { + self.max_level = level; + } + // insert the directive into the vec of directives, ordered by + // specificity (length of target + number of field filters). this + // ensures that, when finding a directive to match a span or event, we + // search the directive set in most specific first order. + match self.directives.binary_search(&directive) { + Ok(i) => self.directives[i] = directive, + Err(i) => self.directives.insert(i, directive), + } + } + + #[cfg(test)] + pub(in crate::filter) fn into_vec(self) -> FilterVec<T> { + self.directives + } +} + +impl<T: Match + Ord> FromIterator<T> for DirectiveSet<T> { + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { + let mut this = Self::default(); + this.extend(iter); + this + } +} + +impl<T: Match + Ord> Extend<T> for DirectiveSet<T> { + fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { + for directive in iter.into_iter() { + self.add(directive); + } + } +} + +impl<T> IntoIterator for DirectiveSet<T> { + type Item = T; + + #[cfg(feature = "smallvec")] + type IntoIter = smallvec::IntoIter<[T; 8]>; + #[cfg(not(feature = "smallvec"))] + type IntoIter = vec::IntoIter<T>; + + fn into_iter(self) -> Self::IntoIter { + self.directives.into_iter() + } +} + +// === impl Statics === + +impl DirectiveSet<StaticDirective> { + pub(crate) fn enabled(&self, meta: &Metadata<'_>) -> bool { + let level = meta.level(); + match self.directives_for(meta).next() { + Some(d) => d.level >= *level, + None => false, + } + } + + /// Same as `enabled` above, but skips `Directive`'s with fields. + pub(crate) fn target_enabled(&self, target: &str, level: &Level) -> bool { + match self.directives_for_target(target).next() { + Some(d) => d.level >= *level, + None => false, + } + } + + pub(crate) fn directives_for_target<'a>( + &'a self, + target: &'a str, + ) -> impl Iterator<Item = &'a StaticDirective> + 'a { + self.directives() + .filter(move |d| d.cares_about_target(target)) + } +} + +// === impl StaticDirective === + +impl StaticDirective { + pub(in crate::filter) fn new( + target: Option<String>, + field_names: Vec<String>, + level: LevelFilter, + ) -> Self { + Self { + target, + field_names, + level, + } + } + + pub(in crate::filter) fn cares_about_target(&self, to_check: &str) -> bool { + // Does this directive have a target filter, and does it match the + // metadata's target? + if let Some(ref target) = self.target { + if !to_check.starts_with(&target[..]) { + return false; + } + } + + if !self.field_names.is_empty() { + return false; + } + + true + } +} + +impl Ord for StaticDirective { + fn cmp(&self, other: &StaticDirective) -> Ordering { + // We attempt to order directives by how "specific" they are. This + // ensures that we try the most specific directives first when + // attempting to match a piece of metadata. + + // First, we compare based on whether a target is specified, and the + // lengths of those targets if both have targets. + let ordering = self + .target + .as_ref() + .map(String::len) + .cmp(&other.target.as_ref().map(String::len)) + // Then we compare how many field names are matched by each directive. + .then_with(|| self.field_names.len().cmp(&other.field_names.len())) + // Finally, we fall back to lexicographical ordering if the directives are + // equally specific. Although this is no longer semantically important, + // we need to define a total ordering to determine the directive's place + // in the BTreeMap. + .then_with(|| { + self.target + .cmp(&other.target) + .then_with(|| self.field_names[..].cmp(&other.field_names[..])) + }) + .reverse(); + + #[cfg(debug_assertions)] + { + if ordering == Ordering::Equal { + debug_assert_eq!( + self.target, other.target, + "invariant violated: Ordering::Equal must imply a.target == b.target" + ); + debug_assert_eq!( + self.field_names, other.field_names, + "invariant violated: Ordering::Equal must imply a.field_names == b.field_names" + ); + } + } + + ordering + } +} + +impl PartialOrd for StaticDirective { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +impl Match for StaticDirective { + fn cares_about(&self, meta: &Metadata<'_>) -> bool { + // Does this directive have a target filter, and does it match the + // metadata's target? + if let Some(ref target) = self.target { + if !meta.target().starts_with(&target[..]) { + return false; + } + } + + if meta.is_event() && !self.field_names.is_empty() { + let fields = meta.fields(); + for name in &self.field_names { + if fields.field(name).is_none() { + return false; + } + } + } + + true + } + + fn level(&self) -> &LevelFilter { + &self.level + } +} + +impl Default for StaticDirective { + fn default() -> Self { + StaticDirective { + target: None, + field_names: Vec::new(), + level: LevelFilter::ERROR, + } + } +} + +impl fmt::Display for StaticDirective { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut wrote_any = false; + if let Some(ref target) = self.target { + fmt::Display::fmt(target, f)?; + wrote_any = true; + } + + if !self.field_names.is_empty() { + f.write_str("[")?; + + let mut fields = self.field_names.iter(); + if let Some(field) = fields.next() { + write!(f, "{{{}", field)?; + for field in fields { + write!(f, ",{}", field)?; + } + f.write_str("}")?; + } + + f.write_str("]")?; + wrote_any = true; + } + + if wrote_any { + f.write_str("=")?; + } + + fmt::Display::fmt(&self.level, f) + } +} + +impl FromStr for StaticDirective { + type Err = ParseError; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + // This method parses a filtering directive in one of the following + // forms: + // + // * `foo=trace` (TARGET=LEVEL) + // * `foo[{bar,baz}]=info` (TARGET[{FIELD,+}]=LEVEL) + // * `trace` (bare LEVEL) + // * `foo` (bare TARGET) + let mut split = s.split('='); + let part0 = split + .next() + .ok_or_else(|| ParseError::msg("string must not be empty"))?; + + // Directive includes an `=`: + // * `foo=trace` + // * `foo[{bar}]=trace` + // * `foo[{bar,baz}]=trace` + if let Some(part1) = split.next() { + if split.next().is_some() { + return Err(ParseError::msg( + "too many '=' in filter directive, expected 0 or 1", + )); + } + + let mut split = part0.split("[{"); + let target = split.next().map(String::from); + let mut field_names = Vec::new(); + // Directive includes fields: + // * `foo[{bar}]=trace` + // * `foo[{bar,baz}]=trace` + if let Some(maybe_fields) = split.next() { + if split.next().is_some() { + return Err(ParseError::msg( + "too many '[{' in filter directive, expected 0 or 1", + )); + } + + if !maybe_fields.ends_with("}]") { + return Err(ParseError::msg("expected fields list to end with '}]'")); + } + + let fields = maybe_fields + .trim_end_matches("}]") + .split(',') + .filter_map(|s| { + if s.is_empty() { + None + } else { + Some(String::from(s)) + } + }); + field_names.extend(fields); + }; + let level = part1.parse()?; + return Ok(Self { + level, + field_names, + target, + }); + } + + // Okay, the part after the `=` was empty, the directive is either a + // bare level or a bare target. + // * `foo` + // * `info` + Ok(match part0.parse::<LevelFilter>() { + Ok(level) => Self { + level, + target: None, + field_names: Vec::new(), + }, + Err(_) => Self { + target: Some(String::from(part0)), + level: LevelFilter::TRACE, + field_names: Vec::new(), + }, + }) + } +} + +// === impl ParseError === + +impl ParseError { + #[cfg(feature = "env-filter")] + pub(crate) fn new() -> Self { + ParseError { + kind: ParseErrorKind::Other(None), + } + } + + pub(crate) fn msg(s: &'static str) -> Self { + ParseError { + kind: ParseErrorKind::Other(Some(s)), + } + } +} + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.kind { + ParseErrorKind::Other(None) => f.pad("invalid filter directive"), + ParseErrorKind::Other(Some(msg)) => write!(f, "invalid filter directive: {}", msg), + ParseErrorKind::Level(ref l) => l.fmt(f), + #[cfg(feature = "std")] + ParseErrorKind::Field(ref e) => write!(f, "invalid field filter: {}", e), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for ParseError { + fn description(&self) -> &str { + "invalid filter directive" + } + + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self.kind { + ParseErrorKind::Other(_) => None, + ParseErrorKind::Level(ref l) => Some(l), + ParseErrorKind::Field(ref n) => Some(n.as_ref()), + } + } +} + +#[cfg(feature = "std")] +impl From<Box<dyn std::error::Error + Send + Sync>> for ParseError { + fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self { + Self { + kind: ParseErrorKind::Field(e), + } + } +} + +impl From<level::ParseError> for ParseError { + fn from(l: level::ParseError) -> Self { + Self { + kind: ParseErrorKind::Level(l), + } + } +} diff --git a/vendor/tracing-subscriber/src/filter/env/builder.rs b/vendor/tracing-subscriber/src/filter/env/builder.rs new file mode 100644 index 000000000..36b520543 --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/env/builder.rs @@ -0,0 +1,324 @@ +use super::{ + directive::{self, Directive}, + EnvFilter, FromEnvError, +}; +use crate::sync::RwLock; +use std::env; +use thread_local::ThreadLocal; +use tracing::level_filters::STATIC_MAX_LEVEL; + +/// A [builder] for constructing new [`EnvFilter`]s. +/// +/// [builder]: https://rust-unofficial.github.io/patterns/patterns/creational/builder.html +#[derive(Debug, Clone)] +pub struct Builder { + regex: bool, + env: Option<String>, + default_directive: Option<Directive>, +} + +impl Builder { + /// Sets whether span field values can be matched with regular expressions. + /// + /// If this is `true`, field filter directives will be interpreted as + /// regular expressions if they are not able to be interpreted as a `bool`, + /// `i64`, `u64`, or `f64` literal. If this is `false,` those field values + /// will be interpreted as literal [`std::fmt::Debug`] output instead. + /// + /// By default, regular expressions are enabled. + /// + /// **Note**: when [`EnvFilter`]s are constructed from untrusted inputs, + /// disabling regular expressions is strongly encouraged. + pub fn with_regex(self, regex: bool) -> Self { + Self { regex, ..self } + } + + /// Sets a default [filtering directive] that will be added to the filter if + /// the parsed string or environment variable contains no filter directives. + /// + /// By default, there is no default directive. + /// + /// # Examples + /// + /// If [`parse`], [`parse_lossy`], [`from_env`], or [`from_env_lossy`] are + /// called with an empty string or environment variable, the default + /// directive is used instead: + /// + /// ```rust + /// # fn main() -> Result<(), Box<dyn std::error::Error>> { + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// let filter = EnvFilter::builder() + /// .with_default_directive(LevelFilter::INFO.into()) + /// .parse("")?; + /// + /// assert_eq!(format!("{}", filter), "info"); + /// # Ok(()) } + /// ``` + /// + /// Note that the `lossy` variants ([`parse_lossy`] and [`from_env_lossy`]) + /// will ignore any invalid directives. If all directives in a filter + /// string or environment variable are invalid, those methods will also use + /// the default directive: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// let filter = EnvFilter::builder() + /// .with_default_directive(LevelFilter::INFO.into()) + /// .parse_lossy("some_target=fake level,foo::bar=lolwut"); + /// + /// assert_eq!(format!("{}", filter), "info"); + /// ``` + /// + /// + /// If the string or environment variable contains valid filtering + /// directives, the default directive is not used: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// let filter = EnvFilter::builder() + /// .with_default_directive(LevelFilter::INFO.into()) + /// .parse_lossy("foo=trace"); + /// + /// // The default directive is *not* used: + /// assert_eq!(format!("{}", filter), "foo=trace"); + /// ``` + /// + /// Parsing a more complex default directive from a string: + /// + /// ```rust + /// # fn main() -> Result<(), Box<dyn std::error::Error>> { + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// let default = "myapp=debug".parse() + /// .expect("hard-coded default directive should be valid"); + /// + /// let filter = EnvFilter::builder() + /// .with_default_directive(default) + /// .parse("")?; + /// + /// assert_eq!(format!("{}", filter), "myapp=debug"); + /// # Ok(()) } + /// ``` + /// + /// [`parse_lossy`]: Self::parse_lossy + /// [`from_env_lossy`]: Self::from_env_lossy + /// [`parse`]: Self::parse + /// [`from_env`]: Self::from_env + pub fn with_default_directive(self, default_directive: Directive) -> Self { + Self { + default_directive: Some(default_directive), + ..self + } + } + + /// Sets the name of the environment variable used by the [`from_env`], + /// [`from_env_lossy`], and [`try_from_env`] methods. + /// + /// By default, this is the value of [`EnvFilter::DEFAULT_ENV`] + /// (`RUST_LOG`). + /// + /// [`from_env`]: Self::from_env + /// [`from_env_lossy`]: Self::from_env_lossy + /// [`try_from_env`]: Self::try_from_env + pub fn with_env_var(self, var: impl ToString) -> Self { + Self { + env: Some(var.to_string()), + ..self + } + } + + /// Returns a new [`EnvFilter`] from the directives in the given string, + /// *ignoring* any that are invalid. + pub fn parse_lossy<S: AsRef<str>>(&self, dirs: S) -> EnvFilter { + let directives = dirs + .as_ref() + .split(',') + .filter(|s| !s.is_empty()) + .filter_map(|s| match Directive::parse(s, self.regex) { + Ok(d) => Some(d), + Err(err) => { + eprintln!("ignoring `{}`: {}", s, err); + None + } + }); + self.from_directives(directives) + } + + /// Returns a new [`EnvFilter`] from the directives in the given string, + /// or an error if any are invalid. + pub fn parse<S: AsRef<str>>(&self, dirs: S) -> Result<EnvFilter, directive::ParseError> { + let dirs = dirs.as_ref(); + if dirs.is_empty() { + return Ok(self.from_directives(std::iter::empty())); + } + let directives = dirs + .split(',') + .filter(|s| !s.is_empty()) + .map(|s| Directive::parse(s, self.regex)) + .collect::<Result<Vec<_>, _>>()?; + Ok(self.from_directives(directives)) + } + + /// Returns a new [`EnvFilter`] from the directives in the configured + /// environment variable, ignoring any directives that are invalid. + pub fn from_env_lossy(&self) -> EnvFilter { + let var = env::var(self.env_var_name()).unwrap_or_default(); + self.parse_lossy(var) + } + + /// Returns a new [`EnvFilter`] from the directives in the in the configured + /// environment variable, or an error if the environment variable is not set + /// or contains invalid directives. + pub fn from_env(&self) -> Result<EnvFilter, FromEnvError> { + let var = env::var(self.env_var_name()).unwrap_or_default(); + self.parse(var).map_err(Into::into) + } + + /// Returns a new [`EnvFilter`] from the directives in the in the configured + /// environment variable, or an error if the environment variable is not set + /// or contains invalid directives. + pub fn try_from_env(&self) -> Result<EnvFilter, FromEnvError> { + let var = env::var(self.env_var_name())?; + self.parse(var).map_err(Into::into) + } + + // TODO(eliza): consider making this a public API? + // Clippy doesn't love this naming, because it suggests that `from_` methods + // should not take a `Self`...but in this case, it's the `EnvFilter` that is + // being constructed "from" the directives, rather than the builder itself. + #[allow(clippy::wrong_self_convention)] + pub(super) fn from_directives( + &self, + directives: impl IntoIterator<Item = Directive>, + ) -> EnvFilter { + use tracing::Level; + + let mut directives: Vec<_> = directives.into_iter().collect(); + let mut disabled = Vec::new(); + for directive in &mut directives { + if directive.level > STATIC_MAX_LEVEL { + disabled.push(directive.clone()); + } + if !self.regex { + directive.deregexify(); + } + } + + if !disabled.is_empty() { + #[cfg(feature = "ansi_term")] + use ansi_term::{Color, Style}; + // NOTE: We can't use a configured `MakeWriter` because the EnvFilter + // has no knowledge of any underlying subscriber or collector, which + // may or may not use a `MakeWriter`. + let warn = |msg: &str| { + #[cfg(not(feature = "ansi_term"))] + let msg = format!("warning: {}", msg); + #[cfg(feature = "ansi_term")] + let msg = { + let bold = Style::new().bold(); + let mut warning = Color::Yellow.paint("warning"); + warning.style_ref_mut().is_bold = true; + format!("{}{} {}", warning, bold.paint(":"), bold.paint(msg)) + }; + eprintln!("{}", msg); + }; + let ctx_prefixed = |prefix: &str, msg: &str| { + #[cfg(not(feature = "ansi_term"))] + let msg = format!("{} {}", prefix, msg); + #[cfg(feature = "ansi_term")] + let msg = { + let mut equal = Color::Fixed(21).paint("="); // dark blue + equal.style_ref_mut().is_bold = true; + format!(" {} {} {}", equal, Style::new().bold().paint(prefix), msg) + }; + eprintln!("{}", msg); + }; + let ctx_help = |msg| ctx_prefixed("help:", msg); + let ctx_note = |msg| ctx_prefixed("note:", msg); + let ctx = |msg: &str| { + #[cfg(not(feature = "ansi_term"))] + let msg = format!("note: {}", msg); + #[cfg(feature = "ansi_term")] + let msg = { + let mut pipe = Color::Fixed(21).paint("|"); + pipe.style_ref_mut().is_bold = true; + format!(" {} {}", pipe, msg) + }; + eprintln!("{}", msg); + }; + warn("some trace filter directives would enable traces that are disabled statically"); + for directive in disabled { + let target = if let Some(target) = &directive.target { + format!("the `{}` target", target) + } else { + "all targets".into() + }; + let level = directive + .level + .into_level() + .expect("=off would not have enabled any filters"); + ctx(&format!( + "`{}` would enable the {} level for {}", + directive, level, target + )); + } + ctx_note(&format!("the static max level is `{}`", STATIC_MAX_LEVEL)); + let help_msg = || { + let (feature, filter) = match STATIC_MAX_LEVEL.into_level() { + Some(Level::TRACE) => unreachable!( + "if the max level is trace, no static filtering features are enabled" + ), + Some(Level::DEBUG) => ("max_level_debug", Level::TRACE), + Some(Level::INFO) => ("max_level_info", Level::DEBUG), + Some(Level::WARN) => ("max_level_warn", Level::INFO), + Some(Level::ERROR) => ("max_level_error", Level::WARN), + None => return ("max_level_off", String::new()), + }; + (feature, format!("{} ", filter)) + }; + let (feature, earlier_level) = help_msg(); + ctx_help(&format!( + "to enable {}logging, remove the `{}` feature", + earlier_level, feature + )); + } + + let (dynamics, statics) = Directive::make_tables(directives); + let has_dynamics = !dynamics.is_empty(); + + let mut filter = EnvFilter { + statics, + dynamics, + has_dynamics, + by_id: RwLock::new(Default::default()), + by_cs: RwLock::new(Default::default()), + scope: ThreadLocal::new(), + regex: self.regex, + }; + + if !has_dynamics && filter.statics.is_empty() { + if let Some(ref default) = self.default_directive { + filter = filter.add_directive(default.clone()); + } + } + + filter + } + + fn env_var_name(&self) -> &str { + self.env.as_deref().unwrap_or(EnvFilter::DEFAULT_ENV) + } +} + +impl Default for Builder { + fn default() -> Self { + Self { + regex: true, + env: None, + default_directive: None, + } + } +} diff --git a/vendor/tracing-subscriber/src/filter/env/directive.rs b/vendor/tracing-subscriber/src/filter/env/directive.rs new file mode 100644 index 000000000..f062e6ef9 --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/env/directive.rs @@ -0,0 +1,860 @@ +pub(crate) use crate::filter::directive::{FilterVec, ParseError, StaticDirective}; +use crate::filter::{ + directive::{DirectiveSet, Match}, + env::{field, FieldMap}, + level::LevelFilter, +}; +use once_cell::sync::Lazy; +use regex::Regex; +use std::{cmp::Ordering, fmt, iter::FromIterator, str::FromStr}; +use tracing_core::{span, Level, Metadata}; + +/// A single filtering directive. +// TODO(eliza): add a builder for programmatically constructing directives? +#[derive(Clone, Debug, Eq, PartialEq)] +#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] +pub struct Directive { + in_span: Option<String>, + fields: Vec<field::Match>, + pub(crate) target: Option<String>, + pub(crate) level: LevelFilter, +} + +/// A set of dynamic filtering directives. +pub(super) type Dynamics = DirectiveSet<Directive>; + +/// A set of static filtering directives. +pub(super) type Statics = DirectiveSet<StaticDirective>; + +pub(crate) type CallsiteMatcher = MatchSet<field::CallsiteMatch>; +pub(crate) type SpanMatcher = MatchSet<field::SpanMatch>; + +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct MatchSet<T> { + field_matches: FilterVec<T>, + base_level: LevelFilter, +} + +impl Directive { + pub(super) fn has_name(&self) -> bool { + self.in_span.is_some() + } + + pub(super) fn has_fields(&self) -> bool { + !self.fields.is_empty() + } + + pub(super) fn to_static(&self) -> Option<StaticDirective> { + if !self.is_static() { + return None; + } + + // TODO(eliza): these strings are all immutable; we should consider + // `Arc`ing them to make this more efficient... + let field_names = self.fields.iter().map(field::Match::name).collect(); + + Some(StaticDirective::new( + self.target.clone(), + field_names, + self.level, + )) + } + + fn is_static(&self) -> bool { + !self.has_name() && !self.fields.iter().any(field::Match::has_value) + } + + pub(super) fn is_dynamic(&self) -> bool { + self.has_name() || self.has_fields() + } + + pub(crate) fn field_matcher(&self, meta: &Metadata<'_>) -> Option<field::CallsiteMatch> { + let fieldset = meta.fields(); + let fields = self + .fields + .iter() + .filter_map( + |field::Match { + ref name, + ref value, + }| { + if let Some(field) = fieldset.field(name) { + let value = value.as_ref().cloned()?; + Some(Ok((field, value))) + } else { + Some(Err(())) + } + }, + ) + .collect::<Result<FieldMap<_>, ()>>() + .ok()?; + Some(field::CallsiteMatch { + fields, + level: self.level, + }) + } + + pub(super) fn make_tables( + directives: impl IntoIterator<Item = Directive>, + ) -> (Dynamics, Statics) { + // TODO(eliza): this could be made more efficient... + let (dyns, stats): (Vec<Directive>, Vec<Directive>) = + directives.into_iter().partition(Directive::is_dynamic); + let statics = stats + .into_iter() + .filter_map(|d| d.to_static()) + .chain(dyns.iter().filter_map(Directive::to_static)) + .collect(); + (Dynamics::from_iter(dyns), statics) + } + + pub(super) fn deregexify(&mut self) { + for field in &mut self.fields { + field.value = match field.value.take() { + Some(field::ValueMatch::Pat(pat)) => { + Some(field::ValueMatch::Debug(pat.into_debug_match())) + } + x => x, + } + } + } + + pub(super) fn parse(from: &str, regex: bool) -> Result<Self, ParseError> { + static DIRECTIVE_RE: Lazy<Regex> = Lazy::new(|| Regex::new( + r"(?x) + ^(?P<global_level>(?i:trace|debug|info|warn|error|off|[0-5]))$ | + # ^^^. + # `note: we match log level names case-insensitively + ^ + (?: # target name or span name + (?P<target>[\w:-]+)|(?P<span>\[[^\]]*\]) + ){1,2} + (?: # level or nothing + =(?P<level>(?i:trace|debug|info|warn|error|off|[0-5]))? + # ^^^. + # `note: we match log level names case-insensitively + )? + $ + " + ) + .unwrap()); + static SPAN_PART_RE: Lazy<Regex> = + Lazy::new(|| Regex::new(r#"(?P<name>[^\]\{]+)?(?:\{(?P<fields>[^\}]*)\})?"#).unwrap()); + static FIELD_FILTER_RE: Lazy<Regex> = + // TODO(eliza): this doesn't _currently_ handle value matchers that include comma + // characters. We should fix that. + Lazy::new(|| Regex::new(r#"(?x) + ( + # field name + [[:word:]][[[:word:]]\.]* + # value part (optional) + (?:=[^,]+)? + ) + # trailing comma or EOS + (?:,\s?|$) + "#).unwrap()); + + let caps = DIRECTIVE_RE.captures(from).ok_or_else(ParseError::new)?; + + if let Some(level) = caps + .name("global_level") + .and_then(|s| s.as_str().parse().ok()) + { + return Ok(Directive { + level, + ..Default::default() + }); + } + + let target = caps.name("target").and_then(|c| { + let s = c.as_str(); + if s.parse::<LevelFilter>().is_ok() { + None + } else { + Some(s.to_owned()) + } + }); + + let (in_span, fields) = caps + .name("span") + .and_then(|cap| { + let cap = cap.as_str().trim_matches(|c| c == '[' || c == ']'); + let caps = SPAN_PART_RE.captures(cap)?; + let span = caps.name("name").map(|c| c.as_str().to_owned()); + let fields = caps + .name("fields") + .map(|c| { + FIELD_FILTER_RE + .find_iter(c.as_str()) + .map(|c| field::Match::parse(c.as_str(), regex)) + .collect::<Result<Vec<_>, _>>() + }) + .unwrap_or_else(|| Ok(Vec::new())); + Some((span, fields)) + }) + .unwrap_or_else(|| (None, Ok(Vec::new()))); + + let level = caps + .name("level") + .and_then(|l| l.as_str().parse().ok()) + // Setting the target without the level enables every level for that target + .unwrap_or(LevelFilter::TRACE); + + Ok(Self { + level, + target, + in_span, + fields: fields?, + }) + } +} + +impl Match for Directive { + fn cares_about(&self, meta: &Metadata<'_>) -> bool { + // Does this directive have a target filter, and does it match the + // metadata's target? + if let Some(ref target) = self.target { + if !meta.target().starts_with(&target[..]) { + return false; + } + } + + // Do we have a name filter, and does it match the metadata's name? + // TODO(eliza): put name globbing here? + if let Some(ref name) = self.in_span { + if name != meta.name() { + return false; + } + } + + // Does the metadata define all the fields that this directive cares about? + let actual_fields = meta.fields(); + for expected_field in &self.fields { + // Does the actual field set (from the metadata) contain this field? + if actual_fields.field(&expected_field.name).is_none() { + return false; + } + } + + true + } + + fn level(&self) -> &LevelFilter { + &self.level + } +} + +impl FromStr for Directive { + type Err = ParseError; + fn from_str(from: &str) -> Result<Self, Self::Err> { + Directive::parse(from, true) + } +} + +impl Default for Directive { + fn default() -> Self { + Directive { + level: LevelFilter::OFF, + target: None, + in_span: None, + fields: Vec::new(), + } + } +} + +impl PartialOrd for Directive { + fn partial_cmp(&self, other: &Directive) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +impl Ord for Directive { + fn cmp(&self, other: &Directive) -> Ordering { + // We attempt to order directives by how "specific" they are. This + // ensures that we try the most specific directives first when + // attempting to match a piece of metadata. + + // First, we compare based on whether a target is specified, and the + // lengths of those targets if both have targets. + let ordering = self + .target + .as_ref() + .map(String::len) + .cmp(&other.target.as_ref().map(String::len)) + // Next compare based on the presence of span names. + .then_with(|| self.in_span.is_some().cmp(&other.in_span.is_some())) + // Then we compare how many fields are defined by each + // directive. + .then_with(|| self.fields.len().cmp(&other.fields.len())) + // Finally, we fall back to lexicographical ordering if the directives are + // equally specific. Although this is no longer semantically important, + // we need to define a total ordering to determine the directive's place + // in the BTreeMap. + .then_with(|| { + self.target + .cmp(&other.target) + .then_with(|| self.in_span.cmp(&other.in_span)) + .then_with(|| self.fields[..].cmp(&other.fields[..])) + }) + .reverse(); + + #[cfg(debug_assertions)] + { + if ordering == Ordering::Equal { + debug_assert_eq!( + self.target, other.target, + "invariant violated: Ordering::Equal must imply a.target == b.target" + ); + debug_assert_eq!( + self.in_span, other.in_span, + "invariant violated: Ordering::Equal must imply a.in_span == b.in_span" + ); + debug_assert_eq!( + self.fields, other.fields, + "invariant violated: Ordering::Equal must imply a.fields == b.fields" + ); + } + } + + ordering + } +} + +impl fmt::Display for Directive { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut wrote_any = false; + if let Some(ref target) = self.target { + fmt::Display::fmt(target, f)?; + wrote_any = true; + } + + if self.in_span.is_some() || !self.fields.is_empty() { + f.write_str("[")?; + + if let Some(ref span) = self.in_span { + fmt::Display::fmt(span, f)?; + } + + let mut fields = self.fields.iter(); + if let Some(field) = fields.next() { + write!(f, "{{{}", field)?; + for field in fields { + write!(f, ",{}", field)?; + } + f.write_str("}")?; + } + + f.write_str("]")?; + wrote_any = true; + } + + if wrote_any { + f.write_str("=")?; + } + + fmt::Display::fmt(&self.level, f) + } +} + +impl From<LevelFilter> for Directive { + fn from(level: LevelFilter) -> Self { + Self { + level, + ..Self::default() + } + } +} + +impl From<Level> for Directive { + fn from(level: Level) -> Self { + LevelFilter::from_level(level).into() + } +} + +// === impl Dynamics === + +impl Dynamics { + pub(crate) fn matcher(&self, metadata: &Metadata<'_>) -> Option<CallsiteMatcher> { + let mut base_level = None; + let field_matches = self + .directives_for(metadata) + .filter_map(|d| { + if let Some(f) = d.field_matcher(metadata) { + return Some(f); + } + match base_level { + Some(ref b) if d.level > *b => base_level = Some(d.level), + None => base_level = Some(d.level), + _ => {} + } + None + }) + .collect(); + + if let Some(base_level) = base_level { + Some(CallsiteMatcher { + field_matches, + base_level, + }) + } else if !field_matches.is_empty() { + Some(CallsiteMatcher { + field_matches, + base_level: base_level.unwrap_or(LevelFilter::OFF), + }) + } else { + None + } + } + + pub(crate) fn has_value_filters(&self) -> bool { + self.directives() + .any(|d| d.fields.iter().any(|f| f.value.is_some())) + } +} + +// ===== impl DynamicMatch ===== + +impl CallsiteMatcher { + /// Create a new `SpanMatch` for a given instance of the matched callsite. + pub(crate) fn to_span_match(&self, attrs: &span::Attributes<'_>) -> SpanMatcher { + let field_matches = self + .field_matches + .iter() + .map(|m| { + let m = m.to_span_match(); + attrs.record(&mut m.visitor()); + m + }) + .collect(); + SpanMatcher { + field_matches, + base_level: self.base_level, + } + } +} + +impl SpanMatcher { + /// Returns the level currently enabled for this callsite. + pub(crate) fn level(&self) -> LevelFilter { + self.field_matches + .iter() + .filter_map(field::SpanMatch::filter) + .max() + .unwrap_or(self.base_level) + } + + pub(crate) fn record_update(&self, record: &span::Record<'_>) { + for m in &self.field_matches { + record.record(&mut m.visitor()) + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + fn parse_directives(dirs: impl AsRef<str>) -> Vec<Directive> { + dirs.as_ref() + .split(',') + .filter_map(|s| s.parse().ok()) + .collect() + } + + fn expect_parse(dirs: impl AsRef<str>) -> Vec<Directive> { + dirs.as_ref() + .split(',') + .map(|s| { + s.parse() + .unwrap_or_else(|err| panic!("directive '{:?}' should parse: {}", s, err)) + }) + .collect() + } + + #[test] + fn directive_ordering_by_target_len() { + // TODO(eliza): it would be nice to have a property-based test for this + // instead. + let mut dirs = expect_parse( + "foo::bar=debug,foo::bar::baz=trace,foo=info,a_really_long_name_with_no_colons=warn", + ); + dirs.sort_unstable(); + + let expected = vec![ + "a_really_long_name_with_no_colons", + "foo::bar::baz", + "foo::bar", + "foo", + ]; + let sorted = dirs + .iter() + .map(|d| d.target.as_ref().unwrap()) + .collect::<Vec<_>>(); + + assert_eq!(expected, sorted); + } + #[test] + fn directive_ordering_by_span() { + // TODO(eliza): it would be nice to have a property-based test for this + // instead. + let mut dirs = expect_parse("bar[span]=trace,foo=debug,baz::quux=info,a[span]=warn"); + dirs.sort_unstable(); + + let expected = vec!["baz::quux", "bar", "foo", "a"]; + let sorted = dirs + .iter() + .map(|d| d.target.as_ref().unwrap()) + .collect::<Vec<_>>(); + + assert_eq!(expected, sorted); + } + + #[test] + fn directive_ordering_uses_lexicographic_when_equal() { + // TODO(eliza): it would be nice to have a property-based test for this + // instead. + let mut dirs = expect_parse("span[b]=debug,b=debug,a=trace,c=info,span[a]=info"); + dirs.sort_unstable(); + + let expected = vec![ + ("span", Some("b")), + ("span", Some("a")), + ("c", None), + ("b", None), + ("a", None), + ]; + let sorted = dirs + .iter() + .map(|d| { + ( + d.target.as_ref().unwrap().as_ref(), + d.in_span.as_ref().map(String::as_ref), + ) + }) + .collect::<Vec<_>>(); + + assert_eq!(expected, sorted); + } + + // TODO: this test requires the parser to support directives with multiple + // fields, which it currently can't handle. We should enable this test when + // that's implemented. + #[test] + #[ignore] + fn directive_ordering_by_field_num() { + // TODO(eliza): it would be nice to have a property-based test for this + // instead. + let mut dirs = expect_parse( + "b[{foo,bar}]=info,c[{baz,quuux,quuux}]=debug,a[{foo}]=warn,bar[{field}]=trace,foo=debug,baz::quux=info" + ); + dirs.sort_unstable(); + + let expected = vec!["baz::quux", "bar", "foo", "c", "b", "a"]; + let sorted = dirs + .iter() + .map(|d| d.target.as_ref().unwrap()) + .collect::<Vec<_>>(); + + assert_eq!(expected, sorted); + } + + #[test] + fn parse_directives_ralith() { + let dirs = parse_directives("common=trace,server=trace"); + assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("common".to_string())); + assert_eq!(dirs[0].level, LevelFilter::TRACE); + assert_eq!(dirs[0].in_span, None); + + assert_eq!(dirs[1].target, Some("server".to_string())); + assert_eq!(dirs[1].level, LevelFilter::TRACE); + assert_eq!(dirs[1].in_span, None); + } + + #[test] + fn parse_directives_ralith_uc() { + let dirs = parse_directives("common=INFO,server=DEBUG"); + assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("common".to_string())); + assert_eq!(dirs[0].level, LevelFilter::INFO); + assert_eq!(dirs[0].in_span, None); + + assert_eq!(dirs[1].target, Some("server".to_string())); + assert_eq!(dirs[1].level, LevelFilter::DEBUG); + assert_eq!(dirs[1].in_span, None); + } + + #[test] + fn parse_directives_ralith_mixed() { + let dirs = parse_directives("common=iNfo,server=dEbUg"); + assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("common".to_string())); + assert_eq!(dirs[0].level, LevelFilter::INFO); + assert_eq!(dirs[0].in_span, None); + + assert_eq!(dirs[1].target, Some("server".to_string())); + assert_eq!(dirs[1].level, LevelFilter::DEBUG); + assert_eq!(dirs[1].in_span, None); + } + + #[test] + fn parse_directives_valid() { + let dirs = parse_directives("crate1::mod1=error,crate1::mod2,crate2=debug,crate3=off"); + assert_eq!(dirs.len(), 4, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate1::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::ERROR); + assert_eq!(dirs[0].in_span, None); + + assert_eq!(dirs[1].target, Some("crate1::mod2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::TRACE); + assert_eq!(dirs[1].in_span, None); + + assert_eq!(dirs[2].target, Some("crate2".to_string())); + assert_eq!(dirs[2].level, LevelFilter::DEBUG); + assert_eq!(dirs[2].in_span, None); + + assert_eq!(dirs[3].target, Some("crate3".to_string())); + assert_eq!(dirs[3].level, LevelFilter::OFF); + assert_eq!(dirs[3].in_span, None); + } + + #[test] + + fn parse_level_directives() { + let dirs = parse_directives( + "crate1::mod1=error,crate1::mod2=warn,crate1::mod2::mod3=info,\ + crate2=debug,crate3=trace,crate3::mod2::mod1=off", + ); + assert_eq!(dirs.len(), 6, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate1::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::ERROR); + assert_eq!(dirs[0].in_span, None); + + assert_eq!(dirs[1].target, Some("crate1::mod2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::WARN); + assert_eq!(dirs[1].in_span, None); + + assert_eq!(dirs[2].target, Some("crate1::mod2::mod3".to_string())); + assert_eq!(dirs[2].level, LevelFilter::INFO); + assert_eq!(dirs[2].in_span, None); + + assert_eq!(dirs[3].target, Some("crate2".to_string())); + assert_eq!(dirs[3].level, LevelFilter::DEBUG); + assert_eq!(dirs[3].in_span, None); + + assert_eq!(dirs[4].target, Some("crate3".to_string())); + assert_eq!(dirs[4].level, LevelFilter::TRACE); + assert_eq!(dirs[4].in_span, None); + + assert_eq!(dirs[5].target, Some("crate3::mod2::mod1".to_string())); + assert_eq!(dirs[5].level, LevelFilter::OFF); + assert_eq!(dirs[5].in_span, None); + } + + #[test] + fn parse_uppercase_level_directives() { + let dirs = parse_directives( + "crate1::mod1=ERROR,crate1::mod2=WARN,crate1::mod2::mod3=INFO,\ + crate2=DEBUG,crate3=TRACE,crate3::mod2::mod1=OFF", + ); + assert_eq!(dirs.len(), 6, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate1::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::ERROR); + assert_eq!(dirs[0].in_span, None); + + assert_eq!(dirs[1].target, Some("crate1::mod2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::WARN); + assert_eq!(dirs[1].in_span, None); + + assert_eq!(dirs[2].target, Some("crate1::mod2::mod3".to_string())); + assert_eq!(dirs[2].level, LevelFilter::INFO); + assert_eq!(dirs[2].in_span, None); + + assert_eq!(dirs[3].target, Some("crate2".to_string())); + assert_eq!(dirs[3].level, LevelFilter::DEBUG); + assert_eq!(dirs[3].in_span, None); + + assert_eq!(dirs[4].target, Some("crate3".to_string())); + assert_eq!(dirs[4].level, LevelFilter::TRACE); + assert_eq!(dirs[4].in_span, None); + + assert_eq!(dirs[5].target, Some("crate3::mod2::mod1".to_string())); + assert_eq!(dirs[5].level, LevelFilter::OFF); + assert_eq!(dirs[5].in_span, None); + } + + #[test] + fn parse_numeric_level_directives() { + let dirs = parse_directives( + "crate1::mod1=1,crate1::mod2=2,crate1::mod2::mod3=3,crate2=4,\ + crate3=5,crate3::mod2::mod1=0", + ); + assert_eq!(dirs.len(), 6, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate1::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::ERROR); + assert_eq!(dirs[0].in_span, None); + + assert_eq!(dirs[1].target, Some("crate1::mod2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::WARN); + assert_eq!(dirs[1].in_span, None); + + assert_eq!(dirs[2].target, Some("crate1::mod2::mod3".to_string())); + assert_eq!(dirs[2].level, LevelFilter::INFO); + assert_eq!(dirs[2].in_span, None); + + assert_eq!(dirs[3].target, Some("crate2".to_string())); + assert_eq!(dirs[3].level, LevelFilter::DEBUG); + assert_eq!(dirs[3].in_span, None); + + assert_eq!(dirs[4].target, Some("crate3".to_string())); + assert_eq!(dirs[4].level, LevelFilter::TRACE); + assert_eq!(dirs[4].in_span, None); + + assert_eq!(dirs[5].target, Some("crate3::mod2::mod1".to_string())); + assert_eq!(dirs[5].level, LevelFilter::OFF); + assert_eq!(dirs[5].in_span, None); + } + + #[test] + fn parse_directives_invalid_crate() { + // test parse_directives with multiple = in specification + let dirs = parse_directives("crate1::mod1=warn=info,crate2=debug"); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::DEBUG); + assert_eq!(dirs[0].in_span, None); + } + + #[test] + fn parse_directives_invalid_level() { + // test parse_directives with 'noNumber' as log level + let dirs = parse_directives("crate1::mod1=noNumber,crate2=debug"); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::DEBUG); + assert_eq!(dirs[0].in_span, None); + } + + #[test] + fn parse_directives_string_level() { + // test parse_directives with 'warn' as log level + let dirs = parse_directives("crate1::mod1=wrong,crate2=warn"); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::WARN); + assert_eq!(dirs[0].in_span, None); + } + + #[test] + fn parse_directives_empty_level() { + // test parse_directives with '' as log level + let dirs = parse_directives("crate1::mod1=wrong,crate2="); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::TRACE); + assert_eq!(dirs[0].in_span, None); + } + + #[test] + fn parse_directives_global() { + // test parse_directives with no crate + let dirs = parse_directives("warn,crate2=debug"); + assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, None); + assert_eq!(dirs[0].level, LevelFilter::WARN); + assert_eq!(dirs[1].in_span, None); + + assert_eq!(dirs[1].target, Some("crate2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::DEBUG); + assert_eq!(dirs[1].in_span, None); + } + + // helper function for tests below + fn test_parse_bare_level(directive_to_test: &str, level_expected: LevelFilter) { + let dirs = parse_directives(directive_to_test); + assert_eq!( + dirs.len(), + 1, + "\ninput: \"{}\"; parsed: {:#?}", + directive_to_test, + dirs + ); + assert_eq!(dirs[0].target, None); + assert_eq!(dirs[0].level, level_expected); + assert_eq!(dirs[0].in_span, None); + } + + #[test] + fn parse_directives_global_bare_warn_lc() { + // test parse_directives with no crate, in isolation, all lowercase + test_parse_bare_level("warn", LevelFilter::WARN); + } + + #[test] + fn parse_directives_global_bare_warn_uc() { + // test parse_directives with no crate, in isolation, all uppercase + test_parse_bare_level("WARN", LevelFilter::WARN); + } + + #[test] + fn parse_directives_global_bare_warn_mixed() { + // test parse_directives with no crate, in isolation, mixed case + test_parse_bare_level("wArN", LevelFilter::WARN); + } + + #[test] + fn parse_directives_valid_with_spans() { + let dirs = parse_directives("crate1::mod1[foo]=error,crate1::mod2[bar],crate2[baz]=debug"); + assert_eq!(dirs.len(), 3, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate1::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::ERROR); + assert_eq!(dirs[0].in_span, Some("foo".to_string())); + + assert_eq!(dirs[1].target, Some("crate1::mod2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::TRACE); + assert_eq!(dirs[1].in_span, Some("bar".to_string())); + + assert_eq!(dirs[2].target, Some("crate2".to_string())); + assert_eq!(dirs[2].level, LevelFilter::DEBUG); + assert_eq!(dirs[2].in_span, Some("baz".to_string())); + } + + #[test] + fn parse_directives_with_dash_in_target_name() { + let dirs = parse_directives("target-name=info"); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("target-name".to_string())); + assert_eq!(dirs[0].level, LevelFilter::INFO); + assert_eq!(dirs[0].in_span, None); + } + + #[test] + fn parse_directives_with_dash_in_span_name() { + // Reproduces https://github.com/tokio-rs/tracing/issues/1367 + + let dirs = parse_directives("target[span-name]=info"); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("target".to_string())); + assert_eq!(dirs[0].level, LevelFilter::INFO); + assert_eq!(dirs[0].in_span, Some("span-name".to_string())); + } + + #[test] + fn parse_directives_with_special_characters_in_span_name() { + let span_name = "!\"#$%&'()*+-./:;<=>?@^_`|~[}"; + + let dirs = parse_directives(format!("target[{}]=info", span_name)); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("target".to_string())); + assert_eq!(dirs[0].level, LevelFilter::INFO); + assert_eq!(dirs[0].in_span, Some(span_name.to_string())); + } + + #[test] + fn parse_directives_with_invalid_span_chars() { + let invalid_span_name = "]{"; + + let dirs = parse_directives(format!("target[{}]=info", invalid_span_name)); + assert_eq!(dirs.len(), 0, "\nparsed: {:#?}", dirs); + } +} diff --git a/vendor/tracing-subscriber/src/filter/env/field.rs b/vendor/tracing-subscriber/src/filter/env/field.rs new file mode 100644 index 000000000..1394fd04a --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/env/field.rs @@ -0,0 +1,626 @@ +use matchers::Pattern; +use std::{ + cmp::Ordering, + error::Error, + fmt::{self, Write}, + str::FromStr, + sync::{ + atomic::{AtomicBool, Ordering::*}, + Arc, + }, +}; + +use super::{FieldMap, LevelFilter}; +use tracing_core::field::{Field, Visit}; + +#[derive(Clone, Debug, Eq, PartialEq)] +pub(crate) struct Match { + pub(crate) name: String, // TODO: allow match patterns for names? + pub(crate) value: Option<ValueMatch>, +} + +#[derive(Debug, Eq, PartialEq)] +pub(crate) struct CallsiteMatch { + pub(crate) fields: FieldMap<ValueMatch>, + pub(crate) level: LevelFilter, +} + +#[derive(Debug)] +pub(crate) struct SpanMatch { + fields: FieldMap<(ValueMatch, AtomicBool)>, + level: LevelFilter, + has_matched: AtomicBool, +} + +pub(crate) struct MatchVisitor<'a> { + inner: &'a SpanMatch, +} + +#[derive(Debug, Clone)] +pub(crate) enum ValueMatch { + /// Matches a specific `bool` value. + Bool(bool), + /// Matches a specific `f64` value. + F64(f64), + /// Matches a specific `u64` value. + U64(u64), + /// Matches a specific `i64` value. + I64(i64), + /// Matches any `NaN` `f64` value. + NaN, + /// Matches any field whose `fmt::Debug` output is equal to a fixed string. + Debug(MatchDebug), + /// Matches any field whose `fmt::Debug` output matches a regular expression + /// pattern. + Pat(Box<MatchPattern>), +} + +impl Eq for ValueMatch {} + +impl PartialEq for ValueMatch { + fn eq(&self, other: &Self) -> bool { + use ValueMatch::*; + match (self, other) { + (Bool(a), Bool(b)) => a.eq(b), + (F64(a), F64(b)) => { + debug_assert!(!a.is_nan()); + debug_assert!(!b.is_nan()); + + a.eq(b) + } + (U64(a), U64(b)) => a.eq(b), + (I64(a), I64(b)) => a.eq(b), + (NaN, NaN) => true, + (Pat(a), Pat(b)) => a.eq(b), + _ => false, + } + } +} + +impl Ord for ValueMatch { + fn cmp(&self, other: &Self) -> Ordering { + use ValueMatch::*; + match (self, other) { + (Bool(this), Bool(that)) => this.cmp(that), + (Bool(_), _) => Ordering::Less, + + (F64(this), F64(that)) => this + .partial_cmp(that) + .expect("`ValueMatch::F64` may not contain `NaN` values"), + (F64(_), Bool(_)) => Ordering::Greater, + (F64(_), _) => Ordering::Less, + + (NaN, NaN) => Ordering::Equal, + (NaN, Bool(_)) | (NaN, F64(_)) => Ordering::Greater, + (NaN, _) => Ordering::Less, + + (U64(this), U64(that)) => this.cmp(that), + (U64(_), Bool(_)) | (U64(_), F64(_)) | (U64(_), NaN) => Ordering::Greater, + (U64(_), _) => Ordering::Less, + + (I64(this), I64(that)) => this.cmp(that), + (I64(_), Bool(_)) | (I64(_), F64(_)) | (I64(_), NaN) | (I64(_), U64(_)) => { + Ordering::Greater + } + (I64(_), _) => Ordering::Less, + + (Pat(this), Pat(that)) => this.cmp(that), + (Pat(_), _) => Ordering::Greater, + + (Debug(this), Debug(that)) => this.cmp(that), + (Debug(_), _) => Ordering::Greater, + } + } +} + +impl PartialOrd for ValueMatch { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +/// Matches a field's `fmt::Debug` output against a regular expression pattern. +/// +/// This is used for matching all non-literal field value filters when regular +/// expressions are enabled. +#[derive(Debug, Clone)] +pub(crate) struct MatchPattern { + pub(crate) matcher: Pattern, + pattern: Arc<str>, +} + +/// Matches a field's `fmt::Debug` output against a fixed string pattern. +/// +/// This is used for matching all non-literal field value filters when regular +/// expressions are disabled. +#[derive(Debug, Clone)] +pub(crate) struct MatchDebug { + pattern: Arc<str>, +} + +/// Indicates that a field name specified in a filter directive was invalid. +#[derive(Clone, Debug)] +#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] +pub struct BadName { + name: String, +} + +// === impl Match === + +impl Match { + pub(crate) fn has_value(&self) -> bool { + self.value.is_some() + } + + // TODO: reference count these strings? + pub(crate) fn name(&self) -> String { + self.name.clone() + } + + pub(crate) fn parse(s: &str, regex: bool) -> Result<Self, Box<dyn Error + Send + Sync>> { + let mut parts = s.split('='); + let name = parts + .next() + .ok_or_else(|| BadName { + name: "".to_string(), + })? + // TODO: validate field name + .to_string(); + let value = parts + .next() + .map(|part| match regex { + true => ValueMatch::parse_regex(part), + false => Ok(ValueMatch::parse_non_regex(part)), + }) + .transpose()?; + Ok(Match { name, value }) + } +} + +impl fmt::Display for Match { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.name, f)?; + if let Some(ref value) = self.value { + write!(f, "={}", value)?; + } + Ok(()) + } +} + +impl Ord for Match { + fn cmp(&self, other: &Self) -> Ordering { + // Ordering for `Match` directives is based first on _whether_ a value + // is matched or not. This is semantically meaningful --- we would + // prefer to check directives that match values first as they are more + // specific. + let has_value = match (self.value.as_ref(), other.value.as_ref()) { + (Some(_), None) => Ordering::Greater, + (None, Some(_)) => Ordering::Less, + _ => Ordering::Equal, + }; + // If both directives match a value, we fall back to the field names in + // length + lexicographic ordering, and if these are equal as well, we + // compare the match directives. + // + // This ordering is no longer semantically meaningful but is necessary + // so that the directives can be stored in the `BTreeMap` in a defined + // order. + has_value + .then_with(|| self.name.cmp(&other.name)) + .then_with(|| self.value.cmp(&other.value)) + } +} + +impl PartialOrd for Match { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +// === impl ValueMatch === + +fn value_match_f64(v: f64) -> ValueMatch { + if v.is_nan() { + ValueMatch::NaN + } else { + ValueMatch::F64(v) + } +} + +impl ValueMatch { + /// Parse a `ValueMatch` that will match `fmt::Debug` fields using regular + /// expressions. + /// + /// This returns an error if the string didn't contain a valid `bool`, + /// `u64`, `i64`, or `f64` literal, and couldn't be parsed as a regular + /// expression. + fn parse_regex(s: &str) -> Result<Self, matchers::Error> { + s.parse::<bool>() + .map(ValueMatch::Bool) + .or_else(|_| s.parse::<u64>().map(ValueMatch::U64)) + .or_else(|_| s.parse::<i64>().map(ValueMatch::I64)) + .or_else(|_| s.parse::<f64>().map(value_match_f64)) + .or_else(|_| { + s.parse::<MatchPattern>() + .map(|p| ValueMatch::Pat(Box::new(p))) + }) + } + + /// Parse a `ValueMatch` that will match `fmt::Debug` against a fixed + /// string. + /// + /// This does *not* return an error, because any string that isn't a valid + /// `bool`, `u64`, `i64`, or `f64` literal is treated as expected + /// `fmt::Debug` output. + fn parse_non_regex(s: &str) -> Self { + s.parse::<bool>() + .map(ValueMatch::Bool) + .or_else(|_| s.parse::<u64>().map(ValueMatch::U64)) + .or_else(|_| s.parse::<i64>().map(ValueMatch::I64)) + .or_else(|_| s.parse::<f64>().map(value_match_f64)) + .unwrap_or_else(|_| ValueMatch::Debug(MatchDebug::new(s))) + } +} + +impl fmt::Display for ValueMatch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ValueMatch::Bool(ref inner) => fmt::Display::fmt(inner, f), + ValueMatch::F64(ref inner) => fmt::Display::fmt(inner, f), + ValueMatch::NaN => fmt::Display::fmt(&std::f64::NAN, f), + ValueMatch::I64(ref inner) => fmt::Display::fmt(inner, f), + ValueMatch::U64(ref inner) => fmt::Display::fmt(inner, f), + ValueMatch::Debug(ref inner) => fmt::Display::fmt(inner, f), + ValueMatch::Pat(ref inner) => fmt::Display::fmt(inner, f), + } + } +} + +// === impl MatchPattern === + +impl FromStr for MatchPattern { + type Err = matchers::Error; + fn from_str(s: &str) -> Result<Self, Self::Err> { + let matcher = s.parse::<Pattern>()?; + Ok(Self { + matcher, + pattern: s.to_owned().into(), + }) + } +} + +impl fmt::Display for MatchPattern { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&*self.pattern, f) + } +} + +impl AsRef<str> for MatchPattern { + #[inline] + fn as_ref(&self) -> &str { + self.pattern.as_ref() + } +} + +impl MatchPattern { + #[inline] + fn str_matches(&self, s: &impl AsRef<str>) -> bool { + self.matcher.matches(s) + } + + #[inline] + fn debug_matches(&self, d: &impl fmt::Debug) -> bool { + self.matcher.debug_matches(d) + } + + pub(super) fn into_debug_match(self) -> MatchDebug { + MatchDebug { + pattern: self.pattern, + } + } +} + +impl PartialEq for MatchPattern { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.pattern == other.pattern + } +} + +impl Eq for MatchPattern {} + +impl PartialOrd for MatchPattern { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.pattern.cmp(&other.pattern)) + } +} + +impl Ord for MatchPattern { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + self.pattern.cmp(&other.pattern) + } +} + +// === impl MatchDebug === + +impl MatchDebug { + fn new(s: &str) -> Self { + Self { + pattern: s.to_owned().into(), + } + } + + #[inline] + fn debug_matches(&self, d: &impl fmt::Debug) -> bool { + // Naively, we would probably match a value's `fmt::Debug` output by + // formatting it to a string, and then checking if the string is equal + // to the expected pattern. However, this would require allocating every + // time we want to match a field value against a `Debug` matcher, which + // can be avoided. + // + // Instead, we implement `fmt::Write` for a type that, rather than + // actually _writing_ the strings to something, matches them against the + // expected pattern, and returns an error if the pattern does not match. + struct Matcher<'a> { + pattern: &'a str, + } + + impl fmt::Write for Matcher<'_> { + fn write_str(&mut self, s: &str) -> fmt::Result { + // If the string is longer than the remaining expected string, + // we know it won't match, so bail. + if s.len() > self.pattern.len() { + return Err(fmt::Error); + } + + // If the expected string begins with the string that was + // written, we are still potentially a match. Advance the + // position in the expected pattern to chop off the matched + // output, and continue. + if self.pattern.starts_with(s) { + self.pattern = &self.pattern[s.len()..]; + return Ok(()); + } + + // Otherwise, the expected string doesn't include the string + // that was written at the current position, so the `fmt::Debug` + // output doesn't match! Return an error signalling that this + // doesn't match. + Err(fmt::Error) + } + } + let mut matcher = Matcher { + pattern: &self.pattern, + }; + + // Try to "write" the value's `fmt::Debug` output to a `Matcher`. This + // returns an error if the `fmt::Debug` implementation wrote any + // characters that did not match the expected pattern. + write!(matcher, "{:?}", d).is_ok() + } +} + +impl fmt::Display for MatchDebug { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&*self.pattern, f) + } +} + +impl AsRef<str> for MatchDebug { + #[inline] + fn as_ref(&self) -> &str { + self.pattern.as_ref() + } +} + +impl PartialEq for MatchDebug { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.pattern == other.pattern + } +} + +impl Eq for MatchDebug {} + +impl PartialOrd for MatchDebug { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.pattern.cmp(&other.pattern)) + } +} + +impl Ord for MatchDebug { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + self.pattern.cmp(&other.pattern) + } +} + +// === impl BadName === + +impl Error for BadName {} + +impl fmt::Display for BadName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "invalid field name `{}`", self.name) + } +} + +impl CallsiteMatch { + pub(crate) fn to_span_match(&self) -> SpanMatch { + let fields = self + .fields + .iter() + .map(|(k, v)| (k.clone(), (v.clone(), AtomicBool::new(false)))) + .collect(); + SpanMatch { + fields, + level: self.level, + has_matched: AtomicBool::new(false), + } + } +} + +impl SpanMatch { + pub(crate) fn visitor(&self) -> MatchVisitor<'_> { + MatchVisitor { inner: self } + } + + #[inline] + pub(crate) fn is_matched(&self) -> bool { + if self.has_matched.load(Acquire) { + return true; + } + self.is_matched_slow() + } + + #[inline(never)] + fn is_matched_slow(&self) -> bool { + let matched = self + .fields + .values() + .all(|(_, matched)| matched.load(Acquire)); + if matched { + self.has_matched.store(true, Release); + } + matched + } + + #[inline] + pub(crate) fn filter(&self) -> Option<LevelFilter> { + if self.is_matched() { + Some(self.level) + } else { + None + } + } +} + +impl<'a> Visit for MatchVisitor<'a> { + fn record_f64(&mut self, field: &Field, value: f64) { + match self.inner.fields.get(field) { + Some((ValueMatch::NaN, ref matched)) if value.is_nan() => { + matched.store(true, Release); + } + Some((ValueMatch::F64(ref e), ref matched)) + if (value - *e).abs() < std::f64::EPSILON => + { + matched.store(true, Release); + } + _ => {} + } + } + + fn record_i64(&mut self, field: &Field, value: i64) { + use std::convert::TryInto; + + match self.inner.fields.get(field) { + Some((ValueMatch::I64(ref e), ref matched)) if value == *e => { + matched.store(true, Release); + } + Some((ValueMatch::U64(ref e), ref matched)) if Ok(value) == (*e).try_into() => { + matched.store(true, Release); + } + _ => {} + } + } + + fn record_u64(&mut self, field: &Field, value: u64) { + match self.inner.fields.get(field) { + Some((ValueMatch::U64(ref e), ref matched)) if value == *e => { + matched.store(true, Release); + } + _ => {} + } + } + + fn record_bool(&mut self, field: &Field, value: bool) { + match self.inner.fields.get(field) { + Some((ValueMatch::Bool(ref e), ref matched)) if value == *e => { + matched.store(true, Release); + } + _ => {} + } + } + + fn record_str(&mut self, field: &Field, value: &str) { + match self.inner.fields.get(field) { + Some((ValueMatch::Pat(ref e), ref matched)) if e.str_matches(&value) => { + matched.store(true, Release); + } + Some((ValueMatch::Debug(ref e), ref matched)) if e.debug_matches(&value) => { + matched.store(true, Release) + } + _ => {} + } + } + + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + match self.inner.fields.get(field) { + Some((ValueMatch::Pat(ref e), ref matched)) if e.debug_matches(&value) => { + matched.store(true, Release); + } + Some((ValueMatch::Debug(ref e), ref matched)) if e.debug_matches(&value) => { + matched.store(true, Release) + } + _ => {} + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[derive(Debug)] + #[allow(dead_code)] + struct MyStruct { + answer: usize, + question: &'static str, + } + + #[test] + fn debug_struct_match() { + let my_struct = MyStruct { + answer: 42, + question: "life, the universe, and everything", + }; + + let pattern = "MyStruct { answer: 42, question: \"life, the universe, and everything\" }"; + + assert_eq!( + format!("{:?}", my_struct), + pattern, + "`MyStruct`'s `Debug` impl doesn't output the expected string" + ); + + let matcher = MatchDebug { + pattern: pattern.into(), + }; + assert!(matcher.debug_matches(&my_struct)) + } + + #[test] + fn debug_struct_not_match() { + let my_struct = MyStruct { + answer: 42, + question: "what shall we have for lunch?", + }; + + let pattern = "MyStruct { answer: 42, question: \"life, the universe, and everything\" }"; + + assert_eq!( + format!("{:?}", my_struct), + "MyStruct { answer: 42, question: \"what shall we have for lunch?\" }", + "`MyStruct`'s `Debug` impl doesn't output the expected string" + ); + + let matcher = MatchDebug { + pattern: pattern.into(), + }; + assert!(!matcher.debug_matches(&my_struct)) + } +} diff --git a/vendor/tracing-subscriber/src/filter/env/mod.rs b/vendor/tracing-subscriber/src/filter/env/mod.rs new file mode 100644 index 000000000..81a9ae2bd --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/env/mod.rs @@ -0,0 +1,991 @@ +//! A `Layer` that enables or disables spans and events based on a set of +//! filtering directives. + +// these are publicly re-exported, but the compiler doesn't realize +// that for some reason. +#[allow(unreachable_pub)] +pub use self::{builder::Builder, directive::Directive, field::BadName as BadFieldName}; +mod builder; +mod directive; +mod field; + +use crate::{ + filter::LevelFilter, + layer::{Context, Layer}, + sync::RwLock, +}; +use directive::ParseError; +use std::{cell::RefCell, collections::HashMap, env, error::Error, fmt, str::FromStr}; +use thread_local::ThreadLocal; +use tracing_core::{ + callsite, + field::Field, + span, + subscriber::{Interest, Subscriber}, + Metadata, +}; + +/// A [`Layer`] which filters spans and events based on a set of filter +/// directives. +/// +/// `EnvFilter` implements both the [`Layer`](#impl-Layer<S>) and [`Filter`] traits, so it may +/// be used for both [global filtering][global] and [per-layer filtering][plf], +/// respectively. See [the documentation on filtering with `Layer`s][filtering] +/// for details. +/// +/// The [`Targets`] type implements a similar form of filtering, but without the +/// ability to dynamically enable events based on the current span context, and +/// without filtering on field values. When these features are not required, +/// [`Targets`] provides a lighter-weight alternative to [`EnvFilter`]. +/// +/// # Directives +/// +/// A filter consists of one or more comma-separated directives which match on [`Span`]s and [`Event`]s. +/// Each directive may have a corresponding maximum verbosity [`level`] which +/// enables (e.g., _selects for_) spans and events that match. Like `log`, +/// `tracing` considers less exclusive levels (like `trace` or `info`) to be more +/// verbose than more exclusive levels (like `error` or `warn`). +/// +/// The directive syntax is similar to that of [`env_logger`]'s. At a high level, the syntax for directives +/// consists of several parts: +/// +/// ```text +/// target[span{field=value}]=level +/// ``` +/// +/// Each component (`target`, `span`, `field`, `value`, and `level`) will be covered in turn. +/// +/// - `target` matches the event or span's target. In general, this is the module path and/or crate name. +/// Examples of targets `h2`, `tokio::net`, or `tide::server`. For more information on targets, +/// please refer to [`Metadata`]'s documentation. +/// - `span` matches on the span's name. If a `span` directive is provided alongside a `target`, +/// the `span` directive will match on spans _within_ the `target`. +/// - `field` matches on [fields] within spans. Field names can also be supplied without a `value` +/// and will match on any [`Span`] or [`Event`] that has a field with that name. +/// For example: `[span{field=\"value\"}]=debug`, `[{field}]=trace`. +/// - `value` matches on the value of a span's field. If a value is a numeric literal or a bool, +/// it will match _only_ on that value. Otherwise, this filter matches the +/// [`std::fmt::Debug`] output from the value. +/// - `level` sets a maximum verbosity level accepted by this directive. +/// +/// When a field value directive (`[{<FIELD NAME>=<FIELD_VALUE>}]=...`) matches a +/// value's [`std::fmt::Debug`] output (i.e., the field value in the directive +/// is not a `bool`, `i64`, `u64`, or `f64` literal), the matched pattern may be +/// interpreted as either a regular expression or as the precise expected +/// output of the field's [`std::fmt::Debug`] implementation. By default, these +/// filters are interpreted as regular expressions, but this can be disabled +/// using the [`Builder::with_regex`] builder method to use precise matching +/// instead. +/// +/// When field value filters are interpreted as regular expressions, the +/// [`regex-automata` crate's regular expression syntax][re-syntax] is +/// supported. +/// +/// **Note**: When filters are constructed from potentially untrusted inputs, +/// [disabling regular expression matching](Builder::with_regex) is strongly +/// recommended. +/// +/// ## Usage Notes +/// +/// - The portion of the directive which is included within the square brackets is `tracing`-specific. +/// - Any portion of the directive can be omitted. +/// - The sole exception are the `field` and `value` directives. If a `value` is provided, +/// a `field` must _also_ be provided. However, the converse does not hold, as fields can +/// be matched without a value. +/// - If only a level is provided, it will set the maximum level for all `Span`s and `Event`s +/// that are not enabled by other filters. +/// - A directive without a level will enable anything that it matches. This is equivalent to `=trace`. +/// - When a crate has a dash in its name, the default target for events will be the +/// crate's module path as it appears in Rust. This means every dash will be replaced +/// with an underscore. +/// - A dash in a target will only appear when being specified explicitly: +/// `tracing::info!(target: "target-name", ...);` +/// +/// ## Example Syntax +/// +/// - `tokio::net=info` will enable all spans or events that: +/// - have the `tokio::net` target, +/// - at the level `info` or above. +/// - `warn,tokio::net=info` will enable all spans and events that: +/// - are at the level `warn` or above, *or* +/// - have the `tokio::net` target at the level `info` or above. +/// - `my_crate[span_a]=trace` will enable all spans and events that: +/// - are within the `span_a` span or named `span_a` _if_ `span_a` has the target `my_crate`, +/// - at the level `trace` or above. +/// - `[span_b{name=\"bob\"}]` will enable all spans or event that: +/// - have _any_ target, +/// - are inside a span named `span_b`, +/// - which has a field named `name` with value `bob`, +/// - at _any_ level. +/// +/// # Examples +/// +/// Parsing an `EnvFilter` from the [default environment +/// variable](EnvFilter::from_default_env) (`RUST_LOG`): +/// +/// ``` +/// use tracing_subscriber::{EnvFilter, fmt, prelude::*}; +/// +/// tracing_subscriber::registry() +/// .with(fmt::layer()) +/// .with(EnvFilter::from_default_env()) +/// .init(); +/// ``` +/// +/// Parsing an `EnvFilter` [from a user-provided environment +/// variable](EnvFilter::from_env): +/// +/// ``` +/// use tracing_subscriber::{EnvFilter, fmt, prelude::*}; +/// +/// tracing_subscriber::registry() +/// .with(fmt::layer()) +/// .with(EnvFilter::from_env("MYAPP_LOG")) +/// .init(); +/// ``` +/// +/// Using `EnvFilter` as a [per-layer filter][plf] to filter only a single +/// [`Layer`]: +/// +/// ``` +/// use tracing_subscriber::{EnvFilter, fmt, prelude::*}; +/// +/// // Parse an `EnvFilter` configuration from the `RUST_LOG` +/// // environment variable. +/// let filter = EnvFilter::from_default_env(); +/// +/// // Apply the filter to this layer *only*. +/// let filtered_layer = fmt::layer().with_filter(filter); +/// +/// // Some other layer, whose output we don't want to filter. +/// let unfiltered_layer = // ... +/// # fmt::layer(); +/// +/// tracing_subscriber::registry() +/// .with(filtered_layer) +/// .with(unfiltered_layer) +/// .init(); +/// ``` +/// # Constructing `EnvFilter`s +/// +/// An `EnvFilter` is be constructed by parsing a string containing one or more +/// directives. The [`EnvFilter::new`] constructor parses an `EnvFilter` from a +/// string, ignoring any invalid directives, while [`EnvFilter::try_new`] +/// returns an error if invalid directives are encountered. Similarly, the +/// [`EnvFilter::from_env`] and [`EnvFilter::try_from_env`] constructors parse +/// an `EnvFilter` from the value of the provided environment variable, with +/// lossy and strict validation, respectively. +/// +/// A [builder](EnvFilter::builder) interface is available to set additional +/// configuration options prior to parsing an `EnvFilter`. See the [`Builder` +/// type's documentation](Builder) for details on the options that can be +/// configured using the builder. +/// +/// [`Span`]: tracing_core::span +/// [fields]: tracing_core::Field +/// [`Event`]: tracing_core::Event +/// [`level`]: tracing_core::Level +/// [`Metadata`]: tracing_core::Metadata +/// [`Targets`]: crate::filter::Targets +/// [`env_logger`]: https://crates.io/crates/env_logger +/// [`Filter`]: #impl-Filter<S> +/// [global]: crate::layer#global-filtering +/// [plf]: crate::layer#per-layer-filtering +/// [filtering]: crate::layer#filtering-with-layers +#[cfg_attr(docsrs, doc(cfg(all(feature = "env-filter", feature = "std"))))] +#[derive(Debug)] +pub struct EnvFilter { + statics: directive::Statics, + dynamics: directive::Dynamics, + has_dynamics: bool, + by_id: RwLock<HashMap<span::Id, directive::SpanMatcher>>, + by_cs: RwLock<HashMap<callsite::Identifier, directive::CallsiteMatcher>>, + scope: ThreadLocal<RefCell<Vec<LevelFilter>>>, + regex: bool, +} + +type FieldMap<T> = HashMap<Field, T>; + +/// Indicates that an error occurred while parsing a `EnvFilter` from an +/// environment variable. +#[cfg_attr(docsrs, doc(cfg(all(feature = "env-filter", feature = "std"))))] +#[derive(Debug)] +pub struct FromEnvError { + kind: ErrorKind, +} + +#[derive(Debug)] +enum ErrorKind { + Parse(ParseError), + Env(env::VarError), +} + +impl EnvFilter { + /// `RUST_LOG` is the default environment variable used by + /// [`EnvFilter::from_default_env`] and [`EnvFilter::try_from_default_env`]. + /// + /// [`EnvFilter::from_default_env`]: EnvFilter::from_default_env() + /// [`EnvFilter::try_from_default_env`]: EnvFilter::try_from_default_env() + pub const DEFAULT_ENV: &'static str = "RUST_LOG"; + + // === constructors, etc === + + /// Returns a [builder] that can be used to configure a new [`EnvFilter`] + /// instance. + /// + /// The [`Builder`] type is used to set additional configurations, such as + /// [whether regular expressions are enabled](Builder::with_regex) or [the + /// default directive](Builder::with_default_directive) before parsing an + /// [`EnvFilter`] from a string or environment variable. + /// + /// [builder]: https://rust-unofficial.github.io/patterns/patterns/creational/builder.html + pub fn builder() -> Builder { + Builder::default() + } + + /// Returns a new `EnvFilter` from the value of the `RUST_LOG` environment + /// variable, ignoring any invalid filter directives. + /// + /// If the environment variable is empty or not set, or if it contains only + /// invalid directives, a default directive enabling the [`ERROR`] level is + /// added. + /// + /// To set additional configuration options prior to parsing the filter, use + /// the [`Builder`] type instead. + /// + /// This function is equivalent to the following: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// # fn docs() -> EnvFilter { + /// EnvFilter::builder() + /// .with_default_directive(LevelFilter::ERROR.into()) + /// .from_env_lossy() + /// # } + /// ``` + /// + /// [`ERROR`]: tracing::Level::ERROR + pub fn from_default_env() -> Self { + Self::builder() + .with_default_directive(LevelFilter::ERROR.into()) + .from_env_lossy() + } + + /// Returns a new `EnvFilter` from the value of the given environment + /// variable, ignoring any invalid filter directives. + /// + /// If the environment variable is empty or not set, or if it contains only + /// invalid directives, a default directive enabling the [`ERROR`] level is + /// added. + /// + /// To set additional configuration options prior to parsing the filter, use + /// the [`Builder`] type instead. + /// + /// This function is equivalent to the following: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// # fn docs() -> EnvFilter { + /// # let env = ""; + /// EnvFilter::builder() + /// .with_default_directive(LevelFilter::ERROR.into()) + /// .with_env_var(env) + /// .from_env_lossy() + /// # } + /// ``` + /// + /// [`ERROR`]: tracing::Level::ERROR + pub fn from_env<A: AsRef<str>>(env: A) -> Self { + Self::builder() + .with_default_directive(LevelFilter::ERROR.into()) + .with_env_var(env.as_ref()) + .from_env_lossy() + } + + /// Returns a new `EnvFilter` from the directives in the given string, + /// ignoring any that are invalid. + /// + /// If the string is empty or contains only invalid directives, a default + /// directive enabling the [`ERROR`] level is added. + /// + /// To set additional configuration options prior to parsing the filter, use + /// the [`Builder`] type instead. + /// + /// This function is equivalent to the following: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// # fn docs() -> EnvFilter { + /// # let directives = ""; + /// EnvFilter::builder() + /// .with_default_directive(LevelFilter::ERROR.into()) + /// .parse_lossy(directives) + /// # } + /// ``` + /// + /// [`ERROR`]: tracing::Level::ERROR + pub fn new<S: AsRef<str>>(directives: S) -> Self { + Self::builder() + .with_default_directive(LevelFilter::ERROR.into()) + .parse_lossy(directives) + } + + /// Returns a new `EnvFilter` from the directives in the given string, + /// or an error if any are invalid. + /// + /// If the string is empty, a default directive enabling the [`ERROR`] level + /// is added. + /// + /// To set additional configuration options prior to parsing the filter, use + /// the [`Builder`] type instead. + /// + /// This function is equivalent to the following: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// + /// # fn docs() -> Result<EnvFilter, tracing_subscriber::filter::ParseError> { + /// # let directives = ""; + /// EnvFilter::builder() + /// .with_default_directive(LevelFilter::ERROR.into()) + /// .parse(directives) + /// # } + /// ``` + /// + /// [`ERROR`]: tracing::Level::ERROR + pub fn try_new<S: AsRef<str>>(dirs: S) -> Result<Self, directive::ParseError> { + Self::builder().parse(dirs) + } + + /// Returns a new `EnvFilter` from the value of the `RUST_LOG` environment + /// variable, or an error if the environment variable is unset or contains + /// any invalid filter directives. + /// + /// To set additional configuration options prior to parsing the filter, use + /// the [`Builder`] type instead. + /// + /// This function is equivalent to the following: + /// + /// ```rust + /// use tracing_subscriber::EnvFilter; + /// + /// # fn docs() -> Result<EnvFilter, tracing_subscriber::filter::FromEnvError> { + /// EnvFilter::builder().try_from_env() + /// # } + /// ``` + pub fn try_from_default_env() -> Result<Self, FromEnvError> { + Self::builder().try_from_env() + } + + /// Returns a new `EnvFilter` from the value of the given environment + /// variable, or an error if the environment variable is unset or contains + /// any invalid filter directives. + /// + /// To set additional configuration options prior to parsing the filter, use + /// the [`Builder`] type instead. + /// + /// This function is equivalent to the following: + /// + /// ```rust + /// use tracing_subscriber::EnvFilter; + /// + /// # fn docs() -> Result<EnvFilter, tracing_subscriber::filter::FromEnvError> { + /// # let env = ""; + /// EnvFilter::builder().with_env_var(env).try_from_env() + /// # } + /// ``` + pub fn try_from_env<A: AsRef<str>>(env: A) -> Result<Self, FromEnvError> { + Self::builder().with_env_var(env.as_ref()).try_from_env() + } + + /// Add a filtering directive to this `EnvFilter`. + /// + /// The added directive will be used in addition to any previously set + /// directives, either added using this method or provided when the filter + /// is constructed. + /// + /// Filters may be created from [`LevelFilter`] or [`Level`], which will + /// enable all traces at or below a certain verbosity level, or + /// parsed from a string specifying a directive. + /// + /// If a filter directive is inserted that matches exactly the same spans + /// and events as a previous filter, but sets a different level for those + /// spans and events, the previous directive is overwritten. + /// + /// [`LevelFilter`]: super::LevelFilter + /// [`Level`]: tracing_core::Level + /// + /// # Examples + /// + /// From [`LevelFilter`]: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// let mut filter = EnvFilter::from_default_env() + /// .add_directive(LevelFilter::INFO.into()); + /// ``` + /// + /// Or from [`Level`]: + /// + /// ```rust + /// # use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + /// # use tracing::Level; + /// let mut filter = EnvFilter::from_default_env() + /// .add_directive(Level::INFO.into()); + /// ``` + /// + /// Parsed from a string: + /// + /// ```rust + /// use tracing_subscriber::filter::{EnvFilter, Directive}; + /// + /// # fn try_mk_filter() -> Result<(), Box<dyn ::std::error::Error>> { + /// let mut filter = EnvFilter::try_from_default_env()? + /// .add_directive("my_crate::module=trace".parse()?) + /// .add_directive("my_crate::my_other_module::something=info".parse()?); + /// # Ok(()) + /// # } + /// ``` + /// In the above example, substitute `my_crate`, `module`, etc. with the + /// name your target crate/module is imported with. This might be + /// different from the package name in Cargo.toml (`-` is replaced by `_`). + /// Example, if the package name in your Cargo.toml is `MY-FANCY-LIB`, then + /// the corresponding Rust identifier would be `MY_FANCY_LIB`: + pub fn add_directive(mut self, mut directive: Directive) -> Self { + if !self.regex { + directive.deregexify(); + } + if let Some(stat) = directive.to_static() { + self.statics.add(stat) + } else { + self.has_dynamics = true; + self.dynamics.add(directive); + } + self + } + + // === filtering methods === + + /// Returns `true` if this `EnvFilter` would enable the provided `metadata` + /// in the current context. + /// + /// This is equivalent to calling the [`Layer::enabled`] or + /// [`Filter::enabled`] methods on `EnvFilter`'s implementations of those + /// traits, but it does not require the trait to be in scope. + pub fn enabled<S>(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool { + let level = metadata.level(); + + // is it possible for a dynamic filter directive to enable this event? + // if not, we can avoid the thread loca'l access + iterating over the + // spans in the current scope. + if self.has_dynamics && self.dynamics.max_level >= *level { + if metadata.is_span() { + // If the metadata is a span, see if we care about its callsite. + let enabled_by_cs = self + .by_cs + .read() + .ok() + .map(|by_cs| by_cs.contains_key(&metadata.callsite())) + .unwrap_or(false); + if enabled_by_cs { + return true; + } + } + + let enabled_by_scope = { + let scope = self.scope.get_or_default().borrow(); + for filter in &*scope { + if filter >= level { + return true; + } + } + false + }; + if enabled_by_scope { + return true; + } + } + + // is it possible for a static filter directive to enable this event? + if self.statics.max_level >= *level { + // Otherwise, fall back to checking if the callsite is + // statically enabled. + return self.statics.enabled(metadata); + } + + false + } + + /// Returns an optional hint of the highest [verbosity level][level] that + /// this `EnvFilter` will enable. + /// + /// This is equivalent to calling the [`Layer::max_level_hint`] or + /// [`Filter::max_level_hint`] methods on `EnvFilter`'s implementations of those + /// traits, but it does not require the trait to be in scope. + /// + /// [level]: tracing_core::metadata::Level + pub fn max_level_hint(&self) -> Option<LevelFilter> { + if self.dynamics.has_value_filters() { + // If we perform any filtering on span field *values*, we will + // enable *all* spans, because their field values are not known + // until recording. + return Some(LevelFilter::TRACE); + } + std::cmp::max( + self.statics.max_level.into(), + self.dynamics.max_level.into(), + ) + } + + /// Informs the filter that a new span was created. + /// + /// This is equivalent to calling the [`Layer::on_new_span`] or + /// [`Filter::on_new_span`] methods on `EnvFilter`'s implementations of those + /// traits, but it does not require the trait to be in scope. + pub fn on_new_span<S>(&self, attrs: &span::Attributes<'_>, id: &span::Id, _: Context<'_, S>) { + let by_cs = try_lock!(self.by_cs.read()); + if let Some(cs) = by_cs.get(&attrs.metadata().callsite()) { + let span = cs.to_span_match(attrs); + try_lock!(self.by_id.write()).insert(id.clone(), span); + } + } + + /// Informs the filter that the span with the provided `id` was entered. + /// + /// This is equivalent to calling the [`Layer::on_enter`] or + /// [`Filter::on_enter`] methods on `EnvFilter`'s implementations of those + /// traits, but it does not require the trait to be in scope. + pub fn on_enter<S>(&self, id: &span::Id, _: Context<'_, S>) { + // XXX: This is where _we_ could push IDs to the stack instead, and use + // that to allow changing the filter while a span is already entered. + // But that might be much less efficient... + if let Some(span) = try_lock!(self.by_id.read()).get(id) { + self.scope.get_or_default().borrow_mut().push(span.level()); + } + } + + /// Informs the filter that the span with the provided `id` was exited. + /// + /// This is equivalent to calling the [`Layer::on_exit`] or + /// [`Filter::on_exit`] methods on `EnvFilter`'s implementations of those + /// traits, but it does not require the trait to be in scope. + pub fn on_exit<S>(&self, id: &span::Id, _: Context<'_, S>) { + if self.cares_about_span(id) { + self.scope.get_or_default().borrow_mut().pop(); + } + } + + /// Informs the filter that the span with the provided `id` was closed. + /// + /// This is equivalent to calling the [`Layer::on_close`] or + /// [`Filter::on_close`] methods on `EnvFilter`'s implementations of those + /// traits, but it does not require the trait to be in scope. + pub fn on_close<S>(&self, id: span::Id, _: Context<'_, S>) { + // If we don't need to acquire a write lock, avoid doing so. + if !self.cares_about_span(&id) { + return; + } + + let mut spans = try_lock!(self.by_id.write()); + spans.remove(&id); + } + + /// Informs the filter that the span with the provided `id` recorded the + /// provided field `values`. + /// + /// This is equivalent to calling the [`Layer::on_record`] or + /// [`Filter::on_record`] methods on `EnvFilter`'s implementations of those + /// traits, but it does not require the trait to be in scope + pub fn on_record<S>(&self, id: &span::Id, values: &span::Record<'_>, _: Context<'_, S>) { + if let Some(span) = try_lock!(self.by_id.read()).get(id) { + span.record_update(values); + } + } + + fn cares_about_span(&self, span: &span::Id) -> bool { + let spans = try_lock!(self.by_id.read(), else return false); + spans.contains_key(span) + } + + fn base_interest(&self) -> Interest { + if self.has_dynamics { + Interest::sometimes() + } else { + Interest::never() + } + } + + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + if self.has_dynamics && metadata.is_span() { + // If this metadata describes a span, first, check if there is a + // dynamic filter that should be constructed for it. If so, it + // should always be enabled, since it influences filtering. + if let Some(matcher) = self.dynamics.matcher(metadata) { + let mut by_cs = try_lock!(self.by_cs.write(), else return self.base_interest()); + by_cs.insert(metadata.callsite(), matcher); + return Interest::always(); + } + } + + // Otherwise, check if any of our static filters enable this metadata. + if self.statics.enabled(metadata) { + Interest::always() + } else { + self.base_interest() + } + } +} + +impl<S: Subscriber> Layer<S> for EnvFilter { + #[inline] + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + EnvFilter::register_callsite(self, metadata) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + EnvFilter::max_level_hint(self) + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool { + self.enabled(metadata, ctx) + } + + #[inline] + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + self.on_new_span(attrs, id, ctx) + } + + #[inline] + fn on_record(&self, id: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + self.on_record(id, values, ctx); + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + self.on_enter(id, ctx); + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + self.on_exit(id, ctx); + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { + self.on_close(id, ctx); + } +} + +feature! { + #![all(feature = "registry", feature = "std")] + use crate::layer::Filter; + + impl<S> Filter<S> for EnvFilter { + #[inline] + fn enabled(&self, meta: &Metadata<'_>, ctx: &Context<'_, S>) -> bool { + self.enabled(meta, ctx.clone()) + } + + #[inline] + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + self.register_callsite(meta) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + EnvFilter::max_level_hint(self) + } + + #[inline] + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + self.on_new_span(attrs, id, ctx) + } + + #[inline] + fn on_record(&self, id: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + self.on_record(id, values, ctx); + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + self.on_enter(id, ctx); + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + self.on_exit(id, ctx); + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { + self.on_close(id, ctx); + } + } +} + +impl FromStr for EnvFilter { + type Err = directive::ParseError; + + fn from_str(spec: &str) -> Result<Self, Self::Err> { + Self::try_new(spec) + } +} + +impl<S> From<S> for EnvFilter +where + S: AsRef<str>, +{ + fn from(s: S) -> Self { + Self::new(s) + } +} + +impl Default for EnvFilter { + fn default() -> Self { + Builder::default().from_directives(std::iter::empty()) + } +} + +impl fmt::Display for EnvFilter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut statics = self.statics.iter(); + let wrote_statics = if let Some(next) = statics.next() { + fmt::Display::fmt(next, f)?; + for directive in statics { + write!(f, ",{}", directive)?; + } + true + } else { + false + }; + + let mut dynamics = self.dynamics.iter(); + if let Some(next) = dynamics.next() { + if wrote_statics { + f.write_str(",")?; + } + fmt::Display::fmt(next, f)?; + for directive in dynamics { + write!(f, ",{}", directive)?; + } + } + Ok(()) + } +} + +// ===== impl FromEnvError ===== + +impl From<directive::ParseError> for FromEnvError { + fn from(p: directive::ParseError) -> Self { + Self { + kind: ErrorKind::Parse(p), + } + } +} + +impl From<env::VarError> for FromEnvError { + fn from(v: env::VarError) -> Self { + Self { + kind: ErrorKind::Env(v), + } + } +} + +impl fmt::Display for FromEnvError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.kind { + ErrorKind::Parse(ref p) => p.fmt(f), + ErrorKind::Env(ref e) => e.fmt(f), + } + } +} + +impl Error for FromEnvError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match self.kind { + ErrorKind::Parse(ref p) => Some(p), + ErrorKind::Env(ref e) => Some(e), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tracing_core::field::FieldSet; + use tracing_core::*; + + struct NoSubscriber; + impl Subscriber for NoSubscriber { + #[inline] + fn register_callsite(&self, _: &'static Metadata<'static>) -> subscriber::Interest { + subscriber::Interest::always() + } + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(0xDEAD) + } + fn event(&self, _event: &Event<'_>) {} + fn record(&self, _span: &span::Id, _values: &span::Record<'_>) {} + fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {} + + #[inline] + fn enabled(&self, _metadata: &Metadata<'_>) -> bool { + true + } + fn enter(&self, _span: &span::Id) {} + fn exit(&self, _span: &span::Id) {} + } + + struct Cs; + impl Callsite for Cs { + fn set_interest(&self, _interest: Interest) {} + fn metadata(&self) -> &Metadata<'_> { + unimplemented!() + } + } + + #[test] + fn callsite_enabled_no_span_directive() { + let filter = EnvFilter::new("app=debug").with_subscriber(NoSubscriber); + static META: &Metadata<'static> = &Metadata::new( + "mySpan", + "app", + Level::TRACE, + None, + None, + None, + FieldSet::new(&[], identify_callsite!(&Cs)), + Kind::SPAN, + ); + + let interest = filter.register_callsite(META); + assert!(interest.is_never()); + } + + #[test] + fn callsite_off() { + let filter = EnvFilter::new("app=off").with_subscriber(NoSubscriber); + static META: &Metadata<'static> = &Metadata::new( + "mySpan", + "app", + Level::ERROR, + None, + None, + None, + FieldSet::new(&[], identify_callsite!(&Cs)), + Kind::SPAN, + ); + + let interest = filter.register_callsite(META); + assert!(interest.is_never()); + } + + #[test] + fn callsite_enabled_includes_span_directive() { + let filter = EnvFilter::new("app[mySpan]=debug").with_subscriber(NoSubscriber); + static META: &Metadata<'static> = &Metadata::new( + "mySpan", + "app", + Level::TRACE, + None, + None, + None, + FieldSet::new(&[], identify_callsite!(&Cs)), + Kind::SPAN, + ); + + let interest = filter.register_callsite(META); + assert!(interest.is_always()); + } + + #[test] + fn callsite_enabled_includes_span_directive_field() { + let filter = + EnvFilter::new("app[mySpan{field=\"value\"}]=debug").with_subscriber(NoSubscriber); + static META: &Metadata<'static> = &Metadata::new( + "mySpan", + "app", + Level::TRACE, + None, + None, + None, + FieldSet::new(&["field"], identify_callsite!(&Cs)), + Kind::SPAN, + ); + + let interest = filter.register_callsite(META); + assert!(interest.is_always()); + } + + #[test] + fn callsite_enabled_includes_span_directive_multiple_fields() { + let filter = EnvFilter::new("app[mySpan{field=\"value\",field2=2}]=debug") + .with_subscriber(NoSubscriber); + static META: &Metadata<'static> = &Metadata::new( + "mySpan", + "app", + Level::TRACE, + None, + None, + None, + FieldSet::new(&["field"], identify_callsite!(&Cs)), + Kind::SPAN, + ); + + let interest = filter.register_callsite(META); + assert!(interest.is_never()); + } + + #[test] + fn roundtrip() { + let f1: EnvFilter = + "[span1{foo=1}]=error,[span2{bar=2 baz=false}],crate2[{quux=\"quuux\"}]=debug" + .parse() + .unwrap(); + let f2: EnvFilter = format!("{}", f1).parse().unwrap(); + assert_eq!(f1.statics, f2.statics); + assert_eq!(f1.dynamics, f2.dynamics); + } + + #[test] + fn size_of_filters() { + fn print_sz(s: &str) { + let filter = s.parse::<EnvFilter>().expect("filter should parse"); + println!( + "size_of_val({:?})\n -> {}B", + s, + std::mem::size_of_val(&filter) + ); + } + + print_sz("info"); + + print_sz("foo=debug"); + + print_sz( + "crate1::mod1=error,crate1::mod2=warn,crate1::mod2::mod3=info,\ + crate2=debug,crate3=trace,crate3::mod2::mod1=off", + ); + + print_sz("[span1{foo=1}]=error,[span2{bar=2 baz=false}],crate2[{quux=\"quuux\"}]=debug"); + + print_sz( + "crate1::mod1=error,crate1::mod2=warn,crate1::mod2::mod3=info,\ + crate2=debug,crate3=trace,crate3::mod2::mod1=off,[span1{foo=1}]=error,\ + [span2{bar=2 baz=false}],crate2[{quux=\"quuux\"}]=debug", + ); + } + + #[test] + fn parse_empty_string() { + // There is no corresponding test for [`Builder::parse_lossy`] as failed + // parsing does not produce any observable side effects. If this test fails + // check that [`Builder::parse_lossy`] is behaving correctly as well. + assert!(EnvFilter::builder().parse("").is_ok()); + } +} diff --git a/vendor/tracing-subscriber/src/filter/filter_fn.rs b/vendor/tracing-subscriber/src/filter/filter_fn.rs new file mode 100644 index 000000000..332bf860a --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/filter_fn.rs @@ -0,0 +1,749 @@ +use crate::{ + filter::LevelFilter, + layer::{Context, Layer}, +}; +use core::{any::type_name, fmt, marker::PhantomData}; +use tracing_core::{Interest, Metadata, Subscriber}; + +/// A filter implemented by a closure or function pointer that +/// determines whether a given span or event is enabled, based on its +/// [`Metadata`]. +/// +/// This type can be used for both [per-layer filtering][plf] (using its +/// [`Filter`] implementation) and [global filtering][global] (using its +/// [`Layer`] implementation). +/// +/// See the [documentation on filtering with layers][filtering] for details. +/// +/// [`Metadata`]: tracing_core::Metadata +/// [`Filter`]: crate::layer::Filter +/// [`Layer`]: crate::layer::Layer +/// [plf]: crate::layer#per-layer-filtering +/// [global]: crate::layer#global-filtering +/// [filtering]: crate::layer#filtering-with-layers +#[derive(Clone)] +pub struct FilterFn<F = fn(&Metadata<'_>) -> bool> { + enabled: F, + max_level_hint: Option<LevelFilter>, +} + +/// A filter implemented by a closure or function pointer that +/// determines whether a given span or event is enabled _dynamically_, +/// potentially based on the current [span context]. +/// +/// This type can be used for both [per-layer filtering][plf] (using its +/// [`Filter`] implementation) and [global filtering][global] (using its +/// [`Layer`] implementation). +/// +/// See the [documentation on filtering with layers][filtering] for details. +/// +/// [span context]: crate::layer::Context +/// [`Filter`]: crate::layer::Filter +/// [`Layer`]: crate::layer::Layer +/// [plf]: crate::layer#per-layer-filtering +/// [global]: crate::layer#global-filtering +/// [filtering]: crate::layer#filtering-with-layers +pub struct DynFilterFn< + S, + // TODO(eliza): should these just be boxed functions? + F = fn(&Metadata<'_>, &Context<'_, S>) -> bool, + R = fn(&'static Metadata<'static>) -> Interest, +> { + enabled: F, + register_callsite: Option<R>, + max_level_hint: Option<LevelFilter>, + _s: PhantomData<fn(S)>, +} + +// === impl FilterFn === + +/// Constructs a [`FilterFn`], from a function or closure that returns `true` if +/// a span or event should be enabled, based on its [`Metadata`]. +/// +/// The returned [`FilterFn`] can be used for both [per-layer filtering][plf] +/// (using its [`Filter`] implementation) and [global filtering][global] (using +/// its [`Layer`] implementation). +/// +/// See the [documentation on filtering with layers][filtering] for details. +/// +/// This is equivalent to calling [`FilterFn::new`]. +/// +/// [`Metadata`]: tracing_core::Metadata +/// [`Filter`]: crate::layer::Filter +/// [`Layer`]: crate::layer::Layer +/// [plf]: crate::layer#per-layer-filtering +/// [global]: crate::layer#global-filtering +/// [filtering]: crate::layer#filtering-with-layers +/// +/// # Examples +/// +/// ``` +/// use tracing_subscriber::{ +/// layer::{Layer, SubscriberExt}, +/// filter, +/// util::SubscriberInitExt, +/// }; +/// +/// let my_filter = filter::filter_fn(|metadata| { +/// // Only enable spans or events with the target "interesting_things" +/// metadata.target() == "interesting_things" +/// }); +/// +/// let my_layer = tracing_subscriber::fmt::layer(); +/// +/// tracing_subscriber::registry() +/// .with(my_layer.with_filter(my_filter)) +/// .init(); +/// +/// // This event will not be enabled. +/// tracing::warn!("something important but uninteresting happened!"); +/// +/// // This event will be enabled. +/// tracing::debug!(target: "interesting_things", "an interesting minor detail..."); +/// ``` +pub fn filter_fn<F>(f: F) -> FilterFn<F> +where + F: Fn(&Metadata<'_>) -> bool, +{ + FilterFn::new(f) +} + +/// Constructs a [`DynFilterFn`] from a function or closure that returns `true` +/// if a span or event should be enabled within a particular [span context][`Context`]. +/// +/// This is equivalent to calling [`DynFilterFn::new`]. +/// +/// Unlike [`filter_fn`], this function takes a closure or function pointer +/// taking the [`Metadata`] for a span or event *and* the current [`Context`]. +/// This means that a [`DynFilterFn`] can choose whether to enable spans or +/// events based on information about the _current_ span (or its parents). +/// +/// If this is *not* necessary, use [`filter_fn`] instead. +/// +/// The returned [`DynFilterFn`] can be used for both [per-layer filtering][plf] +/// (using its [`Filter`] implementation) and [global filtering][global] (using +/// its [`Layer`] implementation). +/// +/// See the [documentation on filtering with layers][filtering] for details. +/// +/// # Examples +/// +/// ``` +/// use tracing_subscriber::{ +/// layer::{Layer, SubscriberExt}, +/// filter, +/// util::SubscriberInitExt, +/// }; +/// +/// // Only enable spans or events within a span named "interesting_span". +/// let my_filter = filter::dynamic_filter_fn(|metadata, cx| { +/// // If this *is* "interesting_span", make sure to enable it. +/// if metadata.is_span() && metadata.name() == "interesting_span" { +/// return true; +/// } +/// +/// // Otherwise, are we in an interesting span? +/// if let Some(current_span) = cx.lookup_current() { +/// return current_span.name() == "interesting_span"; +/// } +/// +/// false +/// }); +/// +/// let my_layer = tracing_subscriber::fmt::layer(); +/// +/// tracing_subscriber::registry() +/// .with(my_layer.with_filter(my_filter)) +/// .init(); +/// +/// // This event will not be enabled. +/// tracing::info!("something happened"); +/// +/// tracing::info_span!("interesting_span").in_scope(|| { +/// // This event will be enabled. +/// tracing::debug!("something else happened"); +/// }); +/// ``` +/// +/// [`Filter`]: crate::layer::Filter +/// [`Layer`]: crate::layer::Layer +/// [plf]: crate::layer#per-layer-filtering +/// [global]: crate::layer#global-filtering +/// [filtering]: crate::layer#filtering-with-layers +/// [`Context`]: crate::layer::Context +/// [`Metadata`]: tracing_core::Metadata +pub fn dynamic_filter_fn<S, F>(f: F) -> DynFilterFn<S, F> +where + F: Fn(&Metadata<'_>, &Context<'_, S>) -> bool, +{ + DynFilterFn::new(f) +} + +impl<F> FilterFn<F> +where + F: Fn(&Metadata<'_>) -> bool, +{ + /// Constructs a [`FilterFn`] from a function or closure that returns `true` + /// if a span or event should be enabled, based on its [`Metadata`]. + /// + /// If determining whether a span or event should be enabled also requires + /// information about the current span context, use [`DynFilterFn`] instead. + /// + /// See the [documentation on per-layer filtering][plf] for details on using + /// [`Filter`]s. + /// + /// [`Filter`]: crate::layer::Filter + /// [plf]: crate::layer#per-layer-filtering + /// [`Metadata`]: tracing_core::Metadata + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::{ + /// layer::{Layer, SubscriberExt}, + /// filter::FilterFn, + /// util::SubscriberInitExt, + /// }; + /// + /// let my_filter = FilterFn::new(|metadata| { + /// // Only enable spans or events with the target "interesting_things" + /// metadata.target() == "interesting_things" + /// }); + /// + /// let my_layer = tracing_subscriber::fmt::layer(); + /// + /// tracing_subscriber::registry() + /// .with(my_layer.with_filter(my_filter)) + /// .init(); + /// + /// // This event will not be enabled. + /// tracing::warn!("something important but uninteresting happened!"); + /// + /// // This event will be enabled. + /// tracing::debug!(target: "interesting_things", "an interesting minor detail..."); + /// ``` + pub fn new(enabled: F) -> Self { + Self { + enabled, + max_level_hint: None, + } + } + + /// Sets the highest verbosity [`Level`] the filter function will enable. + /// + /// The value passed to this method will be returned by this `FilterFn`'s + /// [`Filter::max_level_hint`] method. + /// + /// If the provided function will not enable all levels, it is recommended + /// to call this method to configure it with the most verbose level it will + /// enable. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::{ + /// layer::{Layer, SubscriberExt}, + /// filter::{filter_fn, LevelFilter}, + /// util::SubscriberInitExt, + /// }; + /// use tracing_core::Level; + /// + /// let my_filter = filter_fn(|metadata| { + /// // Only enable spans or events with targets starting with `my_crate` + /// // and levels at or below `INFO`. + /// metadata.level() <= &Level::INFO && metadata.target().starts_with("my_crate") + /// }) + /// // Since the filter closure will only enable the `INFO` level and + /// // below, set the max level hint + /// .with_max_level_hint(LevelFilter::INFO); + /// + /// let my_layer = tracing_subscriber::fmt::layer(); + /// + /// tracing_subscriber::registry() + /// .with(my_layer.with_filter(my_filter)) + /// .init(); + /// ``` + /// + /// [`Level`]: tracing_core::Level + /// [`Filter::max_level_hint`]: crate::layer::Filter::max_level_hint + pub fn with_max_level_hint(self, max_level_hint: impl Into<LevelFilter>) -> Self { + Self { + max_level_hint: Some(max_level_hint.into()), + ..self + } + } + + #[inline] + pub(in crate::filter) fn is_enabled(&self, metadata: &Metadata<'_>) -> bool { + let enabled = (self.enabled)(metadata); + debug_assert!( + !enabled || self.is_below_max_level(metadata), + "FilterFn<{}> claimed it would only enable {:?} and below, \ + but it enabled metadata with the {:?} level\nmetadata={:#?}", + type_name::<F>(), + self.max_level_hint.unwrap(), + metadata.level(), + metadata, + ); + + enabled + } + + #[inline] + pub(in crate::filter) fn is_callsite_enabled( + &self, + metadata: &'static Metadata<'static>, + ) -> Interest { + // Because `self.enabled` takes a `Metadata` only (and no `Context` + // parameter), we can reasonably assume its results are cachable, and + // just return `Interest::always`/`Interest::never`. + if (self.enabled)(metadata) { + debug_assert!( + self.is_below_max_level(metadata), + "FilterFn<{}> claimed it was only interested in {:?} and below, \ + but it enabled metadata with the {:?} level\nmetadata={:#?}", + type_name::<F>(), + self.max_level_hint.unwrap(), + metadata.level(), + metadata, + ); + return Interest::always(); + } + + Interest::never() + } + + fn is_below_max_level(&self, metadata: &Metadata<'_>) -> bool { + self.max_level_hint + .as_ref() + .map(|hint| metadata.level() <= hint) + .unwrap_or(true) + } +} + +impl<S, F> Layer<S> for FilterFn<F> +where + F: Fn(&Metadata<'_>) -> bool + 'static, + S: Subscriber, +{ + fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool { + self.is_enabled(metadata) + } + + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.is_callsite_enabled(metadata) + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + self.max_level_hint + } +} + +impl<F> From<F> for FilterFn<F> +where + F: Fn(&Metadata<'_>) -> bool, +{ + fn from(enabled: F) -> Self { + Self::new(enabled) + } +} + +impl<F> fmt::Debug for FilterFn<F> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FilterFn") + .field("enabled", &format_args!("{}", type_name::<F>())) + .field("max_level_hint", &self.max_level_hint) + .finish() + } +} + +// === impl DynFilterFn == + +impl<S, F> DynFilterFn<S, F> +where + F: Fn(&Metadata<'_>, &Context<'_, S>) -> bool, +{ + /// Constructs a [`Filter`] from a function or closure that returns `true` + /// if a span or event should be enabled in the current [span + /// context][`Context`]. + /// + /// Unlike [`FilterFn`], a `DynFilterFn` is constructed from a closure or + /// function pointer that takes both the [`Metadata`] for a span or event + /// *and* the current [`Context`]. This means that a [`DynFilterFn`] can + /// choose whether to enable spans or events based on information about the + /// _current_ span (or its parents). + /// + /// If this is *not* necessary, use [`FilterFn`] instead. + /// + /// See the [documentation on per-layer filtering][plf] for details on using + /// [`Filter`]s. + /// + /// [`Filter`]: crate::layer::Filter + /// [plf]: crate::layer#per-layer-filtering + /// [`Context`]: crate::layer::Context + /// [`Metadata`]: tracing_core::Metadata + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::{ + /// layer::{Layer, SubscriberExt}, + /// filter::DynFilterFn, + /// util::SubscriberInitExt, + /// }; + /// + /// // Only enable spans or events within a span named "interesting_span". + /// let my_filter = DynFilterFn::new(|metadata, cx| { + /// // If this *is* "interesting_span", make sure to enable it. + /// if metadata.is_span() && metadata.name() == "interesting_span" { + /// return true; + /// } + /// + /// // Otherwise, are we in an interesting span? + /// if let Some(current_span) = cx.lookup_current() { + /// return current_span.name() == "interesting_span"; + /// } + /// + /// false + /// }); + /// + /// let my_layer = tracing_subscriber::fmt::layer(); + /// + /// tracing_subscriber::registry() + /// .with(my_layer.with_filter(my_filter)) + /// .init(); + /// + /// // This event will not be enabled. + /// tracing::info!("something happened"); + /// + /// tracing::info_span!("interesting_span").in_scope(|| { + /// // This event will be enabled. + /// tracing::debug!("something else happened"); + /// }); + /// ``` + pub fn new(enabled: F) -> Self { + Self { + enabled, + register_callsite: None, + max_level_hint: None, + _s: PhantomData, + } + } +} + +impl<S, F, R> DynFilterFn<S, F, R> +where + F: Fn(&Metadata<'_>, &Context<'_, S>) -> bool, +{ + /// Sets the highest verbosity [`Level`] the filter function will enable. + /// + /// The value passed to this method will be returned by this `DynFilterFn`'s + /// [`Filter::max_level_hint`] method. + /// + /// If the provided function will not enable all levels, it is recommended + /// to call this method to configure it with the most verbose level it will + /// enable. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::{ + /// layer::{Layer, SubscriberExt}, + /// filter::{DynFilterFn, LevelFilter}, + /// util::SubscriberInitExt, + /// }; + /// use tracing_core::Level; + /// + /// // Only enable spans or events with levels at or below `INFO`, if + /// // we are inside a span called "interesting_span". + /// let my_filter = DynFilterFn::new(|metadata, cx| { + /// // If the level is greater than INFO, disable it. + /// if metadata.level() > &Level::INFO { + /// return false; + /// } + /// + /// // If any span in the current scope is named "interesting_span", + /// // enable this span or event. + /// for span in cx.lookup_current().iter().flat_map(|span| span.scope()) { + /// if span.name() == "interesting_span" { + /// return true; + /// } + /// } + /// + /// // Otherwise, disable it. + /// false + /// }) + /// // Since the filter closure will only enable the `INFO` level and + /// // below, set the max level hint + /// .with_max_level_hint(LevelFilter::INFO); + /// + /// let my_layer = tracing_subscriber::fmt::layer(); + /// + /// tracing_subscriber::registry() + /// .with(my_layer.with_filter(my_filter)) + /// .init(); + /// ``` + /// + /// [`Level`]: tracing_core::Level + /// [`Filter::max_level_hint`]: crate::layer::Filter::max_level_hint + pub fn with_max_level_hint(self, max_level_hint: impl Into<LevelFilter>) -> Self { + Self { + max_level_hint: Some(max_level_hint.into()), + ..self + } + } + + /// Adds a function for filtering callsites to this filter. + /// + /// When this filter's [`Filter::callsite_enabled`][cse] method is called, + /// the provided function will be used rather than the default. + /// + /// By default, `DynFilterFn` assumes that, because the filter _may_ depend + /// dynamically on the current [span context], its result should never be + /// cached. However, some filtering strategies may require dynamic information + /// from the current span context in *some* cases, but are able to make + /// static filtering decisions from [`Metadata`] alone in others. + /// + /// For example, consider the filter given in the example for + /// [`DynFilterFn::new`]. That filter enables all spans named + /// "interesting_span", and any events and spans that occur inside of an + /// interesting span. Since the span's name is part of its static + /// [`Metadata`], the "interesting_span" can be enabled in + /// [`callsite_enabled`][cse]: + /// + /// ``` + /// use tracing_subscriber::{ + /// layer::{Layer, SubscriberExt}, + /// filter::DynFilterFn, + /// util::SubscriberInitExt, + /// }; + /// use tracing_core::subscriber::Interest; + /// + /// // Only enable spans or events within a span named "interesting_span". + /// let my_filter = DynFilterFn::new(|metadata, cx| { + /// // If this *is* "interesting_span", make sure to enable it. + /// if metadata.is_span() && metadata.name() == "interesting_span" { + /// return true; + /// } + /// + /// // Otherwise, are we in an interesting span? + /// if let Some(current_span) = cx.lookup_current() { + /// return current_span.name() == "interesting_span"; + /// } + /// + /// false + /// }).with_callsite_filter(|metadata| { + /// // If this is an "interesting_span", we know we will always + /// // enable it. + /// if metadata.is_span() && metadata.name() == "interesting_span" { + /// return Interest::always(); + /// } + /// + /// // Otherwise, it depends on whether or not we're in an interesting + /// // span. You'll have to ask us again for each span/event! + /// Interest::sometimes() + /// }); + /// + /// let my_layer = tracing_subscriber::fmt::layer(); + /// + /// tracing_subscriber::registry() + /// .with(my_layer.with_filter(my_filter)) + /// .init(); + /// ``` + /// + /// [cse]: crate::layer::Filter::callsite_enabled + /// [`enabled`]: crate::layer::Filter::enabled + /// [`Metadata`]: tracing_core::Metadata + /// [span context]: crate::layer::Context + pub fn with_callsite_filter<R2>(self, callsite_enabled: R2) -> DynFilterFn<S, F, R2> + where + R2: Fn(&'static Metadata<'static>) -> Interest, + { + let register_callsite = Some(callsite_enabled); + let DynFilterFn { + enabled, + max_level_hint, + _s, + .. + } = self; + DynFilterFn { + enabled, + register_callsite, + max_level_hint, + _s, + } + } + + fn default_callsite_enabled(&self, metadata: &Metadata<'_>) -> Interest { + // If it's below the configured max level, assume that `enabled` will + // never enable it... + if !is_below_max_level(&self.max_level_hint, metadata) { + debug_assert!( + !(self.enabled)(metadata, &Context::none()), + "DynFilterFn<{}> claimed it would only enable {:?} and below, \ + but it enabled metadata with the {:?} level\nmetadata={:#?}", + type_name::<F>(), + self.max_level_hint.unwrap(), + metadata.level(), + metadata, + ); + return Interest::never(); + } + + // Otherwise, since this `enabled` function is dynamic and depends on + // the current context, we don't know whether this span or event will be + // enabled or not. Ask again every time it's recorded! + Interest::sometimes() + } +} + +impl<S, F, R> DynFilterFn<S, F, R> +where + F: Fn(&Metadata<'_>, &Context<'_, S>) -> bool, + R: Fn(&'static Metadata<'static>) -> Interest, +{ + #[inline] + fn is_enabled(&self, metadata: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + let enabled = (self.enabled)(metadata, cx); + debug_assert!( + !enabled || is_below_max_level(&self.max_level_hint, metadata), + "DynFilterFn<{}> claimed it would only enable {:?} and below, \ + but it enabled metadata with the {:?} level\nmetadata={:#?}", + type_name::<F>(), + self.max_level_hint.unwrap(), + metadata.level(), + metadata, + ); + + enabled + } + + #[inline] + fn is_callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest { + let interest = self + .register_callsite + .as_ref() + .map(|callsite_enabled| callsite_enabled(metadata)) + .unwrap_or_else(|| self.default_callsite_enabled(metadata)); + debug_assert!( + interest.is_never() || is_below_max_level(&self.max_level_hint, metadata), + "DynFilterFn<{}, {}> claimed it was only interested in {:?} and below, \ + but it enabled metadata with the {:?} level\nmetadata={:#?}", + type_name::<F>(), + type_name::<R>(), + self.max_level_hint.unwrap(), + metadata.level(), + metadata, + ); + + interest + } +} + +impl<S, F, R> Layer<S> for DynFilterFn<S, F, R> +where + F: Fn(&Metadata<'_>, &Context<'_, S>) -> bool + 'static, + R: Fn(&'static Metadata<'static>) -> Interest + 'static, + S: Subscriber, +{ + fn enabled(&self, metadata: &Metadata<'_>, cx: Context<'_, S>) -> bool { + self.is_enabled(metadata, &cx) + } + + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.is_callsite_enabled(metadata) + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + self.max_level_hint + } +} + +impl<S, F, R> fmt::Debug for DynFilterFn<S, F, R> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut s = f.debug_struct("DynFilterFn"); + s.field("enabled", &format_args!("{}", type_name::<F>())); + if self.register_callsite.is_some() { + s.field( + "register_callsite", + &format_args!("Some({})", type_name::<R>()), + ); + } else { + s.field("register_callsite", &format_args!("None")); + } + + s.field("max_level_hint", &self.max_level_hint).finish() + } +} + +impl<S, F, R> Clone for DynFilterFn<S, F, R> +where + F: Clone, + R: Clone, +{ + fn clone(&self) -> Self { + Self { + enabled: self.enabled.clone(), + register_callsite: self.register_callsite.clone(), + max_level_hint: self.max_level_hint, + _s: PhantomData, + } + } +} + +impl<F, S> From<F> for DynFilterFn<S, F> +where + F: Fn(&Metadata<'_>, &Context<'_, S>) -> bool, +{ + fn from(f: F) -> Self { + Self::new(f) + } +} + +// === PLF impls === + +feature! { + #![all(feature = "registry", feature = "std")] + use crate::layer::Filter; + + impl<S, F> Filter<S> for FilterFn<F> + where + F: Fn(&Metadata<'_>) -> bool, + { + fn enabled(&self, metadata: &Metadata<'_>, _: &Context<'_, S>) -> bool { + self.is_enabled(metadata) + } + + fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest { + self.is_callsite_enabled(metadata) + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + self.max_level_hint + } + } + + impl<S, F, R> Filter<S> for DynFilterFn<S, F, R> + where + F: Fn(&Metadata<'_>, &Context<'_, S>) -> bool, + R: Fn(&'static Metadata<'static>) -> Interest, + { + fn enabled(&self, metadata: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + self.is_enabled(metadata, cx) + } + + fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest { + self.is_callsite_enabled(metadata) + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + self.max_level_hint + } + } +} + +fn is_below_max_level(hint: &Option<LevelFilter>, metadata: &Metadata<'_>) -> bool { + hint.as_ref() + .map(|hint| metadata.level() <= hint) + .unwrap_or(true) +} diff --git a/vendor/tracing-subscriber/src/filter/layer_filters/combinator.rs b/vendor/tracing-subscriber/src/filter/layer_filters/combinator.rs new file mode 100644 index 000000000..e79de2087 --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/layer_filters/combinator.rs @@ -0,0 +1,469 @@ +//! Filter combinators +use crate::layer::{Context, Filter}; +use std::{cmp, fmt, marker::PhantomData}; +use tracing_core::{ + span::{Attributes, Id, Record}, + subscriber::Interest, + LevelFilter, Metadata, +}; + +/// Combines two [`Filter`]s so that spans and events are enabled if and only if +/// *both* filters return `true`. +/// +/// This type is typically returned by the [`FilterExt::and`] method. See that +/// method's documentation for details. +/// +/// [`Filter`]: crate::layer::Filter +/// [`FilterExt::and`]: crate::filter::FilterExt::and +pub struct And<A, B, S> { + a: A, + b: B, + _s: PhantomData<fn(S)>, +} + +/// Combines two [`Filter`]s so that spans and events are enabled if *either* filter +/// returns `true`. +/// +/// This type is typically returned by the [`FilterExt::or`] method. See that +/// method's documentation for details. +/// +/// [`Filter`]: crate::layer::Filter +/// [`FilterExt::or`]: crate::filter::FilterExt::or +pub struct Or<A, B, S> { + a: A, + b: B, + _s: PhantomData<fn(S)>, +} + +/// Inverts the result of a [`Filter`]. +/// +/// If the wrapped filter would enable a span or event, it will be disabled. If +/// it would disable a span or event, that span or event will be enabled. +/// +/// This type is typically returned by the [`FilterExt::or`] method. See that +/// method's documentation for details. +/// +/// [`Filter`]: crate::layer::Filter +/// [`FilterExt::or`]: crate::filter::FilterExt::or +pub struct Not<A, S> { + a: A, + _s: PhantomData<fn(S)>, +} + +// === impl And === + +impl<A, B, S> And<A, B, S> +where + A: Filter<S>, + B: Filter<S>, +{ + /// Combines two [`Filter`]s so that spans and events are enabled if and only if + /// *both* filters return `true`. + /// + /// # Examples + /// + /// Enabling spans or events if they have both a particular target *and* are + /// above a certain level: + /// + /// ```ignore + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, combinator::And}, + /// prelude::*, + /// }; + /// + /// // Enables spans and events with targets starting with `interesting_target`: + /// let target_filter = filter_fn(|meta| { + /// meta.target().starts_with("interesting_target") + /// }); + /// + /// // Enables spans and events with levels `INFO` and below: + /// let level_filter = LevelFilter::INFO; + /// + /// // Combine the two filters together so that a span or event is only enabled + /// // if *both* filters would enable it: + /// let filter = And::new(level_filter, target_filter); + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// + /// // This event will *not* be enabled: + /// tracing::info!("an event with an uninteresting target"); + /// + /// // This event *will* be enabled: + /// tracing::info!(target: "interesting_target", "a very interesting event"); + /// + /// // This event will *not* be enabled: + /// tracing::debug!(target: "interesting_target", "interesting debug event..."); + /// ``` + /// + /// [`Filter`]: crate::layer::Filter + pub(crate) fn new(a: A, b: B) -> Self { + Self { + a, + b, + _s: PhantomData, + } + } +} + +impl<A, B, S> Filter<S> for And<A, B, S> +where + A: Filter<S>, + B: Filter<S>, +{ + #[inline] + fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + self.a.enabled(meta, cx) && self.b.enabled(meta, cx) + } + + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + let a = self.a.callsite_enabled(meta); + if a.is_never() { + return a; + } + + let b = self.b.callsite_enabled(meta); + + if !b.is_always() { + return b; + } + + a + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + // If either hint is `None`, return `None`. Otherwise, return the most restrictive. + cmp::min(self.a.max_level_hint(), self.b.max_level_hint()) + } + + #[inline] + fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { + self.a.on_new_span(attrs, id, ctx.clone()); + self.b.on_new_span(attrs, id, ctx) + } + + #[inline] + fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { + self.a.on_record(id, values, ctx.clone()); + self.b.on_record(id, values, ctx); + } + + #[inline] + fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { + self.a.on_enter(id, ctx.clone()); + self.b.on_enter(id, ctx); + } + + #[inline] + fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { + self.a.on_exit(id, ctx.clone()); + self.b.on_exit(id, ctx); + } + + #[inline] + fn on_close(&self, id: Id, ctx: Context<'_, S>) { + self.a.on_close(id.clone(), ctx.clone()); + self.b.on_close(id, ctx); + } +} + +impl<A, B, S> Clone for And<A, B, S> +where + A: Clone, + B: Clone, +{ + fn clone(&self) -> Self { + Self { + a: self.a.clone(), + b: self.b.clone(), + _s: PhantomData, + } + } +} + +impl<A, B, S> fmt::Debug for And<A, B, S> +where + A: fmt::Debug, + B: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("And") + .field("a", &self.a) + .field("b", &self.b) + .finish() + } +} + +// === impl Or === + +impl<A, B, S> Or<A, B, S> +where + A: Filter<S>, + B: Filter<S>, +{ + /// Combines two [`Filter`]s so that spans and events are enabled if *either* filter + /// returns `true`. + /// + /// # Examples + /// + /// Enabling spans and events at the `INFO` level and above, and all spans + /// and events with a particular target: + /// + /// ```ignore + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, combinator::Or}, + /// prelude::*, + /// }; + /// + /// // Enables spans and events with targets starting with `interesting_target`: + /// let target_filter = filter_fn(|meta| { + /// meta.target().starts_with("interesting_target") + /// }); + /// + /// // Enables spans and events with levels `INFO` and below: + /// let level_filter = LevelFilter::INFO; + /// + /// // Combine the two filters together so that a span or event is enabled + /// // if it is at INFO or lower, or if it has a target starting with + /// // `interesting_target`. + /// let filter = Or::new(level_filter, target_filter); + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// + /// // This event will *not* be enabled: + /// tracing::debug!("an uninteresting event"); + /// + /// // This event *will* be enabled: + /// tracing::info!("an uninteresting INFO event"); + /// + /// // This event *will* be enabled: + /// tracing::info!(target: "interesting_target", "a very interesting event"); + /// + /// // This event *will* be enabled: + /// tracing::debug!(target: "interesting_target", "interesting debug event..."); + /// ``` + /// + /// Enabling a higher level for a particular target by using `Or` in + /// conjunction with the [`And`] combinator: + /// + /// ```ignore + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, combinator}, + /// prelude::*, + /// }; + /// + /// // This filter will enable spans and events with targets beginning with + /// // `my_crate`: + /// let my_crate = filter_fn(|meta| { + /// meta.target().starts_with("my_crate") + /// }); + /// + /// // Combine the `my_crate` filter with a `LevelFilter` to produce a filter + /// // that will enable the `INFO` level and lower for spans and events with + /// // `my_crate` targets: + /// let filter = combinator::And::new(my_crate, LevelFilter::INFO); + /// + /// // If a span or event *doesn't* have a target beginning with + /// // `my_crate`, enable it if it has the `WARN` level or lower: + /// // let filter = combinator::Or::new(filter, LevelFilter::WARN); + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// ``` + /// + /// [`Filter`]: crate::layer::Filter + pub(crate) fn new(a: A, b: B) -> Self { + Self { + a, + b, + _s: PhantomData, + } + } +} + +impl<A, B, S> Filter<S> for Or<A, B, S> +where + A: Filter<S>, + B: Filter<S>, +{ + #[inline] + fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + self.a.enabled(meta, cx) || self.b.enabled(meta, cx) + } + + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + let a = self.a.callsite_enabled(meta); + let b = self.b.callsite_enabled(meta); + + // If either filter will always enable the span or event, return `always`. + if a.is_always() || b.is_always() { + return Interest::always(); + } + + // Okay, if either filter will sometimes enable the span or event, + // return `sometimes`. + if a.is_sometimes() || b.is_sometimes() { + return Interest::sometimes(); + } + + debug_assert!( + a.is_never() && b.is_never(), + "if neither filter was `always` or `sometimes`, both must be `never` (a={:?}; b={:?})", + a, + b, + ); + Interest::never() + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + // If either hint is `None`, return `None`. Otherwise, return the less restrictive. + Some(cmp::max(self.a.max_level_hint()?, self.b.max_level_hint()?)) + } + + #[inline] + fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { + self.a.on_new_span(attrs, id, ctx.clone()); + self.b.on_new_span(attrs, id, ctx) + } + + #[inline] + fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { + self.a.on_record(id, values, ctx.clone()); + self.b.on_record(id, values, ctx); + } + + #[inline] + fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { + self.a.on_enter(id, ctx.clone()); + self.b.on_enter(id, ctx); + } + + #[inline] + fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { + self.a.on_exit(id, ctx.clone()); + self.b.on_exit(id, ctx); + } + + #[inline] + fn on_close(&self, id: Id, ctx: Context<'_, S>) { + self.a.on_close(id.clone(), ctx.clone()); + self.b.on_close(id, ctx); + } +} + +impl<A, B, S> Clone for Or<A, B, S> +where + A: Clone, + B: Clone, +{ + fn clone(&self) -> Self { + Self { + a: self.a.clone(), + b: self.b.clone(), + _s: PhantomData, + } + } +} + +impl<A, B, S> fmt::Debug for Or<A, B, S> +where + A: fmt::Debug, + B: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Or") + .field("a", &self.a) + .field("b", &self.b) + .finish() + } +} + +// === impl Not === + +impl<A, S> Not<A, S> +where + A: Filter<S>, +{ + /// Inverts the result of a [`Filter`]. + /// + /// If the wrapped filter would enable a span or event, it will be disabled. If + /// it would disable a span or event, that span or event will be enabled. + /// + /// [`Filter`]: crate::layer::Filter + pub(crate) fn new(a: A) -> Self { + Self { a, _s: PhantomData } + } +} + +impl<A, S> Filter<S> for Not<A, S> +where + A: Filter<S>, +{ + #[inline] + fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + !self.a.enabled(meta, cx) + } + + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + match self.a.callsite_enabled(meta) { + i if i.is_always() => Interest::never(), + i if i.is_never() => Interest::always(), + _ => Interest::sometimes(), + } + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + // TODO(eliza): figure this out??? + None + } + + #[inline] + fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { + self.a.on_new_span(attrs, id, ctx); + } + + #[inline] + fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { + self.a.on_record(id, values, ctx.clone()); + } + + #[inline] + fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { + self.a.on_enter(id, ctx); + } + + #[inline] + fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { + self.a.on_exit(id, ctx); + } + + #[inline] + fn on_close(&self, id: Id, ctx: Context<'_, S>) { + self.a.on_close(id, ctx); + } +} + +impl<A, S> Clone for Not<A, S> +where + A: Clone, +{ + fn clone(&self) -> Self { + Self { + a: self.a.clone(), + _s: PhantomData, + } + } +} + +impl<A, S> fmt::Debug for Not<A, S> +where + A: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Not").field(&self.a).finish() + } +} diff --git a/vendor/tracing-subscriber/src/filter/layer_filters/mod.rs b/vendor/tracing-subscriber/src/filter/layer_filters/mod.rs new file mode 100644 index 000000000..8949cfb5a --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/layer_filters/mod.rs @@ -0,0 +1,1135 @@ +//! ## Per-Layer Filtering +//! +//! Per-layer filters permit individual `Layer`s to have their own filter +//! configurations without interfering with other `Layer`s. +//! +//! This module is not public; the public APIs defined in this module are +//! re-exported in the top-level `filter` module. Therefore, this documentation +//! primarily concerns the internal implementation details. For the user-facing +//! public API documentation, see the individual public types in this module, as +//! well as the, see the `Layer` trait documentation's [per-layer filtering +//! section]][1]. +//! +//! ## How does per-layer filtering work? +//! +//! As described in the API documentation, the [`Filter`] trait defines a +//! filtering strategy for a per-layer filter. We expect there will be a variety +//! of implementations of [`Filter`], both in `tracing-subscriber` and in user +//! code. +//! +//! To actually *use* a [`Filter`] implementation, it is combined with a +//! [`Layer`] by the [`Filtered`] struct defined in this module. [`Filtered`] +//! implements [`Layer`] by calling into the wrapped [`Layer`], or not, based on +//! the filtering strategy. While there will be a variety of types that implement +//! [`Filter`], all actual *uses* of per-layer filtering will occur through the +//! [`Filtered`] struct. Therefore, most of the implementation details live +//! there. +//! +//! [1]: crate::layer#per-layer-filtering +//! [`Filter`]: crate::layer::Filter +use crate::{ + filter::LevelFilter, + layer::{self, Context, Layer}, + registry, +}; +use std::{ + any::TypeId, + cell::{Cell, RefCell}, + fmt, + marker::PhantomData, + ops::Deref, + sync::Arc, + thread_local, +}; +use tracing_core::{ + span, + subscriber::{Interest, Subscriber}, + Event, Metadata, +}; +pub mod combinator; + +/// A [`Layer`] that wraps an inner [`Layer`] and adds a [`Filter`] which +/// controls what spans and events are enabled for that layer. +/// +/// This is returned by the [`Layer::with_filter`] method. See the +/// [documentation on per-layer filtering][plf] for details. +/// +/// [`Filter`]: crate::layer::Filter +/// [plf]: crate::layer#per-layer-filtering +#[cfg_attr(docsrs, doc(cfg(feature = "registry")))] +#[derive(Clone)] +pub struct Filtered<L, F, S> { + filter: F, + layer: L, + id: MagicPlfDowncastMarker, + _s: PhantomData<fn(S)>, +} + +/// Uniquely identifies an individual [`Filter`] instance in the context of +/// a [`Subscriber`]. +/// +/// When adding a [`Filtered`] [`Layer`] to a [`Subscriber`], the [`Subscriber`] +/// generates a `FilterId` for that [`Filtered`] layer. The [`Filtered`] layer +/// will then use the generated ID to query whether a particular span was +/// previously enabled by that layer's [`Filter`]. +/// +/// **Note**: Currently, the [`Registry`] type provided by this crate is the +/// **only** [`Subscriber`] implementation capable of participating in per-layer +/// filtering. Therefore, the `FilterId` type cannot currently be constructed by +/// code outside of `tracing-subscriber`. In the future, new APIs will be added to `tracing-subscriber` to +/// allow non-Registry [`Subscriber`]s to also participate in per-layer +/// filtering. When those APIs are added, subscribers will be responsible +/// for generating and assigning `FilterId`s. +/// +/// [`Filter`]: crate::layer::Filter +/// [`Subscriber`]: tracing_core::Subscriber +/// [`Layer`]: crate::layer::Layer +/// [`Registry`]: crate::registry::Registry +#[cfg(feature = "registry")] +#[cfg_attr(docsrs, doc(cfg(feature = "registry")))] +#[derive(Copy, Clone)] +pub struct FilterId(u64); + +/// A bitmap tracking which [`FilterId`]s have enabled a given span or +/// event. +/// +/// This is currently a private type that's used exclusively by the +/// [`Registry`]. However, in the future, this may become a public API, in order +/// to allow user subscribers to host [`Filter`]s. +/// +/// [`Registry`]: crate::Registry +/// [`Filter`]: crate::layer::Filter +#[derive(Default, Copy, Clone, Eq, PartialEq)] +pub(crate) struct FilterMap { + bits: u64, +} + +/// The current state of `enabled` calls to per-layer filters on this +/// thread. +/// +/// When `Filtered::enabled` is called, the filter will set the bit +/// corresponding to its ID if the filter will disable the event/span being +/// filtered. When the event or span is recorded, the per-layer filter will +/// check its bit to determine if it disabled that event or span, and skip +/// forwarding the event or span to the inner layer if the bit is set. Once +/// a span or event has been skipped by a per-layer filter, it unsets its +/// bit, so that the `FilterMap` has been cleared for the next set of +/// `enabled` calls. +/// +/// FilterState is also read by the `Registry`, for two reasons: +/// +/// 1. When filtering a span, the Registry must store the `FilterMap` +/// generated by `Filtered::enabled` calls for that span as part of the +/// span's per-span data. This allows `Filtered` layers to determine +/// whether they had previously disabled a given span, and avoid showing it +/// to the wrapped layer if it was disabled. +/// +/// This allows `Filtered` layers to also filter out the spans they +/// disable from span traversals (such as iterating over parents, etc). +/// 2. If all the bits are set, then every per-layer filter has decided it +/// doesn't want to enable that span or event. In that case, the +/// `Registry`'s `enabled` method will return `false`, so that +/// recording a span or event can be skipped entirely. +#[derive(Debug)] +pub(crate) struct FilterState { + enabled: Cell<FilterMap>, + // TODO(eliza): `Interest`s should _probably_ be `Copy`. The only reason + // they're not is our Obsessive Commitment to Forwards-Compatibility. If + // this changes in tracing-core`, we can make this a `Cell` rather than + // `RefCell`... + interest: RefCell<Option<Interest>>, + + #[cfg(debug_assertions)] + counters: DebugCounters, +} + +/// Extra counters added to `FilterState` used only to make debug assertions. +#[cfg(debug_assertions)] +#[derive(Debug, Default)] +struct DebugCounters { + /// How many per-layer filters have participated in the current `enabled` + /// call? + in_filter_pass: Cell<usize>, + + /// How many per-layer filters have participated in the current `register_callsite` + /// call? + in_interest_pass: Cell<usize>, +} + +thread_local! { + pub(crate) static FILTERING: FilterState = FilterState::new(); +} + +/// Extension trait adding [combinators] for combining [`Filter`]. +/// +/// [combinators]: crate::filter::combinator +/// [`Filter`]: crate::layer::Filter +pub trait FilterExt<S>: layer::Filter<S> { + /// Combines this [`Filter`] with another [`Filter`] s so that spans and + /// events are enabled if and only if *both* filters return `true`. + /// + /// # Examples + /// + /// Enabling spans or events if they have both a particular target *and* are + /// above a certain level: + /// + /// ``` + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, FilterExt}, + /// prelude::*, + /// }; + /// + /// // Enables spans and events with targets starting with `interesting_target`: + /// let target_filter = filter_fn(|meta| { + /// meta.target().starts_with("interesting_target") + /// }); + /// + /// // Enables spans and events with levels `INFO` and below: + /// let level_filter = LevelFilter::INFO; + /// + /// // Combine the two filters together, returning a filter that only enables + /// // spans and events that *both* filters will enable: + /// let filter = target_filter.and(level_filter); + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// + /// // This event will *not* be enabled: + /// tracing::info!("an event with an uninteresting target"); + /// + /// // This event *will* be enabled: + /// tracing::info!(target: "interesting_target", "a very interesting event"); + /// + /// // This event will *not* be enabled: + /// tracing::debug!(target: "interesting_target", "interesting debug event..."); + /// ``` + /// + /// [`Filter`]: crate::layer::Filter + fn and<B>(self, other: B) -> combinator::And<Self, B, S> + where + Self: Sized, + B: layer::Filter<S>, + { + combinator::And::new(self, other) + } + + /// Combines two [`Filter`]s so that spans and events are enabled if *either* filter + /// returns `true`. + /// + /// # Examples + /// + /// Enabling spans and events at the `INFO` level and above, and all spans + /// and events with a particular target: + /// ``` + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, FilterExt}, + /// prelude::*, + /// }; + /// + /// // Enables spans and events with targets starting with `interesting_target`: + /// let target_filter = filter_fn(|meta| { + /// meta.target().starts_with("interesting_target") + /// }); + /// + /// // Enables spans and events with levels `INFO` and below: + /// let level_filter = LevelFilter::INFO; + /// + /// // Combine the two filters together so that a span or event is enabled + /// // if it is at INFO or lower, or if it has a target starting with + /// // `interesting_target`. + /// let filter = level_filter.or(target_filter); + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// + /// // This event will *not* be enabled: + /// tracing::debug!("an uninteresting event"); + /// + /// // This event *will* be enabled: + /// tracing::info!("an uninteresting INFO event"); + /// + /// // This event *will* be enabled: + /// tracing::info!(target: "interesting_target", "a very interesting event"); + /// + /// // This event *will* be enabled: + /// tracing::debug!(target: "interesting_target", "interesting debug event..."); + /// ``` + /// + /// Enabling a higher level for a particular target by using `or` in + /// conjunction with the [`and`] combinator: + /// + /// ``` + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, FilterExt}, + /// prelude::*, + /// }; + /// + /// // This filter will enable spans and events with targets beginning with + /// // `my_crate`: + /// let my_crate = filter_fn(|meta| { + /// meta.target().starts_with("my_crate") + /// }); + /// + /// let filter = my_crate + /// // Combine the `my_crate` filter with a `LevelFilter` to produce a + /// // filter that will enable the `INFO` level and lower for spans and + /// // events with `my_crate` targets: + /// .and(LevelFilter::INFO) + /// // If a span or event *doesn't* have a target beginning with + /// // `my_crate`, enable it if it has the `WARN` level or lower: + /// .or(LevelFilter::WARN); + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// ``` + /// + /// [`Filter`]: crate::layer::Filter + /// [`and`]: FilterExt::and + fn or<B>(self, other: B) -> combinator::Or<Self, B, S> + where + Self: Sized, + B: layer::Filter<S>, + { + combinator::Or::new(self, other) + } + + /// Inverts `self`, returning a filter that enables spans and events only if + /// `self` would *not* enable them. + fn not(self) -> combinator::Not<Self, S> + where + Self: Sized, + { + combinator::Not::new(self) + } + + /// [Boxes] `self`, erasing its concrete type. + /// + /// This is equivalent to calling [`Box::new`], but in method form, so that + /// it can be used when chaining combinator methods. + /// + /// # Examples + /// + /// When different combinations of filters are used conditionally, they may + /// have different types. For example, the following code won't compile, + /// since the `if` and `else` clause produce filters of different types: + /// + /// ```compile_fail + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, FilterExt}, + /// prelude::*, + /// }; + /// + /// let enable_bar_target: bool = // ... + /// # false; + /// + /// let filter = if enable_bar_target { + /// filter_fn(|meta| meta.target().starts_with("foo")) + /// // If `enable_bar_target` is true, add a `filter_fn` enabling + /// // spans and events with the target `bar`: + /// .or(filter_fn(|meta| meta.target().starts_with("bar"))) + /// .and(LevelFilter::INFO) + /// } else { + /// filter_fn(|meta| meta.target().starts_with("foo")) + /// .and(LevelFilter::INFO) + /// }; + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// ``` + /// + /// By using `boxed`, the types of the two different branches can be erased, + /// so the assignment to the `filter` variable is valid (as both branches + /// have the type `Box<dyn Filter<S> + Send + Sync + 'static>`). The + /// following code *does* compile: + /// + /// ``` + /// use tracing_subscriber::{ + /// filter::{filter_fn, LevelFilter, FilterExt}, + /// prelude::*, + /// }; + /// + /// let enable_bar_target: bool = // ... + /// # false; + /// + /// let filter = if enable_bar_target { + /// filter_fn(|meta| meta.target().starts_with("foo")) + /// .or(filter_fn(|meta| meta.target().starts_with("bar"))) + /// .and(LevelFilter::INFO) + /// // Boxing the filter erases its type, so both branches now + /// // have the same type. + /// .boxed() + /// } else { + /// filter_fn(|meta| meta.target().starts_with("foo")) + /// .and(LevelFilter::INFO) + /// .boxed() + /// }; + /// + /// tracing_subscriber::registry() + /// .with(tracing_subscriber::fmt::layer().with_filter(filter)) + /// .init(); + /// ``` + /// + /// [Boxes]: std::boxed + /// [`Box::new`]: std::boxed::Box::new + fn boxed(self) -> Box<dyn layer::Filter<S> + Send + Sync + 'static> + where + Self: Sized + Send + Sync + 'static, + { + Box::new(self) + } +} + +// === impl Filter === + +#[cfg(feature = "registry")] +#[cfg_attr(docsrs, doc(cfg(feature = "registry")))] +impl<S> layer::Filter<S> for LevelFilter { + fn enabled(&self, meta: &Metadata<'_>, _: &Context<'_, S>) -> bool { + meta.level() <= self + } + + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + if meta.level() <= self { + Interest::always() + } else { + Interest::never() + } + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + Some(*self) + } +} + +macro_rules! filter_impl_body { + () => { + #[inline] + fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + self.deref().enabled(meta, cx) + } + + #[inline] + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + self.deref().callsite_enabled(meta) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + self.deref().max_level_hint() + } + }; +} + +#[cfg(feature = "registry")] +#[cfg_attr(docsrs, doc(cfg(feature = "registry")))] +impl<S> layer::Filter<S> for Arc<dyn layer::Filter<S> + Send + Sync + 'static> { + filter_impl_body!(); +} + +#[cfg(feature = "registry")] +#[cfg_attr(docsrs, doc(cfg(feature = "registry")))] +impl<S> layer::Filter<S> for Box<dyn layer::Filter<S> + Send + Sync + 'static> { + filter_impl_body!(); +} + +// === impl Filtered === + +impl<L, F, S> Filtered<L, F, S> { + /// Wraps the provided [`Layer`] so that it is filtered by the given + /// [`Filter`]. + /// + /// This is equivalent to calling the [`Layer::with_filter`] method. + /// + /// See the [documentation on per-layer filtering][plf] for details. + /// + /// [`Filter`]: crate::layer::Filter + /// [plf]: crate::layer#per-layer-filtering + pub fn new(layer: L, filter: F) -> Self { + Self { + layer, + filter, + id: MagicPlfDowncastMarker(FilterId::disabled()), + _s: PhantomData, + } + } + + #[inline(always)] + fn id(&self) -> FilterId { + debug_assert!( + !self.id.0.is_disabled(), + "a `Filtered` layer was used, but it had no `FilterId`; \ + was it registered with the subscriber?" + ); + self.id.0 + } + + fn did_enable(&self, f: impl FnOnce()) { + FILTERING.with(|filtering| filtering.did_enable(self.id(), f)) + } + + /// Borrows the [`Filter`](crate::layer::Filter) used by this layer. + pub fn filter(&self) -> &F { + &self.filter + } + + /// Mutably borrows the [`Filter`](crate::layer::Filter) used by this layer. + /// + /// When this layer can be mutably borrowed, this may be used to mutate the filter. + /// Generally, this will primarily be used with the + /// [`reload::Handle::modify`](crate::reload::Handle::modify) method. + /// + /// # Examples + /// + /// ``` + /// # use tracing::info; + /// # use tracing_subscriber::{filter,fmt,reload,Registry,prelude::*}; + /// # fn main() { + /// let filtered_layer = fmt::Layer::default().with_filter(filter::LevelFilter::WARN); + /// let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer); + /// # + /// # // specifying the Registry type is required + /// # let _: &reload::Handle<filter::Filtered<fmt::Layer<Registry>, + /// # filter::LevelFilter, Registry>,Registry> + /// # = &reload_handle; + /// # + /// info!("This will be ignored"); + /// reload_handle.modify(|layer| *layer.filter_mut() = filter::LevelFilter::INFO); + /// info!("This will be logged"); + /// # } + /// ``` + pub fn filter_mut(&mut self) -> &mut F { + &mut self.filter + } + + /// Borrows the inner [`Layer`] wrapped by this `Filtered` layer. + pub fn inner(&self) -> &L { + &self.layer + } + + /// Mutably borrows the inner [`Layer`] wrapped by this `Filtered` layer. + /// + /// This method is primarily expected to be used with the + /// [`reload::Handle::modify`](crate::reload::Handle::modify) method. + /// + /// # Examples + /// + /// ``` + /// # use tracing::info; + /// # use tracing_subscriber::{filter,fmt,reload,Registry,prelude::*}; + /// # fn non_blocking<T: std::io::Write>(writer: T) -> (fn() -> std::io::Stdout) { + /// # std::io::stdout + /// # } + /// # fn main() { + /// let filtered_layer = fmt::layer().with_writer(non_blocking(std::io::stderr())).with_filter(filter::LevelFilter::INFO); + /// let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer); + /// # + /// # // specifying the Registry type is required + /// # let _: &reload::Handle<filter::Filtered<fmt::Layer<Registry, _, _, fn() -> std::io::Stdout>, + /// # filter::LevelFilter, Registry>, Registry> + /// # = &reload_handle; + /// # + /// info!("This will be logged to stderr"); + /// reload_handle.modify(|layer| *layer.inner_mut().writer_mut() = non_blocking(std::io::stdout())); + /// info!("This will be logged to stdout"); + /// # } + /// ``` + /// + /// [subscriber]: Subscribe + pub fn inner_mut(&mut self) -> &mut L { + &mut self.layer + } +} + +impl<S, L, F> Layer<S> for Filtered<L, F, S> +where + S: Subscriber + for<'span> registry::LookupSpan<'span> + 'static, + F: layer::Filter<S> + 'static, + L: Layer<S>, +{ + fn on_layer(&mut self, subscriber: &mut S) { + self.id = MagicPlfDowncastMarker(subscriber.register_filter()); + self.layer.on_layer(subscriber); + } + + // TODO(eliza): can we figure out a nice way to make the `Filtered` layer + // not call `is_enabled_for` in hooks that the inner layer doesn't actually + // have real implementations of? probably not... + // + // it would be cool if there was some wild rust reflection way of checking + // if a trait impl has the default impl of a trait method or not, but that's + // almsot certainly impossible...right? + + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + let interest = self.filter.callsite_enabled(metadata); + + // If the filter didn't disable the callsite, allow the inner layer to + // register it — since `register_callsite` is also used for purposes + // such as reserving/caching per-callsite data, we want the inner layer + // to be able to perform any other registration steps. However, we'll + // ignore its `Interest`. + if !interest.is_never() { + self.layer.register_callsite(metadata); + } + + // Add our `Interest` to the current sum of per-layer filter `Interest`s + // for this callsite. + FILTERING.with(|filtering| filtering.add_interest(interest)); + + // don't short circuit! if the stack consists entirely of `Layer`s with + // per-layer filters, the `Registry` will return the actual `Interest` + // value that's the sum of all the `register_callsite` calls to those + // per-layer filters. if we returned an actual `never` interest here, a + // `Layered` layer would short-circuit and not allow any `Filtered` + // layers below us if _they_ are interested in the callsite. + Interest::always() + } + + fn enabled(&self, metadata: &Metadata<'_>, cx: Context<'_, S>) -> bool { + let cx = cx.with_filter(self.id()); + let enabled = self.filter.enabled(metadata, &cx); + FILTERING.with(|filtering| filtering.set(self.id(), enabled)); + + if enabled { + // If the filter enabled this metadata, ask the wrapped layer if + // _it_ wants it --- it might have a global filter. + self.layer.enabled(metadata, cx) + } else { + // Otherwise, return `true`. The _per-layer_ filter disabled this + // metadata, but returning `false` in `Layer::enabled` will + // short-circuit and globally disable the span or event. This is + // *not* what we want for per-layer filters, as other layers may + // still want this event. Returning `true` here means we'll continue + // asking the next layer in the stack. + // + // Once all per-layer filters have been evaluated, the `Registry` + // at the root of the stack will return `false` from its `enabled` + // method if *every* per-layer filter disabled this metadata. + // Otherwise, the individual per-layer filters will skip the next + // `new_span` or `on_event` call for their layer if *they* disabled + // the span or event, but it was not globally disabled. + true + } + } + + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, cx: Context<'_, S>) { + self.did_enable(|| { + let cx = cx.with_filter(self.id()); + self.filter.on_new_span(attrs, id, cx.clone()); + self.layer.on_new_span(attrs, id, cx); + }) + } + + #[doc(hidden)] + fn max_level_hint(&self) -> Option<LevelFilter> { + self.filter.max_level_hint() + } + + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, cx: Context<'_, S>) { + if let Some(cx) = cx.if_enabled_for(span, self.id()) { + self.filter.on_record(span, values, cx.clone()); + self.layer.on_record(span, values, cx) + } + } + + fn on_follows_from(&self, span: &span::Id, follows: &span::Id, cx: Context<'_, S>) { + // only call `on_follows_from` if both spans are enabled by us + if cx.is_enabled_for(span, self.id()) && cx.is_enabled_for(follows, self.id()) { + self.layer + .on_follows_from(span, follows, cx.with_filter(self.id())) + } + } + + fn on_event(&self, event: &Event<'_>, cx: Context<'_, S>) { + self.did_enable(|| { + self.layer.on_event(event, cx.with_filter(self.id())); + }) + } + + fn on_enter(&self, id: &span::Id, cx: Context<'_, S>) { + if let Some(cx) = cx.if_enabled_for(id, self.id()) { + self.filter.on_enter(id, cx.clone()); + self.layer.on_enter(id, cx); + } + } + + fn on_exit(&self, id: &span::Id, cx: Context<'_, S>) { + if let Some(cx) = cx.if_enabled_for(id, self.id()) { + self.filter.on_exit(id, cx.clone()); + self.layer.on_exit(id, cx); + } + } + + fn on_close(&self, id: span::Id, cx: Context<'_, S>) { + if let Some(cx) = cx.if_enabled_for(&id, self.id()) { + self.filter.on_close(id.clone(), cx.clone()); + self.layer.on_close(id, cx); + } + } + + // XXX(eliza): the existence of this method still makes me sad... + fn on_id_change(&self, old: &span::Id, new: &span::Id, cx: Context<'_, S>) { + if let Some(cx) = cx.if_enabled_for(old, self.id()) { + self.layer.on_id_change(old, new, cx) + } + } + + #[doc(hidden)] + #[inline] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + match id { + id if id == TypeId::of::<Self>() => Some(self as *const _ as *const ()), + id if id == TypeId::of::<L>() => Some(&self.layer as *const _ as *const ()), + id if id == TypeId::of::<F>() => Some(&self.filter as *const _ as *const ()), + id if id == TypeId::of::<MagicPlfDowncastMarker>() => { + Some(&self.id as *const _ as *const ()) + } + _ => self.layer.downcast_raw(id), + } + } +} + +impl<F, L, S> fmt::Debug for Filtered<F, L, S> +where + F: fmt::Debug, + L: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Filtered") + .field("filter", &self.filter) + .field("layer", &self.layer) + .field("id", &self.id) + .finish() + } +} + +// === impl FilterId === + +impl FilterId { + const fn disabled() -> Self { + Self(std::u64::MAX) + } + + /// Returns a `FilterId` that will consider _all_ spans enabled. + pub(crate) const fn none() -> Self { + Self(0) + } + + pub(crate) fn new(id: u8) -> Self { + assert!(id < 64, "filter IDs may not be greater than 64"); + Self(1 << id as usize) + } + + /// Combines two `FilterId`s, returning a new `FilterId` that will match a + /// [`FilterMap`] where the span was disabled by _either_ this `FilterId` + /// *or* the combined `FilterId`. + /// + /// This method is called by [`Context`]s when adding the `FilterId` of a + /// [`Filtered`] layer to the context. + /// + /// This is necessary for cases where we have a tree of nested [`Filtered`] + /// layers, like this: + /// + /// ```text + /// Filtered { + /// filter1, + /// Layered { + /// layer1, + /// Filtered { + /// filter2, + /// layer2, + /// }, + /// } + /// ``` + /// + /// We want `layer2` to be affected by both `filter1` _and_ `filter2`. + /// Without combining `FilterId`s, this works fine when filtering + /// `on_event`/`new_span`, because the outer `Filtered` layer (`filter1`) + /// won't call the inner layer's `on_event` or `new_span` callbacks if it + /// disabled the event/span. + /// + /// However, it _doesn't_ work when filtering span lookups and traversals + /// (e.g. `scope`). This is because the [`Context`] passed to `layer2` + /// would set its filter ID to the filter ID of `filter2`, and would skip + /// spans that were disabled by `filter2`. However, what if a span was + /// disabled by `filter1`? We wouldn't see it in `new_span`, but we _would_ + /// see it in lookups and traversals...which we don't want. + /// + /// When a [`Filtered`] layer adds its ID to a [`Context`], it _combines_ it + /// with any previous filter ID that the context had, rather than replacing + /// it. That way, `layer2`'s context will check if a span was disabled by + /// `filter1` _or_ `filter2`. The way we do this, instead of representing + /// `FilterId`s as a number number that we shift a 1 over by to get a mask, + /// we just store the actual mask,so we can combine them with a bitwise-OR. + /// + /// For example, if we consider the following case (pretending that the + /// masks are 8 bits instead of 64 just so i don't have to write out a bunch + /// of extra zeroes): + /// + /// - `filter1` has the filter id 1 (`0b0000_0001`) + /// - `filter2` has the filter id 2 (`0b0000_0010`) + /// + /// A span that gets disabled by filter 1 would have the [`FilterMap`] with + /// bits `0b0000_0001`. + /// + /// If the `FilterId` was internally represented as `(bits to shift + 1), + /// when `layer2`'s [`Context`] checked if it enabled the span, it would + /// make the mask `0b0000_0010` (`1 << 1`). That bit would not be set in the + /// [`FilterMap`], so it would see that it _didn't_ disable the span. Which + /// is *true*, it just doesn't reflect the tree-like shape of the actual + /// subscriber. + /// + /// By having the IDs be masks instead of shifts, though, when the + /// [`Filtered`] with `filter2` gets the [`Context`] with `filter1`'s filter ID, + /// instead of replacing it, it ors them together: + /// + /// ```ignore + /// 0b0000_0001 | 0b0000_0010 == 0b0000_0011; + /// ``` + /// + /// We then test if the span was disabled by seeing if _any_ bits in the + /// mask are `1`: + /// + /// ```ignore + /// filtermap & mask != 0; + /// 0b0000_0001 & 0b0000_0011 != 0; + /// 0b0000_0001 != 0; + /// true; + /// ``` + /// + /// [`Context`]: crate::layer::Context + pub(crate) fn and(self, FilterId(other): Self) -> Self { + // If this mask is disabled, just return the other --- otherwise, we + // would always see that every span is disabled. + if self.0 == Self::disabled().0 { + return Self(other); + } + + Self(self.0 | other) + } + + fn is_disabled(self) -> bool { + self.0 == Self::disabled().0 + } +} + +impl fmt::Debug for FilterId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // don't print a giant set of the numbers 0..63 if the filter ID is disabled. + if self.0 == Self::disabled().0 { + return f + .debug_tuple("FilterId") + .field(&format_args!("DISABLED")) + .finish(); + } + + if f.alternate() { + f.debug_struct("FilterId") + .field("ids", &format_args!("{:?}", FmtBitset(self.0))) + .field("bits", &format_args!("{:b}", self.0)) + .finish() + } else { + f.debug_tuple("FilterId").field(&FmtBitset(self.0)).finish() + } + } +} + +impl fmt::Binary for FilterId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("FilterId") + .field(&format_args!("{:b}", self.0)) + .finish() + } +} + +// === impl FilterExt === + +impl<F, S> FilterExt<S> for F where F: layer::Filter<S> {} + +// === impl FilterMap === + +impl FilterMap { + pub(crate) fn set(self, FilterId(mask): FilterId, enabled: bool) -> Self { + if mask == std::u64::MAX { + return self; + } + + if enabled { + Self { + bits: self.bits & (!mask), + } + } else { + Self { + bits: self.bits | mask, + } + } + } + + #[inline] + pub(crate) fn is_enabled(self, FilterId(mask): FilterId) -> bool { + self.bits & mask == 0 + } + + #[inline] + pub(crate) fn any_enabled(self) -> bool { + self.bits != std::u64::MAX + } +} + +impl fmt::Debug for FilterMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let alt = f.alternate(); + let mut s = f.debug_struct("FilterMap"); + s.field("disabled_by", &format_args!("{:?}", &FmtBitset(self.bits))); + + if alt { + s.field("bits", &format_args!("{:b}", self.bits)); + } + + s.finish() + } +} + +impl fmt::Binary for FilterMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FilterMap") + .field("bits", &format_args!("{:b}", self.bits)) + .finish() + } +} + +// === impl FilterState === + +impl FilterState { + fn new() -> Self { + Self { + enabled: Cell::new(FilterMap::default()), + interest: RefCell::new(None), + + #[cfg(debug_assertions)] + counters: DebugCounters::default(), + } + } + + fn set(&self, filter: FilterId, enabled: bool) { + #[cfg(debug_assertions)] + { + let in_current_pass = self.counters.in_filter_pass.get(); + if in_current_pass == 0 { + debug_assert_eq!(self.enabled.get(), FilterMap::default()); + } + self.counters.in_filter_pass.set(in_current_pass + 1); + debug_assert_eq!( + self.counters.in_interest_pass.get(), + 0, + "if we are in or starting a filter pass, we must not be in an interest pass." + ) + } + + self.enabled.set(self.enabled.get().set(filter, enabled)) + } + + fn add_interest(&self, interest: Interest) { + let mut curr_interest = self.interest.borrow_mut(); + + #[cfg(debug_assertions)] + { + let in_current_pass = self.counters.in_interest_pass.get(); + if in_current_pass == 0 { + debug_assert!(curr_interest.is_none()); + } + self.counters.in_interest_pass.set(in_current_pass + 1); + } + + if let Some(curr_interest) = curr_interest.as_mut() { + if (curr_interest.is_always() && !interest.is_always()) + || (curr_interest.is_never() && !interest.is_never()) + { + *curr_interest = Interest::sometimes(); + } + // If the two interests are the same, do nothing. If the current + // interest is `sometimes`, stay sometimes. + } else { + *curr_interest = Some(interest); + } + } + + pub(crate) fn event_enabled() -> bool { + FILTERING + .try_with(|this| { + let enabled = this.enabled.get().any_enabled(); + #[cfg(debug_assertions)] + { + if this.counters.in_filter_pass.get() == 0 { + debug_assert_eq!(this.enabled.get(), FilterMap::default()); + } + + // Nothing enabled this event, we won't tick back down the + // counter in `did_enable`. Reset it. + if !enabled { + this.counters.in_filter_pass.set(0); + } + } + enabled + }) + .unwrap_or(true) + } + + /// Executes a closure if the filter with the provided ID did not disable + /// the current span/event. + /// + /// This is used to implement the `on_event` and `new_span` methods for + /// `Filtered`. + fn did_enable(&self, filter: FilterId, f: impl FnOnce()) { + let map = self.enabled.get(); + if map.is_enabled(filter) { + // If the filter didn't disable the current span/event, run the + // callback. + f(); + } else { + // Otherwise, if this filter _did_ disable the span or event + // currently being processed, clear its bit from this thread's + // `FilterState`. The bit has already been "consumed" by skipping + // this callback, and we need to ensure that the `FilterMap` for + // this thread is reset when the *next* `enabled` call occurs. + self.enabled.set(map.set(filter, true)); + } + #[cfg(debug_assertions)] + { + let in_current_pass = self.counters.in_filter_pass.get(); + if in_current_pass <= 1 { + debug_assert_eq!(self.enabled.get(), FilterMap::default()); + } + self.counters + .in_filter_pass + .set(in_current_pass.saturating_sub(1)); + debug_assert_eq!( + self.counters.in_interest_pass.get(), + 0, + "if we are in a filter pass, we must not be in an interest pass." + ) + } + } + + /// Clears the current in-progress filter state. + /// + /// This resets the [`FilterMap`] and current [`Interest`] as well as + /// clearing the debug counters. + pub(crate) fn clear_enabled() { + // Drop the `Result` returned by `try_with` --- if we are in the middle + // a panic and the thread-local has been torn down, that's fine, just + // ignore it ratehr than panicking. + let _ = FILTERING.try_with(|filtering| { + filtering.enabled.set(FilterMap::default()); + + #[cfg(debug_assertions)] + filtering.counters.in_filter_pass.set(0); + }); + } + + pub(crate) fn take_interest() -> Option<Interest> { + FILTERING + .try_with(|filtering| { + #[cfg(debug_assertions)] + { + if filtering.counters.in_interest_pass.get() == 0 { + debug_assert!(filtering.interest.try_borrow().ok()?.is_none()); + } + filtering.counters.in_interest_pass.set(0); + } + filtering.interest.try_borrow_mut().ok()?.take() + }) + .ok()? + } + + pub(crate) fn filter_map(&self) -> FilterMap { + let map = self.enabled.get(); + #[cfg(debug_assertions)] + { + if self.counters.in_filter_pass.get() == 0 { + debug_assert_eq!(map, FilterMap::default()); + } + } + + map + } +} +/// This is a horrible and bad abuse of the downcasting system to expose +/// *internally* whether a layer has per-layer filtering, within +/// `tracing-subscriber`, without exposing a public API for it. +/// +/// If a `Layer` has per-layer filtering, it will downcast to a +/// `MagicPlfDowncastMarker`. Since layers which contain other layers permit +/// downcasting to recurse to their children, this will do the Right Thing with +/// layers like Reload, Option, etc. +/// +/// Why is this a wrapper around the `FilterId`, you may ask? Because +/// downcasting works by returning a pointer, and we don't want to risk +/// introducing UB by constructing pointers that _don't_ point to a valid +/// instance of the type they claim to be. In this case, we don't _intend_ for +/// this pointer to be dereferenced, so it would actually be fine to return one +/// that isn't a valid pointer...but we can't guarantee that the caller won't +/// (accidentally) dereference it, so it's better to be safe than sorry. We +/// could, alternatively, add an additional field to the type that's used only +/// for returning pointers to as as part of the evil downcasting hack, but I +/// thought it was nicer to just add a `repr(transparent)` wrapper to the +/// existing `FilterId` field, since it won't make the struct any bigger. +/// +/// Don't worry, this isn't on the test. :) +#[derive(Clone, Copy)] +#[repr(transparent)] +struct MagicPlfDowncastMarker(FilterId); +impl fmt::Debug for MagicPlfDowncastMarker { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Just pretend that `MagicPlfDowncastMarker` doesn't exist for + // `fmt::Debug` purposes...if no one *sees* it in their `Debug` output, + // they don't have to know I thought this code would be a good idea. + fmt::Debug::fmt(&self.0, f) + } +} + +pub(crate) fn is_plf_downcast_marker(type_id: TypeId) -> bool { + type_id == TypeId::of::<MagicPlfDowncastMarker>() +} + +/// Does a type implementing `Subscriber` contain any per-layer filters? +pub(crate) fn subscriber_has_plf<S>(subscriber: &S) -> bool +where + S: Subscriber, +{ + (subscriber as &dyn Subscriber).is::<MagicPlfDowncastMarker>() +} + +/// Does a type implementing `Layer` contain any per-layer filters? +pub(crate) fn layer_has_plf<L, S>(layer: &L) -> bool +where + L: Layer<S>, + S: Subscriber, +{ + unsafe { + // Safety: we're not actually *doing* anything with this pointer --- we + // only care about the `Option`, which we're turning into a `bool`. So + // even if the layer decides to be evil and give us some kind of invalid + // pointer, we don't ever dereference it, so this is always safe. + layer.downcast_raw(TypeId::of::<MagicPlfDowncastMarker>()) + } + .is_some() +} + +struct FmtBitset(u64); + +impl fmt::Debug for FmtBitset { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut set = f.debug_set(); + for bit in 0..64 { + // if the `bit`-th bit is set, add it to the debug set + if self.0 & (1 << bit) != 0 { + set.entry(&bit); + } + } + set.finish() + } +} diff --git a/vendor/tracing-subscriber/src/filter/level.rs b/vendor/tracing-subscriber/src/filter/level.rs new file mode 100644 index 000000000..0fa601260 --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/level.rs @@ -0,0 +1,27 @@ +use tracing_core::{ + subscriber::{Interest, Subscriber}, + Metadata, +}; + +#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 +pub use tracing_core::metadata::{LevelFilter, ParseLevelFilterError as ParseError}; + +// === impl LevelFilter === + +impl<S: Subscriber> crate::Layer<S> for LevelFilter { + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + if self >= metadata.level() { + Interest::always() + } else { + Interest::never() + } + } + + fn enabled(&self, metadata: &Metadata<'_>, _: crate::layer::Context<'_, S>) -> bool { + self >= metadata.level() + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + Some(*self) + } +} diff --git a/vendor/tracing-subscriber/src/filter/mod.rs b/vendor/tracing-subscriber/src/filter/mod.rs new file mode 100644 index 000000000..000a27195 --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/mod.rs @@ -0,0 +1,66 @@ +//! [`Layer`]s that control which spans and events are enabled by the wrapped +//! subscriber. +//! +//! This module contains a number of types that provide implementations of +//! various strategies for filtering which spans and events are enabled. For +//! details on filtering spans and events using [`Layer`]s, see the +//! [`layer` module's documentation]. +//! +//! [`layer` module's documentation]: crate::layer#filtering-with-layers +//! [`Layer`]: crate::layer +mod filter_fn; + +feature! { + #![all(feature = "env-filter", feature = "std")] + mod env; + pub use self::env::*; +} + +feature! { + #![all(feature = "registry", feature = "std")] + mod layer_filters; + pub use self::layer_filters::*; +} + +mod level; + +pub use self::filter_fn::*; +pub use self::level::{LevelFilter, ParseError as LevelParseError}; + +#[cfg(not(all(feature = "registry", feature = "std")))] +pub(crate) use self::has_plf_stubs::*; + +feature! { + #![any(feature = "std", feature = "alloc")] + pub mod targets; + pub use self::targets::Targets; + + mod directive; + pub use self::directive::ParseError; +} + +/// Stub implementations of the per-layer-fitler detection functions for when the +/// `registry` feature is disabled. +#[cfg(not(all(feature = "registry", feature = "std")))] +mod has_plf_stubs { + pub(crate) fn is_plf_downcast_marker(_: core::any::TypeId) -> bool { + false + } + + /// Does a type implementing `Subscriber` contain any per-layer filters? + pub(crate) fn subscriber_has_plf<S>(_: &S) -> bool + where + S: tracing_core::Subscriber, + { + false + } + + /// Does a type implementing `Layer` contain any per-layer filters? + pub(crate) fn layer_has_plf<L, S>(_: &L) -> bool + where + L: crate::Layer<S>, + S: tracing_core::Subscriber, + { + false + } +} diff --git a/vendor/tracing-subscriber/src/filter/targets.rs b/vendor/tracing-subscriber/src/filter/targets.rs new file mode 100644 index 000000000..2a30d2db6 --- /dev/null +++ b/vendor/tracing-subscriber/src/filter/targets.rs @@ -0,0 +1,710 @@ +//! A [filter] that enables or disables spans and events based on their [target] and [level]. +//! +//! See [`Targets`] for details. +//! +//! [target]: tracing_core::Metadata::target +//! [level]: tracing_core::Level +//! [filter]: crate::layer#filtering-with-layers + +use crate::{ + filter::{ + directive::{DirectiveSet, ParseError, StaticDirective}, + LevelFilter, + }, + layer, +}; +#[cfg(not(feature = "std"))] +use alloc::string::String; +use core::{ + iter::{Extend, FilterMap, FromIterator}, + slice, + str::FromStr, +}; +use tracing_core::{Interest, Level, Metadata, Subscriber}; + +/// A filter that enables or disables spans and events based on their [target] +/// and [level]. +/// +/// Targets are typically equal to the Rust module path of the code where the +/// span or event was recorded, although they may be overridden. +/// +/// This type can be used for both [per-layer filtering][plf] (using its +/// [`Filter`] implementation) and [global filtering][global] (using its +/// [`Layer`] implementation). +/// +/// See the [documentation on filtering with layers][filtering] for details. +/// +/// # Filtering With `Targets` +/// +/// A `Targets` filter consists of one or more [target] prefixes, paired with +/// [`LevelFilter`]s. If a span or event's [target] begins with one of those +/// prefixes, and its [level] is at or below the [`LevelFilter`] enabled for +/// that prefix, then the span or event will be enabled. +/// +/// This is similar to the behavior implemented by the [`env_logger` crate] in +/// the `log` ecosystem. +/// +/// The [`EnvFilter`] type also provided by this crate is very similar to `Targets`, +/// but is capable of a more sophisticated form of filtering where events may +/// also be enabled or disabled based on the span they are recorded in. +/// `Targets` can be thought of as a lighter-weight form of [`EnvFilter`] that +/// can be used instead when this dynamic filtering is not required. +/// +/// # Examples +/// +/// A `Targets` filter can be constructed by programmatically adding targets and +/// levels to enable: +/// +/// ``` +/// use tracing_subscriber::{filter, prelude::*}; +/// use tracing_core::Level; +/// +/// let filter = filter::Targets::new() +/// // Enable the `INFO` level for anything in `my_crate` +/// .with_target("my_crate", Level::INFO) +/// // Enable the `DEBUG` level for a specific module. +/// .with_target("my_crate::interesting_module", Level::DEBUG); +/// +/// // Build a new subscriber with the `fmt` layer using the `Targets` +/// // filter we constructed above. +/// tracing_subscriber::registry() +/// .with(tracing_subscriber::fmt::layer()) +/// .with(filter) +/// .init(); +/// ``` +/// +/// [`LevelFilter::OFF`] can be used to disable a particular target: +/// ``` +/// use tracing_subscriber::filter::{Targets, LevelFilter}; +/// use tracing_core::Level; +/// +/// let filter = Targets::new() +/// .with_target("my_crate", Level::INFO) +/// // Disable all traces from `annoying_module`. +/// .with_target("my_crate::annoying_module", LevelFilter::OFF); +/// # drop(filter); +/// ``` +/// +/// Alternatively, `Targets` implements [`std::str::FromStr`], allowing it to be +/// parsed from a comma-delimited list of `target=level` pairs. For example: +/// +/// ```rust +/// # fn main() -> Result<(), Box<dyn std::error::Error>> { +/// use tracing_subscriber::filter; +/// use tracing_core::Level; +/// +/// let filter = "my_crate=info,my_crate::interesting_module=trace,other_crate=debug" +/// .parse::<filter::Targets>()?; +/// +/// // The parsed filter is identical to a filter constructed using `with_target`: +/// assert_eq!( +/// filter, +/// filter::Targets::new() +/// .with_target("my_crate", Level::INFO) +/// .with_target("my_crate::interesting_module", Level::TRACE) +/// .with_target("other_crate", Level::DEBUG) +/// ); +/// # Ok(()) } +/// ``` +/// +/// This is particularly useful when the list of enabled targets is configurable +/// by the user at runtime. +/// +/// The `Targets` filter can be used as a [per-layer filter][plf] *and* as a +/// [global filter][global]: +/// +/// ```rust +/// use tracing_subscriber::{ +/// fmt, +/// filter::{Targets, LevelFilter}, +/// prelude::*, +/// }; +/// use tracing_core::Level; +/// use std::{sync::Arc, fs::File}; +/// # fn docs() -> Result<(), Box<dyn std::error::Error>> { +/// +/// // A layer that logs events to stdout using the human-readable "pretty" +/// // format. +/// let stdout_log = fmt::layer().pretty(); +/// +/// // A layer that logs events to a file, using the JSON format. +/// let file = File::create("debug_log.json")?; +/// let debug_log = fmt::layer() +/// .with_writer(Arc::new(file)) +/// .json(); +/// +/// tracing_subscriber::registry() +/// // Only log INFO and above to stdout, unless the span or event +/// // has the `my_crate::cool_module` target prefix. +/// .with(stdout_log +/// .with_filter( +/// Targets::default() +/// .with_target("my_crate::cool_module", Level::DEBUG) +/// .with_default(Level::INFO) +/// ) +/// ) +/// // Log everything enabled by the global filter to `debug_log.json`. +/// .with(debug_log) +/// // Configure a global filter for the whole subscriber stack. This will +/// // control what spans and events are recorded by both the `debug_log` +/// // and the `stdout_log` layers, and `stdout_log` will *additionally* be +/// // filtered by its per-layer filter. +/// .with( +/// Targets::default() +/// .with_target("my_crate", Level::TRACE) +/// .with_target("other_crate", Level::INFO) +/// .with_target("other_crate::annoying_module", LevelFilter::OFF) +/// .with_target("third_crate", Level::DEBUG) +/// ).init(); +/// # Ok(()) } +///``` +/// +/// [target]: tracing_core::Metadata::target +/// [level]: tracing_core::Level +/// [`Filter`]: crate::layer::Filter +/// [`Layer`]: crate::layer::Layer +/// [plf]: crate::layer#per-layer-filtering +/// [global]: crate::layer#global-filtering +/// [filtering]: crate::layer#filtering-with-layers +/// [`env_logger` crate]: https://docs.rs/env_logger/0.9.0/env_logger/index.html#enabling-logging +/// [`EnvFilter`]: crate::filter::EnvFilter +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Targets(DirectiveSet<StaticDirective>); + +impl Targets { + /// Returns a new `Targets` filter. + /// + /// This filter will enable no targets. Call [`with_target`] or [`with_targets`] + /// to add enabled targets, and [`with_default`] to change the default level + /// enabled for spans and events that didn't match any of the provided targets. + /// + /// [`with_target`]: Targets::with_target + /// [`with_targets`]: Targets::with_targets + /// [`with_default`]: Targets::with_default + pub fn new() -> Self { + Self::default() + } + + /// Enables spans and events with [target]s starting with the provided target + /// prefix if they are at or below the provided [`LevelFilter`]. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::filter; + /// use tracing_core::Level; + /// + /// let filter = filter::Targets::new() + /// // Enable the `INFO` level for anything in `my_crate` + /// .with_target("my_crate", Level::INFO) + /// // Enable the `DEBUG` level for a specific module. + /// .with_target("my_crate::interesting_module", Level::DEBUG); + /// # drop(filter); + /// ``` + /// + /// [`LevelFilter::OFF`] can be used to disable a particular target: + /// ``` + /// use tracing_subscriber::filter::{Targets, LevelFilter}; + /// use tracing_core::Level; + /// + /// let filter = Targets::new() + /// .with_target("my_crate", Level::INFO) + /// // Disable all traces from `annoying_module`. + /// .with_target("my_crate::interesting_module", LevelFilter::OFF); + /// # drop(filter); + /// ``` + /// + /// [target]: tracing_core::Metadata::target + pub fn with_target(mut self, target: impl Into<String>, level: impl Into<LevelFilter>) -> Self { + self.0.add(StaticDirective::new( + Some(target.into()), + Default::default(), + level.into(), + )); + self + } + /// Adds [target]s from an iterator of [target]-[`LevelFilter`] pairs to this filter. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::filter; + /// use tracing_core::Level; + /// + /// let filter = filter::Targets::new() + /// .with_targets(vec![ + /// ("my_crate", Level::INFO), + /// ("my_crate::some_module", Level::DEBUG), + /// ("my_crate::other_module::cool_stuff", Level::TRACE), + /// ("other_crate", Level::WARN) + /// ]); + /// # drop(filter); + /// ``` + /// + /// [`LevelFilter::OFF`] can be used to disable a particular target: + /// ``` + /// use tracing_subscriber::filter::{Targets, LevelFilter}; + /// use tracing_core::Level; + /// + /// let filter = Targets::new() + /// .with_target("my_crate", Level::INFO) + /// // Disable all traces from `annoying_module`. + /// .with_target("my_crate::interesting_module", LevelFilter::OFF); + /// # drop(filter); + /// ``` + /// + /// [target]: tracing_core::Metadata::target + pub fn with_targets<T, L>(mut self, targets: impl IntoIterator<Item = (T, L)>) -> Self + where + String: From<T>, + LevelFilter: From<L>, + { + self.extend(targets); + self + } + + /// Sets the default level to enable for spans and events whose targets did + /// not match any of the configured prefixes. + /// + /// By default, this is [`LevelFilter::OFF`]. This means that spans and + /// events will only be enabled if they match one of the configured target + /// prefixes. If this is changed to a different [`LevelFilter`], spans and + /// events with targets that did not match any of the configured prefixes + /// will be enabled if their level is at or below the provided level. + pub fn with_default(mut self, level: impl Into<LevelFilter>) -> Self { + self.0 + .add(StaticDirective::new(None, Default::default(), level.into())); + self + } + + /// Returns an iterator over the [target]-[`LevelFilter`] pairs in this filter. + /// + /// The order of iteration is undefined. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::filter::{Targets, LevelFilter}; + /// use tracing_core::Level; + /// + /// let filter = Targets::new() + /// .with_target("my_crate", Level::INFO) + /// .with_target("my_crate::interesting_module", Level::DEBUG); + /// + /// let mut targets: Vec<_> = filter.iter().collect(); + /// targets.sort(); + /// + /// assert_eq!(targets, vec![ + /// ("my_crate", LevelFilter::INFO), + /// ("my_crate::interesting_module", LevelFilter::DEBUG), + /// ]); + /// ``` + /// + /// [target]: tracing_core::Metadata::target + pub fn iter(&self) -> Iter<'_> { + self.into_iter() + } + + #[inline] + fn interested(&self, metadata: &'static Metadata<'static>) -> Interest { + if self.0.enabled(metadata) { + Interest::always() + } else { + Interest::never() + } + } + + /// Returns whether a [target]-[`Level`] pair would be enabled + /// by this `Targets`. + /// + /// This method can be used with [`module_path!`] from `std` as the target + /// in order to emulate the behavior of the [`tracing::event!`] and [`tracing::span!`] + /// macros. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::filter::{Targets, LevelFilter}; + /// use tracing_core::Level; + /// + /// let filter = Targets::new() + /// .with_target("my_crate", Level::INFO) + /// .with_target("my_crate::interesting_module", Level::DEBUG); + /// + /// assert!(filter.would_enable("my_crate", &Level::INFO)); + /// assert!(!filter.would_enable("my_crate::interesting_module", &Level::TRACE)); + /// ``` + /// + /// [target]: tracing_core::Metadata::target + /// [`module_path!`]: std::module_path! + pub fn would_enable(&self, target: &str, level: &Level) -> bool { + // "Correct" to call because `Targets` only produces `StaticDirective`'s with NO + // fields + self.0.target_enabled(target, level) + } +} + +impl<T, L> Extend<(T, L)> for Targets +where + T: Into<String>, + L: Into<LevelFilter>, +{ + fn extend<I: IntoIterator<Item = (T, L)>>(&mut self, iter: I) { + let iter = iter.into_iter().map(|(target, level)| { + StaticDirective::new(Some(target.into()), Default::default(), level.into()) + }); + self.0.extend(iter); + } +} + +impl<T, L> FromIterator<(T, L)> for Targets +where + T: Into<String>, + L: Into<LevelFilter>, +{ + fn from_iter<I: IntoIterator<Item = (T, L)>>(iter: I) -> Self { + let mut this = Self::default(); + this.extend(iter); + this + } +} + +impl FromStr for Targets { + type Err = ParseError; + fn from_str(s: &str) -> Result<Self, Self::Err> { + s.split(',') + .map(StaticDirective::from_str) + .collect::<Result<_, _>>() + .map(Self) + } +} + +impl<S> layer::Layer<S> for Targets +where + S: Subscriber, +{ + fn enabled(&self, metadata: &Metadata<'_>, _: layer::Context<'_, S>) -> bool { + self.0.enabled(metadata) + } + + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.interested(metadata) + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + Some(self.0.max_level) + } +} + +#[cfg(feature = "registry")] +#[cfg_attr(docsrs, doc(cfg(feature = "registry")))] +impl<S> layer::Filter<S> for Targets { + fn enabled(&self, metadata: &Metadata<'_>, _: &layer::Context<'_, S>) -> bool { + self.0.enabled(metadata) + } + + fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest { + self.interested(metadata) + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + Some(self.0.max_level) + } +} + +impl IntoIterator for Targets { + type Item = (String, LevelFilter); + + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + IntoIter::new(self) + } +} + +impl<'a> IntoIterator for &'a Targets { + type Item = (&'a str, LevelFilter); + + type IntoIter = Iter<'a>; + + fn into_iter(self) -> Self::IntoIter { + Iter::new(self) + } +} + +/// An owning iterator over the [target]-[level] pairs of a `Targets` filter. +/// +/// This struct is created by the `IntoIterator` trait implementation of [`Targets`]. +/// +/// # Examples +/// +/// Merge the targets from one `Targets` with another: +/// +/// ``` +/// use tracing_subscriber::filter::Targets; +/// use tracing_core::Level; +/// +/// let mut filter = Targets::new().with_target("my_crate", Level::INFO); +/// let overrides = Targets::new().with_target("my_crate::interesting_module", Level::DEBUG); +/// +/// filter.extend(overrides); +/// # drop(filter); +/// ``` +/// +/// [target]: tracing_core::Metadata::target +/// [level]: tracing_core::Level +#[derive(Debug)] +pub struct IntoIter( + #[allow(clippy::type_complexity)] // alias indirection would probably make this more confusing + FilterMap< + <DirectiveSet<StaticDirective> as IntoIterator>::IntoIter, + fn(StaticDirective) -> Option<(String, LevelFilter)>, + >, +); + +impl IntoIter { + fn new(targets: Targets) -> Self { + Self(targets.0.into_iter().filter_map(|directive| { + let level = directive.level; + directive.target.map(|target| (target, level)) + })) + } +} + +impl Iterator for IntoIter { + type Item = (String, LevelFilter); + + fn next(&mut self) -> Option<Self::Item> { + self.0.next() + } + + fn size_hint(&self) -> (usize, Option<usize>) { + self.0.size_hint() + } +} + +/// A borrowing iterator over the [target]-[level] pairs of a `Targets` filter. +/// +/// This struct is created by [`iter`] method of [`Targets`], or from the `IntoIterator` +/// implementation for `&Targets`. +/// +/// [target]: tracing_core::Metadata::target +/// [level]: tracing_core::Level +/// [`iter`]: Targets::iter +#[derive(Debug)] +pub struct Iter<'a>( + FilterMap< + slice::Iter<'a, StaticDirective>, + fn(&'a StaticDirective) -> Option<(&'a str, LevelFilter)>, + >, +); + +impl<'a> Iter<'a> { + fn new(targets: &'a Targets) -> Self { + Self(targets.0.iter().filter_map(|directive| { + directive + .target + .as_deref() + .map(|target| (target, directive.level)) + })) + } +} + +impl<'a> Iterator for Iter<'a> { + type Item = (&'a str, LevelFilter); + + fn next(&mut self) -> Option<Self::Item> { + self.0.next() + } + + fn size_hint(&self) -> (usize, Option<usize>) { + self.0.size_hint() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + feature! { + #![not(feature = "std")] + use alloc::{vec, vec::Vec, string::ToString}; + + // `dbg!` is only available with `libstd`; just nop it out when testing + // with alloc only. + macro_rules! dbg { + ($x:expr) => { $x } + } + } + + fn expect_parse(s: &str) -> Targets { + match dbg!(s).parse::<Targets>() { + Err(e) => panic!("string {:?} did not parse successfully: {}", s, e), + Ok(e) => e, + } + } + + fn expect_parse_ralith(s: &str) { + let dirs = expect_parse(s).0.into_vec(); + assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("server".to_string())); + assert_eq!(dirs[0].level, LevelFilter::DEBUG); + assert_eq!(dirs[0].field_names, Vec::<String>::new()); + + assert_eq!(dirs[1].target, Some("common".to_string())); + assert_eq!(dirs[1].level, LevelFilter::INFO); + assert_eq!(dirs[1].field_names, Vec::<String>::new()); + } + + fn expect_parse_level_directives(s: &str) { + let dirs = expect_parse(s).0.into_vec(); + assert_eq!(dirs.len(), 6, "\nparsed: {:#?}", dirs); + + assert_eq!(dirs[0].target, Some("crate3::mod2::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::OFF); + assert_eq!(dirs[0].field_names, Vec::<String>::new()); + + assert_eq!(dirs[1].target, Some("crate1::mod2::mod3".to_string())); + assert_eq!(dirs[1].level, LevelFilter::INFO); + assert_eq!(dirs[1].field_names, Vec::<String>::new()); + + assert_eq!(dirs[2].target, Some("crate1::mod2".to_string())); + assert_eq!(dirs[2].level, LevelFilter::WARN); + assert_eq!(dirs[2].field_names, Vec::<String>::new()); + + assert_eq!(dirs[3].target, Some("crate1::mod1".to_string())); + assert_eq!(dirs[3].level, LevelFilter::ERROR); + assert_eq!(dirs[3].field_names, Vec::<String>::new()); + + assert_eq!(dirs[4].target, Some("crate3".to_string())); + assert_eq!(dirs[4].level, LevelFilter::TRACE); + assert_eq!(dirs[4].field_names, Vec::<String>::new()); + + assert_eq!(dirs[5].target, Some("crate2".to_string())); + assert_eq!(dirs[5].level, LevelFilter::DEBUG); + assert_eq!(dirs[5].field_names, Vec::<String>::new()); + } + + #[test] + fn parse_ralith() { + expect_parse_ralith("common=info,server=debug"); + } + + #[test] + fn parse_ralith_uc() { + expect_parse_ralith("common=INFO,server=DEBUG"); + } + + #[test] + fn parse_ralith_mixed() { + expect_parse("common=iNfo,server=dEbUg"); + } + + #[test] + fn expect_parse_valid() { + let dirs = expect_parse("crate1::mod1=error,crate1::mod2,crate2=debug,crate3=off") + .0 + .into_vec(); + assert_eq!(dirs.len(), 4, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("crate1::mod2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::TRACE); + assert_eq!(dirs[0].field_names, Vec::<String>::new()); + + assert_eq!(dirs[1].target, Some("crate1::mod1".to_string())); + assert_eq!(dirs[1].level, LevelFilter::ERROR); + assert_eq!(dirs[1].field_names, Vec::<String>::new()); + + assert_eq!(dirs[2].target, Some("crate3".to_string())); + assert_eq!(dirs[2].level, LevelFilter::OFF); + assert_eq!(dirs[2].field_names, Vec::<String>::new()); + + assert_eq!(dirs[3].target, Some("crate2".to_string())); + assert_eq!(dirs[3].level, LevelFilter::DEBUG); + assert_eq!(dirs[3].field_names, Vec::<String>::new()); + } + + #[test] + fn parse_level_directives() { + expect_parse_level_directives( + "crate1::mod1=error,crate1::mod2=warn,crate1::mod2::mod3=info,\ + crate2=debug,crate3=trace,crate3::mod2::mod1=off", + ) + } + + #[test] + fn parse_uppercase_level_directives() { + expect_parse_level_directives( + "crate1::mod1=ERROR,crate1::mod2=WARN,crate1::mod2::mod3=INFO,\ + crate2=DEBUG,crate3=TRACE,crate3::mod2::mod1=OFF", + ) + } + + #[test] + fn parse_numeric_level_directives() { + expect_parse_level_directives( + "crate1::mod1=1,crate1::mod2=2,crate1::mod2::mod3=3,crate2=4,\ + crate3=5,crate3::mod2::mod1=0", + ) + } + + #[test] + fn targets_iter() { + let filter = expect_parse("crate1::mod1=error,crate1::mod2,crate2=debug,crate3=off") + .with_default(LevelFilter::WARN); + + let mut targets: Vec<_> = filter.iter().collect(); + targets.sort(); + + assert_eq!( + targets, + vec![ + ("crate1::mod1", LevelFilter::ERROR), + ("crate1::mod2", LevelFilter::TRACE), + ("crate2", LevelFilter::DEBUG), + ("crate3", LevelFilter::OFF), + ] + ); + } + + #[test] + fn targets_into_iter() { + let filter = expect_parse("crate1::mod1=error,crate1::mod2,crate2=debug,crate3=off") + .with_default(LevelFilter::WARN); + + let mut targets: Vec<_> = filter.into_iter().collect(); + targets.sort(); + + assert_eq!( + targets, + vec![ + ("crate1::mod1".to_string(), LevelFilter::ERROR), + ("crate1::mod2".to_string(), LevelFilter::TRACE), + ("crate2".to_string(), LevelFilter::DEBUG), + ("crate3".to_string(), LevelFilter::OFF), + ] + ); + } + + #[test] + // `println!` is only available with `libstd`. + #[cfg(feature = "std")] + fn size_of_filters() { + fn print_sz(s: &str) { + let filter = s.parse::<Targets>().expect("filter should parse"); + println!( + "size_of_val({:?})\n -> {}B", + s, + std::mem::size_of_val(&filter) + ); + } + + print_sz("info"); + + print_sz("foo=debug"); + + print_sz( + "crate1::mod1=error,crate1::mod2=warn,crate1::mod2::mod3=info,\ + crate2=debug,crate3=trace,crate3::mod2::mod1=off", + ); + } +} diff --git a/vendor/tracing-subscriber/src/fmt/fmt_layer.rs b/vendor/tracing-subscriber/src/fmt/fmt_layer.rs new file mode 100644 index 000000000..21992e780 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/fmt_layer.rs @@ -0,0 +1,1368 @@ +use crate::{ + field::RecordFields, + fmt::{format, FormatEvent, FormatFields, MakeWriter, TestWriter}, + layer::{self, Context}, + registry::{self, LookupSpan, SpanRef}, +}; +use format::{FmtSpan, TimingDisplay}; +use std::{any::TypeId, cell::RefCell, fmt, io, marker::PhantomData, ops::Deref, time::Instant}; +use tracing_core::{ + field, + span::{Attributes, Current, Id, Record}, + Event, Metadata, Subscriber, +}; + +/// A [`Layer`] that logs formatted representations of `tracing` events. +/// +/// ## Examples +/// +/// Constructing a layer with the default configuration: +/// +/// ```rust +/// use tracing_subscriber::{fmt, Registry}; +/// use tracing_subscriber::prelude::*; +/// +/// let subscriber = Registry::default() +/// .with(fmt::Layer::default()); +/// +/// tracing::subscriber::set_global_default(subscriber).unwrap(); +/// ``` +/// +/// Overriding the layer's behavior: +/// +/// ```rust +/// use tracing_subscriber::{fmt, Registry}; +/// use tracing_subscriber::prelude::*; +/// +/// let fmt_layer = fmt::layer() +/// .with_target(false) // don't include event targets when logging +/// .with_level(false); // don't include event levels when logging +/// +/// let subscriber = Registry::default().with(fmt_layer); +/// # tracing::subscriber::set_global_default(subscriber).unwrap(); +/// ``` +/// +/// Setting a custom event formatter: +/// +/// ```rust +/// use tracing_subscriber::fmt::{self, format, time}; +/// use tracing_subscriber::prelude::*; +/// +/// let fmt = format().with_timer(time::Uptime::default()); +/// let fmt_layer = fmt::layer() +/// .event_format(fmt) +/// .with_target(false); +/// # let subscriber = fmt_layer.with_subscriber(tracing_subscriber::registry::Registry::default()); +/// # tracing::subscriber::set_global_default(subscriber).unwrap(); +/// ``` +/// +/// [`Layer`]: super::layer::Layer +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +#[derive(Debug)] +pub struct Layer< + S, + N = format::DefaultFields, + E = format::Format<format::Full>, + W = fn() -> io::Stdout, +> { + make_writer: W, + fmt_fields: N, + fmt_event: E, + fmt_span: format::FmtSpanConfig, + is_ansi: bool, + _inner: PhantomData<fn(S)>, +} + +impl<S> Layer<S> { + /// Returns a new [`Layer`][self::Layer] with the default configuration. + pub fn new() -> Self { + Self::default() + } +} + +// This needs to be a seperate impl block because they place different bounds on the type parameters. +impl<S, N, E, W> Layer<S, N, E, W> +where + S: Subscriber + for<'a> LookupSpan<'a>, + N: for<'writer> FormatFields<'writer> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, +{ + /// Sets the [event formatter][`FormatEvent`] that the layer being built will + /// use to format events. + /// + /// The event formatter may be any type implementing the [`FormatEvent`] + /// trait, which is implemented for all functions taking a [`FmtContext`], a + /// [`Writer`], and an [`Event`]. + /// + /// # Examples + /// + /// Setting a type implementing [`FormatEvent`] as the formatter: + /// ```rust + /// use tracing_subscriber::fmt::{self, format}; + /// + /// let layer = fmt::layer() + /// .event_format(format().compact()); + /// # // this is necessary for type inference. + /// # use tracing_subscriber::Layer as _; + /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); + /// ``` + /// [`FormatEvent`]: format::FormatEvent + /// [`Event`]: tracing::Event + /// [`Writer`]: format::Writer + pub fn event_format<E2>(self, e: E2) -> Layer<S, N, E2, W> + where + E2: FormatEvent<S, N> + 'static, + { + Layer { + fmt_fields: self.fmt_fields, + fmt_event: e, + fmt_span: self.fmt_span, + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } + + /// Updates the event formatter by applying a function to the existing event formatter. + /// + /// This sets the event formatter that the layer being built will use to record fields. + /// + /// # Examples + /// + /// Updating an event formatter: + /// + /// ```rust + /// let layer = tracing_subscriber::fmt::layer() + /// .map_event_format(|e| e.compact()); + /// # // this is necessary for type inference. + /// # use tracing_subscriber::Layer as _; + /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); + /// ``` + pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> Layer<S, N, E2, W> + where + E2: FormatEvent<S, N> + 'static, + { + Layer { + fmt_fields: self.fmt_fields, + fmt_event: f(self.fmt_event), + fmt_span: self.fmt_span, + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } +} + +// This needs to be a seperate impl block because they place different bounds on the type parameters. +impl<S, N, E, W> Layer<S, N, E, W> { + /// Sets the [`MakeWriter`] that the layer being built will use to write events. + /// + /// # Examples + /// + /// Using `stderr` rather than `stdout`: + /// + /// ```rust + /// use std::io; + /// use tracing_subscriber::fmt; + /// + /// let layer = fmt::layer() + /// .with_writer(io::stderr); + /// # // this is necessary for type inference. + /// # use tracing_subscriber::Layer as _; + /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); + /// ``` + pub fn with_writer<W2>(self, make_writer: W2) -> Layer<S, N, E, W2> + where + W2: for<'writer> MakeWriter<'writer> + 'static, + { + Layer { + fmt_fields: self.fmt_fields, + fmt_event: self.fmt_event, + fmt_span: self.fmt_span, + is_ansi: self.is_ansi, + make_writer, + _inner: self._inner, + } + } + + /// Borrows the [writer] for this [`Layer`]. + /// + /// [writer]: MakeWriter + pub fn writer(&self) -> &W { + &self.make_writer + } + + /// Mutably borrows the [writer] for this [`Layer`]. + /// + /// This method is primarily expected to be used with the + /// [`reload::Handle::modify`](crate::reload::Handle::modify) method. + /// + /// # Examples + /// + /// ``` + /// # use tracing::info; + /// # use tracing_subscriber::{fmt,reload,Registry,prelude::*}; + /// # fn non_blocking<T: std::io::Write>(writer: T) -> (fn() -> std::io::Stdout) { + /// # std::io::stdout + /// # } + /// # fn main() { + /// let layer = fmt::layer().with_writer(non_blocking(std::io::stderr())); + /// let (layer, reload_handle) = reload::Layer::new(layer); + /// # + /// # // specifying the Registry type is required + /// # let _: &reload::Handle<fmt::Layer<Registry, _, _, _>, Registry> = &reload_handle; + /// # + /// info!("This will be logged to stderr"); + /// reload_handle.modify(|layer| *layer.writer_mut() = non_blocking(std::io::stdout())); + /// info!("This will be logged to stdout"); + /// # } + /// ``` + /// + /// [writer]: MakeWriter + pub fn writer_mut(&mut self) -> &mut W { + &mut self.make_writer + } + + /// Sets whether this layer should use ANSI terminal formatting + /// escape codes (such as colors). + /// + /// This method is primarily expected to be used with the + /// [`reload::Handle::modify`](crate::reload::Handle::modify) method when changing + /// the writer. + #[cfg(feature = "ansi")] + #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] + pub fn set_ansi(&mut self, ansi: bool) { + self.is_ansi = ansi; + } + + /// Configures the layer to support [`libtest`'s output capturing][capturing] when used in + /// unit tests. + /// + /// See [`TestWriter`] for additional details. + /// + /// # Examples + /// + /// Using [`TestWriter`] to let `cargo test` capture test output: + /// + /// ```rust + /// use std::io; + /// use tracing_subscriber::fmt; + /// + /// let layer = fmt::layer() + /// .with_test_writer(); + /// # // this is necessary for type inference. + /// # use tracing_subscriber::Layer as _; + /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); + /// ``` + /// [capturing]: + /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output + /// [`TestWriter`]: super::writer::TestWriter + pub fn with_test_writer(self) -> Layer<S, N, E, TestWriter> { + Layer { + fmt_fields: self.fmt_fields, + fmt_event: self.fmt_event, + fmt_span: self.fmt_span, + is_ansi: self.is_ansi, + make_writer: TestWriter::default(), + _inner: self._inner, + } + } + + /// Enable ANSI terminal colors for formatted output. + #[cfg(feature = "ansi")] + #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] + pub fn with_ansi(self, ansi: bool) -> Self { + Self { + is_ansi: ansi, + ..self + } + } + + /// Updates the [`MakeWriter`] by applying a function to the existing [`MakeWriter`]. + /// + /// This sets the [`MakeWriter`] that the layer being built will use to write events. + /// + /// # Examples + /// + /// Redirect output to stderr if level is <= WARN: + /// + /// ```rust + /// use tracing::Level; + /// use tracing_subscriber::fmt::{self, writer::MakeWriterExt}; + /// + /// let stderr = std::io::stderr.with_max_level(Level::WARN); + /// let layer = fmt::layer() + /// .map_writer(move |w| stderr.or_else(w)); + /// # // this is necessary for type inference. + /// # use tracing_subscriber::Layer as _; + /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); + /// ``` + pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> Layer<S, N, E, W2> + where + W2: for<'writer> MakeWriter<'writer> + 'static, + { + Layer { + fmt_fields: self.fmt_fields, + fmt_event: self.fmt_event, + fmt_span: self.fmt_span, + is_ansi: self.is_ansi, + make_writer: f(self.make_writer), + _inner: self._inner, + } + } +} + +impl<S, N, L, T, W> Layer<S, N, format::Format<L, T>, W> +where + N: for<'writer> FormatFields<'writer> + 'static, +{ + /// Use the given [`timer`] for span and event timestamps. + /// + /// See the [`time` module] for the provided timer implementations. + /// + /// Note that using the `"time`"" feature flag enables the + /// additional time formatters [`UtcTime`] and [`LocalTime`], which use the + /// [`time` crate] to provide more sophisticated timestamp formatting + /// options. + /// + /// [`timer`]: super::time::FormatTime + /// [`time` module]: mod@super::time + /// [`UtcTime`]: super::time::UtcTime + /// [`LocalTime`]: super::time::LocalTime + /// [`time` crate]: https://docs.rs/time/0.3 + pub fn with_timer<T2>(self, timer: T2) -> Layer<S, N, format::Format<L, T2>, W> { + Layer { + fmt_event: self.fmt_event.with_timer(timer), + fmt_fields: self.fmt_fields, + fmt_span: self.fmt_span, + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } + + /// Do not emit timestamps with spans and event. + pub fn without_time(self) -> Layer<S, N, format::Format<L, ()>, W> { + Layer { + fmt_event: self.fmt_event.without_time(), + fmt_fields: self.fmt_fields, + fmt_span: self.fmt_span.without_time(), + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } + + /// Configures how synthesized events are emitted at points in the [span + /// lifecycle][lifecycle]. + /// + /// The following options are available: + /// + /// - `FmtSpan::NONE`: No events will be synthesized when spans are + /// created, entered, exited, or closed. Data from spans will still be + /// included as the context for formatted events. This is the default. + /// - `FmtSpan::NEW`: An event will be synthesized when spans are created. + /// - `FmtSpan::ENTER`: An event will be synthesized when spans are entered. + /// - `FmtSpan::EXIT`: An event will be synthesized when spans are exited. + /// - `FmtSpan::CLOSE`: An event will be synthesized when a span closes. If + /// [timestamps are enabled][time] for this formatter, the generated + /// event will contain fields with the span's _busy time_ (the total + /// time for which it was entered) and _idle time_ (the total time that + /// the span existed but was not entered). + /// - `FmtSpan::ACTIVE`: Events will be synthesized when spans are entered + /// or exited. + /// - `FmtSpan::FULL`: Events will be synthesized whenever a span is + /// created, entered, exited, or closed. If timestamps are enabled, the + /// close event will contain the span's busy and idle time, as + /// described above. + /// + /// The options can be enabled in any combination. For instance, the following + /// will synthesize events whenever spans are created and closed: + /// + /// ```rust + /// use tracing_subscriber::fmt; + /// use tracing_subscriber::fmt::format::FmtSpan; + /// + /// let subscriber = fmt() + /// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) + /// .finish(); + /// ``` + /// + /// Note that the generated events will only be part of the log output by + /// this formatter; they will not be recorded by other `Subscriber`s or by + /// `Layer`s added to this subscriber. + /// + /// [lifecycle]: https://docs.rs/tracing/latest/tracing/span/index.html#the-span-lifecycle + /// [time]: Layer::without_time() + pub fn with_span_events(self, kind: FmtSpan) -> Self { + Layer { + fmt_span: self.fmt_span.with_kind(kind), + ..self + } + } + + /// Sets whether or not an event's target is displayed. + pub fn with_target(self, display_target: bool) -> Layer<S, N, format::Format<L, T>, W> { + Layer { + fmt_event: self.fmt_event.with_target(display_target), + ..self + } + } + /// Sets whether or not an event's [source code file path][file] is + /// displayed. + /// + /// [file]: tracing_core::Metadata::file + pub fn with_file(self, display_filename: bool) -> Layer<S, N, format::Format<L, T>, W> { + Layer { + fmt_event: self.fmt_event.with_file(display_filename), + ..self + } + } + + /// Sets whether or not an event's [source code line number][line] is + /// displayed. + /// + /// [line]: tracing_core::Metadata::line + pub fn with_line_number( + self, + display_line_number: bool, + ) -> Layer<S, N, format::Format<L, T>, W> { + Layer { + fmt_event: self.fmt_event.with_line_number(display_line_number), + ..self + } + } + + /// Sets whether or not an event's level is displayed. + pub fn with_level(self, display_level: bool) -> Layer<S, N, format::Format<L, T>, W> { + Layer { + fmt_event: self.fmt_event.with_level(display_level), + ..self + } + } + + /// Sets whether or not the [thread ID] of the current thread is displayed + /// when formatting events. + /// + /// [thread ID]: std::thread::ThreadId + pub fn with_thread_ids(self, display_thread_ids: bool) -> Layer<S, N, format::Format<L, T>, W> { + Layer { + fmt_event: self.fmt_event.with_thread_ids(display_thread_ids), + ..self + } + } + + /// Sets whether or not the [name] of the current thread is displayed + /// when formatting events. + /// + /// [name]: std::thread#naming-threads + pub fn with_thread_names( + self, + display_thread_names: bool, + ) -> Layer<S, N, format::Format<L, T>, W> { + Layer { + fmt_event: self.fmt_event.with_thread_names(display_thread_names), + ..self + } + } + + /// Sets the layer being built to use a [less verbose formatter][super::format::Compact]. + pub fn compact(self) -> Layer<S, N, format::Format<format::Compact, T>, W> + where + N: for<'writer> FormatFields<'writer> + 'static, + { + Layer { + fmt_event: self.fmt_event.compact(), + fmt_fields: self.fmt_fields, + fmt_span: self.fmt_span, + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } + + /// Sets the layer being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty). + #[cfg(feature = "ansi")] + #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] + pub fn pretty(self) -> Layer<S, format::Pretty, format::Format<format::Pretty, T>, W> { + Layer { + fmt_event: self.fmt_event.pretty(), + fmt_fields: format::Pretty::default(), + fmt_span: self.fmt_span, + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } + + /// Sets the layer being built to use a [JSON formatter][super::format::Json]. + /// + /// The full format includes fields from all entered spans. + /// + /// # Example Output + /// + /// ```ignore,json + /// {"timestamp":"Feb 20 11:28:15.096","level":"INFO","target":"mycrate","fields":{"message":"some message", "key": "value"}} + /// ``` + /// + /// # Options + /// + /// - [`Layer::flatten_event`] can be used to enable flattening event fields into the root + /// object. + /// + /// [`Layer::flatten_event`]: Layer::flatten_event() + #[cfg(feature = "json")] + #[cfg_attr(docsrs, doc(cfg(feature = "json")))] + pub fn json(self) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { + Layer { + fmt_event: self.fmt_event.json(), + fmt_fields: format::JsonFields::new(), + fmt_span: self.fmt_span, + make_writer: self.make_writer, + // always disable ANSI escapes in JSON mode! + is_ansi: false, + _inner: self._inner, + } + } +} + +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] +impl<S, T, W> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { + /// Sets the JSON layer being built to flatten event metadata. + /// + /// See [`format::Json`][super::format::Json] + pub fn flatten_event( + self, + flatten_event: bool, + ) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { + Layer { + fmt_event: self.fmt_event.flatten_event(flatten_event), + fmt_fields: format::JsonFields::new(), + ..self + } + } + + /// Sets whether or not the formatter will include the current span in + /// formatted events. + /// + /// See [`format::Json`][super::format::Json] + pub fn with_current_span( + self, + display_current_span: bool, + ) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { + Layer { + fmt_event: self.fmt_event.with_current_span(display_current_span), + fmt_fields: format::JsonFields::new(), + ..self + } + } + + /// Sets whether or not the formatter will include a list (from root to leaf) + /// of all currently entered spans in formatted events. + /// + /// See [`format::Json`][super::format::Json] + pub fn with_span_list( + self, + display_span_list: bool, + ) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { + Layer { + fmt_event: self.fmt_event.with_span_list(display_span_list), + fmt_fields: format::JsonFields::new(), + ..self + } + } +} + +impl<S, N, E, W> Layer<S, N, E, W> { + /// Sets the field formatter that the layer being built will use to record + /// fields. + pub fn fmt_fields<N2>(self, fmt_fields: N2) -> Layer<S, N2, E, W> + where + N2: for<'writer> FormatFields<'writer> + 'static, + { + Layer { + fmt_event: self.fmt_event, + fmt_fields, + fmt_span: self.fmt_span, + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } + + /// Updates the field formatter by applying a function to the existing field formatter. + /// + /// This sets the field formatter that the layer being built will use to record fields. + /// + /// # Examples + /// + /// Updating a field formatter: + /// + /// ```rust + /// use tracing_subscriber::field::MakeExt; + /// let layer = tracing_subscriber::fmt::layer() + /// .map_fmt_fields(|f| f.debug_alt()); + /// # // this is necessary for type inference. + /// # use tracing_subscriber::Layer as _; + /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); + /// ``` + pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> Layer<S, N2, E, W> + where + N2: for<'writer> FormatFields<'writer> + 'static, + { + Layer { + fmt_event: self.fmt_event, + fmt_fields: f(self.fmt_fields), + fmt_span: self.fmt_span, + make_writer: self.make_writer, + is_ansi: self.is_ansi, + _inner: self._inner, + } + } +} + +impl<S> Default for Layer<S> { + fn default() -> Self { + Layer { + fmt_fields: format::DefaultFields::default(), + fmt_event: format::Format::default(), + fmt_span: format::FmtSpanConfig::default(), + make_writer: io::stdout, + is_ansi: cfg!(feature = "ansi"), + _inner: PhantomData, + } + } +} + +impl<S, N, E, W> Layer<S, N, E, W> +where + S: Subscriber + for<'a> LookupSpan<'a>, + N: for<'writer> FormatFields<'writer> + 'static, + E: FormatEvent<S, N> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, +{ + #[inline] + fn make_ctx<'a>(&'a self, ctx: Context<'a, S>, event: &'a Event<'a>) -> FmtContext<'a, S, N> { + FmtContext { + ctx, + fmt_fields: &self.fmt_fields, + event, + } + } +} + +/// A formatted representation of a span's fields stored in its [extensions]. +/// +/// Because `FormattedFields` is generic over the type of the formatter that +/// produced it, multiple versions of a span's formatted fields can be stored in +/// the [`Extensions`][extensions] type-map. This means that when multiple +/// formatters are in use, each can store its own formatted representation +/// without conflicting. +/// +/// [extensions]: crate::registry::Extensions +#[derive(Default)] +pub struct FormattedFields<E: ?Sized> { + _format_fields: PhantomData<fn(E)>, + was_ansi: bool, + /// The formatted fields of a span. + pub fields: String, +} + +impl<E: ?Sized> FormattedFields<E> { + /// Returns a new `FormattedFields`. + pub fn new(fields: String) -> Self { + Self { + fields, + was_ansi: false, + _format_fields: PhantomData, + } + } + + /// Returns a new [`format::Writer`] for writing to this `FormattedFields`. + /// + /// The returned [`format::Writer`] can be used with the + /// [`FormatFields::format_fields`] method. + pub fn as_writer(&mut self) -> format::Writer<'_> { + format::Writer::new(&mut self.fields).with_ansi(self.was_ansi) + } +} + +impl<E: ?Sized> fmt::Debug for FormattedFields<E> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FormattedFields") + .field("fields", &self.fields) + .field("formatter", &format_args!("{}", std::any::type_name::<E>())) + .field("was_ansi", &self.was_ansi) + .finish() + } +} + +impl<E: ?Sized> fmt::Display for FormattedFields<E> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.fields, f) + } +} + +impl<E: ?Sized> Deref for FormattedFields<E> { + type Target = String; + fn deref(&self) -> &Self::Target { + &self.fields + } +} + +// === impl FmtLayer === + +macro_rules! with_event_from_span { + ($id:ident, $span:ident, $($field:literal = $value:expr),*, |$event:ident| $code:block) => { + let meta = $span.metadata(); + let cs = meta.callsite(); + let fs = field::FieldSet::new(&[$($field),*], cs); + #[allow(unused)] + let mut iter = fs.iter(); + let v = [$( + (&iter.next().unwrap(), Some(&$value as &dyn field::Value)), + )*]; + let vs = fs.value_set(&v); + let $event = Event::new_child_of($id, meta, &vs); + $code + }; +} + +impl<S, N, E, W> layer::Layer<S> for Layer<S, N, E, W> +where + S: Subscriber + for<'a> LookupSpan<'a>, + N: for<'writer> FormatFields<'writer> + 'static, + E: FormatEvent<S, N> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, +{ + fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { + let span = ctx.span(id).expect("Span not found, this is a bug"); + let mut extensions = span.extensions_mut(); + + if extensions.get_mut::<FormattedFields<N>>().is_none() { + let mut fields = FormattedFields::<N>::new(String::new()); + if self + .fmt_fields + .format_fields(fields.as_writer().with_ansi(self.is_ansi), attrs) + .is_ok() + { + fields.was_ansi = self.is_ansi; + extensions.insert(fields); + } + } + + if self.fmt_span.fmt_timing + && self.fmt_span.trace_close() + && extensions.get_mut::<Timings>().is_none() + { + extensions.insert(Timings::new()); + } + + if self.fmt_span.trace_new() { + with_event_from_span!(id, span, "message" = "new", |event| { + drop(extensions); + drop(span); + self.on_event(&event, ctx); + }); + } + } + + fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { + let span = ctx.span(id).expect("Span not found, this is a bug"); + let mut extensions = span.extensions_mut(); + if let Some(fields) = extensions.get_mut::<FormattedFields<N>>() { + let _ = self.fmt_fields.add_fields(fields, values); + return; + } + + let mut fields = FormattedFields::<N>::new(String::new()); + if self + .fmt_fields + .format_fields(fields.as_writer().with_ansi(self.is_ansi), values) + .is_ok() + { + fields.was_ansi = self.is_ansi; + extensions.insert(fields); + } + } + + fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { + if self.fmt_span.trace_enter() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { + let span = ctx.span(id).expect("Span not found, this is a bug"); + let mut extensions = span.extensions_mut(); + if let Some(timings) = extensions.get_mut::<Timings>() { + let now = Instant::now(); + timings.idle += (now - timings.last).as_nanos() as u64; + timings.last = now; + } + + if self.fmt_span.trace_enter() { + with_event_from_span!(id, span, "message" = "enter", |event| { + drop(extensions); + drop(span); + self.on_event(&event, ctx); + }); + } + } + } + + fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { + if self.fmt_span.trace_exit() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { + let span = ctx.span(id).expect("Span not found, this is a bug"); + let mut extensions = span.extensions_mut(); + if let Some(timings) = extensions.get_mut::<Timings>() { + let now = Instant::now(); + timings.busy += (now - timings.last).as_nanos() as u64; + timings.last = now; + } + + if self.fmt_span.trace_exit() { + with_event_from_span!(id, span, "message" = "exit", |event| { + drop(extensions); + drop(span); + self.on_event(&event, ctx); + }); + } + } + } + + fn on_close(&self, id: Id, ctx: Context<'_, S>) { + if self.fmt_span.trace_close() { + let span = ctx.span(&id).expect("Span not found, this is a bug"); + let extensions = span.extensions(); + if let Some(timing) = extensions.get::<Timings>() { + let Timings { + busy, + mut idle, + last, + } = *timing; + idle += (Instant::now() - last).as_nanos() as u64; + + let t_idle = field::display(TimingDisplay(idle)); + let t_busy = field::display(TimingDisplay(busy)); + + with_event_from_span!( + id, + span, + "message" = "close", + "time.busy" = t_busy, + "time.idle" = t_idle, + |event| { + drop(extensions); + drop(span); + self.on_event(&event, ctx); + } + ); + } else { + with_event_from_span!(id, span, "message" = "close", |event| { + drop(extensions); + drop(span); + self.on_event(&event, ctx); + }); + } + } + } + + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { + thread_local! { + static BUF: RefCell<String> = RefCell::new(String::new()); + } + + BUF.with(|buf| { + let borrow = buf.try_borrow_mut(); + let mut a; + let mut b; + let mut buf = match borrow { + Ok(buf) => { + a = buf; + &mut *a + } + _ => { + b = String::new(); + &mut b + } + }; + + let ctx = self.make_ctx(ctx, event); + if self + .fmt_event + .format_event( + &ctx, + format::Writer::new(&mut buf).with_ansi(self.is_ansi), + event, + ) + .is_ok() + { + let mut writer = self.make_writer.make_writer_for(event.metadata()); + let _ = io::Write::write_all(&mut writer, buf.as_bytes()); + } + + buf.clear(); + }); + } + + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + // This `downcast_raw` impl allows downcasting a `fmt` layer to any of + // its components (event formatter, field formatter, and `MakeWriter`) + // as well as to the layer's type itself. The potential use-cases for + // this *may* be somewhat niche, though... + match () { + _ if id == TypeId::of::<Self>() => Some(self as *const Self as *const ()), + _ if id == TypeId::of::<E>() => Some(&self.fmt_event as *const E as *const ()), + _ if id == TypeId::of::<N>() => Some(&self.fmt_fields as *const N as *const ()), + _ if id == TypeId::of::<W>() => Some(&self.make_writer as *const W as *const ()), + _ => None, + } + } +} + +/// Provides the current span context to a formatter. +pub struct FmtContext<'a, S, N> { + pub(crate) ctx: Context<'a, S>, + pub(crate) fmt_fields: &'a N, + pub(crate) event: &'a Event<'a>, +} + +impl<'a, S, N> fmt::Debug for FmtContext<'a, S, N> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FmtContext").finish() + } +} + +impl<'cx, 'writer, S, N> FormatFields<'writer> for FmtContext<'cx, S, N> +where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + N: FormatFields<'writer> + 'static, +{ + fn format_fields<R: RecordFields>( + &self, + writer: format::Writer<'writer>, + fields: R, + ) -> fmt::Result { + self.fmt_fields.format_fields(writer, fields) + } +} + +impl<'a, S, N> FmtContext<'a, S, N> +where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static, +{ + /// Visits every span in the current context with a closure. + /// + /// The provided closure will be called first with the current span, + /// and then with that span's parent, and then that span's parent, + /// and so on until a root span is reached. + pub fn visit_spans<E, F>(&self, mut f: F) -> Result<(), E> + where + F: FnMut(&SpanRef<'_, S>) -> Result<(), E>, + { + // visit all the current spans + if let Some(scope) = self.event_scope() { + for span in scope.from_root() { + f(&span)?; + } + } + Ok(()) + } + + /// Returns metadata for the span with the given `id`, if it exists. + /// + /// If this returns `None`, then no span exists for that ID (either it has + /// closed or the ID is invalid). + #[inline] + pub fn metadata(&self, id: &Id) -> Option<&'static Metadata<'static>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + self.ctx.metadata(id) + } + + /// Returns [stored data] for the span with the given `id`, if it exists. + /// + /// If this returns `None`, then no span exists for that ID (either it has + /// closed or the ID is invalid). + /// + /// [stored data]: crate::registry::SpanRef + #[inline] + pub fn span(&self, id: &Id) -> Option<SpanRef<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + self.ctx.span(id) + } + + /// Returns `true` if an active span exists for the given `Id`. + #[inline] + pub fn exists(&self, id: &Id) -> bool + where + S: for<'lookup> LookupSpan<'lookup>, + { + self.ctx.exists(id) + } + + /// Returns [stored data] for the span that the wrapped subscriber considers + /// to be the current. + /// + /// If this returns `None`, then we are not currently within a span. + /// + /// [stored data]: crate::registry::SpanRef + #[inline] + pub fn lookup_current(&self) -> Option<SpanRef<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + self.ctx.lookup_current() + } + + /// Returns the current span for this formatter. + pub fn current_span(&self) -> Current { + self.ctx.current_span() + } + + /// Returns [stored data] for the parent span of the event currently being + /// formatted. + /// + /// If the event has a contextual parent, this will return the current span. If + /// the event has an explicit parent span, this will return that span. If + /// the event does not have a parent span, this will return `None`. + /// + /// [stored data]: SpanRef + pub fn parent_span(&self) -> Option<SpanRef<'_, S>> { + self.ctx.event_span(self.event) + } + + /// Returns an iterator over the [stored data] for all the spans in the + /// current context, starting with the specified span and ending with the + /// root of the trace tree and ending with the current span. + /// + /// This is equivalent to the [`Context::span_scope`] method. + /// + /// <div class="information"> + /// <div class="tooltip ignore" style="">ⓘ<span class="tooltiptext">Note</span></div> + /// </div> + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: Compared to <a href="#method.scope"><code>scope</code></a> this + /// returns the spans in reverse order (from leaf to root). Use + /// <a href="../registry/struct.Scope.html#method.from_root"><code>Scope::from_root</code></a> + /// in case root-to-leaf ordering is desired. + /// </pre></div> + /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to implement the + /// <a href="../registry/trait.LookupSpan.html"><code>LookupSpan</code></a> trait. + /// See the documentation on <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre></div> + /// + /// [stored data]: crate::registry::SpanRef + pub fn span_scope(&self, id: &Id) -> Option<registry::Scope<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + self.ctx.span_scope(id) + } + + /// Returns an iterator over the [stored data] for all the spans in the + /// event's span context, starting with its parent span and ending with the + /// root of the trace tree. + /// + /// This is equivalent to calling the [`Context::event_scope`] method and + /// passing the event currently being formatted. + /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: Compared to <a href="#method.scope"><code>scope</code></a> this + /// returns the spans in reverse order (from leaf to root). Use + /// <a href="../registry/struct.Scope.html#method.from_root"><code>Scope::from_root</code></a> + /// in case root-to-leaf ordering is desired. + /// </pre></div> + /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to implement the + /// <a href="../registry/trait.LookupSpan.html"><code>LookupSpan</code></a> trait. + /// See the documentation on <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre></div> + /// + /// [stored data]: crate::registry::SpanRef + pub fn event_scope(&self) -> Option<registry::Scope<'_, S>> + where + S: for<'lookup> registry::LookupSpan<'lookup>, + { + self.ctx.event_scope(self.event) + } + + /// Returns the [field formatter] configured by the subscriber invoking + /// `format_event`. + /// + /// The event formatter may use the returned field formatter to format the + /// fields of any events it records. + /// + /// [field formatter]: FormatFields + pub fn field_format(&self) -> &N { + self.fmt_fields + } +} + +struct Timings { + idle: u64, + busy: u64, + last: Instant, +} + +impl Timings { + fn new() -> Self { + Self { + idle: 0, + busy: 0, + last: Instant::now(), + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::fmt::{ + self, + format::{self, test::MockTime, Format}, + layer::Layer as _, + test::{MockMakeWriter, MockWriter}, + time, + }; + use crate::Registry; + use format::FmtSpan; + use regex::Regex; + use tracing::subscriber::with_default; + use tracing_core::dispatcher::Dispatch; + + #[test] + fn impls() { + let f = Format::default().with_timer(time::Uptime::default()); + let fmt = fmt::Layer::default().event_format(f); + let subscriber = fmt.with_subscriber(Registry::default()); + let _dispatch = Dispatch::new(subscriber); + + let f = format::Format::default(); + let fmt = fmt::Layer::default().event_format(f); + let subscriber = fmt.with_subscriber(Registry::default()); + let _dispatch = Dispatch::new(subscriber); + + let f = format::Format::default().compact(); + let fmt = fmt::Layer::default().event_format(f); + let subscriber = fmt.with_subscriber(Registry::default()); + let _dispatch = Dispatch::new(subscriber); + } + + #[test] + fn fmt_layer_downcasts() { + let f = format::Format::default(); + let fmt = fmt::Layer::default().event_format(f); + let subscriber = fmt.with_subscriber(Registry::default()); + + let dispatch = Dispatch::new(subscriber); + assert!(dispatch.downcast_ref::<fmt::Layer<Registry>>().is_some()); + } + + #[test] + fn fmt_layer_downcasts_to_parts() { + let f = format::Format::default(); + let fmt = fmt::Layer::default().event_format(f); + let subscriber = fmt.with_subscriber(Registry::default()); + let dispatch = Dispatch::new(subscriber); + assert!(dispatch.downcast_ref::<format::DefaultFields>().is_some()); + assert!(dispatch.downcast_ref::<format::Format>().is_some()) + } + + #[test] + fn is_lookup_span() { + fn assert_lookup_span<T: for<'a> crate::registry::LookupSpan<'a>>(_: T) {} + let fmt = fmt::Layer::default(); + let subscriber = fmt.with_subscriber(Registry::default()); + assert_lookup_span(subscriber) + } + + fn sanitize_timings(s: String) -> String { + let re = Regex::new("time\\.(idle|busy)=([0-9.]+)[mµn]s").unwrap(); + re.replace_all(s.as_str(), "timing").to_string() + } + + #[test] + fn synthesize_span_none() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + // check that FmtSpan::NONE is the default + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("span1", x = 42); + let _e = span1.enter(); + }); + let actual = sanitize_timings(make_writer.get_string()); + assert_eq!("", actual.as_str()); + } + + #[test] + fn synthesize_span_active() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + .with_span_events(FmtSpan::ACTIVE) + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("span1", x = 42); + let _e = span1.enter(); + }); + let actual = sanitize_timings(make_writer.get_string()); + assert_eq!( + "fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: enter\n\ + fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: exit\n", + actual.as_str() + ); + } + + #[test] + fn synthesize_span_close() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + .with_span_events(FmtSpan::CLOSE) + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("span1", x = 42); + let _e = span1.enter(); + }); + let actual = sanitize_timings(make_writer.get_string()); + assert_eq!( + "fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: close timing timing\n", + actual.as_str() + ); + } + + #[test] + fn synthesize_span_close_no_timing() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + .without_time() + .with_span_events(FmtSpan::CLOSE) + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("span1", x = 42); + let _e = span1.enter(); + }); + let actual = sanitize_timings(make_writer.get_string()); + assert_eq!( + "span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: close\n", + actual.as_str() + ); + } + + #[test] + fn synthesize_span_full() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + .with_span_events(FmtSpan::FULL) + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("span1", x = 42); + let _e = span1.enter(); + }); + let actual = sanitize_timings(make_writer.get_string()); + assert_eq!( + "fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: new\n\ + fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: enter\n\ + fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: exit\n\ + fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: close timing timing\n", + actual.as_str() + ); + } + + #[test] + fn make_writer_based_on_meta() { + struct MakeByTarget { + make_writer1: MockMakeWriter, + make_writer2: MockMakeWriter, + } + + impl<'a> MakeWriter<'a> for MakeByTarget { + type Writer = MockWriter; + + fn make_writer(&'a self) -> Self::Writer { + self.make_writer1.make_writer() + } + + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + if meta.target() == "writer2" { + return self.make_writer2.make_writer(); + } + self.make_writer() + } + } + + let make_writer1 = MockMakeWriter::default(); + let make_writer2 = MockMakeWriter::default(); + + let make_writer = MakeByTarget { + make_writer1: make_writer1.clone(), + make_writer2: make_writer2.clone(), + }; + + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer) + .with_level(false) + .with_target(false) + .with_ansi(false) + .with_timer(MockTime) + .with_span_events(FmtSpan::CLOSE) + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("writer1_span", x = 42); + let _e = span1.enter(); + tracing::info!(target: "writer2", "hello writer2!"); + let span2 = tracing::info_span!(target: "writer2", "writer2_span"); + let _e = span2.enter(); + tracing::warn!(target: "writer1", "hello writer1!"); + }); + + let actual = sanitize_timings(make_writer1.get_string()); + assert_eq!( + "fake time writer1_span{x=42}:writer2_span: hello writer1!\n\ + fake time writer1_span{x=42}: close timing timing\n", + actual.as_str() + ); + let actual = sanitize_timings(make_writer2.get_string()); + assert_eq!( + "fake time writer1_span{x=42}: hello writer2!\n\ + fake time writer1_span{x=42}:writer2_span: close timing timing\n", + actual.as_str() + ); + } +} diff --git a/vendor/tracing-subscriber/src/fmt/format/json.rs b/vendor/tracing-subscriber/src/fmt/format/json.rs new file mode 100644 index 000000000..c2f4d3755 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/format/json.rs @@ -0,0 +1,885 @@ +use super::{Format, FormatEvent, FormatFields, FormatTime, Writer}; +use crate::{ + field::{RecordFields, VisitOutput}, + fmt::{ + fmt_layer::{FmtContext, FormattedFields}, + writer::WriteAdaptor, + }, + registry::LookupSpan, +}; +use serde::ser::{SerializeMap, Serializer as _}; +use serde_json::Serializer; +use std::{ + collections::BTreeMap, + fmt::{self, Write}, +}; +use tracing_core::{ + field::{self, Field}, + span::Record, + Event, Subscriber, +}; +use tracing_serde::AsSerde; + +#[cfg(feature = "tracing-log")] +use tracing_log::NormalizeEvent; + +/// Marker for [`Format`] that indicates that the newline-delimited JSON log +/// format should be used. +/// +/// This formatter is intended for production use with systems where structured +/// logs are consumed as JSON by analysis and viewing tools. The JSON output is +/// not optimized for human readability; instead, it should be pretty-printed +/// using external JSON tools such as `jq`, or using a JSON log viewer. +/// +/// # Example Output +/// +/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt-json +/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s +/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-json` +/// {"timestamp":"2022-02-15T18:47:10.821315Z","level":"INFO","fields":{"message":"preparing to shave yaks","number_of_yaks":3},"target":"fmt_json"} +/// {"timestamp":"2022-02-15T18:47:10.821422Z","level":"INFO","fields":{"message":"shaving yaks"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.821495Z","level":"TRACE","fields":{"message":"hello! I'm gonna shave a yak","excitement":"yay!"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":1,"name":"shave"}]} +/// {"timestamp":"2022-02-15T18:47:10.821546Z","level":"TRACE","fields":{"message":"yak shaved successfully"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":1,"name":"shave"}]} +/// {"timestamp":"2022-02-15T18:47:10.821598Z","level":"DEBUG","fields":{"yak":1,"shaved":true},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.821637Z","level":"TRACE","fields":{"yaks_shaved":1},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.821684Z","level":"TRACE","fields":{"message":"hello! I'm gonna shave a yak","excitement":"yay!"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":2,"name":"shave"}]} +/// {"timestamp":"2022-02-15T18:47:10.821727Z","level":"TRACE","fields":{"message":"yak shaved successfully"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":2,"name":"shave"}]} +/// {"timestamp":"2022-02-15T18:47:10.821773Z","level":"DEBUG","fields":{"yak":2,"shaved":true},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.821806Z","level":"TRACE","fields":{"yaks_shaved":2},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.821909Z","level":"TRACE","fields":{"message":"hello! I'm gonna shave a yak","excitement":"yay!"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":3,"name":"shave"}]} +/// {"timestamp":"2022-02-15T18:47:10.821956Z","level":"WARN","fields":{"message":"could not locate yak"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":3,"name":"shave"}]} +/// {"timestamp":"2022-02-15T18:47:10.822006Z","level":"DEBUG","fields":{"yak":3,"shaved":false},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.822041Z","level":"ERROR","fields":{"message":"failed to shave yak","yak":3,"error":"missing yak"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.822079Z","level":"TRACE","fields":{"yaks_shaved":2},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +/// {"timestamp":"2022-02-15T18:47:10.822117Z","level":"INFO","fields":{"message":"yak shaving completed","all_yaks_shaved":false},"target":"fmt_json"} +/// </pre> +/// +/// # Options +/// +/// This formatter exposes additional options to configure the structure of the +/// output JSON objects: +/// +/// - [`Json::flatten_event`] can be used to enable flattening event fields into +/// the root +/// - [`Json::with_current_span`] can be used to control logging of the current +/// span +/// - [`Json::with_span_list`] can be used to control logging of the span list +/// object. +/// +/// By default, event fields are not flattened, and both current span and span +/// list are logged. +/// +/// # Valuable Support +/// +/// Experimental support is available for using the [`valuable`] crate to record +/// user-defined values as structured JSON. When the ["valuable" unstable +/// feature][unstable] is enabled, types implementing [`valuable::Valuable`] will +/// be recorded as structured JSON, rather than +/// using their [`std::fmt::Debug`] implementations. +/// +/// **Note**: This is an experimental feature. [Unstable features][unstable] +/// must be enabled in order to use `valuable` support. +/// +/// [`Json::flatten_event`]: Json::flatten_event() +/// [`Json::with_current_span`]: Json::with_current_span() +/// [`Json::with_span_list`]: Json::with_span_list() +/// [`valuable`]: https://crates.io/crates/valuable +/// [unstable]: crate#unstable-features +/// [`valuable::Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub struct Json { + pub(crate) flatten_event: bool, + pub(crate) display_current_span: bool, + pub(crate) display_span_list: bool, +} + +impl Json { + /// If set to `true` event metadata will be flattened into the root object. + pub fn flatten_event(&mut self, flatten_event: bool) { + self.flatten_event = flatten_event; + } + + /// If set to `false`, formatted events won't contain a field for the current span. + pub fn with_current_span(&mut self, display_current_span: bool) { + self.display_current_span = display_current_span; + } + + /// If set to `false`, formatted events won't contain a list of all currently + /// entered spans. Spans are logged in a list from root to leaf. + pub fn with_span_list(&mut self, display_span_list: bool) { + self.display_span_list = display_span_list; + } +} + +struct SerializableContext<'a, 'b, Span, N>( + &'b crate::layer::Context<'a, Span>, + std::marker::PhantomData<N>, +) +where + Span: Subscriber + for<'lookup> crate::registry::LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static; + +impl<'a, 'b, Span, N> serde::ser::Serialize for SerializableContext<'a, 'b, Span, N> +where + Span: Subscriber + for<'lookup> crate::registry::LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static, +{ + fn serialize<Ser>(&self, serializer_o: Ser) -> Result<Ser::Ok, Ser::Error> + where + Ser: serde::ser::Serializer, + { + use serde::ser::SerializeSeq; + let mut serializer = serializer_o.serialize_seq(None)?; + + if let Some(leaf_span) = self.0.lookup_current() { + for span in leaf_span.scope().from_root() { + serializer.serialize_element(&SerializableSpan(&span, self.1))?; + } + } + + serializer.end() + } +} + +struct SerializableSpan<'a, 'b, Span, N>( + &'b crate::registry::SpanRef<'a, Span>, + std::marker::PhantomData<N>, +) +where + Span: for<'lookup> crate::registry::LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static; + +impl<'a, 'b, Span, N> serde::ser::Serialize for SerializableSpan<'a, 'b, Span, N> +where + Span: for<'lookup> crate::registry::LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static, +{ + fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error> + where + Ser: serde::ser::Serializer, + { + let mut serializer = serializer.serialize_map(None)?; + + let ext = self.0.extensions(); + let data = ext + .get::<FormattedFields<N>>() + .expect("Unable to find FormattedFields in extensions; this is a bug"); + + // TODO: let's _not_ do this, but this resolves + // https://github.com/tokio-rs/tracing/issues/391. + // We should probably rework this to use a `serde_json::Value` or something + // similar in a JSON-specific layer, but I'd (david) + // rather have a uglier fix now rather than shipping broken JSON. + match serde_json::from_str::<serde_json::Value>(data) { + Ok(serde_json::Value::Object(fields)) => { + for field in fields { + serializer.serialize_entry(&field.0, &field.1)?; + } + } + // We have fields for this span which are valid JSON but not an object. + // This is probably a bug, so panic if we're in debug mode + Ok(_) if cfg!(debug_assertions) => panic!( + "span '{}' had malformed fields! this is a bug.\n error: invalid JSON object\n fields: {:?}", + self.0.metadata().name(), + data + ), + // If we *aren't* in debug mode, it's probably best not to + // crash the program, let's log the field found but also an + // message saying it's type is invalid + Ok(value) => { + serializer.serialize_entry("field", &value)?; + serializer.serialize_entry("field_error", "field was no a valid object")? + } + // We have previously recorded fields for this span + // should be valid JSON. However, they appear to *not* + // be valid JSON. This is almost certainly a bug, so + // panic if we're in debug mode + Err(e) if cfg!(debug_assertions) => panic!( + "span '{}' had malformed fields! this is a bug.\n error: {}\n fields: {:?}", + self.0.metadata().name(), + e, + data + ), + // If we *aren't* in debug mode, it's probably best not + // crash the program, but let's at least make sure it's clear + // that the fields are not supposed to be missing. + Err(e) => serializer.serialize_entry("field_error", &format!("{}", e))?, + }; + serializer.serialize_entry("name", self.0.metadata().name())?; + serializer.end() + } +} + +impl<S, N, T> FormatEvent<S, N> for Format<Json, T> +where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static, + T: FormatTime, +{ + fn format_event( + &self, + ctx: &FmtContext<'_, S, N>, + mut writer: Writer<'_>, + event: &Event<'_>, + ) -> fmt::Result + where + S: Subscriber + for<'a> LookupSpan<'a>, + { + let mut timestamp = String::new(); + self.timer.format_time(&mut Writer::new(&mut timestamp))?; + + #[cfg(feature = "tracing-log")] + let normalized_meta = event.normalized_metadata(); + #[cfg(feature = "tracing-log")] + let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); + #[cfg(not(feature = "tracing-log"))] + let meta = event.metadata(); + + let mut visit = || { + let mut serializer = Serializer::new(WriteAdaptor::new(&mut writer)); + + let mut serializer = serializer.serialize_map(None)?; + + if self.display_timestamp { + serializer.serialize_entry("timestamp", ×tamp)?; + } + + if self.display_level { + serializer.serialize_entry("level", &meta.level().as_serde())?; + } + + let format_field_marker: std::marker::PhantomData<N> = std::marker::PhantomData; + + let current_span = if self.format.display_current_span || self.format.display_span_list + { + event + .parent() + .and_then(|id| ctx.span(id)) + .or_else(|| ctx.lookup_current()) + } else { + None + }; + + if self.format.flatten_event { + let mut visitor = tracing_serde::SerdeMapVisitor::new(serializer); + event.record(&mut visitor); + + serializer = visitor.take_serializer()?; + } else { + use tracing_serde::fields::AsMap; + serializer.serialize_entry("fields", &event.field_map())?; + }; + + if self.display_target { + serializer.serialize_entry("target", meta.target())?; + } + + if self.display_filename { + if let Some(filename) = meta.file() { + serializer.serialize_entry("filename", filename)?; + } + } + + if self.display_line_number { + if let Some(line_number) = meta.line() { + serializer.serialize_entry("line_number", &line_number)?; + } + } + + if self.format.display_current_span { + if let Some(ref span) = current_span { + serializer + .serialize_entry("span", &SerializableSpan(span, format_field_marker)) + .unwrap_or(()); + } + } + + if self.format.display_span_list && current_span.is_some() { + serializer.serialize_entry( + "spans", + &SerializableContext(&ctx.ctx, format_field_marker), + )?; + } + + if self.display_thread_name { + let current_thread = std::thread::current(); + match current_thread.name() { + Some(name) => { + serializer.serialize_entry("threadName", name)?; + } + // fall-back to thread id when name is absent and ids are not enabled + None if !self.display_thread_id => { + serializer + .serialize_entry("threadName", &format!("{:?}", current_thread.id()))?; + } + _ => {} + } + } + + if self.display_thread_id { + serializer + .serialize_entry("threadId", &format!("{:?}", std::thread::current().id()))?; + } + + serializer.end() + }; + + visit().map_err(|_| fmt::Error)?; + writeln!(writer) + } +} + +impl Default for Json { + fn default() -> Json { + Json { + flatten_event: false, + display_current_span: true, + display_span_list: true, + } + } +} + +/// The JSON [`FormatFields`] implementation. +/// +#[derive(Debug)] +pub struct JsonFields { + // reserve the ability to add fields to this without causing a breaking + // change in the future. + _private: (), +} + +impl JsonFields { + /// Returns a new JSON [`FormatFields`] implementation. + /// + pub fn new() -> Self { + Self { _private: () } + } +} + +impl Default for JsonFields { + fn default() -> Self { + Self::new() + } +} + +impl<'a> FormatFields<'a> for JsonFields { + /// Format the provided `fields` to the provided `writer`, returning a result. + fn format_fields<R: RecordFields>(&self, mut writer: Writer<'_>, fields: R) -> fmt::Result { + let mut v = JsonVisitor::new(&mut writer); + fields.record(&mut v); + v.finish() + } + + /// Record additional field(s) on an existing span. + /// + /// By default, this appends a space to the current set of fields if it is + /// non-empty, and then calls `self.format_fields`. If different behavior is + /// required, the default implementation of this method can be overridden. + fn add_fields( + &self, + current: &'a mut FormattedFields<Self>, + fields: &Record<'_>, + ) -> fmt::Result { + if current.is_empty() { + // If there are no previously recorded fields, we can just reuse the + // existing string. + let mut writer = current.as_writer(); + let mut v = JsonVisitor::new(&mut writer); + fields.record(&mut v); + v.finish()?; + return Ok(()); + } + + // If fields were previously recorded on this span, we need to parse + // the current set of fields as JSON, add the new fields, and + // re-serialize them. Otherwise, if we just appended the new fields + // to a previously serialized JSON object, we would end up with + // malformed JSON. + // + // XXX(eliza): this is far from efficient, but unfortunately, it is + // necessary as long as the JSON formatter is implemented on top of + // an interface that stores all formatted fields as strings. + // + // We should consider reimplementing the JSON formatter as a + // separate layer, rather than a formatter for the `fmt` layer — + // then, we could store fields as JSON values, and add to them + // without having to parse and re-serialize. + let mut new = String::new(); + let map: BTreeMap<&'_ str, serde_json::Value> = + serde_json::from_str(current).map_err(|_| fmt::Error)?; + let mut v = JsonVisitor::new(&mut new); + v.values = map; + fields.record(&mut v); + v.finish()?; + current.fields = new; + + Ok(()) + } +} + +/// The [visitor] produced by [`JsonFields`]'s [`MakeVisitor`] implementation. +/// +/// [visitor]: crate::field::Visit +/// [`MakeVisitor`]: crate::field::MakeVisitor +pub struct JsonVisitor<'a> { + values: BTreeMap<&'a str, serde_json::Value>, + writer: &'a mut dyn Write, +} + +impl<'a> fmt::Debug for JsonVisitor<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_fmt(format_args!("JsonVisitor {{ values: {:?} }}", self.values)) + } +} + +impl<'a> JsonVisitor<'a> { + /// Returns a new default visitor that formats to the provided `writer`. + /// + /// # Arguments + /// - `writer`: the writer to format to. + /// - `is_empty`: whether or not any fields have been previously written to + /// that writer. + pub fn new(writer: &'a mut dyn Write) -> Self { + Self { + values: BTreeMap::new(), + writer, + } + } +} + +impl<'a> crate::field::VisitFmt for JsonVisitor<'a> { + fn writer(&mut self) -> &mut dyn fmt::Write { + self.writer + } +} + +impl<'a> crate::field::VisitOutput<fmt::Result> for JsonVisitor<'a> { + fn finish(self) -> fmt::Result { + let inner = || { + let mut serializer = Serializer::new(WriteAdaptor::new(self.writer)); + let mut ser_map = serializer.serialize_map(None)?; + + for (k, v) in self.values { + ser_map.serialize_entry(k, &v)?; + } + + ser_map.end() + }; + + if inner().is_err() { + Err(fmt::Error) + } else { + Ok(()) + } + } +} + +impl<'a> field::Visit for JsonVisitor<'a> { + #[cfg(all(tracing_unstable, feature = "valuable"))] + fn record_value(&mut self, field: &Field, value: valuable_crate::Value<'_>) { + let value = match serde_json::to_value(valuable_serde::Serializable::new(value)) { + Ok(value) => value, + Err(_e) => { + #[cfg(debug_assertions)] + unreachable!( + "`valuable::Valuable` implementations should always serialize \ + successfully, but an error occurred: {}", + _e, + ); + + #[cfg(not(debug_assertions))] + return; + } + }; + + self.values.insert(field.name(), value); + } + + /// Visit a double precision floating point value. + fn record_f64(&mut self, field: &Field, value: f64) { + self.values + .insert(field.name(), serde_json::Value::from(value)); + } + + /// Visit a signed 64-bit integer value. + fn record_i64(&mut self, field: &Field, value: i64) { + self.values + .insert(field.name(), serde_json::Value::from(value)); + } + + /// Visit an unsigned 64-bit integer value. + fn record_u64(&mut self, field: &Field, value: u64) { + self.values + .insert(field.name(), serde_json::Value::from(value)); + } + + /// Visit a boolean value. + fn record_bool(&mut self, field: &Field, value: bool) { + self.values + .insert(field.name(), serde_json::Value::from(value)); + } + + /// Visit a string value. + fn record_str(&mut self, field: &Field, value: &str) { + self.values + .insert(field.name(), serde_json::Value::from(value)); + } + + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + match field.name() { + // Skip fields that are actually log metadata that have already been handled + #[cfg(feature = "tracing-log")] + name if name.starts_with("log.") => (), + name if name.starts_with("r#") => { + self.values + .insert(&name[2..], serde_json::Value::from(format!("{:?}", value))); + } + name => { + self.values + .insert(name, serde_json::Value::from(format!("{:?}", value))); + } + }; + } +} +#[cfg(test)] +mod test { + use super::*; + use crate::fmt::{format::FmtSpan, test::MockMakeWriter, time::FormatTime, SubscriberBuilder}; + use tracing::{self, subscriber::with_default}; + + use std::fmt; + use std::path::Path; + + struct MockTime; + impl FormatTime for MockTime { + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + write!(w, "fake time") + } + } + + fn subscriber() -> SubscriberBuilder<JsonFields, Format<Json>> { + SubscriberBuilder::default().json() + } + + #[test] + fn json() { + let expected = + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; + let subscriber = subscriber() + .flatten_event(false) + .with_current_span(true) + .with_span_list(true); + test_json(expected, subscriber, || { + let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); + let _guard = span.enter(); + tracing::info!("some json test"); + }); + } + + #[test] + fn json_filename() { + let current_path = Path::new("tracing-subscriber") + .join("src") + .join("fmt") + .join("format") + .join("json.rs") + .to_str() + .expect("path must be valid unicode") + // escape windows backslashes + .replace('\\', "\\\\"); + let expected = + &format!("{}{}{}", + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"filename\":\"", + current_path, + "\",\"fields\":{\"message\":\"some json test\"}}\n"); + let subscriber = subscriber() + .flatten_event(false) + .with_current_span(true) + .with_file(true) + .with_span_list(true); + test_json(expected, subscriber, || { + let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); + let _guard = span.enter(); + tracing::info!("some json test"); + }); + } + + #[test] + fn json_line_number() { + let expected = + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"line_number\":42,\"fields\":{\"message\":\"some json test\"}}\n"; + let subscriber = subscriber() + .flatten_event(false) + .with_current_span(true) + .with_line_number(true) + .with_span_list(true); + test_json_with_line_number(expected, subscriber, || { + let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); + let _guard = span.enter(); + tracing::info!("some json test"); + }); + } + + #[test] + fn json_flattened_event() { + let expected = + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"message\":\"some json test\"}\n"; + + let subscriber = subscriber() + .flatten_event(true) + .with_current_span(true) + .with_span_list(true); + test_json(expected, subscriber, || { + let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); + let _guard = span.enter(); + tracing::info!("some json test"); + }); + } + + #[test] + fn json_disabled_current_span_event() { + let expected = + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; + let subscriber = subscriber() + .flatten_event(false) + .with_current_span(false) + .with_span_list(true); + test_json(expected, subscriber, || { + let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); + let _guard = span.enter(); + tracing::info!("some json test"); + }); + } + + #[test] + fn json_disabled_span_list_event() { + let expected = + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; + let subscriber = subscriber() + .flatten_event(false) + .with_current_span(true) + .with_span_list(false); + test_json(expected, subscriber, || { + let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); + let _guard = span.enter(); + tracing::info!("some json test"); + }); + } + + #[test] + fn json_nested_span() { + let expected = + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":43,\"name\":\"nested_json_span\",\"number\":4},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3},{\"answer\":43,\"name\":\"nested_json_span\",\"number\":4}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; + let subscriber = subscriber() + .flatten_event(false) + .with_current_span(true) + .with_span_list(true); + test_json(expected, subscriber, || { + let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); + let _guard = span.enter(); + let span = tracing::span!( + tracing::Level::INFO, + "nested_json_span", + answer = 43, + number = 4 + ); + let _guard = span.enter(); + tracing::info!("some json test"); + }); + } + + #[test] + fn json_no_span() { + let expected = + "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; + let subscriber = subscriber() + .flatten_event(false) + .with_current_span(true) + .with_span_list(true); + test_json(expected, subscriber, || { + tracing::info!("some json test"); + }); + } + + #[test] + fn record_works() { + // This test reproduces issue #707, where using `Span::record` causes + // any events inside the span to be ignored. + + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt() + .json() + .with_writer(make_writer.clone()) + .finish(); + + with_default(subscriber, || { + tracing::info!("an event outside the root span"); + assert_eq!( + parse_as_json(&make_writer)["fields"]["message"], + "an event outside the root span" + ); + + let span = tracing::info_span!("the span", na = tracing::field::Empty); + span.record("na", &"value"); + let _enter = span.enter(); + + tracing::info!("an event inside the root span"); + assert_eq!( + parse_as_json(&make_writer)["fields"]["message"], + "an event inside the root span" + ); + }); + } + + #[test] + fn json_span_event_show_correct_context() { + let buffer = MockMakeWriter::default(); + let subscriber = subscriber() + .with_writer(buffer.clone()) + .flatten_event(false) + .with_current_span(true) + .with_span_list(false) + .with_span_events(FmtSpan::FULL) + .finish(); + + with_default(subscriber, || { + let context = "parent"; + let parent_span = tracing::info_span!("parent_span", context); + + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "new"); + assert_eq!(event["span"]["context"], "parent"); + + let _parent_enter = parent_span.enter(); + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "enter"); + assert_eq!(event["span"]["context"], "parent"); + + let context = "child"; + let child_span = tracing::info_span!("child_span", context); + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "new"); + assert_eq!(event["span"]["context"], "child"); + + let _child_enter = child_span.enter(); + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "enter"); + assert_eq!(event["span"]["context"], "child"); + + drop(_child_enter); + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "exit"); + assert_eq!(event["span"]["context"], "child"); + + drop(child_span); + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "close"); + assert_eq!(event["span"]["context"], "child"); + + drop(_parent_enter); + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "exit"); + assert_eq!(event["span"]["context"], "parent"); + + drop(parent_span); + let event = parse_as_json(&buffer); + assert_eq!(event["fields"]["message"], "close"); + assert_eq!(event["span"]["context"], "parent"); + }); + } + + #[test] + fn json_span_event_with_no_fields() { + // Check span events serialize correctly. + // Discussion: https://github.com/tokio-rs/tracing/issues/829#issuecomment-661984255 + let buffer = MockMakeWriter::default(); + let subscriber = subscriber() + .with_writer(buffer.clone()) + .flatten_event(false) + .with_current_span(false) + .with_span_list(false) + .with_span_events(FmtSpan::FULL) + .finish(); + + with_default(subscriber, || { + let span = tracing::info_span!("valid_json"); + assert_eq!(parse_as_json(&buffer)["fields"]["message"], "new"); + + let _enter = span.enter(); + assert_eq!(parse_as_json(&buffer)["fields"]["message"], "enter"); + + drop(_enter); + assert_eq!(parse_as_json(&buffer)["fields"]["message"], "exit"); + + drop(span); + assert_eq!(parse_as_json(&buffer)["fields"]["message"], "close"); + }); + } + + fn parse_as_json(buffer: &MockMakeWriter) -> serde_json::Value { + let buf = String::from_utf8(buffer.buf().to_vec()).unwrap(); + let json = buf + .lines() + .last() + .expect("expected at least one line to be written!"); + match serde_json::from_str(json) { + Ok(v) => v, + Err(e) => panic!( + "assertion failed: JSON shouldn't be malformed\n error: {}\n json: {}", + e, json + ), + } + } + + fn test_json<T>( + expected: &str, + builder: crate::fmt::SubscriberBuilder<JsonFields, Format<Json>>, + producer: impl FnOnce() -> T, + ) { + let make_writer = MockMakeWriter::default(); + let subscriber = builder + .with_writer(make_writer.clone()) + .with_timer(MockTime) + .finish(); + + with_default(subscriber, producer); + + let buf = make_writer.buf(); + let actual = std::str::from_utf8(&buf[..]).unwrap(); + assert_eq!( + serde_json::from_str::<std::collections::HashMap<&str, serde_json::Value>>(expected) + .unwrap(), + serde_json::from_str(actual).unwrap() + ); + } + + fn test_json_with_line_number<T>( + expected: &str, + builder: crate::fmt::SubscriberBuilder<JsonFields, Format<Json>>, + producer: impl FnOnce() -> T, + ) { + let make_writer = MockMakeWriter::default(); + let subscriber = builder + .with_writer(make_writer.clone()) + .with_timer(MockTime) + .finish(); + + with_default(subscriber, producer); + + let buf = make_writer.buf(); + let actual = std::str::from_utf8(&buf[..]).unwrap(); + let mut expected = + serde_json::from_str::<std::collections::HashMap<&str, serde_json::Value>>(expected) + .unwrap(); + let expect_line_number = expected.remove("line_number").is_some(); + let mut actual: std::collections::HashMap<&str, serde_json::Value> = + serde_json::from_str(actual).unwrap(); + let line_number = actual.remove("line_number"); + if expect_line_number { + assert_eq!(line_number.map(|x| x.is_number()), Some(true)); + } else { + assert!(line_number.is_none()); + } + assert_eq!(actual, expected); + } +} diff --git a/vendor/tracing-subscriber/src/fmt/format/mod.rs b/vendor/tracing-subscriber/src/fmt/format/mod.rs new file mode 100644 index 000000000..ec79ac140 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/format/mod.rs @@ -0,0 +1,2161 @@ +//! Formatters for logging `tracing` events. +//! +//! This module provides several formatter implementations, as well as utilities +//! for implementing custom formatters. +//! +//! # Formatters +//! This module provides a number of formatter implementations: +//! +//! * [`Full`]: The default formatter. This emits human-readable, +//! single-line logs for each event that occurs, with the current span context +//! displayed before the formatted representation of the event. See +//! [here](Full#example-output) for sample output. +//! +//! * [`Compact`]: A variant of the default formatter, optimized for +//! short line lengths. Fields from the current span context are appended to +//! the fields of the formatted event, and span names are not shown; the +//! verbosity level is abbreviated to a single character. See +//! [here](Compact#example-output) for sample output. +//! +//! * [`Pretty`]: Emits excessively pretty, multi-line logs, optimized +//! for human readability. This is primarily intended to be used in local +//! development and debugging, or for command-line applications, where +//! automated analysis and compact storage of logs is less of a priority than +//! readability and visual appeal. See [here](Pretty#example-output) +//! for sample output. +//! +//! * [`Json`]: Outputs newline-delimited JSON logs. This is intended +//! for production use with systems where structured logs are consumed as JSON +//! by analysis and viewing tools. The JSON output is not optimized for human +//! readability. See [here](Json#example-output) for sample output. +use super::time::{FormatTime, SystemTime}; +use crate::{ + field::{MakeOutput, MakeVisitor, RecordFields, VisitFmt, VisitOutput}, + fmt::fmt_layer::FmtContext, + fmt::fmt_layer::FormattedFields, + registry::LookupSpan, +}; + +use std::fmt::{self, Debug, Display, Write}; +use tracing_core::{ + field::{self, Field, Visit}, + span, Event, Level, Subscriber, +}; + +#[cfg(feature = "tracing-log")] +use tracing_log::NormalizeEvent; + +#[cfg(feature = "ansi")] +use ansi_term::{Colour, Style}; + +#[cfg(feature = "json")] +mod json; +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] +pub use json::*; + +#[cfg(feature = "ansi")] +mod pretty; +#[cfg(feature = "ansi")] +#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] +pub use pretty::*; + +/// A type that can format a tracing [`Event`] to a [`Writer`]. +/// +/// `FormatEvent` is primarily used in the context of [`fmt::Subscriber`] or +/// [`fmt::Layer`]. Each time an event is dispatched to [`fmt::Subscriber`] or +/// [`fmt::Layer`], the subscriber or layer +/// forwards it to its associated `FormatEvent` to emit a log message. +/// +/// This trait is already implemented for function pointers with the same +/// signature as `format_event`. +/// +/// # Arguments +/// +/// The following arguments are passed to `FormatEvent::format_event`: +/// +/// * A [`FmtContext`]. This is an extension of the [`layer::Context`] type, +/// which can be used for accessing stored information such as the current +/// span context an event occurred in. +/// +/// In addition, [`FmtContext`] exposes access to the [`FormatFields`] +/// implementation that the subscriber was configured to use via the +/// [`FmtContext::field_format`] method. This can be used when the +/// [`FormatEvent`] implementation needs to format the event's fields. +/// +/// For convenience, [`FmtContext`] also [implements `FormatFields`], +/// forwarding to the configured [`FormatFields`] type. +/// +/// * A [`Writer`] to which the formatted representation of the event is +/// written. This type implements the [`std::fmt::Write`] trait, and therefore +/// can be used with the [`std::write!`] and [`std::writeln!`] macros, as well +/// as calling [`std::fmt::Write`] methods directly. +/// +/// The [`Writer`] type also implements additional methods that provide +/// information about how the event should be formatted. The +/// [`Writer::has_ansi_escapes`] method indicates whether [ANSI terminal +/// escape codes] are supported by the underlying I/O writer that the event +/// will be written to. If this returns `true`, the formatter is permitted to +/// use ANSI escape codes to add colors and other text formatting to its +/// output. If it returns `false`, the event will be written to an output that +/// does not support ANSI escape codes (such as a log file), and they should +/// not be emitted. +/// +/// Crates like [`ansi_term`] and [`owo-colors`] can be used to add ANSI +/// escape codes to formatted output. +/// +/// * The actual [`Event`] to be formatted. +/// +/// # Examples +/// +/// This example re-implements a simiplified version of this crate's [default +/// formatter]: +/// +/// ```rust +/// use std::fmt; +/// use tracing_core::{Subscriber, Event}; +/// use tracing_subscriber::fmt::{ +/// format::{self, FormatEvent, FormatFields}, +/// FmtContext, +/// FormattedFields, +/// }; +/// use tracing_subscriber::registry::LookupSpan; +/// +/// struct MyFormatter; +/// +/// impl<S, N> FormatEvent<S, N> for MyFormatter +/// where +/// S: Subscriber + for<'a> LookupSpan<'a>, +/// N: for<'a> FormatFields<'a> + 'static, +/// { +/// fn format_event( +/// &self, +/// ctx: &FmtContext<'_, S, N>, +/// mut writer: format::Writer<'_>, +/// event: &Event<'_>, +/// ) -> fmt::Result { +/// // Format values from the event's's metadata: +/// let metadata = event.metadata(); +/// write!(&mut writer, "{} {}: ", metadata.level(), metadata.target())?; +/// +/// // Format all the spans in the event's span context. +/// if let Some(scope) = ctx.event_scope() { +/// for span in scope.from_root() { +/// write!(writer, "{}", span.name())?; +/// +/// // `FormattedFields` is a formatted representation of the span's +/// // fields, which is stored in its extensions by the `fmt` layer's +/// // `new_span` method. The fields will have been formatted +/// // by the same field formatter that's provided to the event +/// // formatter in the `FmtContext`. +/// let ext = span.extensions(); +/// let fields = &ext +/// .get::<FormattedFields<N>>() +/// .expect("will never be `None`"); +/// +/// // Skip formatting the fields if the span had no fields. +/// if !fields.is_empty() { +/// write!(writer, "{{{}}}", fields)?; +/// } +/// write!(writer, ": ")?; +/// } +/// } +/// +/// // Write fields on the event +/// ctx.field_format().format_fields(writer.by_ref(), event)?; +/// +/// writeln!(writer) +/// } +/// } +/// +/// let _subscriber = tracing_subscriber::fmt() +/// .event_format(MyFormatter) +/// .init(); +/// +/// let _span = tracing::info_span!("my_span", answer = 42).entered(); +/// tracing::info!(question = "life, the universe, and everything", "hello world"); +/// ``` +/// +/// This formatter will print events like this: +/// +/// ```text +/// DEBUG yak_shaving::shaver: some-span{field-on-span=foo}: started shaving yak +/// ``` +/// +/// [`layer::Context`]: crate::layer::Context +/// [`fmt::Layer`]: super::Layer +/// [`fmt::Subscriber`]: super::Subscriber +/// [`Event`]: tracing::Event +/// [implements `FormatFields`]: super::FmtContext#impl-FormatFields<'writer> +/// [ANSI terminal escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code +/// [`Writer::has_ansi_escapes`]: Writer::has_ansi_escapes +/// [`ansi_term`]: https://crates.io/crates/ansi_term +/// [`owo-colors`]: https://crates.io/crates/owo-colors +/// [default formatter]: Full +pub trait FormatEvent<S, N> +where + S: Subscriber + for<'a> LookupSpan<'a>, + N: for<'a> FormatFields<'a> + 'static, +{ + /// Write a log message for `Event` in `Context` to the given [`Writer`]. + fn format_event( + &self, + ctx: &FmtContext<'_, S, N>, + writer: Writer<'_>, + event: &Event<'_>, + ) -> fmt::Result; +} + +impl<S, N> FormatEvent<S, N> + for fn(ctx: &FmtContext<'_, S, N>, Writer<'_>, &Event<'_>) -> fmt::Result +where + S: Subscriber + for<'a> LookupSpan<'a>, + N: for<'a> FormatFields<'a> + 'static, +{ + fn format_event( + &self, + ctx: &FmtContext<'_, S, N>, + writer: Writer<'_>, + event: &Event<'_>, + ) -> fmt::Result { + (*self)(ctx, writer, event) + } +} +/// A type that can format a [set of fields] to a [`Writer`]. +/// +/// `FormatFields` is primarily used in the context of [`FmtSubscriber`]. Each +/// time a span or event with fields is recorded, the subscriber will format +/// those fields with its associated `FormatFields` implementation. +/// +/// [set of fields]: crate::field::RecordFields +/// [`FmtSubscriber`]: super::Subscriber +pub trait FormatFields<'writer> { + /// Format the provided `fields` to the provided [`Writer`], returning a result. + fn format_fields<R: RecordFields>(&self, writer: Writer<'writer>, fields: R) -> fmt::Result; + + /// Record additional field(s) on an existing span. + /// + /// By default, this appends a space to the current set of fields if it is + /// non-empty, and then calls `self.format_fields`. If different behavior is + /// required, the default implementation of this method can be overridden. + fn add_fields( + &self, + current: &'writer mut FormattedFields<Self>, + fields: &span::Record<'_>, + ) -> fmt::Result { + if !current.fields.is_empty() { + current.fields.push(' '); + } + self.format_fields(current.as_writer(), fields) + } +} + +/// Returns the default configuration for an [event formatter]. +/// +/// Methods on the returned event formatter can be used for further +/// configuration. For example: +/// +/// ```rust +/// let format = tracing_subscriber::fmt::format() +/// .without_time() // Don't include timestamps +/// .with_target(false) // Don't include event targets. +/// .with_level(false) // Don't include event levels. +/// .compact(); // Use a more compact, abbreviated format. +/// +/// // Use the configured formatter when building a new subscriber. +/// tracing_subscriber::fmt() +/// .event_format(format) +/// .init(); +/// ``` +pub fn format() -> Format { + Format::default() +} + +/// Returns the default configuration for a JSON [event formatter]. +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] +pub fn json() -> Format<Json> { + format().json() +} + +/// Returns a [`FormatFields`] implementation that formats fields using the +/// provided function or closure. +/// +pub fn debug_fn<F>(f: F) -> FieldFn<F> +where + F: Fn(&mut Writer<'_>, &Field, &dyn fmt::Debug) -> fmt::Result + Clone, +{ + FieldFn(f) +} + +/// A writer to which formatted representations of spans and events are written. +/// +/// This type is provided as input to the [`FormatEvent::format_event`] and +/// [`FormatFields::format_fields`] methods, which will write formatted +/// representations of [`Event`]s and [fields] to the `Writer`. +/// +/// This type implements the [`std::fmt::Write`] trait, allowing it to be used +/// with any function that takes an instance of [`std::fmt::Write`]. +/// Additionally, it can be used with the standard library's [`std::write!`] and +/// [`std::writeln!`] macros. +/// +/// Additionally, a `Writer` may expose additional `tracing`-specific +/// information to the formatter implementation. +/// +/// [fields]: tracing_core::field +pub struct Writer<'writer> { + writer: &'writer mut dyn fmt::Write, + // TODO(eliza): add ANSI support + is_ansi: bool, +} + +/// A [`FormatFields`] implementation that formats fields by calling a function +/// or closure. +/// +#[derive(Debug, Clone)] +pub struct FieldFn<F>(F); +/// The [visitor] produced by [`FieldFn`]'s [`MakeVisitor`] implementation. +/// +/// [visitor]: super::super::field::Visit +/// [`MakeVisitor`]: super::super::field::MakeVisitor +pub struct FieldFnVisitor<'a, F> { + f: F, + writer: Writer<'a>, + result: fmt::Result, +} +/// Marker for [`Format`] that indicates that the compact log format should be used. +/// +/// The compact format includes fields from all currently entered spans, after +/// the event's fields. Span names are listed in order before fields are +/// displayed. +/// +/// # Example Output +/// +/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt-compact +/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s +/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-compact` +/// <font color="#AAAAAA">2022-02-17T19:51:05.809287Z </font><font color="#4E9A06"> INFO</font> <b>fmt_compact</b><font color="#AAAAAA">: preparing to shave yaks </font><i>number_of_yaks</i><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809367Z </font><font color="#4E9A06"> INFO</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: shaving yaks </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809414Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!" </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=1</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809443Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: yak shaved successfully </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=1</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809477Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks</b>: <b>yak_events</b><font color="#AAAAAA">: </font><i>yak</i><font color="#AAAAAA">=1 </font><i>shaved</i><font color="#AAAAAA">=true </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809500Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: </font><i>yaks_shaved</i><font color="#AAAAAA">=1 </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809531Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!" </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=2</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809554Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: yak shaved successfully </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=2</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809581Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks</b>: <b>yak_events</b><font color="#AAAAAA">: </font><i>yak</i><font color="#AAAAAA">=2 </font><i>shaved</i><font color="#AAAAAA">=true </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809606Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: </font><i>yaks_shaved</i><font color="#AAAAAA">=2 </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809635Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!" </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809664Z </font><font color="#C4A000"> WARN</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: could not locate yak </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809693Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks</b>: <b>yak_events</b><font color="#AAAAAA">: </font><i>yak</i><font color="#AAAAAA">=3 </font><i>shaved</i><font color="#AAAAAA">=false </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809717Z </font><font color="#CC0000">ERROR</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: failed to shave yak </font><i>yak</i><font color="#AAAAAA">=3 </font><i>error</i><font color="#AAAAAA">=missing yak </font><i>error.sources</i><font color="#AAAAAA">=[out of space, out of cash] </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809743Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: </font><i>yaks_shaved</i><font color="#AAAAAA">=2 </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-17T19:51:05.809768Z </font><font color="#4E9A06"> INFO</font> <b>fmt_compact</b><font color="#AAAAAA">: yak shaving completed </font><i>all_yaks_shaved</i><font color="#AAAAAA">=false</font> +/// +/// </pre> +#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)] +pub struct Compact; + +/// Marker for [`Format`] that indicates that the default log format should be used. +/// +/// This formatter shows the span context before printing event data. Spans are +/// displayed including their names and fields. +/// +/// # Example Output +/// +/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt +/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s +/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt` +/// <font color="#AAAAAA">2022-02-15T18:40:14.289898Z </font><font color="#4E9A06"> INFO</font> fmt: preparing to shave yaks <i>number_of_yaks</i><font color="#AAAAAA">=3</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.289974Z </font><font color="#4E9A06"> INFO</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: shaving yaks</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290011Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=1</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!"</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290038Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=1</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: yak shaved successfully</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290070Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: yak_events: </font><i>yak</i><font color="#AAAAAA">=1 </font><i>shaved</i><font color="#AAAAAA">=true</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290089Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: </font><i>yaks_shaved</i><font color="#AAAAAA">=1</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290114Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=2</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!"</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290134Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=2</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: yak shaved successfully</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290157Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: yak_events: </font><i>yak</i><font color="#AAAAAA">=2 </font><i>shaved</i><font color="#AAAAAA">=true</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290174Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: </font><i>yaks_shaved</i><font color="#AAAAAA">=2</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290198Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!"</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290222Z </font><font color="#C4A000"> WARN</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: could not locate yak</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290247Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: yak_events: </font><i>yak</i><font color="#AAAAAA">=3 </font><i>shaved</i><font color="#AAAAAA">=false</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290268Z </font><font color="#CC0000">ERROR</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: failed to shave yak </font><i>yak</i><font color="#AAAAAA">=3 </font><i>error</i><font color="#AAAAAA">=missing yak </font><i>error.sources</i><font color="#AAAAAA">=[out of space, out of cash]</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290287Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: </font><i>yaks_shaved</i><font color="#AAAAAA">=2</font> +/// <font color="#AAAAAA">2022-02-15T18:40:14.290309Z </font><font color="#4E9A06"> INFO</font> fmt: yak shaving completed. <i>all_yaks_shaved</i><font color="#AAAAAA">=false</font> +/// </pre> +#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)] +pub struct Full; + +/// A pre-configured event formatter. +/// +/// You will usually want to use this as the `FormatEvent` for a `FmtSubscriber`. +/// +/// The default logging format, [`Full`] includes all fields in each event and its containing +/// spans. The [`Compact`] logging format is intended to produce shorter log +/// lines; it displays each event's fields, along with fields from the current +/// span context, but other information is abbreviated. The [`Pretty`] logging +/// format is an extra-verbose, multi-line human-readable logging format +/// intended for use in development. +#[derive(Debug, Clone)] +pub struct Format<F = Full, T = SystemTime> { + format: F, + pub(crate) timer: T, + pub(crate) ansi: Option<bool>, + pub(crate) display_timestamp: bool, + pub(crate) display_target: bool, + pub(crate) display_level: bool, + pub(crate) display_thread_id: bool, + pub(crate) display_thread_name: bool, + pub(crate) display_filename: bool, + pub(crate) display_line_number: bool, +} + +// === impl Writer === + +impl<'writer> Writer<'writer> { + // TODO(eliza): consider making this a public API? + // We may not want to do that if we choose to expose specialized + // constructors instead (e.g. `from_string` that stores whether the string + // is empty...?) + pub(crate) fn new(writer: &'writer mut impl fmt::Write) -> Self { + Self { + writer: writer as &mut dyn fmt::Write, + is_ansi: false, + } + } + + // TODO(eliza): consider making this a public API? + pub(crate) fn with_ansi(self, is_ansi: bool) -> Self { + Self { is_ansi, ..self } + } + + /// Return a new `Writer` that mutably borrows `self`. + /// + /// This can be used to temporarily borrow a `Writer` to pass a new `Writer` + /// to a function that takes a `Writer` by value, allowing the original writer + /// to still be used once that function returns. + pub fn by_ref(&mut self) -> Writer<'_> { + let is_ansi = self.is_ansi; + Writer { + writer: self as &mut dyn fmt::Write, + is_ansi, + } + } + + /// Writes a string slice into this `Writer`, returning whether the write succeeded. + /// + /// This method can only succeed if the entire string slice was successfully + /// written, and this method will not return until all data has been written + /// or an error occurs. + /// + /// This is identical to calling the [`write_str` method] from the `Writer`'s + /// [`std::fmt::Write`] implementation. However, it is also provided as an + /// inherent method, so that `Writer`s can be used without needing to import the + /// [`std::fmt::Write`] trait. + /// + /// # Errors + /// + /// This function will return an instance of [`std::fmt::Error`] on error. + /// + /// [`write_str` method]: std::fmt::Write::write_str + #[inline] + pub fn write_str(&mut self, s: &str) -> fmt::Result { + self.writer.write_str(s) + } + + /// Writes a [`char`] into this writer, returning whether the write succeeded. + /// + /// A single [`char`] may be encoded as more than one byte. + /// This method can only succeed if the entire byte sequence was successfully + /// written, and this method will not return until all data has been + /// written or an error occurs. + /// + /// This is identical to calling the [`write_char` method] from the `Writer`'s + /// [`std::fmt::Write`] implementation. However, it is also provided as an + /// inherent method, so that `Writer`s can be used without needing to import the + /// [`std::fmt::Write`] trait. + /// + /// # Errors + /// + /// This function will return an instance of [`std::fmt::Error`] on error. + /// + /// [`write_char` method]: std::fmt::Write::write_char + #[inline] + pub fn write_char(&mut self, c: char) -> fmt::Result { + self.writer.write_char(c) + } + + /// Glue for usage of the [`write!`] macro with `Writer`s. + /// + /// This method should generally not be invoked manually, but rather through + /// the [`write!`] macro itself. + /// + /// This is identical to calling the [`write_fmt` method] from the `Writer`'s + /// [`std::fmt::Write`] implementation. However, it is also provided as an + /// inherent method, so that `Writer`s can be used with the [`write!` macro] + /// without needing to import the + /// [`std::fmt::Write`] trait. + /// + /// [`write_fmt` method]: std::fmt::Write::write_fmt + pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { + self.writer.write_fmt(args) + } + + /// Returns `true` if [ANSI escape codes] may be used to add colors + /// and other formatting when writing to this `Writer`. + /// + /// If this returns `false`, formatters should not emit ANSI escape codes. + /// + /// [ANSI escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code + pub fn has_ansi_escapes(&self) -> bool { + self.is_ansi + } + + pub(in crate::fmt::format) fn bold(&self) -> Style { + #[cfg(feature = "ansi")] + { + if self.is_ansi { + return Style::new().bold(); + } + } + + Style::new() + } + + pub(in crate::fmt::format) fn dimmed(&self) -> Style { + #[cfg(feature = "ansi")] + { + if self.is_ansi { + return Style::new().dimmed(); + } + } + + Style::new() + } + + pub(in crate::fmt::format) fn italic(&self) -> Style { + #[cfg(feature = "ansi")] + { + if self.is_ansi { + return Style::new().italic(); + } + } + + Style::new() + } +} + +impl fmt::Write for Writer<'_> { + #[inline] + fn write_str(&mut self, s: &str) -> fmt::Result { + Writer::write_str(self, s) + } + + #[inline] + fn write_char(&mut self, c: char) -> fmt::Result { + Writer::write_char(self, c) + } + + #[inline] + fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { + Writer::write_fmt(self, args) + } +} + +impl fmt::Debug for Writer<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Writer") + .field("writer", &format_args!("<&mut dyn fmt::Write>")) + .field("is_ansi", &self.is_ansi) + .finish() + } +} + +// === impl Format === + +impl Default for Format<Full, SystemTime> { + fn default() -> Self { + Format { + format: Full, + timer: SystemTime, + ansi: None, + display_timestamp: true, + display_target: true, + display_level: true, + display_thread_id: false, + display_thread_name: false, + display_filename: false, + display_line_number: false, + } + } +} + +impl<F, T> Format<F, T> { + /// Use a less verbose output format. + /// + /// See [`Compact`]. + pub fn compact(self) -> Format<Compact, T> { + Format { + format: Compact, + timer: self.timer, + ansi: self.ansi, + display_target: self.display_target, + display_timestamp: self.display_timestamp, + display_level: self.display_level, + display_thread_id: self.display_thread_id, + display_thread_name: self.display_thread_name, + display_filename: self.display_filename, + display_line_number: self.display_line_number, + } + } + + /// Use an excessively pretty, human-readable output format. + /// + /// See [`Pretty`]. + /// + /// Note that this requires the "ansi" feature to be enabled. + /// + /// # Options + /// + /// [`Format::with_ansi`] can be used to disable ANSI terminal escape codes (which enable + /// formatting such as colors, bold, italic, etc) in event formatting. However, a field + /// formatter must be manually provided to avoid ANSI in the formatting of parent spans, like + /// so: + /// + /// ``` + /// # use tracing_subscriber::fmt::format; + /// tracing_subscriber::fmt() + /// .pretty() + /// .with_ansi(false) + /// .fmt_fields(format::PrettyFields::new().with_ansi(false)) + /// // ... other settings ... + /// .init(); + /// ``` + #[cfg(feature = "ansi")] + #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] + pub fn pretty(self) -> Format<Pretty, T> { + Format { + format: Pretty::default(), + timer: self.timer, + ansi: self.ansi, + display_target: self.display_target, + display_timestamp: self.display_timestamp, + display_level: self.display_level, + display_thread_id: self.display_thread_id, + display_thread_name: self.display_thread_name, + display_filename: true, + display_line_number: true, + } + } + + /// Use the full JSON format. + /// + /// The full format includes fields from all entered spans. + /// + /// # Example Output + /// + /// ```ignore,json + /// {"timestamp":"Feb 20 11:28:15.096","level":"INFO","target":"mycrate","fields":{"message":"some message", "key": "value"}} + /// ``` + /// + /// # Options + /// + /// - [`Format::flatten_event`] can be used to enable flattening event fields into the root + /// object. + #[cfg(feature = "json")] + #[cfg_attr(docsrs, doc(cfg(feature = "json")))] + pub fn json(self) -> Format<Json, T> { + Format { + format: Json::default(), + timer: self.timer, + ansi: self.ansi, + display_target: self.display_target, + display_timestamp: self.display_timestamp, + display_level: self.display_level, + display_thread_id: self.display_thread_id, + display_thread_name: self.display_thread_name, + display_filename: self.display_filename, + display_line_number: self.display_line_number, + } + } + + /// Use the given [`timer`] for log message timestamps. + /// + /// See [`time` module] for the provided timer implementations. + /// + /// Note that using the `"time"` feature flag enables the + /// additional time formatters [`UtcTime`] and [`LocalTime`], which use the + /// [`time` crate] to provide more sophisticated timestamp formatting + /// options. + /// + /// [`timer`]: super::time::FormatTime + /// [`time` module]: mod@super::time + /// [`UtcTime`]: super::time::UtcTime + /// [`LocalTime`]: super::time::LocalTime + /// [`time` crate]: https://docs.rs/time/0.3 + pub fn with_timer<T2>(self, timer: T2) -> Format<F, T2> { + Format { + format: self.format, + timer, + ansi: self.ansi, + display_target: self.display_target, + display_timestamp: self.display_timestamp, + display_level: self.display_level, + display_thread_id: self.display_thread_id, + display_thread_name: self.display_thread_name, + display_filename: self.display_filename, + display_line_number: self.display_line_number, + } + } + + /// Do not emit timestamps with log messages. + pub fn without_time(self) -> Format<F, ()> { + Format { + format: self.format, + timer: (), + ansi: self.ansi, + display_timestamp: false, + display_target: self.display_target, + display_level: self.display_level, + display_thread_id: self.display_thread_id, + display_thread_name: self.display_thread_name, + display_filename: self.display_filename, + display_line_number: self.display_line_number, + } + } + + /// Enable ANSI terminal colors for formatted output. + pub fn with_ansi(self, ansi: bool) -> Format<F, T> { + Format { + ansi: Some(ansi), + ..self + } + } + + /// Sets whether or not an event's target is displayed. + pub fn with_target(self, display_target: bool) -> Format<F, T> { + Format { + display_target, + ..self + } + } + + /// Sets whether or not an event's level is displayed. + pub fn with_level(self, display_level: bool) -> Format<F, T> { + Format { + display_level, + ..self + } + } + + /// Sets whether or not the [thread ID] of the current thread is displayed + /// when formatting events. + /// + /// [thread ID]: std::thread::ThreadId + pub fn with_thread_ids(self, display_thread_id: bool) -> Format<F, T> { + Format { + display_thread_id, + ..self + } + } + + /// Sets whether or not the [name] of the current thread is displayed + /// when formatting events. + /// + /// [name]: std::thread#naming-threads + pub fn with_thread_names(self, display_thread_name: bool) -> Format<F, T> { + Format { + display_thread_name, + ..self + } + } + + /// Sets whether or not an event's [source code file path][file] is + /// displayed. + /// + /// [file]: tracing_core::Metadata::file + pub fn with_file(self, display_filename: bool) -> Format<F, T> { + Format { + display_filename, + ..self + } + } + + /// Sets whether or not an event's [source code line number][line] is + /// displayed. + /// + /// [line]: tracing_core::Metadata::line + pub fn with_line_number(self, display_line_number: bool) -> Format<F, T> { + Format { + display_line_number, + ..self + } + } + + /// Sets whether or not the source code location from which an event + /// originated is displayed. + /// + /// This is equivalent to calling [`Format::with_file`] and + /// [`Format::with_line_number`] with the same value. + pub fn with_source_location(self, display_location: bool) -> Self { + self.with_line_number(display_location) + .with_file(display_location) + } + + #[inline] + fn format_timestamp(&self, writer: &mut Writer<'_>) -> fmt::Result + where + T: FormatTime, + { + // If timestamps are disabled, do nothing. + if !self.display_timestamp { + return Ok(()); + } + + // If ANSI color codes are enabled, format the timestamp with ANSI + // colors. + #[cfg(feature = "ansi")] + { + if writer.has_ansi_escapes() { + let style = Style::new().dimmed(); + write!(writer, "{}", style.prefix())?; + + // If getting the timestamp failed, don't bail --- only bail on + // formatting errors. + if self.timer.format_time(writer).is_err() { + writer.write_str("<unknown time>")?; + } + + write!(writer, "{} ", style.suffix())?; + return Ok(()); + } + } + + // Otherwise, just format the timestamp without ANSI formatting. + // If getting the timestamp failed, don't bail --- only bail on + // formatting errors. + if self.timer.format_time(writer).is_err() { + writer.write_str("<unknown time>")?; + } + writer.write_char(' ') + } +} + +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] +impl<T> Format<Json, T> { + /// Use the full JSON format with the event's event fields flattened. + /// + /// # Example Output + /// + /// ```ignore,json + /// {"timestamp":"Feb 20 11:28:15.096","level":"INFO","target":"mycrate", "message":"some message", "key": "value"} + /// ``` + /// See [`Json`][super::format::Json]. + #[cfg(feature = "json")] + #[cfg_attr(docsrs, doc(cfg(feature = "json")))] + pub fn flatten_event(mut self, flatten_event: bool) -> Format<Json, T> { + self.format.flatten_event(flatten_event); + self + } + + /// Sets whether or not the formatter will include the current span in + /// formatted events. + /// + /// See [`format::Json`][Json] + #[cfg(feature = "json")] + #[cfg_attr(docsrs, doc(cfg(feature = "json")))] + pub fn with_current_span(mut self, display_current_span: bool) -> Format<Json, T> { + self.format.with_current_span(display_current_span); + self + } + + /// Sets whether or not the formatter will include a list (from root to + /// leaf) of all currently entered spans in formatted events. + /// + /// See [`format::Json`][Json] + #[cfg(feature = "json")] + #[cfg_attr(docsrs, doc(cfg(feature = "json")))] + pub fn with_span_list(mut self, display_span_list: bool) -> Format<Json, T> { + self.format.with_span_list(display_span_list); + self + } +} + +impl<S, N, T> FormatEvent<S, N> for Format<Full, T> +where + S: Subscriber + for<'a> LookupSpan<'a>, + N: for<'a> FormatFields<'a> + 'static, + T: FormatTime, +{ + fn format_event( + &self, + ctx: &FmtContext<'_, S, N>, + mut writer: Writer<'_>, + event: &Event<'_>, + ) -> fmt::Result { + #[cfg(feature = "tracing-log")] + let normalized_meta = event.normalized_metadata(); + #[cfg(feature = "tracing-log")] + let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); + #[cfg(not(feature = "tracing-log"))] + let meta = event.metadata(); + + // if the `Format` struct *also* has an ANSI color configuration, + // override the writer...the API for configuring ANSI color codes on the + // `Format` struct is deprecated, but we still need to honor those + // configurations. + if let Some(ansi) = self.ansi { + writer = writer.with_ansi(ansi); + } + + self.format_timestamp(&mut writer)?; + + if self.display_level { + let fmt_level = { + #[cfg(feature = "ansi")] + { + FmtLevel::new(meta.level(), writer.has_ansi_escapes()) + } + #[cfg(not(feature = "ansi"))] + { + FmtLevel::new(meta.level()) + } + }; + write!(writer, "{} ", fmt_level)?; + } + + if self.display_thread_name { + let current_thread = std::thread::current(); + match current_thread.name() { + Some(name) => { + write!(writer, "{} ", FmtThreadName::new(name))?; + } + // fall-back to thread id when name is absent and ids are not enabled + None if !self.display_thread_id => { + write!(writer, "{:0>2?} ", current_thread.id())?; + } + _ => {} + } + } + + if self.display_thread_id { + write!(writer, "{:0>2?} ", std::thread::current().id())?; + } + + let dimmed = writer.dimmed(); + + if let Some(scope) = ctx.event_scope() { + let bold = writer.bold(); + + let mut seen = false; + + for span in scope.from_root() { + write!(writer, "{}", bold.paint(span.metadata().name()))?; + seen = true; + + let ext = span.extensions(); + if let Some(fields) = &ext.get::<FormattedFields<N>>() { + if !fields.is_empty() { + write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?; + } + } + write!(writer, "{}", dimmed.paint(":"))?; + } + + if seen { + writer.write_char(' ')?; + } + }; + + if self.display_target { + write!( + writer, + "{}{} ", + dimmed.paint(meta.target()), + dimmed.paint(":") + )?; + } + + let line_number = if self.display_line_number { + meta.line() + } else { + None + }; + + if self.display_filename { + if let Some(filename) = meta.file() { + write!( + writer, + "{}{}{}", + dimmed.paint(filename), + dimmed.paint(":"), + if line_number.is_some() { "" } else { " " } + )?; + } + } + + if let Some(line_number) = line_number { + write!( + writer, + "{}{}:{} ", + dimmed.prefix(), + line_number, + dimmed.suffix() + )?; + } + + ctx.format_fields(writer.by_ref(), event)?; + writeln!(writer) + } +} + +impl<S, N, T> FormatEvent<S, N> for Format<Compact, T> +where + S: Subscriber + for<'a> LookupSpan<'a>, + N: for<'a> FormatFields<'a> + 'static, + T: FormatTime, +{ + fn format_event( + &self, + ctx: &FmtContext<'_, S, N>, + mut writer: Writer<'_>, + event: &Event<'_>, + ) -> fmt::Result { + #[cfg(feature = "tracing-log")] + let normalized_meta = event.normalized_metadata(); + #[cfg(feature = "tracing-log")] + let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); + #[cfg(not(feature = "tracing-log"))] + let meta = event.metadata(); + + // if the `Format` struct *also* has an ANSI color configuration, + // override the writer...the API for configuring ANSI color codes on the + // `Format` struct is deprecated, but we still need to honor those + // configurations. + if let Some(ansi) = self.ansi { + writer = writer.with_ansi(ansi); + } + + self.format_timestamp(&mut writer)?; + + if self.display_level { + let fmt_level = { + #[cfg(feature = "ansi")] + { + FmtLevel::new(meta.level(), writer.has_ansi_escapes()) + } + #[cfg(not(feature = "ansi"))] + { + FmtLevel::new(meta.level()) + } + }; + write!(writer, "{} ", fmt_level)?; + } + + if self.display_thread_name { + let current_thread = std::thread::current(); + match current_thread.name() { + Some(name) => { + write!(writer, "{} ", FmtThreadName::new(name))?; + } + // fall-back to thread id when name is absent and ids are not enabled + None if !self.display_thread_id => { + write!(writer, "{:0>2?} ", current_thread.id())?; + } + _ => {} + } + } + + if self.display_thread_id { + write!(writer, "{:0>2?} ", std::thread::current().id())?; + } + + let fmt_ctx = { + #[cfg(feature = "ansi")] + { + FmtCtx::new(ctx, event.parent(), writer.has_ansi_escapes()) + } + #[cfg(not(feature = "ansi"))] + { + FmtCtx::new(&ctx, event.parent()) + } + }; + write!(writer, "{}", fmt_ctx)?; + + let bold = writer.bold(); + let dimmed = writer.dimmed(); + + let mut needs_space = false; + if self.display_target { + write!(writer, "{}{}", bold.paint(meta.target()), dimmed.paint(":"))?; + needs_space = true; + } + + if self.display_filename { + if let Some(filename) = meta.file() { + if self.display_target { + writer.write_char(' ')?; + } + write!(writer, "{}{}", bold.paint(filename), dimmed.paint(":"))?; + needs_space = true; + } + } + + if self.display_line_number { + if let Some(line_number) = meta.line() { + write!( + writer, + "{}{}{}{}", + bold.prefix(), + line_number, + bold.suffix(), + dimmed.paint(":") + )?; + needs_space = true; + } + } + + if needs_space { + writer.write_char(' ')?; + } + + ctx.format_fields(writer.by_ref(), event)?; + + for span in ctx + .event_scope() + .into_iter() + .flat_map(crate::registry::Scope::from_root) + { + let exts = span.extensions(); + if let Some(fields) = exts.get::<FormattedFields<N>>() { + if !fields.is_empty() { + write!(writer, " {}", dimmed.paint(&fields.fields))?; + } + } + } + writeln!(writer) + } +} + +// === impl FormatFields === +impl<'writer, M> FormatFields<'writer> for M +where + M: MakeOutput<Writer<'writer>, fmt::Result>, + M::Visitor: VisitFmt + VisitOutput<fmt::Result>, +{ + fn format_fields<R: RecordFields>(&self, writer: Writer<'writer>, fields: R) -> fmt::Result { + let mut v = self.make_visitor(writer); + fields.record(&mut v); + v.finish() + } +} + +/// The default [`FormatFields`] implementation. +/// +#[derive(Debug)] +pub struct DefaultFields { + // reserve the ability to add fields to this without causing a breaking + // change in the future. + _private: (), +} + +/// The [visitor] produced by [`DefaultFields`]'s [`MakeVisitor`] implementation. +/// +/// [visitor]: super::super::field::Visit +/// [`MakeVisitor`]: super::super::field::MakeVisitor +#[derive(Debug)] +pub struct DefaultVisitor<'a> { + writer: Writer<'a>, + is_empty: bool, + result: fmt::Result, +} + +impl DefaultFields { + /// Returns a new default [`FormatFields`] implementation. + /// + pub fn new() -> Self { + Self { _private: () } + } +} + +impl Default for DefaultFields { + fn default() -> Self { + Self::new() + } +} + +impl<'a> MakeVisitor<Writer<'a>> for DefaultFields { + type Visitor = DefaultVisitor<'a>; + + #[inline] + fn make_visitor(&self, target: Writer<'a>) -> Self::Visitor { + DefaultVisitor::new(target, true) + } +} + +// === impl DefaultVisitor === + +impl<'a> DefaultVisitor<'a> { + /// Returns a new default visitor that formats to the provided `writer`. + /// + /// # Arguments + /// - `writer`: the writer to format to. + /// - `is_empty`: whether or not any fields have been previously written to + /// that writer. + pub fn new(writer: Writer<'a>, is_empty: bool) -> Self { + Self { + writer, + is_empty, + result: Ok(()), + } + } + + fn maybe_pad(&mut self) { + if self.is_empty { + self.is_empty = false; + } else { + self.result = write!(self.writer, " "); + } + } +} + +impl<'a> field::Visit for DefaultVisitor<'a> { + fn record_str(&mut self, field: &Field, value: &str) { + if self.result.is_err() { + return; + } + + if field.name() == "message" { + self.record_debug(field, &format_args!("{}", value)) + } else { + self.record_debug(field, &value) + } + } + + fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { + if let Some(source) = value.source() { + let italic = self.writer.italic(); + self.record_debug( + field, + &format_args!( + "{} {}{}{}{}", + value, + italic.paint(field.name()), + italic.paint(".sources"), + self.writer.dimmed().paint("="), + ErrorSourceList(source) + ), + ) + } else { + self.record_debug(field, &format_args!("{}", value)) + } + } + + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + if self.result.is_err() { + return; + } + + self.maybe_pad(); + self.result = match field.name() { + "message" => write!(self.writer, "{:?}", value), + // Skip fields that are actually log metadata that have already been handled + #[cfg(feature = "tracing-log")] + name if name.starts_with("log.") => Ok(()), + name if name.starts_with("r#") => write!( + self.writer, + "{}{}{:?}", + self.writer.italic().paint(&name[2..]), + self.writer.dimmed().paint("="), + value + ), + name => write!( + self.writer, + "{}{}{:?}", + self.writer.italic().paint(name), + self.writer.dimmed().paint("="), + value + ), + }; + } +} + +impl<'a> crate::field::VisitOutput<fmt::Result> for DefaultVisitor<'a> { + fn finish(self) -> fmt::Result { + self.result + } +} + +impl<'a> crate::field::VisitFmt for DefaultVisitor<'a> { + fn writer(&mut self) -> &mut dyn fmt::Write { + &mut self.writer + } +} + +/// Renders an error into a list of sources, *including* the error +struct ErrorSourceList<'a>(&'a (dyn std::error::Error + 'static)); + +impl<'a> Display for ErrorSourceList<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut list = f.debug_list(); + let mut curr = Some(self.0); + while let Some(curr_err) = curr { + list.entry(&format_args!("{}", curr_err)); + curr = curr_err.source(); + } + list.finish() + } +} + +struct FmtCtx<'a, S, N> { + ctx: &'a FmtContext<'a, S, N>, + span: Option<&'a span::Id>, + #[cfg(feature = "ansi")] + ansi: bool, +} + +impl<'a, S, N: 'a> FmtCtx<'a, S, N> +where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static, +{ + #[cfg(feature = "ansi")] + pub(crate) fn new( + ctx: &'a FmtContext<'_, S, N>, + span: Option<&'a span::Id>, + ansi: bool, + ) -> Self { + Self { ctx, span, ansi } + } + + #[cfg(not(feature = "ansi"))] + pub(crate) fn new(ctx: &'a FmtContext<'_, S, N>, span: Option<&'a span::Id>) -> Self { + Self { ctx, span } + } + + fn bold(&self) -> Style { + #[cfg(feature = "ansi")] + { + if self.ansi { + return Style::new().bold(); + } + } + + Style::new() + } +} + +impl<'a, S, N: 'a> fmt::Display for FmtCtx<'a, S, N> +where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + N: for<'writer> FormatFields<'writer> + 'static, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let bold = self.bold(); + let mut seen = false; + + let span = self + .span + .and_then(|id| self.ctx.ctx.span(id)) + .or_else(|| self.ctx.ctx.lookup_current()); + + let scope = span.into_iter().flat_map(|span| span.scope().from_root()); + + for span in scope { + seen = true; + write!(f, "{}:", bold.paint(span.metadata().name()))?; + } + + if seen { + f.write_char(' ')?; + } + Ok(()) + } +} + +#[cfg(not(feature = "ansi"))] +struct Style; + +#[cfg(not(feature = "ansi"))] +impl Style { + fn new() -> Self { + Style + } + + fn bold(self) -> Self { + self + } + + fn paint(&self, d: impl fmt::Display) -> impl fmt::Display { + d + } + + fn prefix(&self) -> impl fmt::Display { + "" + } + + fn suffix(&self) -> impl fmt::Display { + "" + } +} + +struct FmtThreadName<'a> { + name: &'a str, +} + +impl<'a> FmtThreadName<'a> { + pub(crate) fn new(name: &'a str) -> Self { + Self { name } + } +} + +impl<'a> fmt::Display for FmtThreadName<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use std::sync::atomic::{ + AtomicUsize, + Ordering::{AcqRel, Acquire, Relaxed}, + }; + + // Track the longest thread name length we've seen so far in an atomic, + // so that it can be updated by any thread. + static MAX_LEN: AtomicUsize = AtomicUsize::new(0); + let len = self.name.len(); + // Snapshot the current max thread name length. + let mut max_len = MAX_LEN.load(Relaxed); + + while len > max_len { + // Try to set a new max length, if it is still the value we took a + // snapshot of. + match MAX_LEN.compare_exchange(max_len, len, AcqRel, Acquire) { + // We successfully set the new max value + Ok(_) => break, + // Another thread set a new max value since we last observed + // it! It's possible that the new length is actually longer than + // ours, so we'll loop again and check whether our length is + // still the longest. If not, we'll just use the newer value. + Err(actual) => max_len = actual, + } + } + + // pad thread name using `max_len` + write!(f, "{:>width$}", self.name, width = max_len) + } +} + +struct FmtLevel<'a> { + level: &'a Level, + #[cfg(feature = "ansi")] + ansi: bool, +} + +impl<'a> FmtLevel<'a> { + #[cfg(feature = "ansi")] + pub(crate) fn new(level: &'a Level, ansi: bool) -> Self { + Self { level, ansi } + } + + #[cfg(not(feature = "ansi"))] + pub(crate) fn new(level: &'a Level) -> Self { + Self { level } + } +} + +const TRACE_STR: &str = "TRACE"; +const DEBUG_STR: &str = "DEBUG"; +const INFO_STR: &str = " INFO"; +const WARN_STR: &str = " WARN"; +const ERROR_STR: &str = "ERROR"; + +#[cfg(not(feature = "ansi"))] +impl<'a> fmt::Display for FmtLevel<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self.level { + Level::TRACE => f.pad(TRACE_STR), + Level::DEBUG => f.pad(DEBUG_STR), + Level::INFO => f.pad(INFO_STR), + Level::WARN => f.pad(WARN_STR), + Level::ERROR => f.pad(ERROR_STR), + } + } +} + +#[cfg(feature = "ansi")] +impl<'a> fmt::Display for FmtLevel<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.ansi { + match *self.level { + Level::TRACE => write!(f, "{}", Colour::Purple.paint(TRACE_STR)), + Level::DEBUG => write!(f, "{}", Colour::Blue.paint(DEBUG_STR)), + Level::INFO => write!(f, "{}", Colour::Green.paint(INFO_STR)), + Level::WARN => write!(f, "{}", Colour::Yellow.paint(WARN_STR)), + Level::ERROR => write!(f, "{}", Colour::Red.paint(ERROR_STR)), + } + } else { + match *self.level { + Level::TRACE => f.pad(TRACE_STR), + Level::DEBUG => f.pad(DEBUG_STR), + Level::INFO => f.pad(INFO_STR), + Level::WARN => f.pad(WARN_STR), + Level::ERROR => f.pad(ERROR_STR), + } + } + } +} + +// === impl FieldFn === + +impl<'a, F> MakeVisitor<Writer<'a>> for FieldFn<F> +where + F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result + Clone, +{ + type Visitor = FieldFnVisitor<'a, F>; + + fn make_visitor(&self, writer: Writer<'a>) -> Self::Visitor { + FieldFnVisitor { + writer, + f: self.0.clone(), + result: Ok(()), + } + } +} + +impl<'a, F> Visit for FieldFnVisitor<'a, F> +where + F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result, +{ + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + if self.result.is_ok() { + self.result = (self.f)(&mut self.writer, field, value) + } + } +} + +impl<'a, F> VisitOutput<fmt::Result> for FieldFnVisitor<'a, F> +where + F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result, +{ + fn finish(self) -> fmt::Result { + self.result + } +} + +impl<'a, F> VisitFmt for FieldFnVisitor<'a, F> +where + F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result, +{ + fn writer(&mut self) -> &mut dyn fmt::Write { + &mut self.writer + } +} + +impl<'a, F> fmt::Debug for FieldFnVisitor<'a, F> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FieldFnVisitor") + .field("f", &format_args!("{}", std::any::type_name::<F>())) + .field("writer", &self.writer) + .field("result", &self.result) + .finish() + } +} + +// === printing synthetic Span events === + +/// Configures what points in the span lifecycle are logged as events. +/// +/// See also [`with_span_events`](super::SubscriberBuilder.html::with_span_events). +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)] +pub struct FmtSpan(u8); + +impl FmtSpan { + /// one event when span is created + pub const NEW: FmtSpan = FmtSpan(1 << 0); + /// one event per enter of a span + pub const ENTER: FmtSpan = FmtSpan(1 << 1); + /// one event per exit of a span + pub const EXIT: FmtSpan = FmtSpan(1 << 2); + /// one event when the span is dropped + pub const CLOSE: FmtSpan = FmtSpan(1 << 3); + + /// spans are ignored (this is the default) + pub const NONE: FmtSpan = FmtSpan(0); + /// one event per enter/exit of a span + pub const ACTIVE: FmtSpan = FmtSpan(FmtSpan::ENTER.0 | FmtSpan::EXIT.0); + /// events at all points (new, enter, exit, drop) + pub const FULL: FmtSpan = + FmtSpan(FmtSpan::NEW.0 | FmtSpan::ENTER.0 | FmtSpan::EXIT.0 | FmtSpan::CLOSE.0); + + /// Check whether or not a certain flag is set for this [`FmtSpan`] + fn contains(&self, other: FmtSpan) -> bool { + self.clone() & other.clone() == other + } +} + +macro_rules! impl_fmt_span_bit_op { + ($trait:ident, $func:ident, $op:tt) => { + impl std::ops::$trait for FmtSpan { + type Output = FmtSpan; + + fn $func(self, rhs: Self) -> Self::Output { + FmtSpan(self.0 $op rhs.0) + } + } + }; +} + +macro_rules! impl_fmt_span_bit_assign_op { + ($trait:ident, $func:ident, $op:tt) => { + impl std::ops::$trait for FmtSpan { + fn $func(&mut self, rhs: Self) { + *self = FmtSpan(self.0 $op rhs.0) + } + } + }; +} + +impl_fmt_span_bit_op!(BitAnd, bitand, &); +impl_fmt_span_bit_op!(BitOr, bitor, |); +impl_fmt_span_bit_op!(BitXor, bitxor, ^); + +impl_fmt_span_bit_assign_op!(BitAndAssign, bitand_assign, &); +impl_fmt_span_bit_assign_op!(BitOrAssign, bitor_assign, |); +impl_fmt_span_bit_assign_op!(BitXorAssign, bitxor_assign, ^); + +impl Debug for FmtSpan { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut wrote_flag = false; + let mut write_flags = |flag, flag_str| -> fmt::Result { + if self.contains(flag) { + if wrote_flag { + f.write_str(" | ")?; + } + + f.write_str(flag_str)?; + wrote_flag = true; + } + + Ok(()) + }; + + if FmtSpan::NONE | self.clone() == FmtSpan::NONE { + f.write_str("FmtSpan::NONE")?; + } else { + write_flags(FmtSpan::NEW, "FmtSpan::NEW")?; + write_flags(FmtSpan::ENTER, "FmtSpan::ENTER")?; + write_flags(FmtSpan::EXIT, "FmtSpan::EXIT")?; + write_flags(FmtSpan::CLOSE, "FmtSpan::CLOSE")?; + } + + Ok(()) + } +} + +pub(super) struct FmtSpanConfig { + pub(super) kind: FmtSpan, + pub(super) fmt_timing: bool, +} + +impl FmtSpanConfig { + pub(super) fn without_time(self) -> Self { + Self { + kind: self.kind, + fmt_timing: false, + } + } + pub(super) fn with_kind(self, kind: FmtSpan) -> Self { + Self { + kind, + fmt_timing: self.fmt_timing, + } + } + pub(super) fn trace_new(&self) -> bool { + self.kind.contains(FmtSpan::NEW) + } + pub(super) fn trace_enter(&self) -> bool { + self.kind.contains(FmtSpan::ENTER) + } + pub(super) fn trace_exit(&self) -> bool { + self.kind.contains(FmtSpan::EXIT) + } + pub(super) fn trace_close(&self) -> bool { + self.kind.contains(FmtSpan::CLOSE) + } +} + +impl Debug for FmtSpanConfig { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.kind.fmt(f) + } +} + +impl Default for FmtSpanConfig { + fn default() -> Self { + Self { + kind: FmtSpan::NONE, + fmt_timing: true, + } + } +} + +pub(super) struct TimingDisplay(pub(super) u64); +impl Display for TimingDisplay { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut t = self.0 as f64; + for unit in ["ns", "µs", "ms", "s"].iter() { + if t < 10.0 { + return write!(f, "{:.2}{}", t, unit); + } else if t < 100.0 { + return write!(f, "{:.1}{}", t, unit); + } else if t < 1000.0 { + return write!(f, "{:.0}{}", t, unit); + } + t /= 1000.0; + } + write!(f, "{:.0}s", t * 1000.0) + } +} + +#[cfg(test)] +pub(super) mod test { + use crate::fmt::{test::MockMakeWriter, time::FormatTime}; + use tracing::{ + self, + dispatcher::{set_default, Dispatch}, + subscriber::with_default, + }; + + use super::*; + + use regex::Regex; + use std::{fmt, path::Path}; + + pub(crate) struct MockTime; + impl FormatTime for MockTime { + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + write!(w, "fake time") + } + } + + #[test] + fn disable_everything() { + // This test reproduces https://github.com/tokio-rs/tracing/issues/1354 + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .without_time() + .with_level(false) + .with_target(false) + .with_thread_ids(false) + .with_thread_names(false); + #[cfg(feature = "ansi")] + let subscriber = subscriber.with_ansi(false); + assert_info_hello(subscriber, make_writer, "hello\n") + } + + fn test_ansi<T>( + is_ansi: bool, + expected: &str, + builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, + ) where + Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, + T: Send + Sync + 'static, + { + let make_writer = MockMakeWriter::default(); + let subscriber = builder + .with_writer(make_writer.clone()) + .with_ansi(is_ansi) + .with_timer(MockTime); + run_test(subscriber, make_writer, expected) + } + + #[cfg(not(feature = "ansi"))] + fn test_without_ansi<T>( + expected: &str, + builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, + ) where + Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, + T: Send + Sync, + { + let make_writer = MockMakeWriter::default(); + let subscriber = builder.with_writer(make_writer).with_timer(MockTime); + run_test(subscriber, make_writer, expected) + } + + fn test_without_level<T>( + expected: &str, + builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, + ) where + Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, + T: Send + Sync + 'static, + { + let make_writer = MockMakeWriter::default(); + let subscriber = builder + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime); + run_test(subscriber, make_writer, expected); + } + + #[test] + fn with_line_number_and_file_name() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_file(true) + .with_line_number(true) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime); + + let expected = Regex::new(&format!( + "^fake time tracing_subscriber::fmt::format::test: {}:[0-9]+: hello\n$", + current_path() + // if we're on Windows, the path might contain backslashes, which + // have to be escpaed before compiling the regex. + .replace('\\', "\\\\") + )) + .unwrap(); + let _default = set_default(&subscriber.into()); + tracing::info!("hello"); + let res = make_writer.get_string(); + assert!(expected.is_match(&res)); + } + + #[test] + fn with_line_number() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_line_number(true) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime); + + let expected = + Regex::new("^fake time tracing_subscriber::fmt::format::test: [0-9]+: hello\n$") + .unwrap(); + let _default = set_default(&subscriber.into()); + tracing::info!("hello"); + let res = make_writer.get_string(); + assert!(expected.is_match(&res)); + } + + #[test] + fn with_filename() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_file(true) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime); + let expected = &format!( + "fake time tracing_subscriber::fmt::format::test: {}: hello\n", + current_path(), + ); + assert_info_hello(subscriber, make_writer, expected); + } + + #[test] + fn with_thread_ids() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_thread_ids(true) + .with_ansi(false) + .with_timer(MockTime); + let expected = + "fake time INFO ThreadId(NUMERIC) tracing_subscriber::fmt::format::test: hello\n"; + + assert_info_hello_ignore_numeric(subscriber, make_writer, expected); + } + + #[test] + fn pretty_default() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .pretty() + .with_writer(make_writer.clone()) + .with_ansi(false) + .with_timer(MockTime); + let expected = format!( + r#" fake time INFO tracing_subscriber::fmt::format::test: hello + at {}:NUMERIC + +"#, + file!() + ); + + assert_info_hello_ignore_numeric(subscriber, make_writer, &expected) + } + + fn assert_info_hello(subscriber: impl Into<Dispatch>, buf: MockMakeWriter, expected: &str) { + let _default = set_default(&subscriber.into()); + tracing::info!("hello"); + let result = buf.get_string(); + + assert_eq!(expected, result) + } + + // When numeric characters are used they often form a non-deterministic value as they usually represent things like a thread id or line number. + // This assert method should be used when non-deterministic numeric characters are present. + fn assert_info_hello_ignore_numeric( + subscriber: impl Into<Dispatch>, + buf: MockMakeWriter, + expected: &str, + ) { + let _default = set_default(&subscriber.into()); + tracing::info!("hello"); + + let regex = Regex::new("[0-9]+").unwrap(); + let result = buf.get_string(); + let result_cleaned = regex.replace_all(&result, "NUMERIC"); + + assert_eq!(expected, result_cleaned) + } + + fn test_overridden_parents<T>( + expected: &str, + builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, + ) where + Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, + T: Send + Sync + 'static, + { + let make_writer = MockMakeWriter::default(); + let subscriber = builder + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("span1", span = 1); + let span2 = tracing::info_span!(parent: &span1, "span2", span = 2); + tracing::info!(parent: &span2, "hello"); + }); + assert_eq!(expected, make_writer.get_string()); + } + + fn test_overridden_parents_in_scope<T>( + expected1: &str, + expected2: &str, + builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, + ) where + Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, + T: Send + Sync + 'static, + { + let make_writer = MockMakeWriter::default(); + let subscriber = builder + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + .finish(); + + with_default(subscriber, || { + let span1 = tracing::info_span!("span1", span = 1); + let span2 = tracing::info_span!(parent: &span1, "span2", span = 2); + let span3 = tracing::info_span!("span3", span = 3); + let _e3 = span3.enter(); + + tracing::info!("hello"); + assert_eq!(expected1, make_writer.get_string().as_str()); + + tracing::info!(parent: &span2, "hello"); + assert_eq!(expected2, make_writer.get_string().as_str()); + }); + } + + fn run_test(subscriber: impl Into<Dispatch>, buf: MockMakeWriter, expected: &str) { + let _default = set_default(&subscriber.into()); + tracing::info!("hello"); + assert_eq!(expected, buf.get_string()) + } + + mod default { + use super::*; + + #[test] + fn with_thread_ids() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .with_writer(make_writer.clone()) + .with_thread_ids(true) + .with_ansi(false) + .with_timer(MockTime); + let expected = + "fake time INFO ThreadId(NUMERIC) tracing_subscriber::fmt::format::test: hello\n"; + + assert_info_hello_ignore_numeric(subscriber, make_writer, expected); + } + + #[cfg(feature = "ansi")] + #[test] + fn with_ansi_true() { + let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m \u{1b}[2mtracing_subscriber::fmt::format::test\u{1b}[0m\u{1b}[2m:\u{1b}[0m hello\n"; + test_ansi(true, expected, crate::fmt::Subscriber::builder()); + } + + #[cfg(feature = "ansi")] + #[test] + fn with_ansi_false() { + let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; + test_ansi(false, expected, crate::fmt::Subscriber::builder()); + } + + #[cfg(not(feature = "ansi"))] + #[test] + fn without_ansi() { + let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; + test_without_ansi(expected, crate::fmt::Subscriber::builder()) + } + + #[test] + fn without_level() { + let expected = "fake time tracing_subscriber::fmt::format::test: hello\n"; + test_without_level(expected, crate::fmt::Subscriber::builder()) + } + + #[test] + fn overridden_parents() { + let expected = "fake time span1{span=1}:span2{span=2}: tracing_subscriber::fmt::format::test: hello\n"; + test_overridden_parents(expected, crate::fmt::Subscriber::builder()) + } + + #[test] + fn overridden_parents_in_scope() { + test_overridden_parents_in_scope( + "fake time span3{span=3}: tracing_subscriber::fmt::format::test: hello\n", + "fake time span1{span=1}:span2{span=2}: tracing_subscriber::fmt::format::test: hello\n", + crate::fmt::Subscriber::builder(), + ) + } + } + + mod compact { + use super::*; + + #[cfg(feature = "ansi")] + #[test] + fn with_ansi_true() { + let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m \u{1b}[1mtracing_subscriber::fmt::format::test\u{1b}[0m\u{1b}[2m:\u{1b}[0m hello\n"; + test_ansi(true, expected, crate::fmt::Subscriber::builder().compact()) + } + + #[cfg(feature = "ansi")] + #[test] + fn with_ansi_false() { + let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; + test_ansi(false, expected, crate::fmt::Subscriber::builder().compact()); + } + + #[cfg(not(feature = "ansi"))] + #[test] + fn without_ansi() { + let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; + test_without_ansi(expected, crate::fmt::Subscriber::builder().compact()) + } + + #[test] + fn without_level() { + let expected = "fake time tracing_subscriber::fmt::format::test: hello\n"; + test_without_level(expected, crate::fmt::Subscriber::builder().compact()); + } + + #[test] + fn overridden_parents() { + let expected = "fake time span1:span2: tracing_subscriber::fmt::format::test: hello span=1 span=2\n"; + test_overridden_parents(expected, crate::fmt::Subscriber::builder().compact()) + } + + #[test] + fn overridden_parents_in_scope() { + test_overridden_parents_in_scope( + "fake time span3: tracing_subscriber::fmt::format::test: hello span=3\n", + "fake time span1:span2: tracing_subscriber::fmt::format::test: hello span=1 span=2\n", + crate::fmt::Subscriber::builder().compact(), + ) + } + } + + mod pretty { + use super::*; + + #[test] + fn pretty_default() { + let make_writer = MockMakeWriter::default(); + let subscriber = crate::fmt::Subscriber::builder() + .pretty() + .with_writer(make_writer.clone()) + .with_ansi(false) + .with_timer(MockTime); + let expected = format!( + " fake time INFO tracing_subscriber::fmt::format::test: hello\n at {}:NUMERIC\n\n", + file!() + ); + + assert_info_hello_ignore_numeric(subscriber, make_writer, &expected) + } + } + + #[test] + fn format_nanos() { + fn fmt(t: u64) -> String { + TimingDisplay(t).to_string() + } + + assert_eq!(fmt(1), "1.00ns"); + assert_eq!(fmt(12), "12.0ns"); + assert_eq!(fmt(123), "123ns"); + assert_eq!(fmt(1234), "1.23µs"); + assert_eq!(fmt(12345), "12.3µs"); + assert_eq!(fmt(123456), "123µs"); + assert_eq!(fmt(1234567), "1.23ms"); + assert_eq!(fmt(12345678), "12.3ms"); + assert_eq!(fmt(123456789), "123ms"); + assert_eq!(fmt(1234567890), "1.23s"); + assert_eq!(fmt(12345678901), "12.3s"); + assert_eq!(fmt(123456789012), "123s"); + assert_eq!(fmt(1234567890123), "1235s"); + } + + #[test] + fn fmt_span_combinations() { + let f = FmtSpan::NONE; + assert!(!f.contains(FmtSpan::NEW)); + assert!(!f.contains(FmtSpan::ENTER)); + assert!(!f.contains(FmtSpan::EXIT)); + assert!(!f.contains(FmtSpan::CLOSE)); + + let f = FmtSpan::ACTIVE; + assert!(!f.contains(FmtSpan::NEW)); + assert!(f.contains(FmtSpan::ENTER)); + assert!(f.contains(FmtSpan::EXIT)); + assert!(!f.contains(FmtSpan::CLOSE)); + + let f = FmtSpan::FULL; + assert!(f.contains(FmtSpan::NEW)); + assert!(f.contains(FmtSpan::ENTER)); + assert!(f.contains(FmtSpan::EXIT)); + assert!(f.contains(FmtSpan::CLOSE)); + + let f = FmtSpan::NEW | FmtSpan::CLOSE; + assert!(f.contains(FmtSpan::NEW)); + assert!(!f.contains(FmtSpan::ENTER)); + assert!(!f.contains(FmtSpan::EXIT)); + assert!(f.contains(FmtSpan::CLOSE)); + } + + /// Returns the test's module path. + fn current_path() -> String { + Path::new("tracing-subscriber") + .join("src") + .join("fmt") + .join("format") + .join("mod.rs") + .to_str() + .expect("path must not contain invalid unicode") + .to_owned() + } +} diff --git a/vendor/tracing-subscriber/src/fmt/format/pretty.rs b/vendor/tracing-subscriber/src/fmt/format/pretty.rs new file mode 100644 index 000000000..a50d08ba7 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/format/pretty.rs @@ -0,0 +1,511 @@ +use super::*; +use crate::{ + field::{VisitFmt, VisitOutput}, + fmt::fmt_layer::{FmtContext, FormattedFields}, + registry::LookupSpan, +}; + +use std::fmt; +use tracing_core::{ + field::{self, Field}, + Event, Level, Subscriber, +}; + +#[cfg(feature = "tracing-log")] +use tracing_log::NormalizeEvent; + +use ansi_term::{Colour, Style}; + +/// An excessively pretty, human-readable event formatter. +/// +/// Unlike the [`Full`], [`Compact`], and [`Json`] formatters, this is a +/// multi-line output format. Each individual event may output multiple lines of +/// text. +/// +/// # Example Output +/// +/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt-pretty +/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s +/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-pretty` +/// 2022-02-15T18:44:24.535324Z <font color="#4E9A06"> INFO</font> <font color="#4E9A06"><b>fmt_pretty</b></font><font color="#4E9A06">: preparing to shave yaks, </font><font color="#4E9A06"><b>number_of_yaks</b></font><font color="#4E9A06">: 3</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt-pretty.rs:16 <font color="#AAAAAA"><i>on</i></font> main +/// +/// 2022-02-15T18:44:24.535403Z <font color="#4E9A06"> INFO</font> <font color="#4E9A06"><b>fmt_pretty::yak_shave</b></font><font color="#4E9A06">: shaving yaks</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:41 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535442Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: hello! I'm gonna shave a yak, </font><font color="#75507B"><b>excitement</b></font><font color="#75507B">: "yay!"</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:16 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 1 +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535469Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: yak shaved successfully</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:25 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 1 +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535502Z <font color="#3465A4">DEBUG</font> <font color="#3465A4"><b>yak_events</b></font><font color="#3465A4">: </font><font color="#3465A4"><b>yak</b></font><font color="#3465A4">: 1, </font><font color="#3465A4"><b>shaved</b></font><font color="#3465A4">: true</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:46 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535524Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: </font><font color="#75507B"><b>yaks_shaved</b></font><font color="#75507B">: 1</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:55 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535551Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: hello! I'm gonna shave a yak, </font><font color="#75507B"><b>excitement</b></font><font color="#75507B">: "yay!"</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:16 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 2 +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535573Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: yak shaved successfully</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:25 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 2 +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535600Z <font color="#3465A4">DEBUG</font> <font color="#3465A4"><b>yak_events</b></font><font color="#3465A4">: </font><font color="#3465A4"><b>yak</b></font><font color="#3465A4">: 2, </font><font color="#3465A4"><b>shaved</b></font><font color="#3465A4">: true</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:46 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535618Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: </font><font color="#75507B"><b>yaks_shaved</b></font><font color="#75507B">: 2</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:55 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535644Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: hello! I'm gonna shave a yak, </font><font color="#75507B"><b>excitement</b></font><font color="#75507B">: "yay!"</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:16 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 3 +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535670Z <font color="#C4A000"> WARN</font> <font color="#C4A000"><b>fmt_pretty::yak_shave</b></font><font color="#C4A000">: could not locate yak</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:18 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 3 +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535698Z <font color="#3465A4">DEBUG</font> <font color="#3465A4"><b>yak_events</b></font><font color="#3465A4">: </font><font color="#3465A4"><b>yak</b></font><font color="#3465A4">: 3, </font><font color="#3465A4"><b>shaved</b></font><font color="#3465A4">: false</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:46 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535720Z <font color="#CC0000">ERROR</font> <font color="#CC0000"><b>fmt_pretty::yak_shave</b></font><font color="#CC0000">: failed to shave yak, </font><font color="#CC0000"><b>yak</b></font><font color="#CC0000">: 3, </font><font color="#CC0000"><b>error</b></font><font color="#CC0000">: missing yak, </font><font color="#CC0000"><b>error.sources</b></font><font color="#CC0000">: [out of space, out of cash]</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:51 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535742Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: </font><font color="#75507B"><b>yaks_shaved</b></font><font color="#75507B">: 2</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:55 <font color="#AAAAAA"><i>on</i></font> main +/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 +/// +/// 2022-02-15T18:44:24.535765Z <font color="#4E9A06"> INFO</font> <font color="#4E9A06"><b>fmt_pretty</b></font><font color="#4E9A06">: yak shaving completed, </font><font color="#4E9A06"><b>all_yaks_shaved</b></font><font color="#4E9A06">: false</font> +/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt-pretty.rs:19 <font color="#AAAAAA"><i>on</i></font> main +/// </pre> +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Pretty { + display_location: bool, +} + +/// The [visitor] produced by [`Pretty`]'s [`MakeVisitor`] implementation. +/// +/// [visitor]: field::Visit +/// [`MakeVisitor`]: crate::field::MakeVisitor +#[derive(Debug)] +pub struct PrettyVisitor<'a> { + writer: Writer<'a>, + is_empty: bool, + style: Style, + result: fmt::Result, +} + +/// An excessively pretty, human-readable [`MakeVisitor`] implementation. +/// +/// [`MakeVisitor`]: crate::field::MakeVisitor +#[derive(Debug)] +pub struct PrettyFields { + /// A value to override the provided `Writer`'s ANSI formatting + /// configuration. + /// + /// If this is `Some`, we override the `Writer`'s ANSI setting. This is + /// necessary in order to continue supporting the deprecated + /// `PrettyFields::with_ansi` method. If it is `None`, we don't override the + /// ANSI formatting configuration (because the deprecated method was not + /// called). + // TODO: when `PrettyFields::with_ansi` is removed, we can get rid + // of this entirely. + ansi: Option<bool>, +} + +// === impl Pretty === + +impl Default for Pretty { + fn default() -> Self { + Self { + display_location: true, + } + } +} + +impl Pretty { + fn style_for(level: &Level) -> Style { + match *level { + Level::TRACE => Style::new().fg(Colour::Purple), + Level::DEBUG => Style::new().fg(Colour::Blue), + Level::INFO => Style::new().fg(Colour::Green), + Level::WARN => Style::new().fg(Colour::Yellow), + Level::ERROR => Style::new().fg(Colour::Red), + } + } + + /// Sets whether the event's source code location is displayed. + /// + /// This defaults to `true`. + #[deprecated( + since = "0.3.6", + note = "all formatters now support configurable source locations. Use `Format::with_source_location` instead." + )] + pub fn with_source_location(self, display_location: bool) -> Self { + Self { + display_location, + ..self + } + } +} + +impl<C, N, T> FormatEvent<C, N> for Format<Pretty, T> +where + C: Subscriber + for<'a> LookupSpan<'a>, + N: for<'a> FormatFields<'a> + 'static, + T: FormatTime, +{ + fn format_event( + &self, + ctx: &FmtContext<'_, C, N>, + mut writer: Writer<'_>, + event: &Event<'_>, + ) -> fmt::Result { + #[cfg(feature = "tracing-log")] + let normalized_meta = event.normalized_metadata(); + #[cfg(feature = "tracing-log")] + let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); + #[cfg(not(feature = "tracing-log"))] + let meta = event.metadata(); + write!(&mut writer, " ")?; + + // if the `Format` struct *also* has an ANSI color configuration, + // override the writer...the API for configuring ANSI color codes on the + // `Format` struct is deprecated, but we still need to honor those + // configurations. + if let Some(ansi) = self.ansi { + writer = writer.with_ansi(ansi); + } + + self.format_timestamp(&mut writer)?; + + let style = if self.display_level && writer.has_ansi_escapes() { + Pretty::style_for(meta.level()) + } else { + Style::new() + }; + + if self.display_level { + write!( + writer, + "{} ", + super::FmtLevel::new(meta.level(), writer.has_ansi_escapes()) + )?; + } + + if self.display_target { + let target_style = if writer.has_ansi_escapes() { + style.bold() + } else { + style + }; + write!( + writer, + "{}{}{}:", + target_style.prefix(), + meta.target(), + target_style.infix(style) + )?; + } + let line_number = if self.display_line_number { + meta.line() + } else { + None + }; + + // If the file name is disabled, format the line number right after the + // target. Otherwise, if we also display the file, it'll go on a + // separate line. + if let (Some(line_number), false, true) = ( + line_number, + self.display_filename, + self.format.display_location, + ) { + write!( + writer, + "{}{}{}:", + style.prefix(), + line_number, + style.infix(style) + )?; + } + + writer.write_char(' ')?; + + let mut v = PrettyVisitor::new(writer.by_ref(), true).with_style(style); + event.record(&mut v); + v.finish()?; + writer.write_char('\n')?; + + let dimmed = if writer.has_ansi_escapes() { + Style::new().dimmed().italic() + } else { + Style::new() + }; + let thread = self.display_thread_name || self.display_thread_id; + + if let (Some(file), true, true) = ( + meta.file(), + self.format.display_location, + self.display_filename, + ) { + write!(writer, " {} {}", dimmed.paint("at"), file,)?; + + if let Some(line) = line_number { + write!(writer, ":{}", line)?; + } + writer.write_char(if thread { ' ' } else { '\n' })?; + } else if thread { + write!(writer, " ")?; + }; + + if thread { + write!(writer, "{} ", dimmed.paint("on"))?; + let thread = std::thread::current(); + if self.display_thread_name { + if let Some(name) = thread.name() { + write!(writer, "{}", name)?; + if self.display_thread_id { + writer.write_char(' ')?; + } + } + } + if self.display_thread_id { + write!(writer, "{:?}", thread.id())?; + } + writer.write_char('\n')?; + } + + let bold = writer.bold(); + let span = event + .parent() + .and_then(|id| ctx.span(id)) + .or_else(|| ctx.lookup_current()); + + let scope = span.into_iter().flat_map(|span| span.scope()); + + for span in scope { + let meta = span.metadata(); + if self.display_target { + write!( + writer, + " {} {}::{}", + dimmed.paint("in"), + meta.target(), + bold.paint(meta.name()), + )?; + } else { + write!( + writer, + " {} {}", + dimmed.paint("in"), + bold.paint(meta.name()), + )?; + } + + let ext = span.extensions(); + let fields = &ext + .get::<FormattedFields<N>>() + .expect("Unable to find FormattedFields in extensions; this is a bug"); + if !fields.is_empty() { + write!(writer, " {} {}", dimmed.paint("with"), fields)?; + } + writer.write_char('\n')?; + } + + writer.write_char('\n') + } +} + +impl<'writer> FormatFields<'writer> for Pretty { + fn format_fields<R: RecordFields>(&self, writer: Writer<'writer>, fields: R) -> fmt::Result { + let mut v = PrettyVisitor::new(writer, true); + fields.record(&mut v); + v.finish() + } + + fn add_fields( + &self, + current: &'writer mut FormattedFields<Self>, + fields: &span::Record<'_>, + ) -> fmt::Result { + let empty = current.is_empty(); + let writer = current.as_writer(); + let mut v = PrettyVisitor::new(writer, empty); + fields.record(&mut v); + v.finish() + } +} + +// === impl PrettyFields === + +impl Default for PrettyFields { + fn default() -> Self { + Self::new() + } +} + +impl PrettyFields { + /// Returns a new default [`PrettyFields`] implementation. + pub fn new() -> Self { + // By default, don't override the `Writer`'s ANSI colors + // configuration. We'll only do this if the user calls the + // deprecated `PrettyFields::with_ansi` method. + Self { ansi: None } + } + + /// Enable ANSI encoding for formatted fields. + #[deprecated( + since = "0.3.3", + note = "Use `fmt::Subscriber::with_ansi` or `fmt::Layer::with_ansi` instead." + )] + pub fn with_ansi(self, ansi: bool) -> Self { + Self { + ansi: Some(ansi), + ..self + } + } +} + +impl<'a> MakeVisitor<Writer<'a>> for PrettyFields { + type Visitor = PrettyVisitor<'a>; + + #[inline] + fn make_visitor(&self, mut target: Writer<'a>) -> Self::Visitor { + if let Some(ansi) = self.ansi { + target = target.with_ansi(ansi); + } + PrettyVisitor::new(target, true) + } +} + +// === impl PrettyVisitor === + +impl<'a> PrettyVisitor<'a> { + /// Returns a new default visitor that formats to the provided `writer`. + /// + /// # Arguments + /// - `writer`: the writer to format to. + /// - `is_empty`: whether or not any fields have been previously written to + /// that writer. + pub fn new(writer: Writer<'a>, is_empty: bool) -> Self { + Self { + writer, + is_empty, + style: Style::default(), + result: Ok(()), + } + } + + pub(crate) fn with_style(self, style: Style) -> Self { + Self { style, ..self } + } + + fn write_padded(&mut self, value: &impl fmt::Debug) { + let padding = if self.is_empty { + self.is_empty = false; + "" + } else { + ", " + }; + self.result = write!(self.writer, "{}{:?}", padding, value); + } + + fn bold(&self) -> Style { + if self.writer.has_ansi_escapes() { + self.style.bold() + } else { + Style::new() + } + } +} + +impl<'a> field::Visit for PrettyVisitor<'a> { + fn record_str(&mut self, field: &Field, value: &str) { + if self.result.is_err() { + return; + } + + if field.name() == "message" { + self.record_debug(field, &format_args!("{}", value)) + } else { + self.record_debug(field, &value) + } + } + + fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { + if let Some(source) = value.source() { + let bold = self.bold(); + self.record_debug( + field, + &format_args!( + "{}, {}{}.sources{}: {}", + value, + bold.prefix(), + field, + bold.infix(self.style), + ErrorSourceList(source), + ), + ) + } else { + self.record_debug(field, &format_args!("{}", value)) + } + } + + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + if self.result.is_err() { + return; + } + let bold = self.bold(); + match field.name() { + "message" => self.write_padded(&format_args!("{}{:?}", self.style.prefix(), value,)), + // Skip fields that are actually log metadata that have already been handled + #[cfg(feature = "tracing-log")] + name if name.starts_with("log.") => self.result = Ok(()), + name if name.starts_with("r#") => self.write_padded(&format_args!( + "{}{}{}: {:?}", + bold.prefix(), + &name[2..], + bold.infix(self.style), + value + )), + name => self.write_padded(&format_args!( + "{}{}{}: {:?}", + bold.prefix(), + name, + bold.infix(self.style), + value + )), + }; + } +} + +impl<'a> VisitOutput<fmt::Result> for PrettyVisitor<'a> { + fn finish(mut self) -> fmt::Result { + write!(&mut self.writer, "{}", self.style.suffix())?; + self.result + } +} + +impl<'a> VisitFmt for PrettyVisitor<'a> { + fn writer(&mut self) -> &mut dyn fmt::Write { + &mut self.writer + } +} diff --git a/vendor/tracing-subscriber/src/fmt/mod.rs b/vendor/tracing-subscriber/src/fmt/mod.rs new file mode 100644 index 000000000..3c6a6ac40 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/mod.rs @@ -0,0 +1,1324 @@ +//! A `Subscriber` for formatting and logging `tracing` data. +//! +//! # Overview +//! +//! [`tracing`] is a framework for instrumenting Rust programs with context-aware, +//! structured, event-based diagnostic information. This crate provides an +//! implementation of the [`Subscriber`] trait that records `tracing`'s `Event`s +//! and `Span`s by formatting them as text and logging them to stdout. +//! +//! # Usage +//! +//! First, add this to your `Cargo.toml` file: +//! +//! ```toml +//! [dependencies] +//! tracing-subscriber = "0.3" +//! ``` +//! +//! *Compiler support: [requires `rustc` 1.49+][msrv]* +//! +//! [msrv]: super#supported-rust-versions +//! +//! Add the following to your executable to initialize the default subscriber: +//! ```rust +//! use tracing_subscriber; +//! +//! tracing_subscriber::fmt::init(); +//! ``` +//! +//! ## Filtering Events with Environment Variables +//! +//! The default subscriber installed by `init` enables you to filter events +//! at runtime using environment variables (using the [`EnvFilter`]). +//! +//! The filter syntax is a superset of the [`env_logger`] syntax. +//! +//! For example: +//! - Setting `RUST_LOG=debug` enables all `Span`s and `Event`s +//! set to the log level `DEBUG` or higher +//! - Setting `RUST_LOG=my_crate=trace` enables `Span`s and `Event`s +//! in `my_crate` at all log levels +//! +//! **Note**: This should **not** be called by libraries. Libraries should use +//! [`tracing`] to publish `tracing` `Event`s. +//! +//! # Configuration +//! +//! You can configure a subscriber instead of using the defaults with +//! the following functions: +//! +//! ### Subscriber +//! +//! The [`FmtSubscriber`] formats and records `tracing` events as line-oriented logs. +//! You can create one by calling: +//! +//! ```rust +//! let subscriber = tracing_subscriber::fmt() +//! // ... add configuration +//! .finish(); +//! ``` +//! +//! You can find the configuration methods for [`FmtSubscriber`] in +//! [`SubscriberBuilder`]. +//! +//! ## Formatters +//! +//! The output format used by the layer and subscriber in this module is +//! represented by implementing the [`FormatEvent`] trait, and can be +//! customized. This module provides a number of formatter implementations: +//! +//! * [`format::Full`]: The default formatter. This emits human-readable, +//! single-line logs for each event that occurs, with the current span context +//! displayed before the formatted representation of the event. See +//! [here](format::Full#example-output) for sample output. +//! +//! * [`format::Compact`]: A variant of the default formatter, optimized for +//! short line lengths. Fields from the current span context are appended to +//! the fields of the formatted event. See +//! [here](format::Compact#example-output) for sample output. +//! +//! * [`format::Pretty`]: Emits excessively pretty, multi-line logs, optimized +//! for human readability. This is primarily intended to be used in local +//! development and debugging, or for command-line applications, where +//! automated analysis and compact storage of logs is less of a priority than +//! readability and visual appeal. See [here](format::Pretty#example-output) +//! for sample output. +//! +//! * [`format::Json`]: Outputs newline-delimited JSON logs. This is intended +//! for production use with systems where structured logs are consumed as JSON +//! by analysis and viewing tools. The JSON output is not optimized for human +//! readability. See [here](format::Json#example-output) for sample output. +//! +//! ### Customizing Formatters +//! +//! The formatting of log lines for spans and events is controlled by two +//! traits, [`FormatEvent`] and [`FormatFields`]. The [`FormatEvent`] trait +//! determines the overall formatting of the log line, such as what information +//! from the event's metadata and span context is included and in what order. +//! The [`FormatFields`] trait determines how fields — both the event's +//! fields and fields on spans — are formatted. +//! +//! The [`fmt::format`] module provides several types which implement these traits, +//! many of which expose additional configuration options to customize their +//! output. The [`format::Format`] type implements common configuration used by +//! all the formatters provided in this crate, and can be used as a builder to +//! set specific formatting settings. For example: +//! +//! ``` +//! use tracing_subscriber::fmt; +//! +//! // Configure a custom event formatter +//! let format = fmt::format() +//! .with_level(false) // don't include levels in formatted output +//! .with_target(false) // don't include targets +//! .with_thread_ids(true) // include the thread ID of the current thread +//! .with_thread_names(true) // include the name of the current thread +//! .compact(); // use the `Compact` formatting style. +//! +//! // Create a `fmt` subscriber that uses our custom event format, and set it +//! // as the default. +//! tracing_subscriber::fmt() +//! .event_format(format) +//! .init(); +//! ``` +//! +//! However, if a specific output format is needed, other crates can +//! also implement [`FormatEvent`] and [`FormatFields`]. See those traits' +//! documentation for details on how to implement them. +//! +//! ## Filters +//! +//! If you want to filter the `tracing` `Events` based on environment +//! variables, you can use the [`EnvFilter`] as follows: +//! +//! ```rust +//! use tracing_subscriber::EnvFilter; +//! +//! let filter = EnvFilter::from_default_env(); +//! ``` +//! +//! As mentioned above, the [`EnvFilter`] allows `Span`s and `Event`s to +//! be filtered at runtime by setting the `RUST_LOG` environment variable. +//! +//! You can find the other available [`filter`]s in the documentation. +//! +//! ### Using Your Subscriber +//! +//! Finally, once you have configured your `Subscriber`, you need to +//! configure your executable to use it. +//! +//! A subscriber can be installed globally using: +//! ```rust +//! use tracing; +//! use tracing_subscriber::FmtSubscriber; +//! +//! let subscriber = FmtSubscriber::new(); +//! +//! tracing::subscriber::set_global_default(subscriber) +//! .map_err(|_err| eprintln!("Unable to set global default subscriber")); +//! // Note this will only fail if you try to set the global default +//! // subscriber multiple times +//! ``` +//! +//! ### Composing Layers +//! +//! Composing an [`EnvFilter`] `Layer` and a [format `Layer`][super::fmt::Layer]: +//! +//! ```rust +//! use tracing_subscriber::{fmt, EnvFilter}; +//! use tracing_subscriber::prelude::*; +//! +//! let fmt_layer = fmt::layer() +//! .with_target(false); +//! let filter_layer = EnvFilter::try_from_default_env() +//! .or_else(|_| EnvFilter::try_new("info")) +//! .unwrap(); +//! +//! tracing_subscriber::registry() +//! .with(filter_layer) +//! .with(fmt_layer) +//! .init(); +//! ``` +//! +//! [`EnvFilter`]: super::filter::EnvFilter +//! [`env_logger`]: https://docs.rs/env_logger/ +//! [`filter`]: super::filter +//! [`FmtSubscriber`]: Subscriber +//! [`Subscriber`]: +//! https://docs.rs/tracing/latest/tracing/trait.Subscriber.html +//! [`tracing`]: https://crates.io/crates/tracing +//! [`fmt::format`]: mod@crate::fmt::format +use std::{any::TypeId, error::Error, io}; +use tracing_core::{span, subscriber::Interest, Event, Metadata}; + +mod fmt_layer; +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +pub mod format; +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +pub mod time; +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +pub mod writer; + +pub use fmt_layer::{FmtContext, FormattedFields, Layer}; + +use crate::layer::Layer as _; +use crate::util::SubscriberInitExt; +use crate::{ + filter::LevelFilter, + layer, + registry::{LookupSpan, Registry}, +}; + +#[doc(inline)] +pub use self::{ + format::{format, FormatEvent, FormatFields}, + time::time, + writer::{MakeWriter, TestWriter}, +}; + +/// A `Subscriber` that logs formatted representations of `tracing` events. +/// +/// This consists of an inner `Formatter` wrapped in a layer that performs filtering. +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +#[derive(Debug)] +pub struct Subscriber< + N = format::DefaultFields, + E = format::Format<format::Full>, + F = LevelFilter, + W = fn() -> io::Stdout, +> { + inner: layer::Layered<F, Formatter<N, E, W>>, +} + +/// A `Subscriber` that logs formatted representations of `tracing` events. +/// This type only logs formatted events; it does not perform any filtering. +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +pub type Formatter< + N = format::DefaultFields, + E = format::Format<format::Full>, + W = fn() -> io::Stdout, +> = layer::Layered<fmt_layer::Layer<Registry, N, E, W>, Registry>; + +/// Configures and constructs `Subscriber`s. +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +#[derive(Debug)] +pub struct SubscriberBuilder< + N = format::DefaultFields, + E = format::Format<format::Full>, + F = LevelFilter, + W = fn() -> io::Stdout, +> { + filter: F, + inner: Layer<Registry, N, E, W>, +} + +/// Returns a new [`SubscriberBuilder`] for configuring a [formatting subscriber]. +/// +/// This is essentially shorthand for [`SubscriberBuilder::default()]`. +/// +/// # Examples +/// +/// Using [`init`] to set the default subscriber: +/// +/// ```rust +/// tracing_subscriber::fmt().init(); +/// ``` +/// +/// Configuring the output format: +/// +/// ```rust +/// +/// tracing_subscriber::fmt() +/// // Configure formatting settings. +/// .with_target(false) +/// .with_timer(tracing_subscriber::fmt::time::uptime()) +/// .with_level(true) +/// // Set the subscriber as the default. +/// .init(); +/// ``` +/// +/// [`try_init`] returns an error if the default subscriber could not be set: +/// +/// ```rust +/// use std::error::Error; +/// +/// fn init_subscriber() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { +/// tracing_subscriber::fmt() +/// // Configure the subscriber to emit logs in JSON format. +/// .json() +/// // Configure the subscriber to flatten event fields in the output JSON objects. +/// .flatten_event(true) +/// // Set the subscriber as the default, returning an error if this fails. +/// .try_init()?; +/// +/// Ok(()) +/// } +/// ``` +/// +/// Rather than setting the subscriber as the default, [`finish`] _returns_ the +/// constructed subscriber, which may then be passed to other functions: +/// +/// ```rust +/// let subscriber = tracing_subscriber::fmt() +/// .with_max_level(tracing::Level::DEBUG) +/// .compact() +/// .finish(); +/// +/// tracing::subscriber::with_default(subscriber, || { +/// // the subscriber will only be set as the default +/// // inside this closure... +/// }) +/// ``` +/// +/// [formatting subscriber]: Subscriber +/// [`SubscriberBuilder::default()`]: SubscriberBuilder::default +/// [`init`]: SubscriberBuilder::init() +/// [`try_init`]: SubscriberBuilder::try_init() +/// [`finish`]: SubscriberBuilder::finish() +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +pub fn fmt() -> SubscriberBuilder { + SubscriberBuilder::default() +} + +/// Returns a new [formatting layer] that can be [composed] with other layers to +/// construct a [`Subscriber`]. +/// +/// This is a shorthand for the equivalent [`Layer::default()`] function. +/// +/// [formatting layer]: Layer +/// [composed]: crate::layer +/// [`Layer::default()`]: Layer::default +#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] +pub fn layer<S>() -> Layer<S> { + Layer::default() +} + +impl Subscriber { + /// The maximum [verbosity level] that is enabled by a `Subscriber` by + /// default. + /// + /// This can be overridden with the [`SubscriberBuilder::with_max_level`] method. + /// + /// [verbosity level]: tracing_core::Level + /// [`SubscriberBuilder::with_max_level`]: SubscriberBuilder::with_max_level + pub const DEFAULT_MAX_LEVEL: LevelFilter = LevelFilter::INFO; + + /// Returns a new `SubscriberBuilder` for configuring a format subscriber. + pub fn builder() -> SubscriberBuilder { + SubscriberBuilder::default() + } + + /// Returns a new format subscriber with the default configuration. + pub fn new() -> Self { + Default::default() + } +} + +impl Default for Subscriber { + fn default() -> Self { + SubscriberBuilder::default().finish() + } +} + +// === impl Subscriber === + +impl<N, E, F, W> tracing_core::Subscriber for Subscriber<N, E, F, W> +where + N: for<'writer> FormatFields<'writer> + 'static, + E: FormatEvent<Registry, N> + 'static, + F: layer::Layer<Formatter<N, E, W>> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, + layer::Layered<F, Formatter<N, E, W>>: tracing_core::Subscriber, + fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry>, +{ + #[inline] + fn register_callsite(&self, meta: &'static Metadata<'static>) -> Interest { + self.inner.register_callsite(meta) + } + + #[inline] + fn enabled(&self, meta: &Metadata<'_>) -> bool { + self.inner.enabled(meta) + } + + #[inline] + fn new_span(&self, attrs: &span::Attributes<'_>) -> span::Id { + self.inner.new_span(attrs) + } + + #[inline] + fn record(&self, span: &span::Id, values: &span::Record<'_>) { + self.inner.record(span, values) + } + + #[inline] + fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { + self.inner.record_follows_from(span, follows) + } + + #[inline] + fn event_enabled(&self, event: &Event<'_>) -> bool { + self.inner.event_enabled(event) + } + + #[inline] + fn event(&self, event: &Event<'_>) { + self.inner.event(event); + } + + #[inline] + fn enter(&self, id: &span::Id) { + // TODO: add on_enter hook + self.inner.enter(id); + } + + #[inline] + fn exit(&self, id: &span::Id) { + self.inner.exit(id); + } + + #[inline] + fn current_span(&self) -> span::Current { + self.inner.current_span() + } + + #[inline] + fn clone_span(&self, id: &span::Id) -> span::Id { + self.inner.clone_span(id) + } + + #[inline] + fn try_close(&self, id: span::Id) -> bool { + self.inner.try_close(id) + } + + #[inline] + fn max_level_hint(&self) -> Option<tracing_core::LevelFilter> { + self.inner.max_level_hint() + } + + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + if id == TypeId::of::<Self>() { + Some(self as *const Self as *const ()) + } else { + self.inner.downcast_raw(id) + } + } +} + +impl<'a, N, E, F, W> LookupSpan<'a> for Subscriber<N, E, F, W> +where + layer::Layered<F, Formatter<N, E, W>>: LookupSpan<'a>, +{ + type Data = <layer::Layered<F, Formatter<N, E, W>> as LookupSpan<'a>>::Data; + + fn span_data(&'a self, id: &span::Id) -> Option<Self::Data> { + self.inner.span_data(id) + } +} + +// ===== impl SubscriberBuilder ===== + +impl Default for SubscriberBuilder { + fn default() -> Self { + SubscriberBuilder { + filter: Subscriber::DEFAULT_MAX_LEVEL, + inner: Default::default(), + } + } +} + +impl<N, E, F, W> SubscriberBuilder<N, E, F, W> +where + N: for<'writer> FormatFields<'writer> + 'static, + E: FormatEvent<Registry, N> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, + F: layer::Layer<Formatter<N, E, W>> + Send + Sync + 'static, + fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry> + Send + Sync + 'static, +{ + /// Finish the builder, returning a new `FmtSubscriber`. + pub fn finish(self) -> Subscriber<N, E, F, W> { + let subscriber = self.inner.with_subscriber(Registry::default()); + Subscriber { + inner: self.filter.with_subscriber(subscriber), + } + } + + /// Install this Subscriber as the global default if one is + /// not already set. + /// + /// If the `tracing-log` feature is enabled, this will also install + /// the LogTracer to convert `Log` records into `tracing` `Event`s. + /// + /// # Errors + /// Returns an Error if the initialization was unsuccessful, likely + /// because a global subscriber was already installed by another + /// call to `try_init`. + pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>> { + use crate::util::SubscriberInitExt; + self.finish().try_init()?; + + Ok(()) + } + + /// Install this Subscriber as the global default. + /// + /// If the `tracing-log` feature is enabled, this will also install + /// the LogTracer to convert `Log` records into `tracing` `Event`s. + /// + /// # Panics + /// Panics if the initialization was unsuccessful, likely because a + /// global subscriber was already installed by another call to `try_init`. + pub fn init(self) { + self.try_init() + .expect("Unable to install global subscriber") + } +} + +impl<N, E, F, W> From<SubscriberBuilder<N, E, F, W>> for tracing_core::Dispatch +where + N: for<'writer> FormatFields<'writer> + 'static, + E: FormatEvent<Registry, N> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, + F: layer::Layer<Formatter<N, E, W>> + Send + Sync + 'static, + fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry> + Send + Sync + 'static, +{ + fn from(builder: SubscriberBuilder<N, E, F, W>) -> tracing_core::Dispatch { + tracing_core::Dispatch::new(builder.finish()) + } +} + +impl<N, L, T, F, W> SubscriberBuilder<N, format::Format<L, T>, F, W> +where + N: for<'writer> FormatFields<'writer> + 'static, +{ + /// Use the given [`timer`] for log message timestamps. + /// + /// See the [`time` module] for the provided timer implementations. + /// + /// Note that using the `"time`"" feature flag enables the + /// additional time formatters [`UtcTime`] and [`LocalTime`], which use the + /// [`time` crate] to provide more sophisticated timestamp formatting + /// options. + /// + /// [`timer`]: time::FormatTime + /// [`time` module]: mod@time + /// [`UtcTime`]: time::UtcTime + /// [`LocalTime`]: time::LocalTime + /// [`time` crate]: https://docs.rs/time/0.3 + pub fn with_timer<T2>(self, timer: T2) -> SubscriberBuilder<N, format::Format<L, T2>, F, W> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.with_timer(timer), + } + } + + /// Do not emit timestamps with log messages. + pub fn without_time(self) -> SubscriberBuilder<N, format::Format<L, ()>, F, W> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.without_time(), + } + } + + /// Configures how synthesized events are emitted at points in the [span + /// lifecycle][lifecycle]. + /// + /// The following options are available: + /// + /// - `FmtSpan::NONE`: No events will be synthesized when spans are + /// created, entered, exited, or closed. Data from spans will still be + /// included as the context for formatted events. This is the default. + /// - `FmtSpan::NEW`: An event will be synthesized when spans are created. + /// - `FmtSpan::ENTER`: An event will be synthesized when spans are entered. + /// - `FmtSpan::EXIT`: An event will be synthesized when spans are exited. + /// - `FmtSpan::CLOSE`: An event will be synthesized when a span closes. If + /// [timestamps are enabled][time] for this formatter, the generated + /// event will contain fields with the span's _busy time_ (the total + /// time for which it was entered) and _idle time_ (the total time that + /// the span existed but was not entered). + /// - `FmtSpan::ACTIVE`: An event will be synthesized when spans are entered + /// or exited. + /// - `FmtSpan::FULL`: Events will be synthesized whenever a span is + /// created, entered, exited, or closed. If timestamps are enabled, the + /// close event will contain the span's busy and idle time, as + /// described above. + /// + /// The options can be enabled in any combination. For instance, the following + /// will synthesize events whenever spans are created and closed: + /// + /// ```rust + /// use tracing_subscriber::fmt::format::FmtSpan; + /// use tracing_subscriber::fmt; + /// + /// let subscriber = fmt() + /// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) + /// .finish(); + /// ``` + /// + /// Note that the generated events will only be part of the log output by + /// this formatter; they will not be recorded by other `Subscriber`s or by + /// `Layer`s added to this subscriber. + /// + /// [lifecycle]: https://docs.rs/tracing/latest/tracing/span/index.html#the-span-lifecycle + /// [time]: SubscriberBuilder::without_time() + pub fn with_span_events(self, kind: format::FmtSpan) -> Self { + SubscriberBuilder { + inner: self.inner.with_span_events(kind), + ..self + } + } + + /// Enable ANSI encoding for formatted events. + #[cfg(feature = "ansi")] + #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] + pub fn with_ansi(self, ansi: bool) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { + SubscriberBuilder { + inner: self.inner.with_ansi(ansi), + ..self + } + } + + /// Sets whether or not an event's target is displayed. + pub fn with_target( + self, + display_target: bool, + ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { + SubscriberBuilder { + inner: self.inner.with_target(display_target), + ..self + } + } + + /// Sets whether or not an event's [source code file path][file] is + /// displayed. + /// + /// [file]: tracing_core::Metadata::file + pub fn with_file( + self, + display_filename: bool, + ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { + SubscriberBuilder { + inner: self.inner.with_file(display_filename), + ..self + } + } + + /// Sets whether or not an event's [source code line number][line] is + /// displayed. + /// + /// [line]: tracing_core::Metadata::line + pub fn with_line_number( + self, + display_line_number: bool, + ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { + SubscriberBuilder { + inner: self.inner.with_line_number(display_line_number), + ..self + } + } + + /// Sets whether or not an event's level is displayed. + pub fn with_level( + self, + display_level: bool, + ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { + SubscriberBuilder { + inner: self.inner.with_level(display_level), + ..self + } + } + + /// Sets whether or not the [name] of the current thread is displayed + /// when formatting events. + /// + /// [name]: std::thread#naming-threads + pub fn with_thread_names( + self, + display_thread_names: bool, + ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { + SubscriberBuilder { + inner: self.inner.with_thread_names(display_thread_names), + ..self + } + } + + /// Sets whether or not the [thread ID] of the current thread is displayed + /// when formatting events. + /// + /// [thread ID]: std::thread::ThreadId + pub fn with_thread_ids( + self, + display_thread_ids: bool, + ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { + SubscriberBuilder { + inner: self.inner.with_thread_ids(display_thread_ids), + ..self + } + } + + /// Sets the subscriber being built to use a less verbose formatter. + /// + /// See [`format::Compact`]. + pub fn compact(self) -> SubscriberBuilder<N, format::Format<format::Compact, T>, F, W> + where + N: for<'writer> FormatFields<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.compact(), + } + } + + /// Sets the subscriber being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty). + #[cfg(feature = "ansi")] + #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] + pub fn pretty( + self, + ) -> SubscriberBuilder<format::Pretty, format::Format<format::Pretty, T>, F, W> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.pretty(), + } + } + + /// Sets the subscriber being built to use a JSON formatter. + /// + /// See [`format::Json`][super::fmt::format::Json] + #[cfg(feature = "json")] + #[cfg_attr(docsrs, doc(cfg(feature = "json")))] + pub fn json( + self, + ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> + where + N: for<'writer> FormatFields<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.json(), + } + } +} + +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] +impl<T, F, W> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { + /// Sets the json subscriber being built to flatten event metadata. + /// + /// See [`format::Json`][super::fmt::format::Json] + pub fn flatten_event( + self, + flatten_event: bool, + ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.flatten_event(flatten_event), + } + } + + /// Sets whether or not the JSON subscriber being built will include the current span + /// in formatted events. + /// + /// See [`format::Json`][super::fmt::format::Json] + pub fn with_current_span( + self, + display_current_span: bool, + ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.with_current_span(display_current_span), + } + } + + /// Sets whether or not the JSON subscriber being built will include a list (from + /// root to leaf) of all currently entered spans in formatted events. + /// + /// See [`format::Json`][super::fmt::format::Json] + pub fn with_span_list( + self, + display_span_list: bool, + ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.with_span_list(display_span_list), + } + } +} + +#[cfg(feature = "env-filter")] +#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] +impl<N, E, W> SubscriberBuilder<N, E, crate::EnvFilter, W> +where + Formatter<N, E, W>: tracing_core::Subscriber + 'static, +{ + /// Configures the subscriber being built to allow filter reloading at + /// runtime. + pub fn with_filter_reloading( + self, + ) -> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W> + { + let (filter, _) = crate::reload::Layer::new(self.filter); + SubscriberBuilder { + filter, + inner: self.inner, + } + } +} + +#[cfg(feature = "env-filter")] +#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] +impl<N, E, W> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W> +where + Formatter<N, E, W>: tracing_core::Subscriber + 'static, +{ + /// Returns a `Handle` that may be used to reload the constructed subscriber's + /// filter. + pub fn reload_handle(&self) -> crate::reload::Handle<crate::EnvFilter, Formatter<N, E, W>> { + self.filter.handle() + } +} + +impl<N, E, F, W> SubscriberBuilder<N, E, F, W> { + /// Sets the field formatter that the subscriber being built will use to record + /// fields. + /// + /// For example: + /// ```rust + /// use tracing_subscriber::fmt::format; + /// use tracing_subscriber::prelude::*; + /// + /// let formatter = + /// // Construct a custom formatter for `Debug` fields + /// format::debug_fn(|writer, field, value| write!(writer, "{}: {:?}", field, value)) + /// // Use the `tracing_subscriber::MakeFmtExt` trait to wrap the + /// // formatter so that a delimiter is added between fields. + /// .delimited(", "); + /// + /// let subscriber = tracing_subscriber::fmt() + /// .fmt_fields(formatter) + /// .finish(); + /// # drop(subscriber) + /// ``` + pub fn fmt_fields<N2>(self, fmt_fields: N2) -> SubscriberBuilder<N2, E, F, W> + where + N2: for<'writer> FormatFields<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.fmt_fields(fmt_fields), + } + } + + /// Sets the [`EnvFilter`] that the subscriber will use to determine if + /// a span or event is enabled. + /// + /// Note that this method requires the "env-filter" feature flag to be enabled. + /// + /// If a filter was previously set, or a maximum level was set by the + /// [`with_max_level`] method, that value is replaced by the new filter. + /// + /// # Examples + /// + /// Setting a filter based on the value of the `RUST_LOG` environment + /// variable: + /// ```rust + /// use tracing_subscriber::{fmt, EnvFilter}; + /// + /// fmt() + /// .with_env_filter(EnvFilter::from_default_env()) + /// .init(); + /// ``` + /// + /// Setting a filter based on a pre-set filter directive string: + /// ```rust + /// use tracing_subscriber::fmt; + /// + /// fmt() + /// .with_env_filter("my_crate=info,my_crate::my_mod=debug,[my_span]=trace") + /// .init(); + /// ``` + /// + /// Adding additional directives to a filter constructed from an env var: + /// ```rust + /// use tracing_subscriber::{fmt, filter::{EnvFilter, LevelFilter}}; + /// + /// # fn filter() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { + /// let filter = EnvFilter::try_from_env("MY_CUSTOM_FILTER_ENV_VAR")? + /// // Set the base level when not matched by other directives to WARN. + /// .add_directive(LevelFilter::WARN.into()) + /// // Set the max level for `my_crate::my_mod` to DEBUG, overriding + /// // any directives parsed from the env variable. + /// .add_directive("my_crate::my_mod=debug".parse()?); + /// + /// fmt() + /// .with_env_filter(filter) + /// .try_init()?; + /// # Ok(())} + /// ``` + /// [`EnvFilter`]: super::filter::EnvFilter + /// [`with_max_level`]: SubscriberBuilder::with_max_level() + #[cfg(feature = "env-filter")] + #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] + pub fn with_env_filter( + self, + filter: impl Into<crate::EnvFilter>, + ) -> SubscriberBuilder<N, E, crate::EnvFilter, W> + where + Formatter<N, E, W>: tracing_core::Subscriber + 'static, + { + let filter = filter.into(); + SubscriberBuilder { + filter, + inner: self.inner, + } + } + + /// Sets the maximum [verbosity level] that will be enabled by the + /// subscriber. + /// + /// If the max level has already been set, or a [`EnvFilter`] was added by + /// [`with_env_filter`], this replaces that configuration with the new + /// maximum level. + /// + /// # Examples + /// + /// Enable up to the `DEBUG` verbosity level: + /// ```rust + /// use tracing_subscriber::fmt; + /// use tracing::Level; + /// + /// fmt() + /// .with_max_level(Level::DEBUG) + /// .init(); + /// ``` + /// This subscriber won't record any spans or events! + /// ```rust + /// use tracing_subscriber::{fmt, filter::LevelFilter}; + /// + /// let subscriber = fmt() + /// .with_max_level(LevelFilter::OFF) + /// .finish(); + /// ``` + /// [verbosity level]: tracing_core::Level + /// [`EnvFilter`]: struct@crate::filter::EnvFilter + /// [`with_env_filter`]: fn@Self::with_env_filter + pub fn with_max_level( + self, + filter: impl Into<LevelFilter>, + ) -> SubscriberBuilder<N, E, LevelFilter, W> { + let filter = filter.into(); + SubscriberBuilder { + filter, + inner: self.inner, + } + } + + /// Sets the [event formatter][`FormatEvent`] that the subscriber being built + /// will use to format events that occur. + /// + /// The event formatter may be any type implementing the [`FormatEvent`] + /// trait, which is implemented for all functions taking a [`FmtContext`], a + /// [`Writer`], and an [`Event`]. + /// + /// # Examples + /// + /// Setting a type implementing [`FormatEvent`] as the formatter: + /// + /// ```rust + /// use tracing_subscriber::fmt::format; + /// + /// let subscriber = tracing_subscriber::fmt() + /// .event_format(format().compact()) + /// .finish(); + /// ``` + /// + /// [`Writer`]: struct@self::format::Writer + pub fn event_format<E2>(self, fmt_event: E2) -> SubscriberBuilder<N, E2, F, W> + where + E2: FormatEvent<Registry, N> + 'static, + N: for<'writer> FormatFields<'writer> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.event_format(fmt_event), + } + } + + /// Sets the [`MakeWriter`] that the subscriber being built will use to write events. + /// + /// # Examples + /// + /// Using `stderr` rather than `stdout`: + /// + /// ```rust + /// use tracing_subscriber::fmt; + /// use std::io; + /// + /// fmt() + /// .with_writer(io::stderr) + /// .init(); + /// ``` + pub fn with_writer<W2>(self, make_writer: W2) -> SubscriberBuilder<N, E, F, W2> + where + W2: for<'writer> MakeWriter<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.with_writer(make_writer), + } + } + + /// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in + /// unit tests. + /// + /// See [`TestWriter`] for additional details. + /// + /// # Examples + /// + /// Using [`TestWriter`] to let `cargo test` capture test output. Note that we do not install it + /// globally as it may cause conflicts. + /// + /// ```rust + /// use tracing_subscriber::fmt; + /// use tracing::subscriber; + /// + /// subscriber::set_default( + /// fmt() + /// .with_test_writer() + /// .finish() + /// ); + /// ``` + /// + /// [capturing]: + /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output + /// [`TestWriter`]: writer::TestWriter + pub fn with_test_writer(self) -> SubscriberBuilder<N, E, F, TestWriter> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.with_writer(TestWriter::default()), + } + } + + /// Updates the event formatter by applying a function to the existing event formatter. + /// + /// This sets the event formatter that the subscriber being built will use to record fields. + /// + /// # Examples + /// + /// Updating an event formatter: + /// + /// ```rust + /// let subscriber = tracing_subscriber::fmt() + /// .map_event_format(|e| e.compact()) + /// .finish(); + /// ``` + pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> SubscriberBuilder<N, E2, F, W> + where + E2: FormatEvent<Registry, N> + 'static, + N: for<'writer> FormatFields<'writer> + 'static, + W: for<'writer> MakeWriter<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.map_event_format(f), + } + } + + /// Updates the field formatter by applying a function to the existing field formatter. + /// + /// This sets the field formatter that the subscriber being built will use to record fields. + /// + /// # Examples + /// + /// Updating a field formatter: + /// + /// ```rust + /// use tracing_subscriber::field::MakeExt; + /// let subscriber = tracing_subscriber::fmt() + /// .map_fmt_fields(|f| f.debug_alt()) + /// .finish(); + /// ``` + pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> SubscriberBuilder<N2, E, F, W> + where + N2: for<'writer> FormatFields<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.map_fmt_fields(f), + } + } + + /// Updates the [`MakeWriter`] by applying a function to the existing [`MakeWriter`]. + /// + /// This sets the [`MakeWriter`] that the subscriber being built will use to write events. + /// + /// # Examples + /// + /// Redirect output to stderr if level is <= WARN: + /// + /// ```rust + /// use tracing::Level; + /// use tracing_subscriber::fmt::{self, writer::MakeWriterExt}; + /// + /// let stderr = std::io::stderr.with_max_level(Level::WARN); + /// let layer = tracing_subscriber::fmt() + /// .map_writer(move |w| stderr.or_else(w)) + /// .finish(); + /// ``` + pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> SubscriberBuilder<N, E, F, W2> + where + W2: for<'writer> MakeWriter<'writer> + 'static, + { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.map_writer(f), + } + } +} + +/// Install a global tracing subscriber that listens for events and +/// filters based on the value of the [`RUST_LOG` environment variable], +/// if one is not already set. +/// +/// If the `tracing-log` feature is enabled, this will also install +/// the [`LogTracer`] to convert `log` records into `tracing` `Event`s. +/// +/// This is shorthand for +/// +/// ```rust +/// # fn doc() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { +/// tracing_subscriber::fmt().try_init() +/// # } +/// ``` +/// +/// +/// # Errors +/// +/// Returns an Error if the initialization was unsuccessful, +/// likely because a global subscriber was already installed by another +/// call to `try_init`. +/// +/// [`LogTracer`]: +/// https://docs.rs/tracing-log/0.1.0/tracing_log/struct.LogTracer.html +/// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV +pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { + let builder = Subscriber::builder(); + + #[cfg(feature = "env-filter")] + let builder = builder.with_env_filter(crate::EnvFilter::from_default_env()); + + // If `env-filter` is disabled, remove the default max level filter from the + // subscriber; it will be added to the `Targets` filter instead if no filter + // is set in `RUST_LOG`. + // Replacing the default `LevelFilter` with an `EnvFilter` would imply this, + // but we can't replace the builder's filter with a `Targets` filter yet. + #[cfg(not(feature = "env-filter"))] + let builder = builder.with_max_level(LevelFilter::TRACE); + + let subscriber = builder.finish(); + #[cfg(not(feature = "env-filter"))] + let subscriber = { + use crate::{filter::Targets, layer::SubscriberExt}; + use std::{env, str::FromStr}; + let targets = match env::var("RUST_LOG") { + Ok(var) => Targets::from_str(&var) + .map_err(|e| { + eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e); + }) + .unwrap_or_default(), + Err(env::VarError::NotPresent) => { + Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) + } + Err(e) => { + eprintln!("Ignoring `RUST_LOG`: {}", e); + Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) + } + }; + subscriber.with(targets) + }; + + subscriber.try_init().map_err(Into::into) +} + +/// Install a global tracing subscriber that listens for events and +/// filters based on the value of the [`RUST_LOG` environment variable]. +/// +/// If the `tracing-log` feature is enabled, this will also install +/// the LogTracer to convert `Log` records into `tracing` `Event`s. +/// +/// This is shorthand for +/// +/// ```rust +/// tracing_subscriber::fmt().init() +/// ``` +/// +/// # Panics +/// Panics if the initialization was unsuccessful, likely because a +/// global subscriber was already installed by another call to `try_init`. +/// +/// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV +pub fn init() { + try_init().expect("Unable to install global subscriber") +} + +#[cfg(test)] +mod test { + use crate::{ + filter::LevelFilter, + fmt::{ + format::{self, Format}, + time, + writer::MakeWriter, + Subscriber, + }, + }; + use std::{ + io, + sync::{Arc, Mutex, MutexGuard, TryLockError}, + }; + use tracing_core::dispatcher::Dispatch; + + pub(crate) struct MockWriter { + buf: Arc<Mutex<Vec<u8>>>, + } + + impl MockWriter { + pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { + Self { buf } + } + + pub(crate) fn map_error<Guard>(err: TryLockError<Guard>) -> io::Error { + match err { + TryLockError::WouldBlock => io::Error::from(io::ErrorKind::WouldBlock), + TryLockError::Poisoned(_) => io::Error::from(io::ErrorKind::Other), + } + } + + pub(crate) fn buf(&self) -> io::Result<MutexGuard<'_, Vec<u8>>> { + self.buf.try_lock().map_err(Self::map_error) + } + } + + impl io::Write for MockWriter { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + self.buf()?.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.buf()?.flush() + } + } + + #[derive(Clone, Default)] + pub(crate) struct MockMakeWriter { + buf: Arc<Mutex<Vec<u8>>>, + } + + impl MockMakeWriter { + pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { + Self { buf } + } + + #[cfg(feature = "json")] + pub(crate) fn buf(&self) -> MutexGuard<'_, Vec<u8>> { + self.buf.lock().unwrap() + } + + pub(crate) fn get_string(&self) -> String { + let mut buf = self.buf.lock().expect("lock shouldn't be poisoned"); + let string = std::str::from_utf8(&buf[..]) + .expect("formatter should not have produced invalid utf-8") + .to_owned(); + buf.clear(); + string + } + } + + impl<'a> MakeWriter<'a> for MockMakeWriter { + type Writer = MockWriter; + + fn make_writer(&'a self) -> Self::Writer { + MockWriter::new(self.buf.clone()) + } + } + + #[test] + fn impls() { + let f = Format::default().with_timer(time::Uptime::default()); + let subscriber = Subscriber::builder().event_format(f).finish(); + let _dispatch = Dispatch::new(subscriber); + + let f = format::Format::default(); + let subscriber = Subscriber::builder().event_format(f).finish(); + let _dispatch = Dispatch::new(subscriber); + + let f = format::Format::default().compact(); + let subscriber = Subscriber::builder().event_format(f).finish(); + let _dispatch = Dispatch::new(subscriber); + } + + #[test] + fn subscriber_downcasts() { + let subscriber = Subscriber::builder().finish(); + let dispatch = Dispatch::new(subscriber); + assert!(dispatch.downcast_ref::<Subscriber>().is_some()); + } + + #[test] + fn subscriber_downcasts_to_parts() { + let subscriber = Subscriber::new(); + let dispatch = Dispatch::new(subscriber); + assert!(dispatch.downcast_ref::<format::DefaultFields>().is_some()); + assert!(dispatch.downcast_ref::<LevelFilter>().is_some()); + assert!(dispatch.downcast_ref::<format::Format>().is_some()) + } + + #[test] + fn is_lookup_span() { + fn assert_lookup_span<T: for<'a> crate::registry::LookupSpan<'a>>(_: T) {} + let subscriber = Subscriber::new(); + assert_lookup_span(subscriber) + } +} diff --git a/vendor/tracing-subscriber/src/fmt/time/datetime.rs b/vendor/tracing-subscriber/src/fmt/time/datetime.rs new file mode 100644 index 000000000..531331687 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/time/datetime.rs @@ -0,0 +1,410 @@ +// musl as a whole is licensed under the following standard MIT license: +// +// ---------------------------------------------------------------------- +// Copyright © 2005-2020 Rich Felker, et al. +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ---------------------------------------------------------------------- +// +// Authors/contributors include: +// +// A. Wilcox +// Ada Worcester +// Alex Dowad +// Alex Suykov +// Alexander Monakov +// Andre McCurdy +// Andrew Kelley +// Anthony G. Basile +// Aric Belsito +// Arvid Picciani +// Bartosz Brachaczek +// Benjamin Peterson +// Bobby Bingham +// Boris Brezillon +// Brent Cook +// Chris Spiegel +// Clément Vasseur +// Daniel Micay +// Daniel Sabogal +// Daurnimator +// David Carlier +// David Edelsohn +// Denys Vlasenko +// Dmitry Ivanov +// Dmitry V. Levin +// Drew DeVault +// Emil Renner Berthing +// Fangrui Song +// Felix Fietkau +// Felix Janda +// Gianluca Anzolin +// Hauke Mehrtens +// He X +// Hiltjo Posthuma +// Isaac Dunham +// Jaydeep Patil +// Jens Gustedt +// Jeremy Huntwork +// Jo-Philipp Wich +// Joakim Sindholt +// John Spencer +// Julien Ramseier +// Justin Cormack +// Kaarle Ritvanen +// Khem Raj +// Kylie McClain +// Leah Neukirchen +// Luca Barbato +// Luka Perkov +// M Farkas-Dyck (Strake) +// Mahesh Bodapati +// Markus Wichmann +// Masanori Ogino +// Michael Clark +// Michael Forney +// Mikhail Kremnyov +// Natanael Copa +// Nicholas J. Kain +// orc +// Pascal Cuoq +// Patrick Oppenlander +// Petr Hosek +// Petr Skocik +// Pierre Carrier +// Reini Urban +// Rich Felker +// Richard Pennington +// Ryan Fairfax +// Samuel Holland +// Segev Finer +// Shiz +// sin +// Solar Designer +// Stefan Kristiansson +// Stefan O'Rear +// Szabolcs Nagy +// Timo Teräs +// Trutz Behn +// Valentin Ochs +// Will Dietz +// William Haddon +// William Pitcock +// +// Portions of this software are derived from third-party works licensed +// under terms compatible with the above MIT license: +// +// The TRE regular expression implementation (src/regex/reg* and +// src/regex/tre*) is Copyright © 2001-2008 Ville Laurikari and licensed +// under a 2-clause BSD license (license text in the source files). The +// included version has been heavily modified by Rich Felker in 2012, in +// the interests of size, simplicity, and namespace cleanliness. +// +// Much of the math library code (src/math/* and src/complex/*) is +// Copyright © 1993,2004 Sun Microsystems or +// Copyright © 2003-2011 David Schultz or +// Copyright © 2003-2009 Steven G. Kargl or +// Copyright © 2003-2009 Bruce D. Evans or +// Copyright © 2008 Stephen L. Moshier or +// Copyright © 2017-2018 Arm Limited +// and labelled as such in comments in the individual source files. All +// have been licensed under extremely permissive terms. +// +// The ARM memcpy code (src/string/arm/memcpy.S) is Copyright © 2008 +// The Android Open Source Project and is licensed under a two-clause BSD +// license. It was taken from Bionic libc, used on Android. +// +// The AArch64 memcpy and memset code (src/string/aarch64/*) are +// Copyright © 1999-2019, Arm Limited. +// +// The implementation of DES for crypt (src/crypt/crypt_des.c) is +// Copyright © 1994 David Burren. It is licensed under a BSD license. +// +// The implementation of blowfish crypt (src/crypt/crypt_blowfish.c) was +// originally written by Solar Designer and placed into the public +// domain. The code also comes with a fallback permissive license for use +// in jurisdictions that may not recognize the public domain. +// +// The smoothsort implementation (src/stdlib/qsort.c) is Copyright © 2011 +// Valentin Ochs and is licensed under an MIT-style license. +// +// The x86_64 port was written by Nicholas J. Kain and is licensed under +// the standard MIT terms. +// +// The mips and microblaze ports were originally written by Richard +// Pennington for use in the ellcc project. The original code was adapted +// by Rich Felker for build system and code conventions during upstream +// integration. It is licensed under the standard MIT terms. +// +// The mips64 port was contributed by Imagination Technologies and is +// licensed under the standard MIT terms. +// +// The powerpc port was also originally written by Richard Pennington, +// and later supplemented and integrated by John Spencer. It is licensed +// under the standard MIT terms. +// +// All other files which have no copyright comments are original works +// produced specifically for use as part of this library, written either +// by Rich Felker, the main author of the library, or by one or more +// contibutors listed above. Details on authorship of individual files +// can be found in the git version control history of the project. The +// omission of copyright and license comments in each file is in the +// interest of source tree size. +// +// In addition, permission is hereby granted for all public header files +// (include/* and arch/*/bits/*) and crt files intended to be linked into +// applications (crt/*, ldso/dlstart.c, and arch/*/crt_arch.h) to omit +// the copyright notice and permission notice otherwise required by the +// license, and to use these files without any requirement of +// attribution. These files include substantial contributions from: +// +// Bobby Bingham +// John Spencer +// Nicholas J. Kain +// Rich Felker +// Richard Pennington +// Stefan Kristiansson +// Szabolcs Nagy +// +// all of whom have explicitly granted such permission. +// +// This file previously contained text expressing a belief that most of +// the files covered by the above exception were sufficiently trivial not +// to be subject to copyright, resulting in confusion over whether it +// negated the permissions granted in the license. In the spirit of +// permissive licensing, and of not having licensing issues being an +// obstacle to adoption, that text has been removed. + + +use std::fmt; + +/// A date/time type which exists primarily to convert `SystemTime` timestamps into an ISO 8601 +/// formatted string. +/// +/// Yes, this exists. Before you have a heart attack, understand that the meat of this is musl's +/// [`__secs_to_tm`][1] converted to Rust via [c2rust][2] and then cleaned up by hand as part of +/// the [kudu-rs project][3], [released under MIT][4]. +/// +/// [1] http://git.musl-libc.org/cgit/musl/tree/src/time/__secs_to_tm.c +/// [2] https://c2rust.com/ +/// [3] https://github.com/danburkert/kudu-rs/blob/c9660067e5f4c1a54143f169b5eeb49446f82e54/src/timestamp.rs#L5-L18 +/// [4] https://github.com/tokio-rs/tracing/issues/1644#issuecomment-963888244 +/// +/// All existing `strftime`-like APIs I found were unable to handle the full range of timestamps representable +/// by `SystemTime`, including `strftime` itself, since tm.tm_year is an int. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct DateTime { + year: i64, + month: u8, + day: u8, + hour: u8, + minute: u8, + second: u8, + nanos: u32, +} + +impl fmt::Display for DateTime { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.year > 9999 { + write!(f, "+{}", self.year)?; + } else if self.year < 0 { + write!(f, "{:05}", self.year)?; + } else { + write!(f, "{:04}", self.year)?; + } + + write!( + f, + "-{:02}-{:02}T{:02}:{:02}:{:02}.{:06}Z", + self.month, + self.day, + self.hour, + self.minute, + self.second, + self.nanos / 1_000 + ) + } +} + +impl From<std::time::SystemTime> for DateTime { + fn from(timestamp: std::time::SystemTime) -> DateTime { + let (t, nanos) = match timestamp.duration_since(std::time::UNIX_EPOCH) { + Ok(duration) => { + debug_assert!(duration.as_secs() <= std::i64::MAX as u64); + (duration.as_secs() as i64, duration.subsec_nanos()) + } + Err(error) => { + let duration = error.duration(); + debug_assert!(duration.as_secs() <= std::i64::MAX as u64); + let (secs, nanos) = (duration.as_secs() as i64, duration.subsec_nanos()); + if nanos == 0 { + (-secs, 0) + } else { + (-secs - 1, 1_000_000_000 - nanos) + } + } + }; + + // 2000-03-01 (mod 400 year, immediately after feb29 + const LEAPOCH: i64 = 946_684_800 + 86400 * (31 + 29); + const DAYS_PER_400Y: i32 = 365 * 400 + 97; + const DAYS_PER_100Y: i32 = 365 * 100 + 24; + const DAYS_PER_4Y: i32 = 365 * 4 + 1; + static DAYS_IN_MONTH: [i8; 12] = [31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29]; + + // Note(dcb): this bit is rearranged slightly to avoid integer overflow. + let mut days: i64 = (t / 86_400) - (LEAPOCH / 86_400); + let mut remsecs: i32 = (t % 86_400) as i32; + if remsecs < 0i32 { + remsecs += 86_400; + days -= 1 + } + + let mut qc_cycles: i32 = (days / i64::from(DAYS_PER_400Y)) as i32; + let mut remdays: i32 = (days % i64::from(DAYS_PER_400Y)) as i32; + if remdays < 0 { + remdays += DAYS_PER_400Y; + qc_cycles -= 1; + } + + let mut c_cycles: i32 = remdays / DAYS_PER_100Y; + if c_cycles == 4 { + c_cycles -= 1; + } + remdays -= c_cycles * DAYS_PER_100Y; + + let mut q_cycles: i32 = remdays / DAYS_PER_4Y; + if q_cycles == 25 { + q_cycles -= 1; + } + remdays -= q_cycles * DAYS_PER_4Y; + + let mut remyears: i32 = remdays / 365; + if remyears == 4 { + remyears -= 1; + } + remdays -= remyears * 365; + + let mut years: i64 = i64::from(remyears) + + 4 * i64::from(q_cycles) + + 100 * i64::from(c_cycles) + + 400 * i64::from(qc_cycles); + + let mut months: i32 = 0; + while i32::from(DAYS_IN_MONTH[months as usize]) <= remdays { + remdays -= i32::from(DAYS_IN_MONTH[months as usize]); + months += 1 + } + + if months >= 10 { + months -= 12; + years += 1; + } + + DateTime { + year: years + 2000, + month: (months + 3) as u8, + day: (remdays + 1) as u8, + hour: (remsecs / 3600) as u8, + minute: (remsecs / 60 % 60) as u8, + second: (remsecs % 60) as u8, + nanos, + } + } +} + +#[cfg(test)] +mod tests { + use std::i32; + use std::time::{Duration, UNIX_EPOCH}; + + use super::*; + + #[test] + fn test_datetime() { + let case = |expected: &str, secs: i64, micros: u32| { + let timestamp = if secs >= 0 { + UNIX_EPOCH + Duration::new(secs as u64, micros * 1_000) + } else { + (UNIX_EPOCH - Duration::new(!secs as u64 + 1, 0)) + Duration::new(0, micros * 1_000) + }; + assert_eq!( + expected, + format!("{}", DateTime::from(timestamp)), + "secs: {}, micros: {}", + secs, + micros + ) + }; + + // Mostly generated with: + // - date -jur <secs> +"%Y-%m-%dT%H:%M:%S.000000Z" + // - http://unixtimestamp.50x.eu/ + + case("1970-01-01T00:00:00.000000Z", 0, 0); + + case("1970-01-01T00:00:00.000001Z", 0, 1); + case("1970-01-01T00:00:00.500000Z", 0, 500_000); + case("1970-01-01T00:00:01.000001Z", 1, 1); + case("1970-01-01T00:01:01.000001Z", 60 + 1, 1); + case("1970-01-01T01:01:01.000001Z", 60 * 60 + 60 + 1, 1); + case( + "1970-01-02T01:01:01.000001Z", + 24 * 60 * 60 + 60 * 60 + 60 + 1, + 1, + ); + + case("1969-12-31T23:59:59.000000Z", -1, 0); + case("1969-12-31T23:59:59.000001Z", -1, 1); + case("1969-12-31T23:59:59.500000Z", -1, 500_000); + case("1969-12-31T23:58:59.000001Z", -60 - 1, 1); + case("1969-12-31T22:58:59.000001Z", -60 * 60 - 60 - 1, 1); + case( + "1969-12-30T22:58:59.000001Z", + -24 * 60 * 60 - 60 * 60 - 60 - 1, + 1, + ); + + case("2038-01-19T03:14:07.000000Z", std::i32::MAX as i64, 0); + case("2038-01-19T03:14:08.000000Z", std::i32::MAX as i64 + 1, 0); + case("1901-12-13T20:45:52.000000Z", i32::MIN as i64, 0); + case("1901-12-13T20:45:51.000000Z", i32::MIN as i64 - 1, 0); + + // Skipping these tests on windows as std::time::SysteTime range is low + // on Windows compared with that of Unix which can cause the following + // high date value tests to panic + #[cfg(not(target_os = "windows"))] + { + case("+292277026596-12-04T15:30:07.000000Z", std::i64::MAX, 0); + case("+292277026596-12-04T15:30:06.000000Z", std::i64::MAX - 1, 0); + case("-292277022657-01-27T08:29:53.000000Z", i64::MIN + 1, 0); + } + + case("1900-01-01T00:00:00.000000Z", -2208988800, 0); + case("1899-12-31T23:59:59.000000Z", -2208988801, 0); + case("0000-01-01T00:00:00.000000Z", -62167219200, 0); + case("-0001-12-31T23:59:59.000000Z", -62167219201, 0); + + case("1234-05-06T07:08:09.000000Z", -23215049511, 0); + case("-1234-05-06T07:08:09.000000Z", -101097651111, 0); + case("2345-06-07T08:09:01.000000Z", 11847456541, 0); + case("-2345-06-07T08:09:01.000000Z", -136154620259, 0); + } +} diff --git a/vendor/tracing-subscriber/src/fmt/time/mod.rs b/vendor/tracing-subscriber/src/fmt/time/mod.rs new file mode 100644 index 000000000..e5b7c83b0 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/time/mod.rs @@ -0,0 +1,138 @@ +//! Formatters for event timestamps. +use crate::fmt::format::Writer; +use std::fmt; +use std::time::Instant; + +mod datetime; + +#[cfg(feature = "time")] +mod time_crate; +#[cfg(feature = "time")] +#[cfg_attr(docsrs, doc(cfg(feature = "time")))] +pub use time_crate::UtcTime; + +#[cfg(feature = "local-time")] +#[cfg_attr(docsrs, doc(cfg(unsound_local_offset, feature = "local-time")))] +pub use time_crate::LocalTime; + +#[cfg(feature = "time")] +#[cfg_attr(docsrs, doc(cfg(feature = "time")))] +pub use time_crate::OffsetTime; + +/// A type that can measure and format the current time. +/// +/// This trait is used by `Format` to include a timestamp with each `Event` when it is logged. +/// +/// Notable default implementations of this trait are `SystemTime` and `()`. The former prints the +/// current time as reported by `std::time::SystemTime`, and the latter does not print the current +/// time at all. `FormatTime` is also automatically implemented for any function pointer with the +/// appropriate signature. +/// +/// The full list of provided implementations can be found in [`time`]. +/// +/// [`time`]: self +pub trait FormatTime { + /// Measure and write out the current time. + /// + /// When `format_time` is called, implementors should get the current time using their desired + /// mechanism, and write it out to the given `fmt::Write`. Implementors must insert a trailing + /// space themselves if they wish to separate the time from subsequent log message text. + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result; +} + +/// Returns a new `SystemTime` timestamp provider. +/// +/// This can then be configured further to determine how timestamps should be +/// configured. +/// +/// This is equivalent to calling +/// ```rust +/// # fn timer() -> tracing_subscriber::fmt::time::SystemTime { +/// tracing_subscriber::fmt::time::SystemTime::default() +/// # } +/// ``` +pub fn time() -> SystemTime { + SystemTime::default() +} + +/// Returns a new `Uptime` timestamp provider. +/// +/// With this timer, timestamps will be formatted with the amount of time +/// elapsed since the timestamp provider was constructed. +/// +/// This can then be configured further to determine how timestamps should be +/// configured. +/// +/// This is equivalent to calling +/// ```rust +/// # fn timer() -> tracing_subscriber::fmt::time::Uptime { +/// tracing_subscriber::fmt::time::Uptime::default() +/// # } +/// ``` +pub fn uptime() -> Uptime { + Uptime::default() +} + +impl<'a, F> FormatTime for &'a F +where + F: FormatTime, +{ + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + (*self).format_time(w) + } +} + +impl FormatTime for () { + fn format_time(&self, _: &mut Writer<'_>) -> fmt::Result { + Ok(()) + } +} + +impl FormatTime for fn(&mut Writer<'_>) -> fmt::Result { + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + (*self)(w) + } +} + +/// Retrieve and print the current wall-clock time. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] +pub struct SystemTime; + +/// Retrieve and print the relative elapsed wall-clock time since an epoch. +/// +/// The `Default` implementation for `Uptime` makes the epoch the current time. +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub struct Uptime { + epoch: Instant, +} + +impl Default for Uptime { + fn default() -> Self { + Uptime { + epoch: Instant::now(), + } + } +} + +impl From<Instant> for Uptime { + fn from(epoch: Instant) -> Self { + Uptime { epoch } + } +} + +impl FormatTime for SystemTime { + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + write!( + w, + "{}", + datetime::DateTime::from(std::time::SystemTime::now()) + ) + } +} + +impl FormatTime for Uptime { + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + let e = self.epoch.elapsed(); + write!(w, "{:4}.{:09}s", e.as_secs(), e.subsec_nanos()) + } +} diff --git a/vendor/tracing-subscriber/src/fmt/time/time_crate.rs b/vendor/tracing-subscriber/src/fmt/time/time_crate.rs new file mode 100644 index 000000000..60d57fd0b --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/time/time_crate.rs @@ -0,0 +1,470 @@ +use crate::fmt::{format::Writer, time::FormatTime, writer::WriteAdaptor}; +use std::fmt; +use time::{format_description::well_known, formatting::Formattable, OffsetDateTime, UtcOffset}; + +/// Formats the current [local time] using a [formatter] from the [`time` crate]. +/// +/// To format the current [UTC time] instead, use the [`UtcTime`] type. +/// +/// <div class="example-wrap" style="display:inline-block"> +/// <pre class="compile_fail" style="white-space:normal;font:inherit;"> +/// <strong>Warning</strong>: The <a href = "https://docs.rs/time/0.3/time/"><code>time</code> +/// crate</a> must be compiled with <code>--cfg unsound_local_offset</code> in order to use +/// local timestamps. When this cfg is not enabled, local timestamps cannot be recorded, and +/// events will be logged without timestamps. +/// +/// Alternatively, [`OffsetTime`] can log with a local offset if it is initialized early. +/// +/// See the <a href="https://docs.rs/time/0.3.4/time/#feature-flags"><code>time</code> +/// documentation</a> for more details. +/// </pre></div> +/// +/// [local time]: time::OffsetDateTime::now_local +/// [UTC time]: time::OffsetDateTime::now_utc +/// [formatter]: time::formatting::Formattable +/// [`time` crate]: time +#[derive(Clone, Debug)] +#[cfg_attr( + docsrs, + doc(cfg(all(unsound_local_offset, feature = "time", feature = "local-time"))) +)] +#[cfg(feature = "local-time")] +pub struct LocalTime<F> { + format: F, +} + +/// Formats the current [UTC time] using a [formatter] from the [`time` crate]. +/// +/// To format the current [local time] instead, use the [`LocalTime`] type. +/// +/// [local time]: time::OffsetDateTime::now_local +/// [UTC time]: time::OffsetDateTime::now_utc +/// [formatter]: time::formatting::Formattable +/// [`time` crate]: time +#[cfg_attr(docsrs, doc(cfg(feature = "time")))] +#[derive(Clone, Debug)] +pub struct UtcTime<F> { + format: F, +} + +/// Formats the current time using a fixed offset and a [formatter] from the [`time` crate]. +/// +/// This is typically used as an alternative to [`LocalTime`]. `LocalTime` determines the offset +/// every time it formats a message, which may be unsound or fail. With `OffsetTime`, the offset is +/// determined once. This makes it possible to do so while the program is still single-threaded and +/// handle any errors. However, this also means the offset cannot change while the program is +/// running (the offset will not change across DST changes). +/// +/// [formatter]: time::formatting::Formattable +/// [`time` crate]: time +#[derive(Clone, Debug)] +#[cfg_attr(docsrs, doc(cfg(feature = "time")))] +pub struct OffsetTime<F> { + offset: time::UtcOffset, + format: F, +} + +// === impl LocalTime === + +#[cfg(feature = "local-time")] +impl LocalTime<well_known::Rfc3339> { + /// Returns a formatter that formats the current [local time] in the + /// [RFC 3339] format (a subset of the [ISO 8601] timestamp format). + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time}; + /// + /// let collector = tracing_subscriber::fmt() + /// .with_timer(time::LocalTime::rfc_3339()); + /// # drop(collector); + /// ``` + /// + /// [local time]: time::OffsetDateTime::now_local + /// [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339 + /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 + pub fn rfc_3339() -> Self { + Self::new(well_known::Rfc3339) + } +} + +#[cfg(feature = "local-time")] +impl<F: Formattable> LocalTime<F> { + /// Returns a formatter that formats the current [local time] using the + /// [`time` crate] with the provided provided format. The format may be any + /// type that implements the [`Formattable`] trait. + /// + /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="compile_fail" style="white-space:normal;font:inherit;"> + /// <strong>Warning</strong>: The <a href = "https://docs.rs/time/0.3/time/"> + /// <code>time</code> crate</a> must be compiled with <code>--cfg + /// unsound_local_offset</code> in order to use local timestamps. When this + /// cfg is not enabled, local timestamps cannot be recorded, and + /// events will be logged without timestamps. + /// + /// See the <a href="https://docs.rs/time/0.3.4/time/#feature-flags"> + /// <code>time</code> documentation</a> for more details. + /// </pre></div> + /// + /// Typically, the format will be a format description string, or one of the + /// `time` crate's [well-known formats]. + /// + /// If the format description is statically known, then the + /// [`format_description!`] macro should be used. This is identical to the + /// [`time::format_description::parse`] method, but runs at compile-time, + /// throwing an error if the format description is invalid. If the desired format + /// is not known statically (e.g., a user is providing a format string), then the + /// [`time::format_description::parse`] method should be used. Note that this + /// method is fallible. + /// + /// See the [`time` book] for details on the format description syntax. + /// + /// # Examples + /// + /// Using the [`format_description!`] macro: + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::LocalTime}; + /// use time::macros::format_description; + /// + /// let timer = LocalTime::new(format_description!("[hour]:[minute]:[second]")); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// Using [`time::format_description::parse`]: + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::LocalTime}; + /// + /// let time_format = time::format_description::parse("[hour]:[minute]:[second]") + /// .expect("format string should be valid!"); + /// let timer = LocalTime::new(time_format); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// Using the [`format_description!`] macro requires enabling the `time` + /// crate's "macros" feature flag. + /// + /// Using a [well-known format][well-known formats] (this is equivalent to + /// [`LocalTime::rfc_3339`]): + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::LocalTime}; + /// + /// let timer = LocalTime::new(time::format_description::well_known::Rfc3339); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// [local time]: time::OffsetDateTime::now_local() + /// [`time` crate]: time + /// [`Formattable`]: time::formatting::Formattable + /// [well-known formats]: time::format_description::well_known + /// [`format_description!`]: time::macros::format_description! + /// [`time::format_description::parse`]: time::format_description::parse() + /// [`time` book]: https://time-rs.github.io/book/api/format-description.html + pub fn new(format: F) -> Self { + Self { format } + } +} + +#[cfg(feature = "local-time")] +impl<F> FormatTime for LocalTime<F> +where + F: Formattable, +{ + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + let now = OffsetDateTime::now_local().map_err(|_| fmt::Error)?; + format_datetime(now, w, &self.format) + } +} + +#[cfg(feature = "local-time")] +impl<F> Default for LocalTime<F> +where + F: Formattable + Default, +{ + fn default() -> Self { + Self::new(F::default()) + } +} + +// === impl UtcTime === + +impl UtcTime<well_known::Rfc3339> { + /// Returns a formatter that formats the current [UTC time] in the + /// [RFC 3339] format, which is a subset of the [ISO 8601] timestamp format. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time}; + /// + /// let collector = tracing_subscriber::fmt() + /// .with_timer(time::UtcTime::rfc_3339()); + /// # drop(collector); + /// ``` + /// + /// [local time]: time::OffsetDateTime::now_utc + /// [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339 + /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 + pub fn rfc_3339() -> Self { + Self::new(well_known::Rfc3339) + } +} + +impl<F: Formattable> UtcTime<F> { + /// Returns a formatter that formats the current [UTC time] using the + /// [`time` crate], with the provided provided format. The format may be any + /// type that implements the [`Formattable`] trait. + /// + /// Typically, the format will be a format description string, or one of the + /// `time` crate's [well-known formats]. + /// + /// If the format description is statically known, then the + /// [`format_description!`] macro should be used. This is identical to the + /// [`time::format_description::parse`] method, but runs at compile-time, + /// failing an error if the format description is invalid. If the desired format + /// is not known statically (e.g., a user is providing a format string), then the + /// [`time::format_description::parse`] method should be used. Note that this + /// method is fallible. + /// + /// See the [`time` book] for details on the format description syntax. + /// + /// # Examples + /// + /// Using the [`format_description!`] macro: + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::UtcTime}; + /// use time::macros::format_description; + /// + /// let timer = UtcTime::new(format_description!("[hour]:[minute]:[second]")); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// Using the [`format_description!`] macro requires enabling the `time` + /// crate's "macros" feature flag. + /// + /// Using [`time::format_description::parse`]: + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::UtcTime}; + /// + /// let time_format = time::format_description::parse("[hour]:[minute]:[second]") + /// .expect("format string should be valid!"); + /// let timer = UtcTime::new(time_format); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// Using a [well-known format][well-known formats] (this is equivalent to + /// [`UtcTime::rfc_3339`]): + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::UtcTime}; + /// + /// let timer = UtcTime::new(time::format_description::well_known::Rfc3339); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// [UTC time]: time::OffsetDateTime::now_utc() + /// [`time` crate]: time + /// [`Formattable`]: time::formatting::Formattable + /// [well-known formats]: time::format_description::well_known + /// [`format_description!`]: time::macros::format_description! + /// [`time::format_description::parse`]: time::format_description::parse + /// [`time` book]: https://time-rs.github.io/book/api/format-description.html + pub fn new(format: F) -> Self { + Self { format } + } +} + +impl<F> FormatTime for UtcTime<F> +where + F: Formattable, +{ + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + format_datetime(OffsetDateTime::now_utc(), w, &self.format) + } +} + +impl<F> Default for UtcTime<F> +where + F: Formattable + Default, +{ + fn default() -> Self { + Self::new(F::default()) + } +} + +// === impl OffsetTime === + +#[cfg(feature = "local-time")] +impl OffsetTime<well_known::Rfc3339> { + /// Returns a formatter that formats the current time using the [local time offset] in the [RFC + /// 3339] format (a subset of the [ISO 8601] timestamp format). + /// + /// Returns an error if the local time offset cannot be determined. This typically occurs in + /// multithreaded programs. To avoid this problem, initialize `OffsetTime` before forking + /// threads. When using Tokio, this means initializing `OffsetTime` before the Tokio runtime. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time}; + /// + /// let collector = tracing_subscriber::fmt() + /// .with_timer(time::OffsetTime::local_rfc_3339().expect("could not get local offset!")); + /// # drop(collector); + /// ``` + /// + /// Using `OffsetTime` with Tokio: + /// + /// ``` + /// use tracing_subscriber::fmt::time::OffsetTime; + /// + /// #[tokio::main] + /// async fn run() { + /// tracing::info!("runtime initialized"); + /// + /// // At this point the Tokio runtime is initialized, and we can use both Tokio and Tracing + /// // normally. + /// } + /// + /// fn main() { + /// // Because we need to get the local offset before Tokio spawns any threads, our `main` + /// // function cannot use `tokio::main`. + /// tracing_subscriber::fmt() + /// .with_timer(OffsetTime::local_rfc_3339().expect("could not get local time offset")) + /// .init(); + /// + /// // Even though `run` is written as an `async fn`, because we used `tokio::main` on it + /// // we can call it as a synchronous function. + /// run(); + /// } + /// ``` + /// + /// [local time offset]: time::UtcOffset::current_local_offset + /// [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339 + /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 + pub fn local_rfc_3339() -> Result<Self, time::error::IndeterminateOffset> { + Ok(Self::new( + UtcOffset::current_local_offset()?, + well_known::Rfc3339, + )) + } +} + +impl<F: time::formatting::Formattable> OffsetTime<F> { + /// Returns a formatter that formats the current time using the [`time` crate] with the provided + /// provided format and [timezone offset]. The format may be any type that implements the + /// [`Formattable`] trait. + /// + /// + /// Typically, the offset will be the [local offset], and format will be a format description + /// string, or one of the `time` crate's [well-known formats]. + /// + /// If the format description is statically known, then the + /// [`format_description!`] macro should be used. This is identical to the + /// [`time::format_description::parse`] method, but runs at compile-time, + /// throwing an error if the format description is invalid. If the desired format + /// is not known statically (e.g., a user is providing a format string), then the + /// [`time::format_description::parse`] method should be used. Note that this + /// method is fallible. + /// + /// See the [`time` book] for details on the format description syntax. + /// + /// # Examples + /// + /// Using the [`format_description!`] macro: + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::OffsetTime}; + /// use time::macros::format_description; + /// use time::UtcOffset; + /// + /// let offset = UtcOffset::current_local_offset().expect("should get local offset!"); + /// let timer = OffsetTime::new(offset, format_description!("[hour]:[minute]:[second]")); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// Using [`time::format_description::parse`]: + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::OffsetTime}; + /// use time::UtcOffset; + /// + /// let offset = UtcOffset::current_local_offset().expect("should get local offset!"); + /// let time_format = time::format_description::parse("[hour]:[minute]:[second]") + /// .expect("format string should be valid!"); + /// let timer = OffsetTime::new(offset, time_format); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// Using the [`format_description!`] macro requires enabling the `time` + /// crate's "macros" feature flag. + /// + /// Using a [well-known format][well-known formats] (this is equivalent to + /// [`OffsetTime::local_rfc_3339`]): + /// + /// ``` + /// use tracing_subscriber::fmt::{self, time::OffsetTime}; + /// use time::UtcOffset; + /// + /// let offset = UtcOffset::current_local_offset().expect("should get local offset!"); + /// let timer = OffsetTime::new(offset, time::format_description::well_known::Rfc3339); + /// let collector = tracing_subscriber::fmt() + /// .with_timer(timer); + /// # drop(collector); + /// ``` + /// + /// [`time` crate]: time + /// [timezone offset]: time::UtcOffset + /// [`Formattable`]: time::formatting::Formattable + /// [local offset]: time::UtcOffset::current_local_offset() + /// [well-known formats]: time::format_description::well_known + /// [`format_description!`]: time::macros::format_description + /// [`time::format_description::parse`]: time::format_description::parse + /// [`time` book]: https://time-rs.github.io/book/api/format-description.html + pub fn new(offset: time::UtcOffset, format: F) -> Self { + Self { offset, format } + } +} + +impl<F> FormatTime for OffsetTime<F> +where + F: time::formatting::Formattable, +{ + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { + let now = OffsetDateTime::now_utc().to_offset(self.offset); + format_datetime(now, w, &self.format) + } +} + +fn format_datetime( + now: OffsetDateTime, + into: &mut Writer<'_>, + fmt: &impl Formattable, +) -> fmt::Result { + let mut into = WriteAdaptor::new(into); + now.format_into(&mut into, fmt) + .map_err(|_| fmt::Error) + .map(|_| ()) +} diff --git a/vendor/tracing-subscriber/src/fmt/writer.rs b/vendor/tracing-subscriber/src/fmt/writer.rs new file mode 100644 index 000000000..4aacd6d54 --- /dev/null +++ b/vendor/tracing-subscriber/src/fmt/writer.rs @@ -0,0 +1,1464 @@ +//! Abstractions for creating [`io::Write`] instances. +//! +//! [`io::Write`]: std::io::Write +use std::{ + fmt, + io::{self, Write}, + sync::{Arc, Mutex, MutexGuard}, +}; +use tracing_core::Metadata; + +/// A type that can create [`io::Write`] instances. +/// +/// `MakeWriter` is used by [`fmt::Layer`] or [`fmt::Subscriber`] to print +/// formatted text representations of [`Event`]s. +/// +/// This trait is already implemented for function pointers and +/// immutably-borrowing closures that return an instance of [`io::Write`], such +/// as [`io::stdout`] and [`io::stderr`]. Additionally, it is implemented for +/// [`std::sync::Mutex`][mutex] when the tyoe inside the mutex implements +/// [`io::Write`]. +/// +/// # Examples +/// +/// The simplest usage is to pass in a named function that returns a writer. For +/// example, to log all events to stderr, we could write: +/// ``` +/// let subscriber = tracing_subscriber::fmt() +/// .with_writer(std::io::stderr) +/// .finish(); +/// # drop(subscriber); +/// ``` +/// +/// Any function that returns a writer can be used: +/// +/// ``` +/// fn make_my_great_writer() -> impl std::io::Write { +/// // ... +/// # std::io::stdout() +/// } +/// +/// let subscriber = tracing_subscriber::fmt() +/// .with_writer(make_my_great_writer) +/// .finish(); +/// # drop(subscriber); +/// ``` +/// +/// A closure can be used to introduce arbitrary logic into how the writer is +/// created. Consider the (admittedly rather silly) example of sending every 5th +/// event to stderr, and all other events to stdout: +/// +/// ``` +/// use std::io; +/// use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; +/// +/// let n = AtomicUsize::new(0); +/// let subscriber = tracing_subscriber::fmt() +/// .with_writer(move || -> Box<dyn io::Write> { +/// if n.fetch_add(1, Relaxed) % 5 == 0 { +/// Box::new(io::stderr()) +/// } else { +/// Box::new(io::stdout()) +/// } +/// }) +/// .finish(); +/// # drop(subscriber); +/// ``` +/// +/// A single instance of a type implementing [`io::Write`] may be used as a +/// `MakeWriter` by wrapping it in a [`Mutex`][mutex]. For example, we could +/// write to a file like so: +/// +/// ``` +/// use std::{fs::File, sync::Mutex}; +/// +/// # fn docs() -> Result<(), Box<dyn std::error::Error>> { +/// let log_file = File::create("my_cool_trace.log")?; +/// let subscriber = tracing_subscriber::fmt() +/// .with_writer(Mutex::new(log_file)) +/// .finish(); +/// # drop(subscriber); +/// # Ok(()) +/// # } +/// ``` +/// +/// [`io::Write`]: std::io::Write +/// [`fmt::Layer`]: crate::fmt::Layer +/// [`fmt::Subscriber`]: crate::fmt::Subscriber +/// [`Event`]: tracing_core::event::Event +/// [`io::stdout`]: std::io::stdout() +/// [`io::stderr`]: std::io::stderr() +/// [mutex]: std::sync::Mutex +/// [`MakeWriter::make_writer_for`]: MakeWriter::make_writer_for +/// [`Metadata`]: tracing_core::Metadata +/// [levels]: tracing_core::Level +/// [targets]: tracing_core::Metadata::target +pub trait MakeWriter<'a> { + /// The concrete [`io::Write`] implementation returned by [`make_writer`]. + /// + /// [`io::Write`]: std::io::Write + /// [`make_writer`]: MakeWriter::make_writer + type Writer: io::Write; + + /// Returns an instance of [`Writer`]. + /// + /// # Implementer notes + /// + /// [`fmt::Layer`] or [`fmt::Subscriber`] will call this method each time an event is recorded. Ensure any state + /// that must be saved across writes is not lost when the [`Writer`] instance is dropped. If + /// creating a [`io::Write`] instance is expensive, be sure to cache it when implementing + /// [`MakeWriter`] to improve performance. + /// + /// [`Writer`]: MakeWriter::Writer + /// [`fmt::Layer`]: crate::fmt::Layer + /// [`fmt::Subscriber`]: crate::fmt::Subscriber + /// [`io::Write`]: std::io::Write + fn make_writer(&'a self) -> Self::Writer; + + /// Returns a [`Writer`] for writing data from the span or event described + /// by the provided [`Metadata`]. + /// + /// By default, this calls [`self.make_writer()`][make_writer], ignoring + /// the provided metadata, but implementations can override this to provide + /// metadata-specific behaviors. + /// + /// This method allows `MakeWriter` implementations to implement different + /// behaviors based on the span or event being written. The `MakeWriter` + /// type might return different writers based on the provided metadata, or + /// might write some values to the writer before or after providing it to + /// the caller. + /// + /// For example, we might want to write data from spans and events at the + /// [`ERROR`] and [`WARN`] levels to `stderr`, and data from spans or events + /// at lower levels to stdout: + /// + /// ``` + /// use std::io::{self, Stdout, Stderr, StdoutLock, StderrLock}; + /// use tracing_subscriber::fmt::writer::MakeWriter; + /// use tracing_core::{Metadata, Level}; + /// + /// pub struct MyMakeWriter { + /// stdout: Stdout, + /// stderr: Stderr, + /// } + /// + /// /// A lock on either stdout or stderr, depending on the verbosity level + /// /// of the event being written. + /// pub enum StdioLock<'a> { + /// Stdout(StdoutLock<'a>), + /// Stderr(StderrLock<'a>), + /// } + /// + /// impl<'a> io::Write for StdioLock<'a> { + /// fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + /// match self { + /// StdioLock::Stdout(lock) => lock.write(buf), + /// StdioLock::Stderr(lock) => lock.write(buf), + /// } + /// } + /// + /// fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + /// // ... + /// # match self { + /// # StdioLock::Stdout(lock) => lock.write_all(buf), + /// # StdioLock::Stderr(lock) => lock.write_all(buf), + /// # } + /// } + /// + /// fn flush(&mut self) -> io::Result<()> { + /// // ... + /// # match self { + /// # StdioLock::Stdout(lock) => lock.flush(), + /// # StdioLock::Stderr(lock) => lock.flush(), + /// # } + /// } + /// } + /// + /// impl<'a> MakeWriter<'a> for MyMakeWriter { + /// type Writer = StdioLock<'a>; + /// + /// fn make_writer(&'a self) -> Self::Writer { + /// // We must have an implementation of `make_writer` that makes + /// // a "default" writer without any configuring metadata. Let's + /// // just return stdout in that case. + /// StdioLock::Stdout(self.stdout.lock()) + /// } + /// + /// fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + /// // Here's where we can implement our special behavior. We'll + /// // check if the metadata's verbosity level is WARN or ERROR, + /// // and return stderr in that case. + /// if meta.level() <= &Level::WARN { + /// return StdioLock::Stderr(self.stderr.lock()); + /// } + /// + /// // Otherwise, we'll return stdout. + /// StdioLock::Stdout(self.stdout.lock()) + /// } + /// } + /// ``` + /// + /// [`Writer`]: MakeWriter::Writer + /// [`Metadata`]: tracing_core::Metadata + /// [make_writer]: MakeWriter::make_writer + /// [`WARN`]: tracing_core::Level::WARN + /// [`ERROR`]: tracing_core::Level::ERROR + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + let _ = meta; + self.make_writer() + } +} + +/// Extension trait adding combinators for working with types implementing +/// [`MakeWriter`]. +/// +/// This is not intended to be implemented directly for user-defined +/// [`MakeWriter`]s; instead, it should be imported when the desired methods are +/// used. +pub trait MakeWriterExt<'a>: MakeWriter<'a> { + /// Wraps `self` and returns a [`MakeWriter`] that will only write output + /// for events at or below the provided verbosity [`Level`]. For instance, + /// `Level::TRACE` is considered to be _more verbose` than `Level::INFO`. + /// + /// Events whose level is more verbose than `level` will be ignored, and no + /// output will be written. + /// + /// # Examples + /// + /// ``` + /// use tracing::Level; + /// use tracing_subscriber::fmt::writer::MakeWriterExt; + /// + /// // Construct a writer that outputs events to `stderr` only if the span or + /// // event's level is >= WARN (WARN and ERROR). + /// let mk_writer = std::io::stderr.with_max_level(Level::WARN); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// ``` + /// + /// Writing the `ERROR` and `WARN` levels to `stderr`, and everything else + /// to `stdout`: + /// + /// ``` + /// # use tracing::Level; + /// # use tracing_subscriber::fmt::writer::MakeWriterExt; + /// + /// let mk_writer = std::io::stderr + /// .with_max_level(Level::WARN) + /// .or_else(std::io::stdout); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// ``` + /// + /// Writing the `ERROR` level to `stderr`, the `INFO` and `WARN` levels to + /// `stdout`, and the `INFO` and DEBUG` levels to a file: + /// + /// ``` + /// # use tracing::Level; + /// # use tracing_subscriber::fmt::writer::MakeWriterExt; + /// use std::{sync::Arc, fs::File}; + /// # // don't actually create the file when running the tests. + /// # fn docs() -> std::io::Result<()> { + /// let debug_log = Arc::new(File::create("debug.log")?); + /// + /// let mk_writer = std::io::stderr + /// .with_max_level(Level::ERROR) + /// .or_else(std::io::stdout + /// .with_max_level(Level::INFO) + /// .and(debug_log.with_max_level(Level::DEBUG)) + /// ); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// # Ok(()) } + /// ``` + /// + /// [`Level`]: tracing_core::Level + /// [`io::Write`]: std::io::Write + fn with_max_level(self, level: tracing_core::Level) -> WithMaxLevel<Self> + where + Self: Sized, + { + WithMaxLevel::new(self, level) + } + + /// Wraps `self` and returns a [`MakeWriter`] that will only write output + /// for events at or above the provided verbosity [`Level`]. + /// + /// Events whose level is less verbose than `level` will be ignored, and no + /// output will be written. + /// + /// # Examples + /// + /// ``` + /// use tracing::Level; + /// use tracing_subscriber::fmt::writer::MakeWriterExt; + /// + /// // Construct a writer that outputs events to `stdout` only if the span or + /// // event's level is <= DEBUG (DEBUG and TRACE). + /// let mk_writer = std::io::stdout.with_min_level(Level::DEBUG); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// ``` + /// This can be combined with [`MakeWriterExt::with_max_level`] to write + /// only within a range of levels: + /// + /// ``` + /// # use tracing::Level; + /// # use tracing_subscriber::fmt::writer::MakeWriterExt; + /// // Only write the `DEBUG` and `INFO` levels to stdout. + /// let mk_writer = std::io::stdout + /// .with_max_level(Level::DEBUG) + /// .with_min_level(Level::INFO) + /// // Write the `WARN` and `ERROR` levels to stderr. + /// .and(std::io::stderr.with_min_level(Level::WARN)); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// ``` + /// [`Level`]: tracing_core::Level + /// [`io::Write`]: std::io::Write + fn with_min_level(self, level: tracing_core::Level) -> WithMinLevel<Self> + where + Self: Sized, + { + WithMinLevel::new(self, level) + } + + /// Wraps `self` with a predicate that takes a span or event's [`Metadata`] + /// and returns a `bool`. The returned [`MakeWriter`]'s + /// [`MakeWriter::make_writer_for`][mwf] method will check the predicate to + /// determine if a writer should be produced for a given span or event. + /// + /// If the predicate returns `false`, the wrapped [`MakeWriter`]'s + /// [`make_writer_for`][mwf] will return [`OptionalWriter::none`][own]. + /// Otherwise, it calls the wrapped [`MakeWriter`]'s + /// [`make_writer_for`][mwf] method, and returns the produced writer. + /// + /// This can be used to filter an output based on arbitrary [`Metadata`] + /// parameters. + /// + /// # Examples + /// + /// Writing events with a specific target to an HTTP access log, and other + /// events to stdout: + /// + /// ``` + /// use tracing_subscriber::fmt::writer::MakeWriterExt; + /// use std::{sync::Arc, fs::File}; + /// # // don't actually create the file when running the tests. + /// # fn docs() -> std::io::Result<()> { + /// let access_log = Arc::new(File::create("access.log")?); + /// + /// let mk_writer = access_log + /// // Only write events with the target "http::access_log" to the + /// // access log file. + /// .with_filter(|meta| meta.target() == "http::access_log") + /// // Write events with all other targets to stdout. + /// .or_else(std::io::stdout); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// # Ok(()) + /// # } + /// ``` + /// + /// Conditionally enabling or disabling a log file: + /// ``` + /// use tracing_subscriber::fmt::writer::MakeWriterExt; + /// use std::{ + /// sync::{Arc, atomic::{AtomicBool, Ordering}}, + /// fs::File, + /// }; + /// + /// static DEBUG_LOG_ENABLED: AtomicBool = AtomicBool::new(false); + /// + /// # // don't actually create the file when running the tests. + /// # fn docs() -> std::io::Result<()> { + /// // Create the debug log file + /// let debug_file = Arc::new(File::create("debug.log")?) + /// // Enable the debug log only if the flag is enabled. + /// .with_filter(|_| DEBUG_LOG_ENABLED.load(Ordering::Acquire)); + /// + /// // Always write to stdout + /// let mk_writer = std::io::stdout + /// // Write to the debug file if it's enabled + /// .and(debug_file); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// + /// // ... + /// + /// // Later, we can toggle on or off the debug log file. + /// DEBUG_LOG_ENABLED.store(true, Ordering::Release); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`Metadata`]: tracing_core::Metadata + /// [mwf]: MakeWriter::make_writer_for + /// [own]: EitherWriter::none + fn with_filter<F>(self, filter: F) -> WithFilter<Self, F> + where + Self: Sized, + F: Fn(&Metadata<'_>) -> bool, + { + WithFilter::new(self, filter) + } + + /// Combines `self` with another type implementing [`MakeWriter`], returning + /// a new [`MakeWriter`] that produces [writers] that write to *both* + /// outputs. + /// + /// If writing to either writer returns an error, the returned writer will + /// return that error. However, both writers will still be written to before + /// the error is returned, so it is possible for one writer to fail while + /// the other is written to successfully. + /// + /// # Examples + /// + /// ``` + /// use tracing_subscriber::fmt::writer::MakeWriterExt; + /// + /// // Construct a writer that outputs events to `stdout` *and* `stderr`. + /// let mk_writer = std::io::stdout.and(std::io::stderr); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// ``` + /// + /// `and` can be used in conjunction with filtering combinators. For + /// example, if we want to write to a number of outputs depending on the + /// level of an event, we could write: + /// + /// ``` + /// use tracing::Level; + /// # use tracing_subscriber::fmt::writer::MakeWriterExt; + /// use std::{sync::Arc, fs::File}; + /// # // don't actually create the file when running the tests. + /// # fn docs() -> std::io::Result<()> { + /// let debug_log = Arc::new(File::create("debug.log")?); + /// + /// // Write everything to the debug log. + /// let mk_writer = debug_log + /// // Write the `ERROR` and `WARN` levels to stderr. + /// .and(std::io::stderr.with_max_level(Level::WARN)) + /// // Write `INFO` to `stdout`. + /// .and(std::io::stdout + /// .with_max_level(Level::INFO) + /// .with_min_level(Level::INFO) + /// ); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// # Ok(()) } + /// ``` + /// + /// [writers]: std::io::Write + fn and<B>(self, other: B) -> Tee<Self, B> + where + Self: Sized, + B: MakeWriter<'a> + Sized, + { + Tee::new(self, other) + } + + /// Combines `self` with another type implementing [`MakeWriter`], returning + /// a new [`MakeWriter`] that calls `other`'s [`make_writer`] if `self`'s + /// `make_writer` returns [`OptionalWriter::none`][own]. + /// + /// # Examples + /// + /// ``` + /// use tracing::Level; + /// use tracing_subscriber::fmt::writer::MakeWriterExt; + /// + /// // Produces a writer that writes to `stderr` if the level is >= WARN, + /// // or returns `OptionalWriter::none()` otherwise. + /// let stderr = std::io::stderr.with_max_level(Level::WARN); + /// + /// // If the `stderr` `MakeWriter` is disabled by the max level filter, + /// // write to stdout instead: + /// let mk_writer = stderr.or_else(std::io::stdout); + /// + /// tracing_subscriber::fmt().with_writer(mk_writer).init(); + /// ``` + /// + /// [`make_writer`]: MakeWriter::make_writer + /// [own]: EitherWriter::none + fn or_else<W, B>(self, other: B) -> OrElse<Self, B> + where + Self: MakeWriter<'a, Writer = OptionalWriter<W>> + Sized, + B: MakeWriter<'a> + Sized, + W: Write, + { + OrElse::new(self, other) + } +} + +/// A writer intended to support [`libtest`'s output capturing][capturing] for use in unit tests. +/// +/// `TestWriter` is used by [`fmt::Subscriber`] or [`fmt::Layer`] to enable capturing support. +/// +/// `cargo test` can only capture output from the standard library's [`print!`] macro. See +/// [`libtest`'s output capturing][capturing] for more details about output capturing. +/// +/// Writing to [`io::stdout`] and [`io::stderr`] produces the same results as using +/// [`libtest`'s `--nocapture` option][nocapture] which may make the results look unreadable. +/// +/// [`fmt::Subscriber`]: super::Subscriber +/// [`fmt::Layer`]: super::Layer +/// [capturing]: https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output +/// [nocapture]: https://doc.rust-lang.org/cargo/commands/cargo-test.html +/// [`io::stdout`]: std::io::stdout +/// [`io::stderr`]: std::io::stderr +/// [`print!`]: std::print! +#[derive(Default, Debug)] +pub struct TestWriter { + _p: (), +} + +/// A writer that erases the specific [`io::Write`] and [`MakeWriter`] types being used. +/// +/// This is useful in cases where the concrete type of the writer cannot be known +/// until runtime. +/// +/// # Examples +/// +/// A function that returns a [`Subscriber`] that will write to either stdout or stderr: +/// +/// ```rust +/// # use tracing::Subscriber; +/// # use tracing_subscriber::fmt::writer::BoxMakeWriter; +/// +/// fn dynamic_writer(use_stderr: bool) -> impl Subscriber { +/// let writer = if use_stderr { +/// BoxMakeWriter::new(std::io::stderr) +/// } else { +/// BoxMakeWriter::new(std::io::stdout) +/// }; +/// +/// tracing_subscriber::fmt().with_writer(writer).finish() +/// } +/// ``` +/// +/// [`Subscriber`]: tracing::Subscriber +/// [`io::Write`]: std::io::Write +pub struct BoxMakeWriter { + inner: Box<dyn for<'a> MakeWriter<'a, Writer = Box<dyn Write + 'a>> + Send + Sync>, + name: &'static str, +} + +/// A [writer] that is one of two types implementing [`io::Write`][writer]. +/// +/// This may be used by [`MakeWriter`] implementations that may conditionally +/// return one of two writers. +/// +/// [writer]: std::io::Write +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum EitherWriter<A, B> { + /// A writer of type `A`. + A(A), + /// A writer of type `B`. + B(B), +} + +/// A [writer] which may or may not be enabled. +/// +/// This may be used by [`MakeWriter`] implementations that wish to +/// conditionally enable or disable the returned writer based on a span or +/// event's [`Metadata`]. +/// +/// [writer]: std::io::Write +pub type OptionalWriter<T> = EitherWriter<T, std::io::Sink>; + +/// A [`MakeWriter`] combinator that only returns an enabled [writer] for spans +/// and events with metadata at or below a specified verbosity [`Level`]. +/// +/// This is returned by the [`MakeWriterExt::with_max_level`] method. See the +/// method documentation for details. +/// +/// [writer]: std::io::Write +/// [`Level`]: tracing_core::Level +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct WithMaxLevel<M> { + make: M, + level: tracing_core::Level, +} + +/// A [`MakeWriter`] combinator that only returns an enabled [writer] for spans +/// and events with metadata at or above a specified verbosity [`Level`]. +/// +/// This is returned by the [`MakeWriterExt::with_min_level`] method. See the +/// method documentation for details. +/// +/// [writer]: std::io::Write +/// [`Level`]: tracing_core::Level +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct WithMinLevel<M> { + make: M, + level: tracing_core::Level, +} + +/// A [`MakeWriter`] combinator that wraps a [`MakeWriter`] with a predicate for +/// span and event [`Metadata`], so that the [`MakeWriter::make_writer_for`] +/// method returns [`OptionalWriter::some`][ows] when the predicate returns `true`, +/// and [`OptionalWriter::none`][own] when the predicate returns `false`. +/// +/// This is returned by the [`MakeWriterExt::with_filter`] method. See the +/// method documentation for details. +/// +/// [`Metadata`]: tracing_core::Metadata +/// [ows]: EitherWriter::some +/// [own]: EitherWriter::none +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct WithFilter<M, F> { + make: M, + filter: F, +} + +/// Combines a [`MakeWriter`] that returns an [`OptionalWriter`] with another +/// [`MakeWriter`], so that the second [`MakeWriter`] is used when the first +/// [`MakeWriter`] returns [`OptionalWriter::none`][own]. +/// +/// This is returned by the [`MakeWriterExt::or_else] method. See the +/// method documentation for details. +/// +/// [own]: EitherWriter::none +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct OrElse<A, B> { + inner: A, + or_else: B, +} + +/// Combines two types implementing [`MakeWriter`] (or [`std::io::Write`]) to +/// produce a writer that writes to both [`MakeWriter`]'s returned writers. +/// +/// This is returned by the [`MakeWriterExt::and`] method. See the method +/// documentation for details. +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct Tee<A, B> { + a: A, + b: B, +} + +/// A type implementing [`io::Write`] for a [`MutexGuard`] where the type +/// inside the [`Mutex`] implements [`io::Write`]. +/// +/// This is used by the [`MakeWriter`] implementation for [`Mutex`], because +/// [`MutexGuard`] itself will not implement [`io::Write`] — instead, it +/// _dereferences_ to a type implementing [`io::Write`]. Because [`MakeWriter`] +/// requires the `Writer` type to implement [`io::Write`], it's necessary to add +/// a newtype that forwards the trait implementation. +/// +/// [`io::Write`]: std::io::Write +/// [`MutexGuard`]: std::sync::MutexGuard +/// [`Mutex`]: std::sync::Mutex +#[derive(Debug)] +pub struct MutexGuardWriter<'a, W>(MutexGuard<'a, W>); + +/// Implements [`std::io::Write`] for an [`Arc`]<W> where `&W: Write`. +/// +/// This is an implementation detail of the [`MakeWriter`] impl for [`Arc`]. +#[derive(Clone, Debug)] +pub struct ArcWriter<W>(Arc<W>); + +/// A bridge between `fmt::Write` and `io::Write`. +/// +/// This is used by the timestamp formatting implementation for the `time` +/// crate and by the JSON formatter. In both cases, this is needed because +/// `tracing-subscriber`'s `FormatEvent`/`FormatTime` traits expect a +/// `fmt::Write` implementation, while `serde_json::Serializer` and `time`'s +/// `format_into` methods expect an `io::Write`. +#[cfg(any(feature = "json", feature = "time"))] +pub(in crate::fmt) struct WriteAdaptor<'a> { + fmt_write: &'a mut dyn fmt::Write, +} + +impl<'a, F, W> MakeWriter<'a> for F +where + F: Fn() -> W, + W: io::Write, +{ + type Writer = W; + + fn make_writer(&'a self) -> Self::Writer { + (self)() + } +} + +impl<'a, W> MakeWriter<'a> for Arc<W> +where + &'a W: io::Write + 'a, +{ + type Writer = &'a W; + fn make_writer(&'a self) -> Self::Writer { + &*self + } +} + +impl<'a> MakeWriter<'a> for std::fs::File { + type Writer = &'a std::fs::File; + fn make_writer(&'a self) -> Self::Writer { + self + } +} + +// === impl TestWriter === + +impl TestWriter { + /// Returns a new `TestWriter` with the default configuration. + pub fn new() -> Self { + Self::default() + } +} + +impl io::Write for TestWriter { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let out_str = String::from_utf8_lossy(buf); + print!("{}", out_str); + Ok(buf.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl<'a> MakeWriter<'a> for TestWriter { + type Writer = Self; + + fn make_writer(&'a self) -> Self::Writer { + Self::default() + } +} + +// === impl BoxMakeWriter === + +impl BoxMakeWriter { + /// Constructs a `BoxMakeWriter` wrapping a type implementing [`MakeWriter`]. + /// + pub fn new<M>(make_writer: M) -> Self + where + M: for<'a> MakeWriter<'a> + Send + Sync + 'static, + { + Self { + inner: Box::new(Boxed(make_writer)), + name: std::any::type_name::<M>(), + } + } +} + +impl fmt::Debug for BoxMakeWriter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("BoxMakeWriter") + .field(&format_args!("<{}>", self.name)) + .finish() + } +} + +impl<'a> MakeWriter<'a> for BoxMakeWriter { + type Writer = Box<dyn Write + 'a>; + + #[inline] + fn make_writer(&'a self) -> Self::Writer { + self.inner.make_writer() + } + + #[inline] + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + self.inner.make_writer_for(meta) + } +} + +struct Boxed<M>(M); + +impl<'a, M> MakeWriter<'a> for Boxed<M> +where + M: MakeWriter<'a>, +{ + type Writer = Box<dyn Write + 'a>; + + fn make_writer(&'a self) -> Self::Writer { + let w = self.0.make_writer(); + Box::new(w) + } + + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + let w = self.0.make_writer_for(meta); + Box::new(w) + } +} + +// === impl Mutex/MutexGuardWriter === + +impl<'a, W> MakeWriter<'a> for Mutex<W> +where + W: io::Write + 'a, +{ + type Writer = MutexGuardWriter<'a, W>; + + fn make_writer(&'a self) -> Self::Writer { + MutexGuardWriter(self.lock().expect("lock poisoned")) + } +} + +impl<'a, W> io::Write for MutexGuardWriter<'a, W> +where + W: io::Write, +{ + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + self.0.write(buf) + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + self.0.flush() + } + + #[inline] + fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { + self.0.write_vectored(bufs) + } + + #[inline] + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + self.0.write_all(buf) + } + + #[inline] + fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { + self.0.write_fmt(fmt) + } +} + +// === impl EitherWriter === + +impl<A, B> io::Write for EitherWriter<A, B> +where + A: io::Write, + B: io::Write, +{ + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + match self { + EitherWriter::A(a) => a.write(buf), + EitherWriter::B(b) => b.write(buf), + } + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + match self { + EitherWriter::A(a) => a.flush(), + EitherWriter::B(b) => b.flush(), + } + } + + #[inline] + fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { + match self { + EitherWriter::A(a) => a.write_vectored(bufs), + EitherWriter::B(b) => b.write_vectored(bufs), + } + } + + #[inline] + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + match self { + EitherWriter::A(a) => a.write_all(buf), + EitherWriter::B(b) => b.write_all(buf), + } + } + + #[inline] + fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { + match self { + EitherWriter::A(a) => a.write_fmt(fmt), + EitherWriter::B(b) => b.write_fmt(fmt), + } + } +} + +impl<T> OptionalWriter<T> { + /// Returns a [disabled writer]. + /// + /// Any bytes written to the returned writer are discarded. + /// + /// This is equivalent to returning [`Option::None`]. + /// + /// [disabled writer]: std::io::sink + #[inline] + pub fn none() -> Self { + EitherWriter::B(std::io::sink()) + } + + /// Returns an enabled writer of type `T`. + /// + /// This is equivalent to returning [`Option::Some`]. + #[inline] + pub fn some(t: T) -> Self { + EitherWriter::A(t) + } +} + +impl<T> From<Option<T>> for OptionalWriter<T> { + #[inline] + fn from(opt: Option<T>) -> Self { + match opt { + Some(writer) => Self::some(writer), + None => Self::none(), + } + } +} + +// === impl WithMaxLevel === + +impl<M> WithMaxLevel<M> { + /// Wraps the provided [`MakeWriter`] with a maximum [`Level`], so that it + /// returns [`OptionalWriter::none`] for spans and events whose level is + /// more verbose than the maximum level. + /// + /// See [`MakeWriterExt::with_max_level`] for details. + /// + /// [`Level`]: tracing_core::Level + pub fn new(make: M, level: tracing_core::Level) -> Self { + Self { make, level } + } +} + +impl<'a, M: MakeWriter<'a>> MakeWriter<'a> for WithMaxLevel<M> { + type Writer = OptionalWriter<M::Writer>; + + #[inline] + fn make_writer(&'a self) -> Self::Writer { + // If we don't know the level, assume it's disabled. + OptionalWriter::none() + } + + #[inline] + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + if meta.level() <= &self.level { + return OptionalWriter::some(self.make.make_writer_for(meta)); + } + OptionalWriter::none() + } +} + +// === impl WithMinLevel === + +impl<M> WithMinLevel<M> { + /// Wraps the provided [`MakeWriter`] with a minimum [`Level`], so that it + /// returns [`OptionalWriter::none`] for spans and events whose level is + /// less verbose than the maximum level. + /// + /// See [`MakeWriterExt::with_min_level`] for details. + /// + /// [`Level`]: tracing_core::Level + pub fn new(make: M, level: tracing_core::Level) -> Self { + Self { make, level } + } +} + +impl<'a, M: MakeWriter<'a>> MakeWriter<'a> for WithMinLevel<M> { + type Writer = OptionalWriter<M::Writer>; + + #[inline] + fn make_writer(&'a self) -> Self::Writer { + // If we don't know the level, assume it's disabled. + OptionalWriter::none() + } + + #[inline] + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + if meta.level() >= &self.level { + return OptionalWriter::some(self.make.make_writer_for(meta)); + } + OptionalWriter::none() + } +} + +// ==== impl WithFilter === + +impl<M, F> WithFilter<M, F> { + /// Wraps `make` with the provided `filter`, returning a [`MakeWriter`] that + /// will call `make.make_writer_for()` when `filter` returns `true` for a + /// span or event's [`Metadata`], and returns a [`sink`] otherwise. + /// + /// See [`MakeWriterExt::with_filter`] for details. + /// + /// [`Metadata`]: tracing_core::Metadata + /// [`sink`]: std::io::sink + pub fn new(make: M, filter: F) -> Self + where + F: Fn(&Metadata<'_>) -> bool, + { + Self { make, filter } + } +} + +impl<'a, M, F> MakeWriter<'a> for WithFilter<M, F> +where + M: MakeWriter<'a>, + F: Fn(&Metadata<'_>) -> bool, +{ + type Writer = OptionalWriter<M::Writer>; + + #[inline] + fn make_writer(&'a self) -> Self::Writer { + OptionalWriter::some(self.make.make_writer()) + } + + #[inline] + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + if (self.filter)(meta) { + OptionalWriter::some(self.make.make_writer_for(meta)) + } else { + OptionalWriter::none() + } + } +} + +// === impl Tee === + +impl<A, B> Tee<A, B> { + /// Combines two types implementing [`MakeWriter`], returning + /// a new [`MakeWriter`] that produces [writers] that write to *both* + /// outputs. + /// + /// See the documentation for [`MakeWriterExt::and`] for details. + /// + /// [writers]: std::io::Write + pub fn new(a: A, b: B) -> Self { + Self { a, b } + } +} + +impl<'a, A, B> MakeWriter<'a> for Tee<A, B> +where + A: MakeWriter<'a>, + B: MakeWriter<'a>, +{ + type Writer = Tee<A::Writer, B::Writer>; + + #[inline] + fn make_writer(&'a self) -> Self::Writer { + Tee::new(self.a.make_writer(), self.b.make_writer()) + } + + #[inline] + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + Tee::new(self.a.make_writer_for(meta), self.b.make_writer_for(meta)) + } +} + +macro_rules! impl_tee { + ($self_:ident.$f:ident($($arg:ident),*)) => { + { + let res_a = $self_.a.$f($($arg),*); + let res_b = $self_.b.$f($($arg),*); + (res_a?, res_b?) + } + } +} + +impl<A, B> io::Write for Tee<A, B> +where + A: io::Write, + B: io::Write, +{ + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let (a, b) = impl_tee!(self.write(buf)); + Ok(std::cmp::max(a, b)) + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + impl_tee!(self.flush()); + Ok(()) + } + + #[inline] + fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { + let (a, b) = impl_tee!(self.write_vectored(bufs)); + Ok(std::cmp::max(a, b)) + } + + #[inline] + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + impl_tee!(self.write_all(buf)); + Ok(()) + } + + #[inline] + fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { + impl_tee!(self.write_fmt(fmt)); + Ok(()) + } +} + +// === impl OrElse === + +impl<A, B> OrElse<A, B> { + /// Combines + pub fn new<'a, W>(inner: A, or_else: B) -> Self + where + A: MakeWriter<'a, Writer = OptionalWriter<W>>, + B: MakeWriter<'a>, + W: Write, + { + Self { inner, or_else } + } +} + +impl<'a, A, B, W> MakeWriter<'a> for OrElse<A, B> +where + A: MakeWriter<'a, Writer = OptionalWriter<W>>, + B: MakeWriter<'a>, + W: io::Write, +{ + type Writer = EitherWriter<W, B::Writer>; + + #[inline] + fn make_writer(&'a self) -> Self::Writer { + match self.inner.make_writer() { + EitherWriter::A(writer) => EitherWriter::A(writer), + EitherWriter::B(_) => EitherWriter::B(self.or_else.make_writer()), + } + } + + #[inline] + fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { + match self.inner.make_writer_for(meta) { + EitherWriter::A(writer) => EitherWriter::A(writer), + EitherWriter::B(_) => EitherWriter::B(self.or_else.make_writer_for(meta)), + } + } +} + +// === impl ArcWriter === + +impl<W> io::Write for ArcWriter<W> +where + for<'a> &'a W: io::Write, +{ + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + (&*self.0).write(buf) + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + (&*self.0).flush() + } + + #[inline] + fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { + (&*self.0).write_vectored(bufs) + } + + #[inline] + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + (&*self.0).write_all(buf) + } + + #[inline] + fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { + (&*self.0).write_fmt(fmt) + } +} + +// === impl WriteAdaptor === + +#[cfg(any(feature = "json", feature = "time"))] +impl<'a> WriteAdaptor<'a> { + pub(in crate::fmt) fn new(fmt_write: &'a mut dyn fmt::Write) -> Self { + Self { fmt_write } + } +} +#[cfg(any(feature = "json", feature = "time"))] +impl<'a> io::Write for WriteAdaptor<'a> { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let s = + std::str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + self.fmt_write + .write_str(s) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + + Ok(s.as_bytes().len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +#[cfg(any(feature = "json", feature = "time"))] +impl<'a> fmt::Debug for WriteAdaptor<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad("WriteAdaptor { .. }") + } +} +// === blanket impls === + +impl<'a, M> MakeWriterExt<'a> for M where M: MakeWriter<'a> {} +#[cfg(test)] +mod test { + use super::*; + use crate::fmt::format::Format; + use crate::fmt::test::{MockMakeWriter, MockWriter}; + use crate::fmt::Subscriber; + use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::{Arc, Mutex}; + use tracing::{debug, error, info, trace, warn, Level}; + use tracing_core::dispatcher::{self, Dispatch}; + + fn test_writer<T>(make_writer: T, msg: &str, buf: &Mutex<Vec<u8>>) + where + T: for<'writer> MakeWriter<'writer> + Send + Sync + 'static, + { + let subscriber = { + #[cfg(feature = "ansi")] + let f = Format::default().without_time().with_ansi(false); + #[cfg(not(feature = "ansi"))] + let f = Format::default().without_time(); + Subscriber::builder() + .event_format(f) + .with_writer(make_writer) + .finish() + }; + let dispatch = Dispatch::from(subscriber); + + dispatcher::with_default(&dispatch, || { + error!("{}", msg); + }); + + let expected = format!("ERROR {}: {}\n", module_path!(), msg); + let actual = String::from_utf8(buf.try_lock().unwrap().to_vec()).unwrap(); + assert!(actual.contains(expected.as_str())); + } + + fn has_lines(buf: &Mutex<Vec<u8>>, msgs: &[(tracing::Level, &str)]) { + let actual = String::from_utf8(buf.try_lock().unwrap().to_vec()).unwrap(); + let mut expected_lines = msgs.iter(); + for line in actual.lines() { + let line = dbg!(line).trim(); + let (level, msg) = expected_lines + .next() + .unwrap_or_else(|| panic!("expected no more lines, but got: {:?}", line)); + let expected = format!("{} {}: {}", level, module_path!(), msg); + assert_eq!(line, expected.as_str()); + } + } + + #[test] + fn custom_writer_closure() { + let buf = Arc::new(Mutex::new(Vec::new())); + let buf2 = buf.clone(); + let make_writer = move || MockWriter::new(buf2.clone()); + let msg = "my custom writer closure error"; + test_writer(make_writer, msg, &buf); + } + + #[test] + fn custom_writer_struct() { + let buf = Arc::new(Mutex::new(Vec::new())); + let make_writer = MockMakeWriter::new(buf.clone()); + let msg = "my custom writer struct error"; + test_writer(make_writer, msg, &buf); + } + + #[test] + fn custom_writer_mutex() { + let buf = Arc::new(Mutex::new(Vec::new())); + let writer = MockWriter::new(buf.clone()); + let make_writer = Mutex::new(writer); + let msg = "my mutex writer error"; + test_writer(make_writer, msg, &buf); + } + + #[test] + fn combinators_level_filters() { + let info_buf = Arc::new(Mutex::new(Vec::new())); + let info = MockMakeWriter::new(info_buf.clone()); + + let debug_buf = Arc::new(Mutex::new(Vec::new())); + let debug = MockMakeWriter::new(debug_buf.clone()); + + let warn_buf = Arc::new(Mutex::new(Vec::new())); + let warn = MockMakeWriter::new(warn_buf.clone()); + + let err_buf = Arc::new(Mutex::new(Vec::new())); + let err = MockMakeWriter::new(err_buf.clone()); + + let make_writer = info + .with_max_level(Level::INFO) + .and(debug.with_max_level(Level::DEBUG)) + .and(warn.with_max_level(Level::WARN)) + .and(err.with_max_level(Level::ERROR)); + + let c = { + #[cfg(feature = "ansi")] + let f = Format::default().without_time().with_ansi(false); + #[cfg(not(feature = "ansi"))] + let f = Format::default().without_time(); + Subscriber::builder() + .event_format(f) + .with_writer(make_writer) + .with_max_level(Level::TRACE) + .finish() + }; + + let _s = tracing::subscriber::set_default(c); + + trace!("trace"); + debug!("debug"); + info!("info"); + warn!("warn"); + error!("error"); + + let all_lines = [ + (Level::TRACE, "trace"), + (Level::DEBUG, "debug"), + (Level::INFO, "info"), + (Level::WARN, "warn"), + (Level::ERROR, "error"), + ]; + + println!("max level debug"); + has_lines(&debug_buf, &all_lines[1..]); + + println!("max level info"); + has_lines(&info_buf, &all_lines[2..]); + + println!("max level warn"); + has_lines(&warn_buf, &all_lines[3..]); + + println!("max level error"); + has_lines(&err_buf, &all_lines[4..]); + } + + #[test] + fn combinators_or_else() { + let some_buf = Arc::new(Mutex::new(Vec::new())); + let some = MockMakeWriter::new(some_buf.clone()); + + let or_else_buf = Arc::new(Mutex::new(Vec::new())); + let or_else = MockMakeWriter::new(or_else_buf.clone()); + + let return_some = AtomicBool::new(true); + let make_writer = move || { + if return_some.swap(false, Ordering::Relaxed) { + OptionalWriter::some(some.make_writer()) + } else { + OptionalWriter::none() + } + }; + let make_writer = make_writer.or_else(or_else); + let c = { + #[cfg(feature = "ansi")] + let f = Format::default().without_time().with_ansi(false); + #[cfg(not(feature = "ansi"))] + let f = Format::default().without_time(); + Subscriber::builder() + .event_format(f) + .with_writer(make_writer) + .with_max_level(Level::TRACE) + .finish() + }; + + let _s = tracing::subscriber::set_default(c); + info!("hello"); + info!("world"); + info!("goodbye"); + + has_lines(&some_buf, &[(Level::INFO, "hello")]); + has_lines( + &or_else_buf, + &[(Level::INFO, "world"), (Level::INFO, "goodbye")], + ); + } + + #[test] + fn combinators_or_else_chain() { + let info_buf = Arc::new(Mutex::new(Vec::new())); + let info = MockMakeWriter::new(info_buf.clone()); + + let debug_buf = Arc::new(Mutex::new(Vec::new())); + let debug = MockMakeWriter::new(debug_buf.clone()); + + let warn_buf = Arc::new(Mutex::new(Vec::new())); + let warn = MockMakeWriter::new(warn_buf.clone()); + + let err_buf = Arc::new(Mutex::new(Vec::new())); + let err = MockMakeWriter::new(err_buf.clone()); + + let make_writer = err.with_max_level(Level::ERROR).or_else( + warn.with_max_level(Level::WARN).or_else( + info.with_max_level(Level::INFO) + .or_else(debug.with_max_level(Level::DEBUG)), + ), + ); + + let c = { + #[cfg(feature = "ansi")] + let f = Format::default().without_time().with_ansi(false); + #[cfg(not(feature = "ansi"))] + let f = Format::default().without_time(); + Subscriber::builder() + .event_format(f) + .with_writer(make_writer) + .with_max_level(Level::TRACE) + .finish() + }; + + let _s = tracing::subscriber::set_default(c); + + trace!("trace"); + debug!("debug"); + info!("info"); + warn!("warn"); + error!("error"); + + println!("max level debug"); + has_lines(&debug_buf, &[(Level::DEBUG, "debug")]); + + println!("max level info"); + has_lines(&info_buf, &[(Level::INFO, "info")]); + + println!("max level warn"); + has_lines(&warn_buf, &[(Level::WARN, "warn")]); + + println!("max level error"); + has_lines(&err_buf, &[(Level::ERROR, "error")]); + } + + #[test] + fn combinators_and() { + let a_buf = Arc::new(Mutex::new(Vec::new())); + let a = MockMakeWriter::new(a_buf.clone()); + + let b_buf = Arc::new(Mutex::new(Vec::new())); + let b = MockMakeWriter::new(b_buf.clone()); + + let lines = &[(Level::INFO, "hello"), (Level::INFO, "world")]; + + let make_writer = a.and(b); + let c = { + #[cfg(feature = "ansi")] + let f = Format::default().without_time().with_ansi(false); + #[cfg(not(feature = "ansi"))] + let f = Format::default().without_time(); + Subscriber::builder() + .event_format(f) + .with_writer(make_writer) + .with_max_level(Level::TRACE) + .finish() + }; + + let _s = tracing::subscriber::set_default(c); + info!("hello"); + info!("world"); + + has_lines(&a_buf, &lines[..]); + has_lines(&b_buf, &lines[..]); + } +} diff --git a/vendor/tracing-subscriber/src/layer/context.rs b/vendor/tracing-subscriber/src/layer/context.rs new file mode 100644 index 000000000..46254994f --- /dev/null +++ b/vendor/tracing-subscriber/src/layer/context.rs @@ -0,0 +1,434 @@ +use tracing_core::{metadata::Metadata, span, subscriber::Subscriber, Event}; + +use crate::registry::{self, LookupSpan, SpanRef}; + +#[cfg(all(feature = "registry", feature = "std"))] +use crate::{filter::FilterId, registry::Registry}; +/// Represents information about the current context provided to [`Layer`]s by the +/// wrapped [`Subscriber`]. +/// +/// To access [stored data] keyed by a span ID, implementors of the `Layer` +/// trait should ensure that the `Subscriber` type parameter is *also* bound by the +/// [`LookupSpan`]: +/// +/// ```rust +/// use tracing::Subscriber; +/// use tracing_subscriber::{Layer, registry::LookupSpan}; +/// +/// pub struct MyLayer; +/// +/// impl<S> Layer<S> for MyLayer +/// where +/// S: Subscriber + for<'a> LookupSpan<'a>, +/// { +/// // ... +/// } +/// ``` +/// +/// [`Layer`]: super::Layer +/// [`Subscriber`]: tracing_core::Subscriber +/// [stored data]: crate::registry::SpanRef +/// [`LookupSpan`]: crate::registry::LookupSpan +#[derive(Debug)] +pub struct Context<'a, S> { + subscriber: Option<&'a S>, + /// The bitmask of all [`Filtered`] layers that currently apply in this + /// context. If there is only a single [`Filtered`] wrapping the layer that + /// produced this context, then this is that filter's ID. Otherwise, if we + /// are in a nested tree with multiple filters, this is produced by + /// [`and`]-ing together the [`FilterId`]s of each of the filters that wrap + /// the current layer. + /// + /// [`Filtered`]: crate::filter::Filtered + /// [`FilterId`]: crate::filter::FilterId + /// [`and`]: crate::filter::FilterId::and + #[cfg(all(feature = "registry", feature = "std"))] + filter: FilterId, +} + +// === impl Context === + +impl<'a, S> Context<'a, S> +where + S: Subscriber, +{ + pub(super) fn new(subscriber: &'a S) -> Self { + Self { + subscriber: Some(subscriber), + + #[cfg(feature = "registry")] + filter: FilterId::none(), + } + } + + /// Returns the wrapped subscriber's view of the current span. + #[inline] + pub fn current_span(&self) -> span::Current { + self.subscriber + .map(Subscriber::current_span) + // TODO: this would be more correct as "unknown", so perhaps + // `tracing-core` should make `Current::unknown()` public? + .unwrap_or_else(span::Current::none) + } + + /// Returns whether the wrapped subscriber would enable the current span. + #[inline] + pub fn enabled(&self, metadata: &Metadata<'_>) -> bool { + self.subscriber + .map(|subscriber| subscriber.enabled(metadata)) + // If this context is `None`, we are registering a callsite, so + // return `true` so that the layer does not incorrectly assume that + // the inner subscriber has disabled this metadata. + // TODO(eliza): would it be more correct for this to return an `Option`? + .unwrap_or(true) + } + + /// Records the provided `event` with the wrapped subscriber. + /// + /// # Notes + /// + /// - The subscriber is free to expect that the event's callsite has been + /// [registered][register], and may panic or fail to observe the event if this is + /// not the case. The `tracing` crate's macros ensure that all events are + /// registered, but if the event is constructed through other means, the + /// user is responsible for ensuring that [`register_callsite`][register] + /// has been called prior to calling this method. + /// - This does _not_ call [`enabled`] on the inner subscriber. If the + /// caller wishes to apply the wrapped subscriber's filter before choosing + /// whether to record the event, it may first call [`Context::enabled`] to + /// check whether the event would be enabled. This allows `Layer`s to + /// elide constructing the event if it would not be recorded. + /// + /// [register]: tracing_core::subscriber::Subscriber::register_callsite() + /// [`enabled`]: tracing_core::subscriber::Subscriber::enabled() + /// [`Context::enabled`]: Context::enabled() + #[inline] + pub fn event(&self, event: &Event<'_>) { + if let Some(subscriber) = self.subscriber { + subscriber.event(event); + } + } + + /// Returns a [`SpanRef`] for the parent span of the given [`Event`], if + /// it has a parent. + /// + /// If the event has an explicitly overridden parent, this method returns + /// a reference to that span. If the event's parent is the current span, + /// this returns a reference to the current span, if there is one. If this + /// returns `None`, then either the event's parent was explicitly set to + /// `None`, or the event's parent was defined contextually, but no span + /// is currently entered. + /// + /// Compared to [`Context::current_span`] and [`Context::lookup_current`], + /// this respects overrides provided by the [`Event`]. + /// + /// Compared to [`Event::parent`], this automatically falls back to the contextual + /// span, if required. + /// + /// ```rust + /// use tracing::{Event, Subscriber}; + /// use tracing_subscriber::{ + /// layer::{Context, Layer}, + /// prelude::*, + /// registry::LookupSpan, + /// }; + /// + /// struct PrintingLayer; + /// impl<S> Layer<S> for PrintingLayer + /// where + /// S: Subscriber + for<'lookup> LookupSpan<'lookup>, + /// { + /// fn on_event(&self, event: &Event, ctx: Context<S>) { + /// let span = ctx.event_span(event); + /// println!("Event in span: {:?}", span.map(|s| s.name())); + /// } + /// } + /// + /// tracing::subscriber::with_default(tracing_subscriber::registry().with(PrintingLayer), || { + /// tracing::info!("no span"); + /// // Prints: Event in span: None + /// + /// let span = tracing::info_span!("span"); + /// tracing::info!(parent: &span, "explicitly specified"); + /// // Prints: Event in span: Some("span") + /// + /// let _guard = span.enter(); + /// tracing::info!("contextual span"); + /// // Prints: Event in span: Some("span") + /// }); + /// ``` + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to + /// implement the <a href="../registry/trait.LookupSpan.html"><code> + /// LookupSpan</code></a> trait. See the documentation on + /// <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre> + #[inline] + pub fn event_span(&self, event: &Event<'_>) -> Option<SpanRef<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + if event.is_root() { + None + } else if event.is_contextual() { + self.lookup_current() + } else { + // TODO(eliza): this should handle parent IDs + event.parent().and_then(|id| self.span(id)) + } + } + + /// Returns metadata for the span with the given `id`, if it exists. + /// + /// If this returns `None`, then no span exists for that ID (either it has + /// closed or the ID is invalid). + #[inline] + pub fn metadata(&self, id: &span::Id) -> Option<&'static Metadata<'static>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + let span = self.span(id)?; + Some(span.metadata()) + } + + /// Returns [stored data] for the span with the given `id`, if it exists. + /// + /// If this returns `None`, then no span exists for that ID (either it has + /// closed or the ID is invalid). + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to + /// implement the <a href="../registry/trait.LookupSpan.html"><code> + /// LookupSpan</code></a> trait. See the documentation on + /// <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre> + /// + /// [stored data]: crate::registry::SpanRef + #[inline] + pub fn span(&self, id: &span::Id) -> Option<registry::SpanRef<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + let span = self.subscriber.as_ref()?.span(id)?; + + #[cfg(all(feature = "registry", feature = "std"))] + return span.try_with_filter(self.filter); + + #[cfg(not(feature = "registry"))] + Some(span) + } + + /// Returns `true` if an active span exists for the given `Id`. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to + /// implement the <a href="../registry/trait.LookupSpan.html"><code> + /// LookupSpan</code></a> trait. See the documentation on + /// <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre> + #[inline] + pub fn exists(&self, id: &span::Id) -> bool + where + S: for<'lookup> LookupSpan<'lookup>, + { + self.subscriber.as_ref().and_then(|s| s.span(id)).is_some() + } + + /// Returns [stored data] for the span that the wrapped subscriber considers + /// to be the current. + /// + /// If this returns `None`, then we are not currently within a span. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to + /// implement the <a href="../registry/trait.LookupSpan.html"><code> + /// LookupSpan</code></a> trait. See the documentation on + /// <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre> + /// + /// [stored data]: crate::registry::SpanRef + #[inline] + pub fn lookup_current(&self) -> Option<registry::SpanRef<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + let subscriber = *self.subscriber.as_ref()?; + let current = subscriber.current_span(); + let id = current.id()?; + let span = subscriber.span(id); + debug_assert!( + span.is_some(), + "the subscriber should have data for the current span ({:?})!", + id, + ); + + // If we found a span, and our per-layer filter enables it, return that + // span! + #[cfg(all(feature = "registry", feature = "std"))] + { + if let Some(span) = span?.try_with_filter(self.filter) { + Some(span) + } else { + // Otherwise, the span at the *top* of the stack is disabled by + // per-layer filtering, but there may be additional spans in the stack. + // + // Currently, `LookupSpan` doesn't have a nice way of exposing access to + // the whole span stack. However, if we can downcast the innermost + // subscriber to a a `Registry`, we can iterate over its current span + // stack. + // + // TODO(eliza): when https://github.com/tokio-rs/tracing/issues/1459 is + // implemented, change this to use that instead... + self.lookup_current_filtered(subscriber) + } + } + + #[cfg(not(feature = "registry"))] + span + } + + /// Slow path for when the current span is disabled by PLF and we have a + /// registry. + // This is called by `lookup_current` in the case that per-layer filtering + // is in use. `lookup_current` is allowed to be inlined, but this method is + // factored out to prevent the loop and (potentially-recursive) subscriber + // downcasting from being inlined if `lookup_current` is inlined. + #[inline(never)] + #[cfg(all(feature = "registry", feature = "std"))] + fn lookup_current_filtered<'lookup>( + &self, + subscriber: &'lookup S, + ) -> Option<registry::SpanRef<'lookup, S>> + where + S: LookupSpan<'lookup>, + { + let registry = (subscriber as &dyn Subscriber).downcast_ref::<Registry>()?; + registry + .span_stack() + .iter() + .find_map(|id| subscriber.span(id)?.try_with_filter(self.filter)) + } + + /// Returns an iterator over the [stored data] for all the spans in the + /// current context, starting with the specified span and ending with the + /// root of the trace tree and ending with the current span. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: Compared to <a href="#method.scope"><code>scope</code></a> this + /// returns the spans in reverse order (from leaf to root). Use + /// <a href="../registry/struct.Scope.html#method.from_root"><code>Scope::from_root</code></a> + /// in case root-to-leaf ordering is desired. + /// </pre> + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to + /// implement the <a href="../registry/trait.LookupSpan.html"><code> + /// LookupSpan</code></a> trait. See the documentation on + /// <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre> + /// + /// [stored data]: crate::registry::SpanRef + pub fn span_scope(&self, id: &span::Id) -> Option<registry::Scope<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + Some(self.span(id)?.scope()) + } + + /// Returns an iterator over the [stored data] for all the spans in the + /// current context, starting with the parent span of the specified event, + /// and ending with the root of the trace tree and ending with the current span. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: Compared to <a href="#method.scope"><code>scope</code></a> this + /// returns the spans in reverse order (from leaf to root). Use + /// <a href="../registry/struct.Scope.html#method.from_root"><code>Scope::from_root</code></a> + /// in case root-to-leaf ordering is desired. + /// </pre> + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This requires the wrapped subscriber to + /// implement the <a href="../registry/trait.LookupSpan.html"><code> + /// LookupSpan</code></a> trait. See the documentation on + /// <a href="./struct.Context.html"><code>Context</code>'s + /// declaration</a> for details. + /// </pre> + /// + /// [stored data]: crate::registry::SpanRef + pub fn event_scope(&self, event: &Event<'_>) -> Option<registry::Scope<'_, S>> + where + S: for<'lookup> LookupSpan<'lookup>, + { + Some(self.event_span(event)?.scope()) + } + + #[cfg(all(feature = "registry", feature = "std"))] + pub(crate) fn with_filter(self, filter: FilterId) -> Self { + // If we already have our own `FilterId`, combine it with the provided + // one. That way, the new `FilterId` will consider a span to be disabled + // if it was disabled by the given `FilterId` *or* any `FilterId`s for + // layers "above" us in the stack. + // + // See the doc comment for `FilterId::and` for details. + let filter = self.filter.and(filter); + Self { filter, ..self } + } + + #[cfg(all(feature = "registry", feature = "std"))] + pub(crate) fn is_enabled_for(&self, span: &span::Id, filter: FilterId) -> bool + where + S: for<'lookup> LookupSpan<'lookup>, + { + self.is_enabled_inner(span, filter).unwrap_or(false) + } + + #[cfg(all(feature = "registry", feature = "std"))] + pub(crate) fn if_enabled_for(self, span: &span::Id, filter: FilterId) -> Option<Self> + where + S: for<'lookup> LookupSpan<'lookup>, + { + if self.is_enabled_inner(span, filter)? { + Some(self.with_filter(filter)) + } else { + None + } + } + + #[cfg(all(feature = "registry", feature = "std"))] + fn is_enabled_inner(&self, span: &span::Id, filter: FilterId) -> Option<bool> + where + S: for<'lookup> LookupSpan<'lookup>, + { + Some(self.span(span)?.is_enabled_for(filter)) + } +} + +impl<'a, S> Context<'a, S> { + pub(crate) fn none() -> Self { + Self { + subscriber: None, + + #[cfg(feature = "registry")] + filter: FilterId::none(), + } + } +} + +impl<'a, S> Clone for Context<'a, S> { + #[inline] + fn clone(&self) -> Self { + let subscriber = self.subscriber.as_ref().copied(); + Context { + subscriber, + + #[cfg(all(feature = "registry", feature = "std"))] + filter: self.filter, + } + } +} diff --git a/vendor/tracing-subscriber/src/layer/layered.rs b/vendor/tracing-subscriber/src/layer/layered.rs new file mode 100644 index 000000000..805ec13dc --- /dev/null +++ b/vendor/tracing-subscriber/src/layer/layered.rs @@ -0,0 +1,520 @@ +use tracing_core::{ + metadata::Metadata, + span, + subscriber::{Interest, Subscriber}, + Event, LevelFilter, +}; + +use crate::{ + filter, + layer::{Context, Layer}, + registry::LookupSpan, +}; +#[cfg(all(feature = "registry", feature = "std"))] +use crate::{filter::FilterId, registry::Registry}; +use core::{ + any::{Any, TypeId}, + cmp, fmt, + marker::PhantomData, +}; + +/// A [`Subscriber`] composed of a `Subscriber` wrapped by one or more +/// [`Layer`]s. +/// +/// [`Layer`]: crate::Layer +/// [`Subscriber`]: tracing_core::Subscriber +#[derive(Clone)] +pub struct Layered<L, I, S = I> { + /// The layer. + layer: L, + + /// The inner value that `self.layer` was layered onto. + /// + /// If this is also a `Layer`, then this `Layered` will implement `Layer`. + /// If this is a `Subscriber`, then this `Layered` will implement + /// `Subscriber` instead. + inner: I, + + // These booleans are used to determine how to combine `Interest`s and max + // level hints when per-layer filters are in use. + /// Is `self.inner` a `Registry`? + /// + /// If so, when combining `Interest`s, we want to "bubble up" its + /// `Interest`. + inner_is_registry: bool, + + /// Does `self.layer` have per-layer filters? + /// + /// This will be true if: + /// - `self.inner` is a `Filtered`. + /// - `self.inner` is a tree of `Layered`s where _all_ arms of those + /// `Layered`s have per-layer filters. + /// + /// Otherwise, if it's a `Layered` with one per-layer filter in one branch, + /// but a non-per-layer-filtered layer in the other branch, this will be + /// _false_, because the `Layered` is already handling the combining of + /// per-layer filter `Interest`s and max level hints with its non-filtered + /// `Layer`. + has_layer_filter: bool, + + /// Does `self.inner` have per-layer filters? + /// + /// This is determined according to the same rules as + /// `has_layer_filter` above. + inner_has_layer_filter: bool, + _s: PhantomData<fn(S)>, +} + +// === impl Layered === + +impl<L, S> Layered<L, S> +where + L: Layer<S>, + S: Subscriber, +{ + /// Returns `true` if this [`Subscriber`] is the same type as `T`. + pub fn is<T: Any>(&self) -> bool { + self.downcast_ref::<T>().is_some() + } + + /// Returns some reference to this [`Subscriber`] value if it is of type `T`, + /// or `None` if it isn't. + pub fn downcast_ref<T: Any>(&self) -> Option<&T> { + unsafe { + let raw = self.downcast_raw(TypeId::of::<T>())?; + if raw.is_null() { + None + } else { + Some(&*(raw as *const T)) + } + } + } +} + +impl<L, S> Subscriber for Layered<L, S> +where + L: Layer<S>, + S: Subscriber, +{ + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.pick_interest(self.layer.register_callsite(metadata), || { + self.inner.register_callsite(metadata) + }) + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + if self.layer.enabled(metadata, self.ctx()) { + // if the outer layer enables the callsite metadata, ask the subscriber. + self.inner.enabled(metadata) + } else { + // otherwise, the callsite is disabled by the layer + + // If per-layer filters are in use, and we are short-circuiting + // (rather than calling into the inner type), clear the current + // per-layer filter `enabled` state. + #[cfg(feature = "registry")] + filter::FilterState::clear_enabled(); + + false + } + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + self.pick_level_hint(self.layer.max_level_hint(), self.inner.max_level_hint()) + } + + fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { + let id = self.inner.new_span(span); + self.layer.on_new_span(span, &id, self.ctx()); + id + } + + fn record(&self, span: &span::Id, values: &span::Record<'_>) { + self.inner.record(span, values); + self.layer.on_record(span, values, self.ctx()); + } + + fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { + self.inner.record_follows_from(span, follows); + self.layer.on_follows_from(span, follows, self.ctx()); + } + + fn event_enabled(&self, event: &Event<'_>) -> bool { + if self.layer.event_enabled(event, self.ctx()) { + // if the outer layer enables the event, ask the inner subscriber. + self.inner.event_enabled(event) + } else { + // otherwise, the event is disabled by this layer + false + } + } + + fn event(&self, event: &Event<'_>) { + self.inner.event(event); + self.layer.on_event(event, self.ctx()); + } + + fn enter(&self, span: &span::Id) { + self.inner.enter(span); + self.layer.on_enter(span, self.ctx()); + } + + fn exit(&self, span: &span::Id) { + self.inner.exit(span); + self.layer.on_exit(span, self.ctx()); + } + + fn clone_span(&self, old: &span::Id) -> span::Id { + let new = self.inner.clone_span(old); + if &new != old { + self.layer.on_id_change(old, &new, self.ctx()) + }; + new + } + + #[inline] + fn drop_span(&self, id: span::Id) { + self.try_close(id); + } + + fn try_close(&self, id: span::Id) -> bool { + #[cfg(all(feature = "registry", feature = "std"))] + let subscriber = &self.inner as &dyn Subscriber; + #[cfg(all(feature = "registry", feature = "std"))] + let mut guard = subscriber + .downcast_ref::<Registry>() + .map(|registry| registry.start_close(id.clone())); + if self.inner.try_close(id.clone()) { + // If we have a registry's close guard, indicate that the span is + // closing. + #[cfg(all(feature = "registry", feature = "std"))] + { + if let Some(g) = guard.as_mut() { + g.set_closing() + }; + } + + self.layer.on_close(id, self.ctx()); + true + } else { + false + } + } + + #[inline] + fn current_span(&self) -> span::Current { + self.inner.current_span() + } + + #[doc(hidden)] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + // Unlike the implementation of `Layer` for `Layered`, we don't have to + // handle the "magic PLF downcast marker" here. If a `Layered` + // implements `Subscriber`, we already know that the `inner` branch is + // going to contain something that doesn't have per-layer filters (the + // actual root `Subscriber`). Thus, a `Layered` that implements + // `Subscriber` will always be propagating the root subscriber's + // `Interest`/level hint, even if it includes a `Layer` that has + // per-layer filters, because it will only ever contain layers where + // _one_ child has per-layer filters. + // + // The complex per-layer filter detection logic is only relevant to + // *trees* of layers, which involve the `Layer` implementation for + // `Layered`, not *lists* of layers, where every `Layered` implements + // `Subscriber`. Of course, a linked list can be thought of as a + // degenerate tree...but luckily, we are able to make a type-level + // distinction between individual `Layered`s that are definitely + // list-shaped (their inner child implements `Subscriber`), and + // `Layered`s that might be tree-shaped (the inner child is also a + // `Layer`). + + // If downcasting to `Self`, return a pointer to `self`. + if id == TypeId::of::<Self>() { + return Some(self as *const _ as *const ()); + } + + self.layer + .downcast_raw(id) + .or_else(|| self.inner.downcast_raw(id)) + } +} + +impl<S, A, B> Layer<S> for Layered<A, B, S> +where + A: Layer<S>, + B: Layer<S>, + S: Subscriber, +{ + fn on_layer(&mut self, subscriber: &mut S) { + self.layer.on_layer(subscriber); + self.inner.on_layer(subscriber); + } + + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.pick_interest(self.layer.register_callsite(metadata), || { + self.inner.register_callsite(metadata) + }) + } + + fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool { + if self.layer.enabled(metadata, ctx.clone()) { + // if the outer subscriber enables the callsite metadata, ask the inner layer. + self.inner.enabled(metadata, ctx) + } else { + // otherwise, the callsite is disabled by this layer + false + } + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + self.pick_level_hint(self.layer.max_level_hint(), self.inner.max_level_hint()) + } + + #[inline] + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + self.inner.on_new_span(attrs, id, ctx.clone()); + self.layer.on_new_span(attrs, id, ctx); + } + + #[inline] + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + self.inner.on_record(span, values, ctx.clone()); + self.layer.on_record(span, values, ctx); + } + + #[inline] + fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: Context<'_, S>) { + self.inner.on_follows_from(span, follows, ctx.clone()); + self.layer.on_follows_from(span, follows, ctx); + } + + #[inline] + fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, S>) -> bool { + if self.layer.event_enabled(event, ctx.clone()) { + // if the outer layer enables the event, ask the inner subscriber. + self.inner.event_enabled(event, ctx) + } else { + // otherwise, the event is disabled by this layer + false + } + } + + #[inline] + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { + self.inner.on_event(event, ctx.clone()); + self.layer.on_event(event, ctx); + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + self.inner.on_enter(id, ctx.clone()); + self.layer.on_enter(id, ctx); + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + self.inner.on_exit(id, ctx.clone()); + self.layer.on_exit(id, ctx); + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { + self.inner.on_close(id.clone(), ctx.clone()); + self.layer.on_close(id, ctx); + } + + #[inline] + fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: Context<'_, S>) { + self.inner.on_id_change(old, new, ctx.clone()); + self.layer.on_id_change(old, new, ctx); + } + + #[doc(hidden)] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + match id { + // If downcasting to `Self`, return a pointer to `self`. + id if id == TypeId::of::<Self>() => Some(self as *const _ as *const ()), + + // Oh, we're looking for per-layer filters! + // + // This should only happen if we are inside of another `Layered`, + // and it's trying to determine how it should combine `Interest`s + // and max level hints. + // + // In that case, this `Layered` should be considered to be + // "per-layer filtered" if *both* the outer layer and the inner + // layer/subscriber have per-layer filters. Otherwise, this `Layered + // should *not* be considered per-layer filtered (even if one or the + // other has per layer filters). If only one `Layer` is per-layer + // filtered, *this* `Layered` will handle aggregating the `Interest` + // and level hints on behalf of its children, returning the + // aggregate (which is the value from the &non-per-layer-filtered* + // child). + // + // Yes, this rule *is* slightly counter-intuitive, but it's + // necessary due to a weird edge case that can occur when two + // `Layered`s where one side is per-layer filtered and the other + // isn't are `Layered` together to form a tree. If we didn't have + // this rule, we would actually end up *ignoring* `Interest`s from + // the non-per-layer-filtered layers, since both branches would + // claim to have PLF. + // + // If you don't understand this...that's fine, just don't mess with + // it. :) + id if filter::is_plf_downcast_marker(id) => { + self.layer.downcast_raw(id).and(self.inner.downcast_raw(id)) + } + + // Otherwise, try to downcast both branches normally... + _ => self + .layer + .downcast_raw(id) + .or_else(|| self.inner.downcast_raw(id)), + } + } +} + +impl<'a, L, S> LookupSpan<'a> for Layered<L, S> +where + S: Subscriber + LookupSpan<'a>, +{ + type Data = S::Data; + + fn span_data(&'a self, id: &span::Id) -> Option<Self::Data> { + self.inner.span_data(id) + } + + #[cfg(all(feature = "registry", feature = "std"))] + fn register_filter(&mut self) -> FilterId { + self.inner.register_filter() + } +} + +impl<L, S> Layered<L, S> +where + S: Subscriber, +{ + fn ctx(&self) -> Context<'_, S> { + Context::new(&self.inner) + } +} + +impl<A, B, S> Layered<A, B, S> +where + A: Layer<S>, + S: Subscriber, +{ + pub(super) fn new(layer: A, inner: B, inner_has_layer_filter: bool) -> Self { + #[cfg(all(feature = "registry", feature = "std"))] + let inner_is_registry = TypeId::of::<S>() == TypeId::of::<crate::registry::Registry>(); + + #[cfg(not(all(feature = "registry", feature = "std")))] + let inner_is_registry = false; + + let inner_has_layer_filter = inner_has_layer_filter || inner_is_registry; + let has_layer_filter = filter::layer_has_plf(&layer); + Self { + layer, + inner, + has_layer_filter, + inner_has_layer_filter, + inner_is_registry, + _s: PhantomData, + } + } + + fn pick_interest(&self, outer: Interest, inner: impl FnOnce() -> Interest) -> Interest { + if self.has_layer_filter { + return inner(); + } + + // If the outer layer has disabled the callsite, return now so that + // the inner layer/subscriber doesn't get its hopes up. + if outer.is_never() { + // If per-layer filters are in use, and we are short-circuiting + // (rather than calling into the inner type), clear the current + // per-layer filter interest state. + #[cfg(feature = "registry")] + filter::FilterState::take_interest(); + + return outer; + } + + // The `inner` closure will call `inner.register_callsite()`. We do this + // before the `if` statement to ensure that the inner subscriber is + // informed that the callsite exists regardless of the outer layer's + // filtering decision. + let inner = inner(); + if outer.is_sometimes() { + // if this interest is "sometimes", return "sometimes" to ensure that + // filters are reevaluated. + return outer; + } + + // If there is a per-layer filter in the `inner` stack, and it returns + // `never`, change the interest to `sometimes`, because the `outer` + // layer didn't return `never`. This means that _some_ layer still wants + // to see that callsite, even though the inner stack's per-layer filter + // didn't want it. Therefore, returning `sometimes` will ensure + // `enabled` is called so that the per-layer filter can skip that + // span/event, while the `outer` layer still gets to see it. + if inner.is_never() && self.inner_has_layer_filter { + return Interest::sometimes(); + } + + // otherwise, allow the inner subscriber or collector to weigh in. + inner + } + + fn pick_level_hint( + &self, + outer_hint: Option<LevelFilter>, + inner_hint: Option<LevelFilter>, + ) -> Option<LevelFilter> { + if self.inner_is_registry { + return outer_hint; + } + + if self.has_layer_filter && self.inner_has_layer_filter { + return Some(cmp::max(outer_hint?, inner_hint?)); + } + + if self.has_layer_filter && inner_hint.is_none() { + return None; + } + + if self.inner_has_layer_filter && outer_hint.is_none() { + return None; + } + + cmp::max(outer_hint, inner_hint) + } +} + +impl<A, B, S> fmt::Debug for Layered<A, B, S> +where + A: fmt::Debug, + B: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + #[cfg(all(feature = "registry", feature = "std"))] + let alt = f.alternate(); + let mut s = f.debug_struct("Layered"); + // These additional fields are more verbose and usually only necessary + // for internal debugging purposes, so only print them if alternate mode + // is enabled. + + #[cfg(all(feature = "registry", feature = "std"))] + { + if alt { + s.field("inner_is_registry", &self.inner_is_registry) + .field("has_layer_filter", &self.has_layer_filter) + .field("inner_has_layer_filter", &self.inner_has_layer_filter); + } + } + + s.field("layer", &self.layer) + .field("inner", &self.inner) + .finish() + } +} diff --git a/vendor/tracing-subscriber/src/layer/mod.rs b/vendor/tracing-subscriber/src/layer/mod.rs new file mode 100644 index 000000000..24b853323 --- /dev/null +++ b/vendor/tracing-subscriber/src/layer/mod.rs @@ -0,0 +1,1798 @@ +//! The [`Layer`] trait, a composable abstraction for building [`Subscriber`]s. +//! +//! The [`Subscriber`] trait in `tracing-core` represents the _complete_ set of +//! functionality required to consume `tracing` instrumentation. This means that +//! a single `Subscriber` instance is a self-contained implementation of a +//! complete strategy for collecting traces; but it _also_ means that the +//! `Subscriber` trait cannot easily be composed with other `Subscriber`s. +//! +//! In particular, [`Subscriber`]s are responsible for generating [span IDs] and +//! assigning them to spans. Since these IDs must uniquely identify a span +//! within the context of the current trace, this means that there may only be +//! a single `Subscriber` for a given thread at any point in time — +//! otherwise, there would be no authoritative source of span IDs. +//! +//! On the other hand, the majority of the [`Subscriber`] trait's functionality +//! is composable: any number of subscribers may _observe_ events, span entry +//! and exit, and so on, provided that there is a single authoritative source of +//! span IDs. The [`Layer`] trait represents this composable subset of the +//! [`Subscriber`] behavior; it can _observe_ events and spans, but does not +//! assign IDs. +//! +//! # Composing Layers +//! +//! Since a [`Layer`] does not implement a complete strategy for collecting +//! traces, it must be composed with a `Subscriber` in order to be used. The +//! [`Layer`] trait is generic over a type parameter (called `S` in the trait +//! definition), representing the types of `Subscriber` they can be composed +//! with. Thus, a [`Layer`] may be implemented that will only compose with a +//! particular `Subscriber` implementation, or additional trait bounds may be +//! added to constrain what types implementing `Subscriber` a `Layer` can wrap. +//! +//! `Layer`s may be added to a `Subscriber` by using the [`SubscriberExt::with`] +//! method, which is provided by `tracing-subscriber`'s [prelude]. This method +//! returns a [`Layered`] struct that implements `Subscriber` by composing the +//! `Layer` with the `Subscriber`. +//! +//! For example: +//! ```rust +//! use tracing_subscriber::Layer; +//! use tracing_subscriber::prelude::*; +//! use tracing::Subscriber; +//! +//! pub struct MyLayer { +//! // ... +//! } +//! +//! impl<S: Subscriber> Layer<S> for MyLayer { +//! // ... +//! } +//! +//! pub struct MySubscriber { +//! // ... +//! } +//! +//! # use tracing_core::{span::{Id, Attributes, Record}, Metadata, Event}; +//! impl Subscriber for MySubscriber { +//! // ... +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(1) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! } +//! # impl MyLayer { +//! # fn new() -> Self { Self {} } +//! # } +//! # impl MySubscriber { +//! # fn new() -> Self { Self { }} +//! # } +//! +//! let subscriber = MySubscriber::new() +//! .with(MyLayer::new()); +//! +//! tracing::subscriber::set_global_default(subscriber); +//! ``` +//! +//! Multiple `Layer`s may be composed in the same manner: +//! ```rust +//! # use tracing_subscriber::{Layer, layer::SubscriberExt}; +//! # use tracing::Subscriber; +//! pub struct MyOtherLayer { +//! // ... +//! } +//! +//! impl<S: Subscriber> Layer<S> for MyOtherLayer { +//! // ... +//! } +//! +//! pub struct MyThirdLayer { +//! // ... +//! } +//! +//! impl<S: Subscriber> Layer<S> for MyThirdLayer { +//! // ... +//! } +//! # pub struct MyLayer {} +//! # impl<S: Subscriber> Layer<S> for MyLayer {} +//! # pub struct MySubscriber { } +//! # use tracing_core::{span::{Id, Attributes, Record}, Metadata, Event}; +//! # impl Subscriber for MySubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(1) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! } +//! # impl MyLayer { +//! # fn new() -> Self { Self {} } +//! # } +//! # impl MyOtherLayer { +//! # fn new() -> Self { Self {} } +//! # } +//! # impl MyThirdLayer { +//! # fn new() -> Self { Self {} } +//! # } +//! # impl MySubscriber { +//! # fn new() -> Self { Self { }} +//! # } +//! +//! let subscriber = MySubscriber::new() +//! .with(MyLayer::new()) +//! .with(MyOtherLayer::new()) +//! .with(MyThirdLayer::new()); +//! +//! tracing::subscriber::set_global_default(subscriber); +//! ``` +//! +//! The [`Layer::with_subscriber`] constructs the [`Layered`] type from a +//! [`Layer`] and [`Subscriber`], and is called by [`SubscriberExt::with`]. In +//! general, it is more idiomatic to use [`SubscriberExt::with`], and treat +//! [`Layer::with_subscriber`] as an implementation detail, as `with_subscriber` +//! calls must be nested, leading to less clear code for the reader. +//! +//! ## Runtime Configuration With `Layer`s +//! +//! In some cases, a particular [`Layer`] may be enabled or disabled based on +//! runtime configuration. This can introduce challenges, because the type of a +//! layered [`Subscriber`] depends on which layers are added to it: if an `if` +//! or `match` expression adds some [`Layer`] implementation in one branch, +//! and other layers in another, the [`Subscriber`] values returned by those +//! branches will have different types. For example, the following _will not_ +//! work: +//! +//! ```compile_fail +//! # fn docs() -> Result<(), Box<dyn std::error::Error + 'static>> { +//! # struct Config { +//! # is_prod: bool, +//! # path: &'static str, +//! # } +//! # let cfg = Config { is_prod: false, path: "debug.log" }; +//! use std::fs::File; +//! use tracing_subscriber::{Registry, prelude::*}; +//! +//! let stdout_log = tracing_subscriber::fmt::layer().pretty(); +//! let subscriber = Registry::default().with(stdout_log); +//! +//! // The compile error will occur here because the if and else +//! // branches have different (and therefore incompatible) types. +//! let subscriber = if cfg.is_prod { +//! let file = File::create(cfg.path)?; +//! let layer = tracing_subscriber::fmt::layer() +//! .json() +//! .with_writer(Arc::new(file)); +//! layer.with(subscriber) +//! } else { +//! layer +//! }; +//! +//! tracing::subscriber::set_global_default(subscriber) +//! .expect("Unable to set global subscriber"); +//! # Ok(()) } +//! ``` +//! +//! However, a [`Layer`] wrapped in an [`Option`] [also implements the `Layer` +//! trait][option-impl]. This allows individual layers to be enabled or disabled at +//! runtime while always producing a [`Subscriber`] of the same type. For +//! example: +//! +//! ``` +//! # fn docs() -> Result<(), Box<dyn std::error::Error + 'static>> { +//! # struct Config { +//! # is_prod: bool, +//! # path: &'static str, +//! # } +//! # let cfg = Config { is_prod: false, path: "debug.log" }; +//! use std::fs::File; +//! use tracing_subscriber::{Registry, prelude::*}; +//! +//! let stdout_log = tracing_subscriber::fmt::layer().pretty(); +//! let subscriber = Registry::default().with(stdout_log); +//! +//! // if `cfg.is_prod` is true, also log JSON-formatted logs to a file. +//! let json_log = if cfg.is_prod { +//! let file = File::create(cfg.path)?; +//! let json_log = tracing_subscriber::fmt::layer() +//! .json() +//! .with_writer(file); +//! Some(json_log) +//! } else { +//! None +//! }; +//! +//! // If `cfg.is_prod` is false, then `json` will be `None`, and this layer +//! // will do nothing. However, the subscriber will still have the same type +//! // regardless of whether the `Option`'s value is `None` or `Some`. +//! let subscriber = subscriber.with(json_log); +//! +//! tracing::subscriber::set_global_default(subscriber) +//! .expect("Unable to set global subscriber"); +//! # Ok(()) } +//! ``` +//! +//! If a [`Layer`] may be one of several different types, note that [`Box<dyn +//! Layer<S> + Send + Sync>` implements `Layer`][box-impl]. +//! This may be used to erase the type of a [`Layer`]. +//! +//! For example, a function that configures a [`Layer`] to log to one of +//! several outputs might return a `Box<dyn Layer<S> + Send + Sync + 'static>`: +//! ``` +//! use tracing_subscriber::{ +//! Layer, +//! registry::LookupSpan, +//! prelude::*, +//! }; +//! use std::{path::PathBuf, fs::File, io}; +//! +//! /// Configures whether logs are emitted to a file, to stdout, or to stderr. +//! pub enum LogConfig { +//! File(PathBuf), +//! Stdout, +//! Stderr, +//! } +//! +//! impl LogConfig { +//! pub fn layer<S>(self) -> Box<dyn Layer<S> + Send + Sync + 'static> +//! where +//! S: tracing_core::Subscriber, +//! for<'a> S: LookupSpan<'a>, +//! { +//! // Shared configuration regardless of where logs are output to. +//! let fmt = tracing_subscriber::fmt::layer() +//! .with_target(true) +//! .with_thread_names(true); +//! +//! // Configure the writer based on the desired log target: +//! match self { +//! LogConfig::File(path) => { +//! let file = File::create(path).expect("failed to create log file"); +//! Box::new(fmt.with_writer(file)) +//! }, +//! LogConfig::Stdout => Box::new(fmt.with_writer(io::stdout)), +//! LogConfig::Stderr => Box::new(fmt.with_writer(io::stderr)), +//! } +//! } +//! } +//! +//! let config = LogConfig::Stdout; +//! tracing_subscriber::registry() +//! .with(config.layer()) +//! .init(); +//! ``` +//! +//! The [`Layer::boxed`] method is provided to make boxing a `Layer` +//! more convenient, but [`Box::new`] may be used as well. +//! +//! When the number of `Layer`s varies at runtime, note that a +//! [`Vec<L> where L: Layer` also implements `Layer`][vec-impl]. This +//! can be used to add a variable number of `Layer`s to a `Subscriber`: +//! +//! ``` +//! use tracing_subscriber::{Layer, prelude::*}; +//! struct MyLayer { +//! // ... +//! } +//! # impl MyLayer { fn new() -> Self { Self {} }} +//! +//! impl<S: tracing_core::Subscriber> Layer<S> for MyLayer { +//! // ... +//! } +//! +//! /// Returns how many layers we need +//! fn how_many_layers() -> usize { +//! // ... +//! # 3 +//! } +//! +//! // Create a variable-length `Vec` of layers +//! let mut layers = Vec::new(); +//! for _ in 0..how_many_layers() { +//! layers.push(MyLayer::new()); +//! } +//! +//! tracing_subscriber::registry() +//! .with(layers) +//! .init(); +//! ``` +//! +//! If a variable number of `Layer` is needed and those `Layer`s have +//! different types, a `Vec` of [boxed `Layer` trait objects][box-impl] may +//! be used. For example: +//! +//! ``` +//! use tracing_subscriber::{filter::LevelFilter, Layer, prelude::*}; +//! use std::fs::File; +//! # fn main() -> Result<(), Box<dyn std::error::Error>> { +//! struct Config { +//! enable_log_file: bool, +//! enable_stdout: bool, +//! enable_stderr: bool, +//! // ... +//! } +//! # impl Config { +//! # fn from_config_file()-> Result<Self, Box<dyn std::error::Error>> { +//! # // don't enable the log file so that the example doesn't actually create it +//! # Ok(Self { enable_log_file: false, enable_stdout: true, enable_stderr: true }) +//! # } +//! # } +//! +//! let cfg = Config::from_config_file()?; +//! +//! // Based on our dynamically loaded config file, create any number of layers: +//! let mut layers = Vec::new(); +//! +//! if cfg.enable_log_file { +//! let file = File::create("myapp.log")?; +//! let layer = tracing_subscriber::fmt::layer() +//! .with_thread_names(true) +//! .with_target(true) +//! .json() +//! .with_writer(file) +//! // Box the layer as a type-erased trait object, so that it can +//! // be pushed to the `Vec`. +//! .boxed(); +//! layers.push(layer); +//! } +//! +//! if cfg.enable_stdout { +//! let layer = tracing_subscriber::fmt::layer() +//! .pretty() +//! .with_filter(LevelFilter::INFO) +//! // Box the layer as a type-erased trait object, so that it can +//! // be pushed to the `Vec`. +//! .boxed(); +//! layers.push(layer); +//! } +//! +//! if cfg.enable_stdout { +//! let layer = tracing_subscriber::fmt::layer() +//! .with_target(false) +//! .with_filter(LevelFilter::WARN) +//! // Box the layer as a type-erased trait object, so that it can +//! // be pushed to the `Vec`. +//! .boxed(); +//! layers.push(layer); +//! } +//! +//! tracing_subscriber::registry() +//! .with(layers) +//! .init(); +//!# Ok(()) } +//! ``` +//! +//! Finally, if the number of layers _changes_ at runtime, a `Vec` of +//! subscribers can be used alongside the [`reload`](crate::reload) module to +//! add or remove subscribers dynamically at runtime. +//! +//! [option-impl]: Layer#impl-Layer<S>-for-Option<L> +//! [box-impl]: Layer#impl-Layer%3CS%3E-for-Box%3Cdyn%20Layer%3CS%3E%20+%20Send%20+%20Sync%3E +//! [vec-impl]: Layer#impl-Layer<S>-for-Vec<L> +//! [prelude]: crate::prelude +//! +//! # Recording Traces +//! +//! The [`Layer`] trait defines a set of methods for consuming notifications from +//! tracing instrumentation, which are generally equivalent to the similarly +//! named methods on [`Subscriber`]. Unlike [`Subscriber`], the methods on +//! `Layer` are additionally passed a [`Context`] type, which exposes additional +//! information provided by the wrapped subscriber (such as [the current span]) +//! to the layer. +//! +//! # Filtering with `Layer`s +//! +//! As well as strategies for handling trace events, the `Layer` trait may also +//! be used to represent composable _filters_. This allows the determination of +//! what spans and events should be recorded to be decoupled from _how_ they are +//! recorded: a filtering layer can be applied to other layers or +//! subscribers. `Layer`s can be used to implement _global filtering_, where a +//! `Layer` provides a filtering strategy for the entire subscriber. +//! Additionally, individual recording `Layer`s or sets of `Layer`s may be +//! combined with _per-layer filters_ that control what spans and events are +//! recorded by those layers. +//! +//! ## Global Filtering +//! +//! A `Layer` that implements a filtering strategy should override the +//! [`register_callsite`] and/or [`enabled`] methods. It may also choose to implement +//! methods such as [`on_enter`], if it wishes to filter trace events based on +//! the current span context. +//! +//! Note that the [`Layer::register_callsite`] and [`Layer::enabled`] methods +//! determine whether a span or event is enabled *globally*. Thus, they should +//! **not** be used to indicate whether an individual layer wishes to record a +//! particular span or event. Instead, if a layer is only interested in a subset +//! of trace data, but does *not* wish to disable other spans and events for the +//! rest of the layer stack should ignore those spans and events in its +//! notification methods. +//! +//! The filtering methods on a stack of `Layer`s are evaluated in a top-down +//! order, starting with the outermost `Layer` and ending with the wrapped +//! [`Subscriber`]. If any layer returns `false` from its [`enabled`] method, or +//! [`Interest::never()`] from its [`register_callsite`] method, filter +//! evaluation will short-circuit and the span or event will be disabled. +//! +//! ### Enabling Interest +//! +//! Whenever an tracing event (or span) is emitted, it goes through a number of +//! steps to determine how and how much it should be processed. The earlier an +//! event is disabled, the less work has to be done to process the event, so +//! `Layer`s that implement filtering should attempt to disable unwanted +//! events as early as possible. In order, each event checks: +//! +//! - [`register_callsite`], once per callsite (roughly: once per time that +//! `event!` or `span!` is written in the source code; this is cached at the +//! callsite). See [`Subscriber::register_callsite`] and +//! [`tracing_core::callsite`] for a summary of how this behaves. +//! - [`enabled`], once per emitted event (roughly: once per time that `event!` +//! or `span!` is *executed*), and only if `register_callsite` regesters an +//! [`Interest::sometimes`]. This is the main customization point to globally +//! filter events based on their [`Metadata`]. If an event can be disabled +//! based only on [`Metadata`], it should be, as this allows the construction +//! of the actual `Event`/`Span` to be skipped. +//! - For events only (and not spans), [`event_enabled`] is called just before +//! processing the event. This gives layers one last chance to say that +//! an event should be filtered out, now that the event's fields are known. +//! +//! ## Per-Layer Filtering +//! +//! **Note**: per-layer filtering APIs currently require the [`"registry"` crate +//! feature flag][feat] to be enabled. +//! +//! Sometimes, it may be desirable for one `Layer` to record a particular subset +//! of spans and events, while a different subset of spans and events are +//! recorded by other `Layer`s. For example: +//! +//! - A layer that records metrics may wish to observe only events including +//! particular tracked values, while a logging layer ignores those events. +//! - If recording a distributed trace is expensive, it might be desirable to +//! only send spans with `INFO` and lower verbosity to the distributed tracing +//! system, while logging more verbose spans to a file. +//! - Spans and events with a particular target might be recorded differently +//! from others, such as by generating an HTTP access log from a span that +//! tracks the lifetime of an HTTP request. +//! +//! The [`Filter`] trait is used to control what spans and events are +//! observed by an individual `Layer`, while still allowing other `Layer`s to +//! potentially record them. The [`Layer::with_filter`] method combines a +//! `Layer` with a [`Filter`], returning a [`Filtered`] layer. +//! +//! This crate's [`filter`] module provides a number of types which implement +//! the [`Filter`] trait, such as [`LevelFilter`], [`Targets`], and +//! [`FilterFn`]. These [`Filter`]s provide ready-made implementations of +//! common forms of filtering. For custom filtering policies, the [`FilterFn`] +//! and [`DynFilterFn`] types allow implementing a [`Filter`] with a closure or +//! function pointer. In addition, when more control is required, the [`Filter`] +//! trait may also be implemented for user-defined types. +//! +//! <pre class="compile_fail" style="white-space:normal;font:inherit;"> +//! <strong>Warning</strong>: Currently, the <a href="../struct.Registry.html"> +//! <code>Registry</code></a> type defined in this crate is the only root +//! <code>Subscriber</code> capable of supporting <code>Layer</code>s with +//! per-layer filters. In the future, new APIs will be added to allow other +//! root <code>Subscriber</code>s to support per-layer filters. +//! </pre> +//! +//! For example, to generate an HTTP access log based on spans with +//! the `http_access` target, while logging other spans and events to +//! standard out, a [`Filter`] can be added to the access log layer: +//! +//! ``` +//! use tracing_subscriber::{filter, prelude::*}; +//! +//! // Generates an HTTP access log. +//! let access_log = // ... +//! # filter::LevelFilter::INFO; +//! +//! // Add a filter to the access log layer so that it only observes +//! // spans and events with the `http_access` target. +//! let access_log = access_log.with_filter(filter::filter_fn(|metadata| { +//! // Returns `true` if and only if the span or event's target is +//! // "http_access". +//! metadata.target() == "http_access" +//! })); +//! +//! // A general-purpose logging layer. +//! let fmt_layer = tracing_subscriber::fmt::layer(); +//! +//! // Build a subscriber that combines the access log and stdout log +//! // layers. +//! tracing_subscriber::registry() +//! .with(fmt_layer) +//! .with(access_log) +//! .init(); +//! ``` +//! +//! Multiple layers can have their own, separate per-layer filters. A span or +//! event will be recorded if it is enabled by _any_ per-layer filter, but it +//! will be skipped by the layers whose filters did not enable it. Building on +//! the previous example: +//! +//! ``` +//! use tracing_subscriber::{filter::{filter_fn, LevelFilter}, prelude::*}; +//! +//! let access_log = // ... +//! # LevelFilter::INFO; +//! let fmt_layer = tracing_subscriber::fmt::layer(); +//! +//! tracing_subscriber::registry() +//! // Add the filter for the "http_access" target to the access +//! // log layer, like before. +//! .with(access_log.with_filter(filter_fn(|metadata| { +//! metadata.target() == "http_access" +//! }))) +//! // Add a filter for spans and events with the INFO level +//! // and below to the logging layer. +//! .with(fmt_layer.with_filter(LevelFilter::INFO)) +//! .init(); +//! +//! // Neither layer will observe this event +//! tracing::debug!(does_anyone_care = false, "a tree fell in the forest"); +//! +//! // This event will be observed by the logging layer, but not +//! // by the access log layer. +//! tracing::warn!(dose_roentgen = %3.8, "not great, but not terrible"); +//! +//! // This event will be observed only by the access log layer. +//! tracing::trace!(target: "http_access", "HTTP request started"); +//! +//! // Both layers will observe this event. +//! tracing::error!(target: "http_access", "HTTP request failed with a very bad error!"); +//! ``` +//! +//! A per-layer filter can be applied to multiple [`Layer`]s at a time, by +//! combining them into a [`Layered`] layer using [`Layer::and_then`], and then +//! calling [`Layer::with_filter`] on the resulting [`Layered`] layer. +//! +//! Consider the following: +//! - `layer_a` and `layer_b`, which should only receive spans and events at +//! the [`INFO`] [level] and above. +//! - A third layer, `layer_c`, which should receive spans and events at +//! the [`DEBUG`] [level] as well. +//! The layers and filters would be composed thusly: +//! +//! ``` +//! use tracing_subscriber::{filter::LevelFilter, prelude::*}; +//! +//! let layer_a = // ... +//! # LevelFilter::INFO; +//! let layer_b = // ... +//! # LevelFilter::INFO; +//! let layer_c = // ... +//! # LevelFilter::INFO; +//! +//! let info_layers = layer_a +//! // Combine `layer_a` and `layer_b` into a `Layered` layer: +//! .and_then(layer_b) +//! // ...and then add an `INFO` `LevelFilter` to that layer: +//! .with_filter(LevelFilter::INFO); +//! +//! tracing_subscriber::registry() +//! // Add `layer_c` with a `DEBUG` filter. +//! .with(layer_c.with_filter(LevelFilter::DEBUG)) +//! .with(info_layers) +//! .init(); +//!``` +//! +//! If a [`Filtered`] [`Layer`] is combined with another [`Layer`] +//! [`Layer::and_then`], and a filter is added to the [`Layered`] layer, that +//! layer will be filtered by *both* the inner filter and the outer filter. +//! Only spans and events that are enabled by *both* filters will be +//! observed by that layer. This can be used to implement complex filtering +//! trees. +//! +//! As an example, consider the following constraints: +//! - Suppose that a particular [target] is used to indicate events that +//! should be counted as part of a metrics system, which should be only +//! observed by a layer that collects metrics. +//! - A log of high-priority events ([`INFO`] and above) should be logged +//! to stdout, while more verbose events should be logged to a debugging log file. +//! - Metrics-focused events should *not* be included in either log output. +//! +//! In that case, it is possible to apply a filter to both logging layers to +//! exclude the metrics events, while additionally adding a [`LevelFilter`] +//! to the stdout log: +//! +//! ``` +//! # // wrap this in a function so we don't actually create `debug.log` when +//! # // running the doctests.. +//! # fn docs() -> Result<(), Box<dyn std::error::Error + 'static>> { +//! use tracing_subscriber::{filter, prelude::*}; +//! use std::{fs::File, sync::Arc}; +//! +//! // A layer that logs events to stdout using the human-readable "pretty" +//! // format. +//! let stdout_log = tracing_subscriber::fmt::layer() +//! .pretty(); +//! +//! // A layer that logs events to a file. +//! let file = File::create("debug.log")?; +//! let debug_log = tracing_subscriber::fmt::layer() +//! .with_writer(Arc::new(file)); +//! +//! // A layer that collects metrics using specific events. +//! let metrics_layer = /* ... */ filter::LevelFilter::INFO; +//! +//! tracing_subscriber::registry() +//! .with( +//! stdout_log +//! // Add an `INFO` filter to the stdout logging layer +//! .with_filter(filter::LevelFilter::INFO) +//! // Combine the filtered `stdout_log` layer with the +//! // `debug_log` layer, producing a new `Layered` layer. +//! .and_then(debug_log) +//! // Add a filter to *both* layers that rejects spans and +//! // events whose targets start with `metrics`. +//! .with_filter(filter::filter_fn(|metadata| { +//! !metadata.target().starts_with("metrics") +//! })) +//! ) +//! .with( +//! // Add a filter to the metrics label that *only* enables +//! // events whose targets start with `metrics`. +//! metrics_layer.with_filter(filter::filter_fn(|metadata| { +//! metadata.target().starts_with("metrics") +//! })) +//! ) +//! .init(); +//! +//! // This event will *only* be recorded by the metrics layer. +//! tracing::info!(target: "metrics::cool_stuff_count", value = 42); +//! +//! // This event will only be seen by the debug log file layer: +//! tracing::debug!("this is a message, and part of a system of messages"); +//! +//! // This event will be seen by both the stdout log layer *and* +//! // the debug log file layer, but not by the metrics layer. +//! tracing::warn!("the message is a warning about danger!"); +//! # Ok(()) } +//! ``` +//! +//! [`Subscriber`]: tracing_core::subscriber::Subscriber +//! [span IDs]: tracing_core::span::Id +//! [the current span]: Context::current_span +//! [`register_callsite`]: Layer::register_callsite +//! [`enabled`]: Layer::enabled +//! [`event_enabled`]: Layer::event_enabled +//! [`on_enter`]: Layer::on_enter +//! [`Layer::register_callsite`]: Layer::register_callsite +//! [`Layer::enabled`]: Layer::enabled +//! [`Interest::never()`]: tracing_core::subscriber::Interest::never() +//! [`Filtered`]: crate::filter::Filtered +//! [`filter`]: crate::filter +//! [`Targets`]: crate::filter::Targets +//! [`FilterFn`]: crate::filter::FilterFn +//! [`DynFilterFn`]: crate::filter::DynFilterFn +//! [level]: tracing_core::Level +//! [`INFO`]: tracing_core::Level::INFO +//! [`DEBUG`]: tracing_core::Level::DEBUG +//! [target]: tracing_core::Metadata::target +//! [`LevelFilter`]: crate::filter::LevelFilter +//! [feat]: crate#feature-flags +use crate::filter; + +use tracing_core::{ + metadata::Metadata, + span, + subscriber::{Interest, Subscriber}, + Event, LevelFilter, +}; + +use core::any::TypeId; + +feature! { + #![feature = "alloc"] + use alloc::boxed::Box; + use core::ops::{Deref, DerefMut}; +} + +mod context; +mod layered; +pub use self::{context::*, layered::*}; + +// The `tests` module is `pub(crate)` because it contains test utilities used by +// other modules. +#[cfg(test)] +pub(crate) mod tests; + +/// A composable handler for `tracing` events. +/// +/// A `Layer` implements a behavior for recording or collecting traces that can +/// be composed together with other `Layer`s to build a [`Subscriber`]. See the +/// [module-level documentation](crate::layer) for details. +/// +/// [`Subscriber`]: tracing_core::Subscriber +#[cfg_attr(docsrs, doc(notable_trait))] +pub trait Layer<S> +where + S: Subscriber, + Self: 'static, +{ + /// Performs late initialization when attaching a `Layer` to a + /// [`Subscriber`]. + /// + /// This is a callback that is called when the `Layer` is added to a + /// [`Subscriber`] (e.g. in [`Layer::with_subscriber`] and + /// [`SubscriberExt::with`]). Since this can only occur before the + /// [`Subscriber`] has been set as the default, both the `Layer` and + /// [`Subscriber`] are passed to this method _mutably_. This gives the + /// `Layer` the opportunity to set any of its own fields with values + /// recieved by method calls on the [`Subscriber`]. + /// + /// For example, [`Filtered`] layers implement `on_layer` to call the + /// [`Subscriber`]'s [`register_filter`] method, and store the returned + /// [`FilterId`] as a field. + /// + /// **Note** In most cases, `Layer` implementations will not need to + /// implement this method. However, in cases where a type implementing + /// `Layer` wraps one or more other types that implement `Layer`, like the + /// [`Layered`] and [`Filtered`] types in this crate, that type MUST ensure + /// that the inner `Layer`s' `on_layer` methods are called. Otherwise, + /// functionality that relies on `on_layer`, such as [per-layer filtering], + /// may not work correctly. + /// + /// [`Filtered`]: crate::filter::Filtered + /// [`register_filter`]: crate::registry::LookupSpan::register_filter + /// [per-layer filtering]: #per-layer-filtering + /// [`FilterId`]: crate::filter::FilterId + fn on_layer(&mut self, subscriber: &mut S) { + let _ = subscriber; + } + + /// Registers a new callsite with this layer, returning whether or not + /// the layer is interested in being notified about the callsite, similarly + /// to [`Subscriber::register_callsite`]. + /// + /// By default, this returns [`Interest::always()`] if [`self.enabled`] returns + /// true, or [`Interest::never()`] if it returns false. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This method (and <a href="#method.enabled"> + /// <code>Layer::enabled</code></a>) determine whether a span or event is + /// globally enabled, <em>not</em> whether the individual layer will be + /// notified about that span or event. This is intended to be used + /// by layers that implement filtering for the entire stack. Layers which do + /// not wish to be notified about certain spans or events but do not wish to + /// globally disable them should ignore those spans or events in their + /// <a href="#method.on_event"><code>on_event</code></a>, + /// <a href="#method.on_enter"><code>on_enter</code></a>, + /// <a href="#method.on_exit"><code>on_exit</code></a>, and other notification + /// methods. + /// </pre> + /// + /// See [the trait-level documentation] for more information on filtering + /// with `Layer`s. + /// + /// Layers may also implement this method to perform any behaviour that + /// should be run once per callsite. If the layer wishes to use + /// `register_callsite` for per-callsite behaviour, but does not want to + /// globally enable or disable those callsites, it should always return + /// [`Interest::always()`]. + /// + /// [`Interest`]: tracing_core::Interest + /// [`Subscriber::register_callsite`]: tracing_core::Subscriber::register_callsite() + /// [`Interest::never()`]: tracing_core::subscriber::Interest::never() + /// [`Interest::always()`]: tracing_core::subscriber::Interest::always() + /// [`self.enabled`]: Layer::enabled() + /// [`Layer::enabled`]: Layer::enabled() + /// [`on_event`]: Layer::on_event() + /// [`on_enter`]: Layer::on_enter() + /// [`on_exit`]: Layer::on_exit() + /// [the trait-level documentation]: #filtering-with-layers + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + if self.enabled(metadata, Context::none()) { + Interest::always() + } else { + Interest::never() + } + } + + /// Returns `true` if this layer is interested in a span or event with the + /// given `metadata` in the current [`Context`], similarly to + /// [`Subscriber::enabled`]. + /// + /// By default, this always returns `true`, allowing the wrapped subscriber + /// to choose to disable the span. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: This method (and <a href="#method.register_callsite"> + /// <code>Layer::register_callsite</code></a>) determine whether a span or event is + /// globally enabled, <em>not</em> whether the individual layer will be + /// notified about that span or event. This is intended to be used + /// by layers that implement filtering for the entire stack. Layers which do + /// not wish to be notified about certain spans or events but do not wish to + /// globally disable them should ignore those spans or events in their + /// <a href="#method.on_event"><code>on_event</code></a>, + /// <a href="#method.on_enter"><code>on_enter</code></a>, + /// <a href="#method.on_exit"><code>on_exit</code></a>, and other notification + /// methods. + /// </pre> + /// + /// + /// See [the trait-level documentation] for more information on filtering + /// with `Layer`s. + /// + /// [`Interest`]: tracing_core::Interest + /// [`Subscriber::enabled`]: tracing_core::Subscriber::enabled() + /// [`Layer::register_callsite`]: Layer::register_callsite() + /// [`on_event`]: Layer::on_event() + /// [`on_enter`]: Layer::on_enter() + /// [`on_exit`]: Layer::on_exit() + /// [the trait-level documentation]: #filtering-with-layers + fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool { + let _ = (metadata, ctx); + true + } + + /// Notifies this layer that a new span was constructed with the given + /// `Attributes` and `Id`. + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + let _ = (attrs, id, ctx); + } + + // TODO(eliza): do we want this to be a public API? If we end up moving + // filtering layers to a separate trait, we may no longer want `Layer`s to + // be able to participate in max level hinting... + #[doc(hidden)] + fn max_level_hint(&self) -> Option<LevelFilter> { + None + } + + /// Notifies this layer that a span with the given `Id` recorded the given + /// `values`. + // Note: it's unclear to me why we'd need the current span in `record` (the + // only thing the `Context` type currently provides), but passing it in anyway + // seems like a good future-proofing measure as it may grow other methods later... + fn on_record(&self, _span: &span::Id, _values: &span::Record<'_>, _ctx: Context<'_, S>) {} + + /// Notifies this layer that a span with the ID `span` recorded that it + /// follows from the span with the ID `follows`. + // Note: it's unclear to me why we'd need the current span in `record` (the + // only thing the `Context` type currently provides), but passing it in anyway + // seems like a good future-proofing measure as it may grow other methods later... + fn on_follows_from(&self, _span: &span::Id, _follows: &span::Id, _ctx: Context<'_, S>) {} + + /// Called before [`on_event`], to determine if `on_event` should be called. + /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// + /// **Note**: This method determines whether an event is globally enabled, + /// *not* whether the individual `Layer` will be notified about the + /// event. This is intended to be used by `Layer`s that implement + /// filtering for the entire stack. `Layer`s which do not wish to be + /// notified about certain events but do not wish to globally disable them + /// should ignore those events in their [on_event][Self::on_event]. + /// + /// </pre></div> + /// + /// See [the trait-level documentation] for more information on filtering + /// with `Layer`s. + /// + /// [`on_event`]: Self::on_event + /// [`Interest`]: tracing_core::Interest + /// [the trait-level documentation]: #filtering-with-layers + #[inline] // collapse this to a constant please mrs optimizer + fn event_enabled(&self, _event: &Event<'_>, _ctx: Context<'_, S>) -> bool { + true + } + + /// Notifies this layer that an event has occurred. + fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, S>) {} + + /// Notifies this layer that a span with the given ID was entered. + fn on_enter(&self, _id: &span::Id, _ctx: Context<'_, S>) {} + + /// Notifies this layer that the span with the given ID was exited. + fn on_exit(&self, _id: &span::Id, _ctx: Context<'_, S>) {} + + /// Notifies this layer that the span with the given ID has been closed. + fn on_close(&self, _id: span::Id, _ctx: Context<'_, S>) {} + + /// Notifies this layer that a span ID has been cloned, and that the + /// subscriber returned a different ID. + fn on_id_change(&self, _old: &span::Id, _new: &span::Id, _ctx: Context<'_, S>) {} + + /// Composes this layer around the given `Layer`, returning a `Layered` + /// struct implementing `Layer`. + /// + /// The returned `Layer` will call the methods on this `Layer` and then + /// those of the new `Layer`, before calling the methods on the subscriber + /// it wraps. For example: + /// + /// ```rust + /// # use tracing_subscriber::layer::Layer; + /// # use tracing_core::Subscriber; + /// pub struct FooLayer { + /// // ... + /// } + /// + /// pub struct BarLayer { + /// // ... + /// } + /// + /// pub struct MySubscriber { + /// // ... + /// } + /// + /// impl<S: Subscriber> Layer<S> for FooLayer { + /// // ... + /// } + /// + /// impl<S: Subscriber> Layer<S> for BarLayer { + /// // ... + /// } + /// + /// # impl FooLayer { + /// # fn new() -> Self { Self {} } + /// # } + /// # impl BarLayer { + /// # fn new() -> Self { Self { }} + /// # } + /// # impl MySubscriber { + /// # fn new() -> Self { Self { }} + /// # } + /// # use tracing_core::{span::{Id, Attributes, Record}, Metadata, Event}; + /// # impl tracing_core::Subscriber for MySubscriber { + /// # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(1) } + /// # fn record(&self, _: &Id, _: &Record) {} + /// # fn event(&self, _: &Event) {} + /// # fn record_follows_from(&self, _: &Id, _: &Id) {} + /// # fn enabled(&self, _: &Metadata) -> bool { false } + /// # fn enter(&self, _: &Id) {} + /// # fn exit(&self, _: &Id) {} + /// # } + /// let subscriber = FooLayer::new() + /// .and_then(BarLayer::new()) + /// .with_subscriber(MySubscriber::new()); + /// ``` + /// + /// Multiple layers may be composed in this manner: + /// + /// ```rust + /// # use tracing_subscriber::layer::Layer; + /// # use tracing_core::Subscriber; + /// # pub struct FooLayer {} + /// # pub struct BarLayer {} + /// # pub struct MySubscriber {} + /// # impl<S: Subscriber> Layer<S> for FooLayer {} + /// # impl<S: Subscriber> Layer<S> for BarLayer {} + /// # impl FooLayer { + /// # fn new() -> Self { Self {} } + /// # } + /// # impl BarLayer { + /// # fn new() -> Self { Self { }} + /// # } + /// # impl MySubscriber { + /// # fn new() -> Self { Self { }} + /// # } + /// # use tracing_core::{span::{Id, Attributes, Record}, Metadata, Event}; + /// # impl tracing_core::Subscriber for MySubscriber { + /// # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(1) } + /// # fn record(&self, _: &Id, _: &Record) {} + /// # fn event(&self, _: &Event) {} + /// # fn record_follows_from(&self, _: &Id, _: &Id) {} + /// # fn enabled(&self, _: &Metadata) -> bool { false } + /// # fn enter(&self, _: &Id) {} + /// # fn exit(&self, _: &Id) {} + /// # } + /// pub struct BazLayer { + /// // ... + /// } + /// + /// impl<S: Subscriber> Layer<S> for BazLayer { + /// // ... + /// } + /// # impl BazLayer { fn new() -> Self { BazLayer {} } } + /// + /// let subscriber = FooLayer::new() + /// .and_then(BarLayer::new()) + /// .and_then(BazLayer::new()) + /// .with_subscriber(MySubscriber::new()); + /// ``` + fn and_then<L>(self, layer: L) -> Layered<L, Self, S> + where + L: Layer<S>, + Self: Sized, + { + let inner_has_layer_filter = filter::layer_has_plf(&self); + Layered::new(layer, self, inner_has_layer_filter) + } + + /// Composes this `Layer` with the given [`Subscriber`], returning a + /// `Layered` struct that implements [`Subscriber`]. + /// + /// The returned `Layered` subscriber will call the methods on this `Layer` + /// and then those of the wrapped subscriber. + /// + /// For example: + /// ```rust + /// # use tracing_subscriber::layer::Layer; + /// # use tracing_core::Subscriber; + /// pub struct FooLayer { + /// // ... + /// } + /// + /// pub struct MySubscriber { + /// // ... + /// } + /// + /// impl<S: Subscriber> Layer<S> for FooLayer { + /// // ... + /// } + /// + /// # impl FooLayer { + /// # fn new() -> Self { Self {} } + /// # } + /// # impl MySubscriber { + /// # fn new() -> Self { Self { }} + /// # } + /// # use tracing_core::{span::{Id, Attributes, Record}, Metadata}; + /// # impl tracing_core::Subscriber for MySubscriber { + /// # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } + /// # fn record(&self, _: &Id, _: &Record) {} + /// # fn event(&self, _: &tracing_core::Event) {} + /// # fn record_follows_from(&self, _: &Id, _: &Id) {} + /// # fn enabled(&self, _: &Metadata) -> bool { false } + /// # fn enter(&self, _: &Id) {} + /// # fn exit(&self, _: &Id) {} + /// # } + /// let subscriber = FooLayer::new() + /// .with_subscriber(MySubscriber::new()); + ///``` + /// + /// [`Subscriber`]: tracing_core::Subscriber + fn with_subscriber(mut self, mut inner: S) -> Layered<Self, S> + where + Self: Sized, + { + let inner_has_layer_filter = filter::subscriber_has_plf(&inner); + self.on_layer(&mut inner); + Layered::new(self, inner, inner_has_layer_filter) + } + + /// Combines `self` with a [`Filter`], returning a [`Filtered`] layer. + /// + /// The [`Filter`] will control which spans and events are enabled for + /// this layer. See [the trait-level documentation][plf] for details on + /// per-layer filtering. + /// + /// [`Filtered`]: crate::filter::Filtered + /// [plf]: crate::layer#per-layer-filtering + #[cfg(all(feature = "registry", feature = "std"))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))] + fn with_filter<F>(self, filter: F) -> filter::Filtered<Self, F, S> + where + Self: Sized, + F: Filter<S>, + { + filter::Filtered::new(self, filter) + } + + /// Erases the type of this [`Layer`], returning a [`Box`]ed `dyn + /// Layer` trait object. + /// + /// This can be used when a function returns a `Layer` which may be of + /// one of several types, or when a `Layer` subscriber has a very long type + /// signature. + /// + /// # Examples + /// + /// The following example will *not* compile, because the value assigned to + /// `log_layer` may have one of several different types: + /// + /// ```compile_fail + /// # fn main() -> Result<(), Box<dyn std::error::Error>> { + /// use tracing_subscriber::{Layer, filter::LevelFilter, prelude::*}; + /// use std::{path::PathBuf, fs::File, io}; + /// + /// /// Configures whether logs are emitted to a file, to stdout, or to stderr. + /// pub enum LogConfig { + /// File(PathBuf), + /// Stdout, + /// Stderr, + /// } + /// + /// let config = // ... + /// # LogConfig::Stdout; + /// + /// // Depending on the config, construct a layer of one of several types. + /// let log_layer = match config { + /// // If logging to a file, use a maximally-verbose configuration. + /// LogConfig::File(path) => { + /// let file = File::create(path)?; + /// tracing_subscriber::fmt::layer() + /// .with_thread_ids(true) + /// .with_thread_names(true) + /// // Selecting the JSON logging format changes the layer's + /// // type. + /// .json() + /// .with_span_list(true) + /// // Setting the writer to use our log file changes the + /// // layer's type again. + /// .with_writer(file) + /// }, + /// + /// // If logging to stdout, use a pretty, human-readable configuration. + /// LogConfig::Stdout => tracing_subscriber::fmt::layer() + /// // Selecting the "pretty" logging format changes the + /// // layer's type! + /// .pretty() + /// .with_writer(io::stdout) + /// // Add a filter based on the RUST_LOG environment variable; + /// // this changes the type too! + /// .and_then(tracing_subscriber::EnvFilter::from_default_env()), + /// + /// // If logging to stdout, only log errors and warnings. + /// LogConfig::Stderr => tracing_subscriber::fmt::layer() + /// // Changing the writer changes the layer's type + /// .with_writer(io::stderr) + /// // Only log the `WARN` and `ERROR` levels. Adding a filter + /// // changes the layer's type to `Filtered<LevelFilter, ...>`. + /// .with_filter(LevelFilter::WARN), + /// }; + /// + /// tracing_subscriber::registry() + /// .with(log_layer) + /// .init(); + /// # Ok(()) } + /// ``` + /// + /// However, adding a call to `.boxed()` after each match arm erases the + /// layer's type, so this code *does* compile: + /// + /// ``` + /// # fn main() -> Result<(), Box<dyn std::error::Error>> { + /// # use tracing_subscriber::{Layer, filter::LevelFilter, prelude::*}; + /// # use std::{path::PathBuf, fs::File, io}; + /// # pub enum LogConfig { + /// # File(PathBuf), + /// # Stdout, + /// # Stderr, + /// # } + /// # let config = LogConfig::Stdout; + /// let log_layer = match config { + /// LogConfig::File(path) => { + /// let file = File::create(path)?; + /// tracing_subscriber::fmt::layer() + /// .with_thread_ids(true) + /// .with_thread_names(true) + /// .json() + /// .with_span_list(true) + /// .with_writer(file) + /// // Erase the type by boxing the layer + /// .boxed() + /// }, + /// + /// LogConfig::Stdout => tracing_subscriber::fmt::layer() + /// .pretty() + /// .with_writer(io::stdout) + /// .and_then(tracing_subscriber::EnvFilter::from_default_env()) + /// // Erase the type by boxing the layer + /// .boxed(), + /// + /// LogConfig::Stderr => tracing_subscriber::fmt::layer() + /// .with_writer(io::stderr) + /// .with_filter(LevelFilter::WARN) + /// // Erase the type by boxing the layer + /// .boxed(), + /// }; + /// + /// tracing_subscriber::registry() + /// .with(log_layer) + /// .init(); + /// # Ok(()) } + /// ``` + #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))] + fn boxed(self) -> Box<dyn Layer<S> + Send + Sync + 'static> + where + Self: Sized, + Self: Layer<S> + Send + Sync + 'static, + S: Subscriber, + { + Box::new(self) + } + + #[doc(hidden)] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + if id == TypeId::of::<Self>() { + Some(self as *const _ as *const ()) + } else { + None + } + } +} + +feature! { + #![all(feature = "registry", feature = "std")] + + /// A per-[`Layer`] filter that determines whether a span or event is enabled + /// for an individual layer. + /// + /// See [the module-level documentation][plf] for details on using [`Filter`]s. + /// + /// [plf]: crate::layer#per-layer-filtering + #[cfg_attr(docsrs, doc(notable_trait))] + pub trait Filter<S> { + /// Returns `true` if this layer is interested in a span or event with the + /// given [`Metadata`] in the current [`Context`], similarly to + /// [`Subscriber::enabled`]. + /// + /// If this returns `false`, the span or event will be disabled _for the + /// wrapped [`Layer`]_. Unlike [`Layer::enabled`], the span or event will + /// still be recorded if any _other_ layers choose to enable it. However, + /// the layer [filtered] by this filter will skip recording that span or + /// event. + /// + /// If all layers indicate that they do not wish to see this span or event, + /// it will be disabled. + /// + /// [`metadata`]: tracing_core::Metadata + /// [`Subscriber::enabled`]: tracing_core::Subscriber::enabled + /// [filtered]: crate::filter::Filtered + fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool; + + /// Returns an [`Interest`] indicating whether this layer will [always], + /// [sometimes], or [never] be interested in the given [`Metadata`]. + /// + /// When a given callsite will [always] or [never] be enabled, the results + /// of evaluating the filter may be cached for improved performance. + /// Therefore, if a filter is capable of determining that it will always or + /// never enable a particular callsite, providing an implementation of this + /// function is recommended. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: If a <code>Filter</code> will perform + /// <em>dynamic filtering</em> that depends on the current context in which + /// a span or event was observered (e.g. only enabling an event when it + /// occurs within a particular span), it <strong>must</strong> return + /// <code>Interest::sometimes()</code> from this method. If it returns + /// <code>Interest::always()</code> or <code>Interest::never()</code>, the + /// <code>enabled</code> method may not be called when a particular instance + /// of that span or event is recorded. + /// </pre> + /// + /// This method is broadly similar to [`Subscriber::register_callsite`]; + /// however, since the returned value represents only the interest of + /// *this* layer, the resulting behavior is somewhat different. + /// + /// If a [`Subscriber`] returns [`Interest::always()`][always] or + /// [`Interest::never()`][never] for a given [`Metadata`], its [`enabled`] + /// method is then *guaranteed* to never be called for that callsite. On the + /// other hand, when a `Filter` returns [`Interest::always()`][always] or + /// [`Interest::never()`][never] for a callsite, _other_ [`Layer`]s may have + /// differing interests in that callsite. If this is the case, the callsite + /// will recieve [`Interest::sometimes()`][sometimes], and the [`enabled`] + /// method will still be called for that callsite when it records a span or + /// event. + /// + /// Returning [`Interest::always()`][always] or [`Interest::never()`][never] from + /// `Filter::callsite_enabled` will permanently enable or disable a + /// callsite (without requiring subsequent calls to [`enabled`]) if and only + /// if the following is true: + /// + /// - all [`Layer`]s that comprise the subscriber include `Filter`s + /// (this includes a tree of [`Layered`] layers that share the same + /// `Filter`) + /// - all those `Filter`s return the same [`Interest`]. + /// + /// For example, if a [`Subscriber`] consists of two [`Filtered`] layers, + /// and both of those layers return [`Interest::never()`][never], that + /// callsite *will* never be enabled, and the [`enabled`] methods of those + /// [`Filter`]s will not be called. + /// + /// ## Default Implementation + /// + /// The default implementation of this method assumes that the + /// `Filter`'s [`enabled`] method _may_ perform dynamic filtering, and + /// returns [`Interest::sometimes()`][sometimes], to ensure that [`enabled`] + /// is called to determine whether a particular _instance_ of the callsite + /// is enabled in the current context. If this is *not* the case, and the + /// `Filter`'s [`enabled`] method will always return the same result + /// for a particular [`Metadata`], this method can be overridden as + /// follows: + /// + /// ``` + /// use tracing_subscriber::layer; + /// use tracing_core::{Metadata, subscriber::Interest}; + /// + /// struct MyFilter { + /// // ... + /// } + /// + /// impl MyFilter { + /// // The actual logic for determining whether a `Metadata` is enabled + /// // must be factored out from the `enabled` method, so that it can be + /// // called without a `Context` (which is not provided to the + /// // `callsite_enabled` method). + /// fn is_enabled(&self, metadata: &Metadata<'_>) -> bool { + /// // ... + /// # drop(metadata); true + /// } + /// } + /// + /// impl<S> layer::Filter<S> for MyFilter { + /// fn enabled(&self, metadata: &Metadata<'_>, _: &layer::Context<'_, S>) -> bool { + /// // Even though we are implementing `callsite_enabled`, we must still provide a + /// // working implementation of `enabled`, as returning `Interest::always()` or + /// // `Interest::never()` will *allow* caching, but will not *guarantee* it. + /// // Other filters may still return `Interest::sometimes()`, so we may be + /// // asked again in `enabled`. + /// self.is_enabled(metadata) + /// } + /// + /// fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest { + /// // The result of `self.enabled(metadata, ...)` will always be + /// // the same for any given `Metadata`, so we can convert it into + /// // an `Interest`: + /// if self.is_enabled(metadata) { + /// Interest::always() + /// } else { + /// Interest::never() + /// } + /// } + /// } + /// ``` + /// + /// [`Metadata`]: tracing_core::Metadata + /// [`Interest`]: tracing_core::Interest + /// [always]: tracing_core::Interest::always + /// [sometimes]: tracing_core::Interest::sometimes + /// [never]: tracing_core::Interest::never + /// [`Subscriber::register_callsite`]: tracing_core::Subscriber::register_callsite + /// [`Subscriber`]: tracing_core::Subscriber + /// [`enabled`]: Filter::enabled + /// [`Filtered`]: crate::filter::Filtered + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + let _ = meta; + Interest::sometimes() + } + + /// Returns an optional hint of the highest [verbosity level][level] that + /// this `Filter` will enable. + /// + /// If this method returns a [`LevelFilter`], it will be used as a hint to + /// determine the most verbose level that will be enabled. This will allow + /// spans and events which are more verbose than that level to be skipped + /// more efficiently. An implementation of this method is optional, but + /// strongly encouraged. + /// + /// If the maximum level the `Filter` will enable can change over the + /// course of its lifetime, it is free to return a different value from + /// multiple invocations of this method. However, note that changes in the + /// maximum level will **only** be reflected after the callsite [`Interest`] + /// cache is rebuilt, by calling the + /// [`tracing_core::callsite::rebuild_interest_cache`][rebuild] function. + /// Therefore, if the `Filter will change the value returned by this + /// method, it is responsible for ensuring that + /// [`rebuild_interest_cache`][rebuild] is called after the value of the max + /// level changes. + /// + /// ## Default Implementation + /// + /// By default, this method returns `None`, indicating that the maximum + /// level is unknown. + /// + /// [level]: tracing_core::metadata::Level + /// [`LevelFilter`]: crate::filter::LevelFilter + /// [`Interest`]: tracing_core::subscriber::Interest + /// [rebuild]: tracing_core::callsite::rebuild_interest_cache + fn max_level_hint(&self) -> Option<LevelFilter> { + None + } + + /// Notifies this filter that a new span was constructed with the given + /// `Attributes` and `Id`. + /// + /// By default, this method does nothing. `Filter` implementations that + /// need to be notified when new spans are created can override this + /// method. + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + let _ = (attrs, id, ctx); + } + + + /// Notifies this filter that a span with the given `Id` recorded the given + /// `values`. + /// + /// By default, this method does nothing. `Filter` implementations that + /// need to be notified when new spans are created can override this + /// method. + fn on_record(&self, id: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + let _ = (id, values, ctx); + } + + /// Notifies this filter that a span with the given ID was entered. + /// + /// By default, this method does nothing. `Filter` implementations that + /// need to be notified when a span is entered can override this method. + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + let _ = (id, ctx); + } + + /// Notifies this filter that a span with the given ID was exited. + /// + /// By default, this method does nothing. `Filter` implementations that + /// need to be notified when a span is exited can override this method. + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + let _ = (id, ctx); + } + + /// Notifies this filter that a span with the given ID has been closed. + /// + /// By default, this method does nothing. `Filter` implementations that + /// need to be notified when a span is closed can override this method. + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { + let _ = (id, ctx); + } + } +} + +/// Extension trait adding a `with(Layer)` combinator to `Subscriber`s. +pub trait SubscriberExt: Subscriber + crate::sealed::Sealed { + /// Wraps `self` with the provided `layer`. + fn with<L>(self, layer: L) -> Layered<L, Self> + where + L: Layer<Self>, + Self: Sized, + { + layer.with_subscriber(self) + } +} + +/// A layer that does nothing. +#[derive(Clone, Debug, Default)] +pub struct Identity { + _p: (), +} + +// === impl Layer === + +impl<L, S> Layer<S> for Option<L> +where + L: Layer<S>, + S: Subscriber, +{ + fn on_layer(&mut self, subscriber: &mut S) { + if let Some(ref mut layer) = self { + layer.on_layer(subscriber) + } + } + + #[inline] + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_new_span(attrs, id, ctx) + } + } + + #[inline] + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + match self { + Some(ref inner) => inner.register_callsite(metadata), + None => Interest::always(), + } + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool { + match self { + Some(ref inner) => inner.enabled(metadata, ctx), + None => true, + } + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + match self { + Some(ref inner) => inner.max_level_hint(), + None => { + // There is no inner layer, so this layer will + // never enable anything. + Some(LevelFilter::OFF) + } + } + } + + #[inline] + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_record(span, values, ctx); + } + } + + #[inline] + fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_follows_from(span, follows, ctx); + } + } + + #[inline] + fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, S>) -> bool { + match self { + Some(ref inner) => inner.event_enabled(event, ctx), + None => true, + } + } + + #[inline] + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_event(event, ctx); + } + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_enter(id, ctx); + } + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_exit(id, ctx); + } + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_close(id, ctx); + } + } + + #[inline] + fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: Context<'_, S>) { + if let Some(ref inner) = self { + inner.on_id_change(old, new, ctx) + } + } + + #[doc(hidden)] + #[inline] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + if id == TypeId::of::<Self>() { + Some(self as *const _ as *const ()) + } else { + self.as_ref().and_then(|inner| inner.downcast_raw(id)) + } + } +} + +feature! { + #![any(feature = "std", feature = "alloc")] + #[cfg(not(feature = "std"))] + use alloc::vec::Vec; + + macro_rules! layer_impl_body { + () => { + #[inline] + fn on_layer(&mut self, subscriber: &mut S) { + self.deref_mut().on_layer(subscriber); + } + + #[inline] + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + self.deref().on_new_span(attrs, id, ctx) + } + + #[inline] + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + self.deref().register_callsite(metadata) + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool { + self.deref().enabled(metadata, ctx) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + self.deref().max_level_hint() + } + + #[inline] + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + self.deref().on_record(span, values, ctx) + } + + #[inline] + fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: Context<'_, S>) { + self.deref().on_follows_from(span, follows, ctx) + } + + #[inline] + fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, S>) -> bool { + self.deref().event_enabled(event, ctx) + } + + #[inline] + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { + self.deref().on_event(event, ctx) + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + self.deref().on_enter(id, ctx) + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + self.deref().on_exit(id, ctx) + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { + self.deref().on_close(id, ctx) + } + + #[inline] + fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: Context<'_, S>) { + self.deref().on_id_change(old, new, ctx) + } + + #[doc(hidden)] + #[inline] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + self.deref().downcast_raw(id) + } + }; + } + + impl<L, S> Layer<S> for Box<L> + where + L: Layer<S>, + S: Subscriber, + { + layer_impl_body! {} + } + + impl<S> Layer<S> for Box<dyn Layer<S> + Send + Sync> + where + S: Subscriber, + { + layer_impl_body! {} + } + + + + impl<S, L> Layer<S> for Vec<L> + where + L: Layer<S>, + S: Subscriber, + { + + fn on_layer(&mut self, subscriber: &mut S) { + for l in self { + l.on_layer(subscriber); + } + } + + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + // Return highest level of interest. + let mut interest = Interest::never(); + for l in self { + let new_interest = l.register_callsite(metadata); + if (interest.is_sometimes() && new_interest.is_always()) + || (interest.is_never() && !new_interest.is_never()) + { + interest = new_interest; + } + } + + interest + } + + fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool { + self.iter().all(|l| l.enabled(metadata, ctx.clone())) + } + + fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, S>) -> bool { + self.iter().all(|l| l.event_enabled(event, ctx.clone())) + } + + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + for l in self { + l.on_new_span(attrs, id, ctx.clone()); + } + } + + fn max_level_hint(&self) -> Option<LevelFilter> { + // Default to `OFF` if there are no inner layers. + let mut max_level = LevelFilter::OFF; + for l in self { + // NOTE(eliza): this is slightly subtle: if *any* layer + // returns `None`, we have to return `None`, assuming there is + // no max level hint, since that particular layer cannot + // provide a hint. + let hint = l.max_level_hint()?; + max_level = core::cmp::max(hint, max_level); + } + Some(max_level) + } + + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { + for l in self { + l.on_record(span, values, ctx.clone()) + } + } + + fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: Context<'_, S>) { + for l in self { + l.on_follows_from(span, follows, ctx.clone()); + } + } + + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { + for l in self { + l.on_event(event, ctx.clone()); + } + } + + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + for l in self { + l.on_enter(id, ctx.clone()); + } + } + + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + for l in self { + l.on_exit(id, ctx.clone()); + } + } + + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { + for l in self { + l.on_close(id.clone(), ctx.clone()); + } + } + + #[doc(hidden)] + unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { + // If downcasting to `Self`, return a pointer to `self`. + if id == TypeId::of::<Self>() { + return Some(self as *const _ as *const ()); + } + + // Someone is looking for per-layer filters. But, this `Vec` + // might contain layers with per-layer filters *and* + // layers without filters. It should only be treated as a + // per-layer-filtered layer if *all* its layers have + // per-layer filters. + // XXX(eliza): it's a bummer we have to do this linear search every + // time. It would be nice if this could be cached, but that would + // require replacing the `Vec` impl with an impl for a newtype... + if filter::is_plf_downcast_marker(id) && self.iter().any(|s| s.downcast_raw(id).is_none()) { + return None; + } + + // Otherwise, return the first child of `self` that downcaasts to + // the selected type, if any. + // XXX(eliza): hope this is reasonable lol + self.iter().find_map(|l| l.downcast_raw(id)) + } + } +} + +// === impl SubscriberExt === + +impl<S: Subscriber> crate::sealed::Sealed for S {} +impl<S: Subscriber> SubscriberExt for S {} + +// === impl Identity === + +impl<S: Subscriber> Layer<S> for Identity {} + +impl Identity { + /// Returns a new `Identity` layer. + pub fn new() -> Self { + Self { _p: () } + } +} diff --git a/vendor/tracing-subscriber/src/layer/tests.rs b/vendor/tracing-subscriber/src/layer/tests.rs new file mode 100644 index 000000000..d7ad61769 --- /dev/null +++ b/vendor/tracing-subscriber/src/layer/tests.rs @@ -0,0 +1,308 @@ +use super::*; +use tracing_core::subscriber::NoSubscriber; + +#[derive(Debug)] +pub(crate) struct NopLayer; +impl<S: Subscriber> Layer<S> for NopLayer {} + +#[allow(dead_code)] +struct NopLayer2; +impl<S: Subscriber> Layer<S> for NopLayer2 {} + +/// A layer that holds a string. +/// +/// Used to test that pointers returned by downcasting are actually valid. +struct StringLayer(&'static str); +impl<S: Subscriber> Layer<S> for StringLayer {} +struct StringLayer2(&'static str); +impl<S: Subscriber> Layer<S> for StringLayer2 {} + +struct StringLayer3(&'static str); +impl<S: Subscriber> Layer<S> for StringLayer3 {} + +pub(crate) struct StringSubscriber(&'static str); + +impl Subscriber for StringSubscriber { + fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest { + Interest::never() + } + + fn enabled(&self, _: &Metadata<'_>) -> bool { + false + } + + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(1) + } + + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + fn event(&self, _: &Event<'_>) {} + fn enter(&self, _: &span::Id) {} + fn exit(&self, _: &span::Id) {} +} + +fn assert_subscriber(_s: impl Subscriber) {} +fn assert_layer<S: Subscriber>(_l: &impl Layer<S>) {} + +#[test] +fn layer_is_subscriber() { + let s = NopLayer.with_subscriber(NoSubscriber::default()); + assert_subscriber(s) +} + +#[test] +fn two_layers_are_subscriber() { + let s = NopLayer + .and_then(NopLayer) + .with_subscriber(NoSubscriber::default()); + assert_subscriber(s) +} + +#[test] +fn three_layers_are_subscriber() { + let s = NopLayer + .and_then(NopLayer) + .and_then(NopLayer) + .with_subscriber(NoSubscriber::default()); + assert_subscriber(s) +} + +#[test] +fn three_layers_are_layer() { + let layers = NopLayer.and_then(NopLayer).and_then(NopLayer); + assert_layer(&layers); + let _ = layers.with_subscriber(NoSubscriber::default()); +} + +#[test] +#[cfg(feature = "alloc")] +fn box_layer_is_layer() { + use alloc::boxed::Box; + let l: Box<dyn Layer<NoSubscriber> + Send + Sync> = Box::new(NopLayer); + assert_layer(&l); + l.with_subscriber(NoSubscriber::default()); +} + +#[test] +fn downcasts_to_subscriber() { + let s = NopLayer + .and_then(NopLayer) + .and_then(NopLayer) + .with_subscriber(StringSubscriber("subscriber")); + let subscriber = + <dyn Subscriber>::downcast_ref::<StringSubscriber>(&s).expect("subscriber should downcast"); + assert_eq!(subscriber.0, "subscriber"); +} + +#[test] +fn downcasts_to_layer() { + let s = StringLayer("layer_1") + .and_then(StringLayer2("layer_2")) + .and_then(StringLayer3("layer_3")) + .with_subscriber(NoSubscriber::default()); + let layer = <dyn Subscriber>::downcast_ref::<StringLayer>(&s).expect("layer 1 should downcast"); + assert_eq!(layer.0, "layer_1"); + let layer = + <dyn Subscriber>::downcast_ref::<StringLayer2>(&s).expect("layer 2 should downcast"); + assert_eq!(layer.0, "layer_2"); + let layer = + <dyn Subscriber>::downcast_ref::<StringLayer3>(&s).expect("layer 3 should downcast"); + assert_eq!(layer.0, "layer_3"); +} + +#[cfg(all(feature = "registry", feature = "std"))] +mod registry_tests { + use super::*; + use crate::registry::LookupSpan; + + #[test] + fn context_event_span() { + use std::sync::{Arc, Mutex}; + let last_event_span = Arc::new(Mutex::new(None)); + + struct RecordingLayer { + last_event_span: Arc<Mutex<Option<&'static str>>>, + } + + impl<S> Layer<S> for RecordingLayer + where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + { + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { + let span = ctx.event_span(event); + *self.last_event_span.lock().unwrap() = span.map(|s| s.name()); + } + } + + tracing::subscriber::with_default( + crate::registry().with(RecordingLayer { + last_event_span: last_event_span.clone(), + }), + || { + tracing::info!("no span"); + assert_eq!(*last_event_span.lock().unwrap(), None); + + let parent = tracing::info_span!("explicit"); + tracing::info!(parent: &parent, "explicit span"); + assert_eq!(*last_event_span.lock().unwrap(), Some("explicit")); + + let _guard = tracing::info_span!("contextual").entered(); + tracing::info!("contextual span"); + assert_eq!(*last_event_span.lock().unwrap(), Some("contextual")); + }, + ); + } + + /// Tests for how max-level hints are calculated when combining layers + /// with and without per-layer filtering. + mod max_level_hints { + + use super::*; + use crate::filter::*; + + #[test] + fn mixed_with_unfiltered() { + let subscriber = crate::registry() + .with(NopLayer) + .with(NopLayer.with_filter(LevelFilter::INFO)); + assert_eq!(subscriber.max_level_hint(), None); + } + + #[test] + fn mixed_with_unfiltered_layered() { + let subscriber = crate::registry().with(NopLayer).with( + NopLayer + .with_filter(LevelFilter::INFO) + .and_then(NopLayer.with_filter(LevelFilter::TRACE)), + ); + assert_eq!(dbg!(subscriber).max_level_hint(), None); + } + + #[test] + fn mixed_interleaved() { + let subscriber = crate::registry() + .with(NopLayer) + .with(NopLayer.with_filter(LevelFilter::INFO)) + .with(NopLayer) + .with(NopLayer.with_filter(LevelFilter::INFO)); + assert_eq!(dbg!(subscriber).max_level_hint(), None); + } + + #[test] + fn mixed_layered() { + let subscriber = crate::registry() + .with(NopLayer.with_filter(LevelFilter::INFO).and_then(NopLayer)) + .with(NopLayer.and_then(NopLayer.with_filter(LevelFilter::INFO))); + assert_eq!(dbg!(subscriber).max_level_hint(), None); + } + + #[test] + fn plf_only_unhinted() { + let subscriber = crate::registry() + .with(NopLayer.with_filter(LevelFilter::INFO)) + .with(NopLayer.with_filter(filter_fn(|_| true))); + assert_eq!(dbg!(subscriber).max_level_hint(), None); + } + + #[test] + fn plf_only_unhinted_nested_outer() { + // if a nested tree of per-layer filters has an _outer_ filter with + // no max level hint, it should return `None`. + let subscriber = crate::registry() + .with( + NopLayer + .with_filter(LevelFilter::INFO) + .and_then(NopLayer.with_filter(LevelFilter::WARN)), + ) + .with( + NopLayer + .with_filter(filter_fn(|_| true)) + .and_then(NopLayer.with_filter(LevelFilter::DEBUG)), + ); + assert_eq!(dbg!(subscriber).max_level_hint(), None); + } + + #[test] + fn plf_only_unhinted_nested_inner() { + // If a nested tree of per-layer filters has an _inner_ filter with + // no max-level hint, but the _outer_ filter has a max level hint, + // it should pick the outer hint. This is because the outer filter + // will disable the spans/events before they make it to the inner + // filter. + let subscriber = dbg!(crate::registry().with( + NopLayer + .with_filter(filter_fn(|_| true)) + .and_then(NopLayer.with_filter(filter_fn(|_| true))) + .with_filter(LevelFilter::INFO), + )); + assert_eq!(dbg!(subscriber).max_level_hint(), Some(LevelFilter::INFO)); + } + + #[test] + fn unhinted_nested_inner() { + let subscriber = dbg!(crate::registry() + .with(NopLayer.and_then(NopLayer).with_filter(LevelFilter::INFO)) + .with( + NopLayer + .with_filter(filter_fn(|_| true)) + .and_then(NopLayer.with_filter(filter_fn(|_| true))) + .with_filter(LevelFilter::WARN), + )); + assert_eq!(dbg!(subscriber).max_level_hint(), Some(LevelFilter::INFO)); + } + + #[test] + fn unhinted_nested_inner_mixed() { + let subscriber = dbg!(crate::registry() + .with( + NopLayer + .and_then(NopLayer.with_filter(filter_fn(|_| true))) + .with_filter(LevelFilter::INFO) + ) + .with( + NopLayer + .with_filter(filter_fn(|_| true)) + .and_then(NopLayer.with_filter(filter_fn(|_| true))) + .with_filter(LevelFilter::WARN), + )); + assert_eq!(dbg!(subscriber).max_level_hint(), Some(LevelFilter::INFO)); + } + + #[test] + fn plf_only_picks_max() { + let subscriber = crate::registry() + .with(NopLayer.with_filter(LevelFilter::WARN)) + .with(NopLayer.with_filter(LevelFilter::DEBUG)); + assert_eq!(dbg!(subscriber).max_level_hint(), Some(LevelFilter::DEBUG)); + } + + #[test] + fn many_plf_only_picks_max() { + let subscriber = crate::registry() + .with(NopLayer.with_filter(LevelFilter::WARN)) + .with(NopLayer.with_filter(LevelFilter::DEBUG)) + .with(NopLayer.with_filter(LevelFilter::INFO)) + .with(NopLayer.with_filter(LevelFilter::ERROR)); + assert_eq!(dbg!(subscriber).max_level_hint(), Some(LevelFilter::DEBUG)); + } + + #[test] + fn nested_plf_only_picks_max() { + let subscriber = crate::registry() + .with( + NopLayer.with_filter(LevelFilter::INFO).and_then( + NopLayer + .with_filter(LevelFilter::WARN) + .and_then(NopLayer.with_filter(LevelFilter::DEBUG)), + ), + ) + .with( + NopLayer + .with_filter(LevelFilter::INFO) + .and_then(NopLayer.with_filter(LevelFilter::ERROR)), + ); + assert_eq!(dbg!(subscriber).max_level_hint(), Some(LevelFilter::DEBUG)); + } + } +} diff --git a/vendor/tracing-subscriber/src/lib.rs b/vendor/tracing-subscriber/src/lib.rs new file mode 100644 index 000000000..4bd61000e --- /dev/null +++ b/vendor/tracing-subscriber/src/lib.rs @@ -0,0 +1,252 @@ +//! Utilities for implementing and composing [`tracing`] subscribers. +//! +//! [`tracing`] is a framework for instrumenting Rust programs to collect +//! scoped, structured, and async-aware diagnostics. The [`Subscriber`] trait +//! represents the functionality necessary to collect this trace data. This +//! crate contains tools for composing subscribers out of smaller units of +//! behaviour, and batteries-included implementations of common subscriber +//! functionality. +//! +//! `tracing-subscriber` is intended for use by both `Subscriber` authors and +//! application authors using `tracing` to instrument their applications. +//! +//! *Compiler support: [requires `rustc` 1.49+][msrv]* +//! +//! [msrv]: #supported-rust-versions +//! +//! ## `Layer`s and `Filter`s +//! +//! The most important component of the `tracing-subscriber` API is the +//! [`Layer`] trait, which provides a composable abstraction for building +//! [`Subscriber`]s. Like the [`Subscriber`] trait, a [`Layer`] defines a +//! particular behavior for collecting trace data. Unlike [`Subscriber`]s, +//! which implement a *complete* strategy for how trace data is collected, +//! [`Layer`]s provide *modular* implementations of specific behaviors. +//! Therefore, they can be [composed together] to form a [`Subscriber`] which is +//! capable of recording traces in a variety of ways. See the [`layer` module's +//! documentation][layer] for details on using [`Layer`]s. +//! +//! In addition, the [`Filter`] trait defines an interface for filtering what +//! spans and events are recorded by a particular layer. This allows different +//! [`Layer`]s to handle separate subsets of the trace data emitted by a +//! program. See the [documentation on per-layer filtering][plf] for more +//! information on using [`Filter`]s. +//! +//! [`Layer`]: crate::layer::Layer +//! [composed together]: crate::layer#composing-layers +//! [layer]: crate::layer +//! [`Filter`]: crate::layer::Filter +//! [plf]: crate::layer#per-layer-filtering +//! +//! ## Included Subscribers +//! +//! The following `Subscriber`s are provided for application authors: +//! +//! - [`fmt`] - Formats and logs tracing data (requires the `fmt` feature flag) +//! +//! ## Feature Flags +//! +//! - `std`: Enables APIs that depend on the on the Rust standard library +//! (enabled by default). +//! - `alloc`: Depend on [`liballoc`] (enabled by "std"). +//! - `env-filter`: Enables the [`EnvFilter`] type, which implements filtering +//! similar to the [`env_logger` crate]. **Requires "std"**. +//! - `fmt`: Enables the [`fmt`] module, which provides a subscriber +//! implementation for printing formatted representations of trace events. +//! Enabled by default. **Requires "std"**. +//! - `ansi`: Enables `fmt` support for ANSI terminal colors. Enabled by +//! default. +//! - `registry`: enables the [`registry`] module. Enabled by default. +//! **Requires "std"**. +//! - `json`: Enables `fmt` support for JSON output. In JSON output, the ANSI +//! feature does nothing. **Requires "fmt" and "std"**. +//! - `local-time`: Enables local time formatting when using the [`time` +//! crate]'s timestamp formatters with the `fmt` subscriber. +//! +//! [`registry`]: mod@registry +//! +//! ### Optional Dependencies +//! +//! - [`tracing-log`]: Enables better formatting for events emitted by `log` +//! macros in the `fmt` subscriber. Enabled by default. +//! - [`time`][`time` crate]: Enables support for using the [`time` crate] for timestamp +//! formatting in the `fmt` subscriber. +//! - [`smallvec`]: Causes the `EnvFilter` type to use the `smallvec` crate (rather +//! than `Vec`) as a performance optimization. Enabled by default. +//! - [`parking_lot`]: Use the `parking_lot` crate's `RwLock` implementation +//! rather than the Rust standard library's implementation. +//! +//! ### `no_std` Support +//! +//! In embedded systems and other bare-metal applications, `tracing` can be +//! used without requiring the Rust standard library, although some features are +//! disabled. Although most of the APIs provided by `tracing-subscriber`, such +//! as [`fmt`] and [`EnvFilter`], require the standard library, some +//! functionality, such as the [`Layer`] trait, can still be used in +//! `no_std` environments. +//! +//! The dependency on the standard library is controlled by two crate feature +//! flags, "std", which enables the dependency on [`libstd`], and "alloc", which +//! enables the dependency on [`liballoc`] (and is enabled by the "std" +//! feature). These features are enabled by default, but `no_std` users can +//! disable them using: +//! +//! ```toml +//! # Cargo.toml +//! tracing-subscriber = { version = "0.3", default-features = false } +//! ``` +//! +//! Additional APIs are available when [`liballoc`] is available. To enable +//! `liballoc` but not `std`, use: +//! +//! ```toml +//! # Cargo.toml +//! tracing-subscriber = { version = "0.3", default-features = false, features = ["alloc"] } +//! ``` +//! +//! ### Unstable Features +//! +//! These feature flags enable **unstable** features. The public API may break in 0.1.x +//! releases. To enable these features, the `--cfg tracing_unstable` must be passed to +//! `rustc` when compiling. +//! +//! The following unstable feature flags are currently available: +//! +//! * `valuable`: Enables support for serializing values recorded using the +//! [`valuable`] crate as structured JSON in the [`format::Json`] formatter. +//! +//! #### Enabling Unstable Features +//! +//! The easiest way to set the `tracing_unstable` cfg is to use the `RUSTFLAGS` +//! env variable when running `cargo` commands: +//! +//! ```shell +//! RUSTFLAGS="--cfg tracing_unstable" cargo build +//! ``` +//! Alternatively, the following can be added to the `.cargo/config` file in a +//! project to automatically enable the cfg flag for that project: +//! +//! ```toml +//! [build] +//! rustflags = ["--cfg", "tracing_unstable"] +//! ``` +//! +//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section +//! [`valuable`]: https://crates.io/crates/valuable +//! [`format::Json`]: crate::fmt::format::Json +//! +//! ## Supported Rust Versions +//! +//! Tracing is built against the latest stable release. The minimum supported +//! version is 1.49. 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. +//! +//! [`Subscriber`]: tracing_core::subscriber::Subscriber +//! [`tracing`]: https://docs.rs/tracing/latest/tracing +//! [`EnvFilter`]: filter::EnvFilter +//! [`fmt`]: mod@fmt +//! [`tracing-log`]: https://crates.io/crates/tracing-log +//! [`smallvec`]: https://crates.io/crates/smallvec +//! [`env_logger` crate]: https://crates.io/crates/env_logger +//! [`parking_lot`]: https://crates.io/crates/parking_lot +//! [`time` crate]: https://crates.io/crates/time +//! [`libstd`]: std +//! [`liballoc`]: alloc +#![doc(html_root_url = "https://docs.rs/tracing-subscriber/0.3.14")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png", + issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" +)] +#![cfg_attr( + docsrs, + // Allows displaying cfgs/feature flags in the documentation. + feature(doc_cfg), + // Allows adding traits to RustDoc's list of "notable traits" + feature(doc_notable_trait), + // Fail the docs build if any intra-docs links are broken + deny(rustdoc::broken_intra_doc_links), +)] +#![warn( + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + unreachable_pub, + bad_style, + const_err, + dead_code, + improper_ctypes, + non_shorthand_field_patterns, + no_mangle_generic_items, + overflowing_literals, + path_statements, + patterns_in_fns_without_body, + private_in_public, + unconditional_recursion, + unused, + unused_allocation, + unused_comparisons, + unused_parens, + while_true +)] +// Using struct update syntax when a struct has no additional fields avoids +// a potential source change if additional fields are added to the struct in the +// future, reducing diff noise. Allow this even though clippy considers it +// "needless". +#![allow(clippy::needless_update)] +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(feature = "alloc")] +extern crate alloc; + +#[macro_use] +mod macros; + +pub mod field; +pub mod filter; +pub mod prelude; +pub mod registry; + +pub mod layer; +pub mod util; + +feature! { + #![feature = "std"] + pub mod reload; + pub(crate) mod sync; +} + +feature! { + #![all(feature = "fmt", feature = "std")] + pub mod fmt; + pub use fmt::fmt; + pub use fmt::Subscriber as FmtSubscriber; +} + +feature! { + #![all(feature = "env-filter", feature = "std")] + pub use filter::EnvFilter; +} + +pub use layer::Layer; + +feature! { + #![all(feature = "registry", feature = "std")] + pub use registry::Registry; + + /// + pub fn registry() -> Registry { + Registry::default() + } +} + +mod sealed { + pub trait Sealed<A = ()> {} +} diff --git a/vendor/tracing-subscriber/src/macros.rs b/vendor/tracing-subscriber/src/macros.rs new file mode 100644 index 000000000..81351132f --- /dev/null +++ b/vendor/tracing-subscriber/src/macros.rs @@ -0,0 +1,28 @@ +#[cfg(feature = "std")] +macro_rules! try_lock { + ($lock:expr) => { + try_lock!($lock, else return) + }; + ($lock:expr, else $els:expr) => { + if let Ok(l) = $lock { + l + } else if std::thread::panicking() { + $els + } else { + panic!("lock poisoned") + } + }; +} + +macro_rules! feature { + ( + #![$meta:meta] + $($item:item)* + ) => { + $( + #[cfg($meta)] + #[cfg_attr(docsrs, doc(cfg($meta)))] + $item + )* + } +} diff --git a/vendor/tracing-subscriber/src/prelude.rs b/vendor/tracing-subscriber/src/prelude.rs new file mode 100644 index 000000000..c2230907b --- /dev/null +++ b/vendor/tracing-subscriber/src/prelude.rs @@ -0,0 +1,19 @@ +//! The `tracing-subscriber` prelude. +//! +//! This brings into scope a number of extension traits that define methods on +//! types defined here and in other crates. + +pub use crate::field::{ + MakeExt as __tracing_subscriber_field_MakeExt, + RecordFields as __tracing_subscriber_field_RecordFields, +}; +pub use crate::layer::{ + Layer as __tracing_subscriber_Layer, SubscriberExt as __tracing_subscriber_SubscriberExt, +}; + +pub use crate::util::SubscriberInitExt as _; + +feature! { + #![all(feature = "fmt", feature = "std")] + pub use crate::fmt::writer::MakeWriterExt as _; +} diff --git a/vendor/tracing-subscriber/src/registry/extensions.rs b/vendor/tracing-subscriber/src/registry/extensions.rs new file mode 100644 index 000000000..ff76fb599 --- /dev/null +++ b/vendor/tracing-subscriber/src/registry/extensions.rs @@ -0,0 +1,274 @@ +// taken from https://github.com/hyperium/http/blob/master/src/extensions.rs. + +use crate::sync::{RwLockReadGuard, RwLockWriteGuard}; +use std::{ + any::{Any, TypeId}, + collections::HashMap, + fmt, + hash::{BuildHasherDefault, Hasher}, +}; + +#[allow(warnings)] +type AnyMap = HashMap<TypeId, Box<dyn Any + Send + Sync>, BuildHasherDefault<IdHasher>>; + +/// With TypeIds as keys, there's no need to hash them. They are already hashes +/// themselves, coming from the compiler. The IdHasher holds the u64 of +/// the TypeId, and then returns it, instead of doing any bit fiddling. +#[derive(Default, Debug)] +struct IdHasher(u64); + +impl Hasher for IdHasher { + fn write(&mut self, _: &[u8]) { + unreachable!("TypeId calls write_u64"); + } + + #[inline] + fn write_u64(&mut self, id: u64) { + self.0 = id; + } + + #[inline] + fn finish(&self) -> u64 { + self.0 + } +} + +/// An immutable, read-only reference to a Span's extensions. +#[derive(Debug)] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub struct Extensions<'a> { + inner: RwLockReadGuard<'a, ExtensionsInner>, +} + +impl<'a> Extensions<'a> { + #[cfg(feature = "registry")] + pub(crate) fn new(inner: RwLockReadGuard<'a, ExtensionsInner>) -> Self { + Self { inner } + } + + /// Immutably borrows a type previously inserted into this `Extensions`. + pub fn get<T: 'static>(&self) -> Option<&T> { + self.inner.get::<T>() + } +} + +/// An mutable reference to a Span's extensions. +#[derive(Debug)] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub struct ExtensionsMut<'a> { + inner: RwLockWriteGuard<'a, ExtensionsInner>, +} + +impl<'a> ExtensionsMut<'a> { + #[cfg(feature = "registry")] + pub(crate) fn new(inner: RwLockWriteGuard<'a, ExtensionsInner>) -> Self { + Self { inner } + } + + /// Insert a type into this `Extensions`. + /// + /// Note that extensions are _not_ + /// `Layer`-specific—they are _span_-specific. This means that + /// other layers can access and mutate extensions that + /// a different Layer recorded. For example, an application might + /// have a layer that records execution timings, alongside a layer + /// that reports spans and events to a distributed + /// tracing system that requires timestamps for spans. + /// Ideally, if one layer records a timestamp _x_, the other layer + /// should be able to reuse timestamp _x_. + /// + /// Therefore, extensions should generally be newtypes, rather than common + /// types like [`String`](std::string::String), to avoid accidental + /// cross-`Layer` clobbering. + /// + /// ## Panics + /// + /// If `T` is already present in `Extensions`, then this method will panic. + pub fn insert<T: Send + Sync + 'static>(&mut self, val: T) { + assert!(self.replace(val).is_none()) + } + + /// Replaces an existing `T` into this extensions. + /// + /// If `T` is not present, `Option::None` will be returned. + pub fn replace<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T> { + self.inner.insert(val) + } + + /// Get a mutable reference to a type previously inserted on this `ExtensionsMut`. + pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> { + self.inner.get_mut::<T>() + } + + /// Remove a type from this `Extensions`. + /// + /// If a extension of this type existed, it will be returned. + pub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T> { + self.inner.remove::<T>() + } +} + +/// A type map of span extensions. +/// +/// [ExtensionsInner] is used by `SpanData` to store and +/// span-specific data. A given `Layer` can read and write +/// data that it is interested in recording and emitting. +#[derive(Default)] +pub(crate) struct ExtensionsInner { + map: AnyMap, +} + +impl ExtensionsInner { + /// Create an empty `Extensions`. + #[cfg(any(test, feature = "registry"))] + #[inline] + #[cfg(any(test, feature = "registry"))] + pub(crate) fn new() -> ExtensionsInner { + ExtensionsInner { + map: AnyMap::default(), + } + } + + /// Insert a type into this `Extensions`. + /// + /// If a extension of this type already existed, it will + /// be returned. + pub(crate) fn insert<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T> { + self.map + .insert(TypeId::of::<T>(), Box::new(val)) + .and_then(|boxed| { + #[allow(warnings)] + { + (boxed as Box<Any + 'static>) + .downcast() + .ok() + .map(|boxed| *boxed) + } + }) + } + + /// Get a reference to a type previously inserted on this `Extensions`. + pub(crate) fn get<T: 'static>(&self) -> Option<&T> { + self.map + .get(&TypeId::of::<T>()) + .and_then(|boxed| (&**boxed as &(dyn Any + 'static)).downcast_ref()) + } + + /// Get a mutable reference to a type previously inserted on this `Extensions`. + pub(crate) fn get_mut<T: 'static>(&mut self) -> Option<&mut T> { + self.map + .get_mut(&TypeId::of::<T>()) + .and_then(|boxed| (&mut **boxed as &mut (dyn Any + 'static)).downcast_mut()) + } + + /// Remove a type from this `Extensions`. + /// + /// If a extension of this type existed, it will be returned. + pub(crate) fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T> { + self.map.remove(&TypeId::of::<T>()).and_then(|boxed| { + #[allow(warnings)] + { + (boxed as Box<Any + 'static>) + .downcast() + .ok() + .map(|boxed| *boxed) + } + }) + } + + /// Clear the `ExtensionsInner` in-place, dropping any elements in the map but + /// retaining allocated capacity. + /// + /// This permits the hash map allocation to be pooled by the registry so + /// that future spans will not need to allocate new hashmaps. + #[cfg(any(test, feature = "registry"))] + pub(crate) fn clear(&mut self) { + self.map.clear(); + } +} + +impl fmt::Debug for ExtensionsInner { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Extensions") + .field("len", &self.map.len()) + .field("capacity", &self.map.capacity()) + .finish() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Debug, PartialEq)] + struct MyType(i32); + + #[test] + fn test_extensions() { + let mut extensions = ExtensionsInner::new(); + + extensions.insert(5i32); + extensions.insert(MyType(10)); + + assert_eq!(extensions.get(), Some(&5i32)); + assert_eq!(extensions.get_mut(), Some(&mut 5i32)); + + assert_eq!(extensions.remove::<i32>(), Some(5i32)); + assert!(extensions.get::<i32>().is_none()); + + assert_eq!(extensions.get::<bool>(), None); + assert_eq!(extensions.get(), Some(&MyType(10))); + } + + #[test] + fn clear_retains_capacity() { + let mut extensions = ExtensionsInner::new(); + extensions.insert(5i32); + extensions.insert(MyType(10)); + extensions.insert(true); + + assert_eq!(extensions.map.len(), 3); + let prev_capacity = extensions.map.capacity(); + extensions.clear(); + + assert_eq!( + extensions.map.len(), + 0, + "after clear(), extensions map should have length 0" + ); + assert_eq!( + extensions.map.capacity(), + prev_capacity, + "after clear(), extensions map should retain prior capacity" + ); + } + + #[test] + fn clear_drops_elements() { + use std::sync::Arc; + struct DropMePlease(Arc<()>); + struct DropMeTooPlease(Arc<()>); + + let mut extensions = ExtensionsInner::new(); + let val1 = DropMePlease(Arc::new(())); + let val2 = DropMeTooPlease(Arc::new(())); + + let val1_dropped = Arc::downgrade(&val1.0); + let val2_dropped = Arc::downgrade(&val2.0); + extensions.insert(val1); + extensions.insert(val2); + + assert!(val1_dropped.upgrade().is_some()); + assert!(val2_dropped.upgrade().is_some()); + + extensions.clear(); + assert!( + val1_dropped.upgrade().is_none(), + "after clear(), val1 should be dropped" + ); + assert!( + val2_dropped.upgrade().is_none(), + "after clear(), val2 should be dropped" + ); + } +} diff --git a/vendor/tracing-subscriber/src/registry/mod.rs b/vendor/tracing-subscriber/src/registry/mod.rs new file mode 100644 index 000000000..38af53e8a --- /dev/null +++ b/vendor/tracing-subscriber/src/registry/mod.rs @@ -0,0 +1,600 @@ +//! Storage for span data shared by multiple [`Layer`]s. +//! +//! ## Using the Span Registry +//! +//! This module provides the [`Registry`] type, a [`Subscriber`] implementation +//! which tracks per-span data and exposes it to [`Layer`]s. When a `Registry` +//! is used as the base `Subscriber` of a `Layer` stack, the +//! [`layer::Context`][ctx] type will provide methods allowing `Layer`s to +//! [look up span data][lookup] stored in the registry. While [`Registry`] is a +//! reasonable default for storing spans and events, other stores that implement +//! [`LookupSpan`] and [`Subscriber`] themselves (with [`SpanData`] implemented +//! by the per-span data they store) can be used as a drop-in replacement. +//! +//! For example, we might create a `Registry` and add multiple `Layer`s like so: +//! ```rust +//! use tracing_subscriber::{registry::Registry, Layer, prelude::*}; +//! # use tracing_core::Subscriber; +//! # pub struct FooLayer {} +//! # pub struct BarLayer {} +//! # impl<S: Subscriber> Layer<S> for FooLayer {} +//! # impl<S: Subscriber> Layer<S> for BarLayer {} +//! # impl FooLayer { +//! # fn new() -> Self { Self {} } +//! # } +//! # impl BarLayer { +//! # fn new() -> Self { Self {} } +//! # } +//! +//! let subscriber = Registry::default() +//! .with(FooLayer::new()) +//! .with(BarLayer::new()); +//! ``` +//! +//! If a type implementing `Layer` depends on the functionality of a `Registry` +//! implementation, it should bound its `Subscriber` type parameter with the +//! [`LookupSpan`] trait, like so: +//! +//! ```rust +//! use tracing_subscriber::{registry, Layer}; +//! use tracing_core::Subscriber; +//! +//! pub struct MyLayer { +//! // ... +//! } +//! +//! impl<S> Layer<S> for MyLayer +//! where +//! S: Subscriber + for<'a> registry::LookupSpan<'a>, +//! { +//! // ... +//! } +//! ``` +//! When this bound is added, the `Layer` implementation will be guaranteed +//! access to the [`Context`][ctx] methods, such as [`Context::span`][lookup], that +//! require the root subscriber to be a registry. +//! +//! [`Layer`]: crate::layer::Layer +//! [`Subscriber`]: tracing_core::Subscriber +//! [ctx]: crate::layer::Context +//! [lookup]: crate::layer::Context::span() +use tracing_core::{field::FieldSet, span::Id, Metadata}; + +feature! { + #![feature = "std"] + /// A module containing a type map of span extensions. + mod extensions; + pub use extensions::{Extensions, ExtensionsMut}; + +} + +feature! { + #![all(feature = "registry", feature = "std")] + + mod sharded; + mod stack; + + pub use sharded::Data; + pub use sharded::Registry; + + use crate::filter::FilterId; +} + +/// Provides access to stored span data. +/// +/// Subscribers which store span data and associate it with span IDs should +/// implement this trait; if they do, any [`Layer`]s wrapping them can look up +/// metadata via the [`Context`] type's [`span()`] method. +/// +/// [`Layer`]: super::layer::Layer +/// [`Context`]: super::layer::Context +/// [`span()`]: super::layer::Context::span +pub trait LookupSpan<'a> { + /// The type of span data stored in this registry. + type Data: SpanData<'a>; + + /// Returns the [`SpanData`] for a given `Id`, if it exists. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: users of the <code>LookupSpan</code> trait should + /// typically call the <a href="#method.span"><code>span</code></a> method rather + /// than this method. The <code>span</code> method is implemented by + /// <em>calling</em> <code>span_data</code>, but returns a reference which is + /// capable of performing more sophisiticated queries. + /// </pre> + /// + fn span_data(&'a self, id: &Id) -> Option<Self::Data>; + + /// Returns a [`SpanRef`] for the span with the given `Id`, if it exists. + /// + /// A `SpanRef` is similar to [`SpanData`], but it allows performing + /// additional lookups against the registryr that stores the wrapped data. + /// + /// In general, _users_ of the `LookupSpan` trait should use this method + /// rather than the [`span_data`] method; while _implementors_ of this trait + /// should only implement `span_data`. + /// + /// [`span_data`]: LookupSpan::span_data() + fn span(&'a self, id: &Id) -> Option<SpanRef<'_, Self>> + where + Self: Sized, + { + let data = self.span_data(id)?; + Some(SpanRef { + registry: self, + data, + #[cfg(feature = "registry")] + filter: FilterId::none(), + }) + } + + /// Registers a [`Filter`] for [per-layer filtering] with this + /// [`Subscriber`]. + /// + /// The [`Filter`] can then use the returned [`FilterId`] to + /// [check if it previously enabled a span][check]. + /// + /// # Panics + /// + /// If this `Subscriber` does not support [per-layer filtering]. + /// + /// [`Filter`]: crate::layer::Filter + /// [per-layer filtering]: crate::layer::Layer#per-layer-filtering + /// [`Subscriber`]: tracing_core::Subscriber + /// [`FilterId`]: crate::filter::FilterId + /// [check]: SpanData::is_enabled_for + #[cfg(feature = "registry")] + #[cfg_attr(docsrs, doc(cfg(feature = "registry")))] + fn register_filter(&mut self) -> FilterId { + panic!( + "{} does not currently support filters", + std::any::type_name::<Self>() + ) + } +} + +/// A stored representation of data associated with a span. +pub trait SpanData<'a> { + /// Returns this span's ID. + fn id(&self) -> Id; + + /// Returns a reference to the span's `Metadata`. + fn metadata(&self) -> &'static Metadata<'static>; + + /// Returns a reference to the ID + fn parent(&self) -> Option<&Id>; + + /// Returns a reference to this span's `Extensions`. + /// + /// The extensions may be used by `Layer`s to store additional data + /// describing the span. + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + fn extensions(&self) -> Extensions<'_>; + + /// Returns a mutable reference to this span's `Extensions`. + /// + /// The extensions may be used by `Layer`s to store additional data + /// describing the span. + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + fn extensions_mut(&self) -> ExtensionsMut<'_>; + + /// Returns `true` if this span is enabled for the [per-layer filter][plf] + /// corresponding to the provided [`FilterId`]. + /// + /// ## Default Implementation + /// + /// By default, this method assumes that the [`LookupSpan`] implementation + /// does not support [per-layer filtering][plf], and always returns `true`. + /// + /// [plf]: crate::layer::Layer#per-layer-filtering + /// [`FilterId`]: crate::filter::FilterId + #[cfg(feature = "registry")] + #[cfg_attr(docsrs, doc(cfg(feature = "registry")))] + fn is_enabled_for(&self, filter: FilterId) -> bool { + let _ = filter; + true + } +} + +/// A reference to [span data] and the associated [registry]. +/// +/// This type implements all the same methods as [`SpanData`][span data], and +/// provides additional methods for querying the registry based on values from +/// the span. +/// +/// [span data]: SpanData +/// [registry]: LookupSpan +#[derive(Debug)] +pub struct SpanRef<'a, R: LookupSpan<'a>> { + registry: &'a R, + data: R::Data, + + #[cfg(feature = "registry")] + filter: FilterId, +} + +/// An iterator over the parents of a span, ordered from leaf to root. +/// +/// This is returned by the [`SpanRef::scope`] method. +#[derive(Debug)] +pub struct Scope<'a, R> { + registry: &'a R, + next: Option<Id>, + + #[cfg(all(feature = "registry", feature = "std"))] + filter: FilterId, +} + +feature! { + #![any(feature = "alloc", feature = "std")] + + #[cfg(not(feature = "smallvec"))] + use alloc::vec::{self, Vec}; + + use core::{fmt,iter}; + + /// An iterator over the parents of a span, ordered from root to leaf. + /// + /// This is returned by the [`Scope::from_root`] method. + pub struct ScopeFromRoot<'a, R> + where + R: LookupSpan<'a>, + { + #[cfg(feature = "smallvec")] + spans: iter::Rev<smallvec::IntoIter<SpanRefVecArray<'a, R>>>, + #[cfg(not(feature = "smallvec"))] + spans: iter::Rev<vec::IntoIter<SpanRef<'a, R>>>, + } + + #[cfg(feature = "smallvec")] + type SpanRefVecArray<'span, L> = [SpanRef<'span, L>; 16]; + + impl<'a, R> Scope<'a, R> + where + R: LookupSpan<'a>, + { + /// Flips the order of the iterator, so that it is ordered from root to leaf. + /// + /// The iterator will first return the root span, then that span's immediate child, + /// and so on until it finally returns the span that [`SpanRef::scope`] was called on. + /// + /// If any items were consumed from the [`Scope`] before calling this method then they + /// will *not* be returned from the [`ScopeFromRoot`]. + /// + /// **Note**: this will allocate if there are many spans remaining, or if the + /// "smallvec" feature flag is not enabled. + #[allow(clippy::wrong_self_convention)] + pub fn from_root(self) -> ScopeFromRoot<'a, R> { + #[cfg(feature = "smallvec")] + type Buf<T> = smallvec::SmallVec<T>; + #[cfg(not(feature = "smallvec"))] + type Buf<T> = Vec<T>; + ScopeFromRoot { + spans: self.collect::<Buf<_>>().into_iter().rev(), + } + } + } + + impl<'a, R> Iterator for ScopeFromRoot<'a, R> + where + R: LookupSpan<'a>, + { + type Item = SpanRef<'a, R>; + + #[inline] + fn next(&mut self) -> Option<Self::Item> { + self.spans.next() + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + self.spans.size_hint() + } + } + + impl<'a, R> fmt::Debug for ScopeFromRoot<'a, R> + where + R: LookupSpan<'a>, + { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad("ScopeFromRoot { .. }") + } + } +} + +impl<'a, R> Iterator for Scope<'a, R> +where + R: LookupSpan<'a>, +{ + type Item = SpanRef<'a, R>; + + fn next(&mut self) -> Option<Self::Item> { + loop { + let curr = self.registry.span(self.next.as_ref()?)?; + + #[cfg(all(feature = "registry", feature = "std"))] + let curr = curr.with_filter(self.filter); + self.next = curr.data.parent().cloned(); + + // If the `Scope` is filtered, check if the current span is enabled + // by the selected filter ID. + + #[cfg(all(feature = "registry", feature = "std"))] + { + if !curr.is_enabled_for(self.filter) { + // The current span in the chain is disabled for this + // filter. Try its parent. + continue; + } + } + + return Some(curr); + } + } +} + +impl<'a, R> SpanRef<'a, R> +where + R: LookupSpan<'a>, +{ + /// Returns this span's ID. + pub fn id(&self) -> Id { + self.data.id() + } + + /// Returns a static reference to the span's metadata. + pub fn metadata(&self) -> &'static Metadata<'static> { + self.data.metadata() + } + + /// Returns the span's name, + pub fn name(&self) -> &'static str { + self.data.metadata().name() + } + + /// Returns a list of [fields] defined by the span. + /// + /// [fields]: tracing_core::field + pub fn fields(&self) -> &FieldSet { + self.data.metadata().fields() + } + + /// Returns a `SpanRef` describing this span's parent, or `None` if this + /// span is the root of its trace tree. + pub fn parent(&self) -> Option<Self> { + let id = self.data.parent()?; + let data = self.registry.span_data(id)?; + + #[cfg(all(feature = "registry", feature = "std"))] + { + // move these into mut bindings if the registry feature is enabled, + // since they may be mutated in the loop. + let mut data = data; + loop { + // Is this parent enabled by our filter? + if data.is_enabled_for(self.filter) { + return Some(Self { + registry: self.registry, + filter: self.filter, + data, + }); + } + + // It's not enabled. If the disabled span has a parent, try that! + let id = data.parent()?; + data = self.registry.span_data(id)?; + } + } + + #[cfg(not(all(feature = "registry", feature = "std")))] + Some(Self { + registry: self.registry, + data, + }) + } + + /// Returns an iterator over all parents of this span, starting with this span, + /// ordered from leaf to root. + /// + /// The iterator will first return the span, then the span's immediate parent, + /// followed by that span's parent, and so on, until it reaches a root span. + /// + /// ```rust + /// use tracing::{span, Subscriber}; + /// use tracing_subscriber::{ + /// layer::{Context, Layer}, + /// prelude::*, + /// registry::LookupSpan, + /// }; + /// + /// struct PrintingLayer; + /// impl<S> Layer<S> for PrintingLayer + /// where + /// S: Subscriber + for<'lookup> LookupSpan<'lookup>, + /// { + /// fn on_enter(&self, id: &span::Id, ctx: Context<S>) { + /// let span = ctx.span(id).unwrap(); + /// let scope = span.scope().map(|span| span.name()).collect::<Vec<_>>(); + /// println!("Entering span: {:?}", scope); + /// } + /// } + /// + /// tracing::subscriber::with_default(tracing_subscriber::registry().with(PrintingLayer), || { + /// let _root = tracing::info_span!("root").entered(); + /// // Prints: Entering span: ["root"] + /// let _child = tracing::info_span!("child").entered(); + /// // Prints: Entering span: ["child", "root"] + /// let _leaf = tracing::info_span!("leaf").entered(); + /// // Prints: Entering span: ["leaf", "child", "root"] + /// }); + /// ``` + /// + /// If the opposite order (from the root to this span) is desired, calling [`Scope::from_root`] on + /// the returned iterator reverses the order. + /// + /// ```rust + /// # use tracing::{span, Subscriber}; + /// # use tracing_subscriber::{ + /// # layer::{Context, Layer}, + /// # prelude::*, + /// # registry::LookupSpan, + /// # }; + /// # struct PrintingLayer; + /// impl<S> Layer<S> for PrintingLayer + /// where + /// S: Subscriber + for<'lookup> LookupSpan<'lookup>, + /// { + /// fn on_enter(&self, id: &span::Id, ctx: Context<S>) { + /// let span = ctx.span(id).unwrap(); + /// let scope = span.scope().from_root().map(|span| span.name()).collect::<Vec<_>>(); + /// println!("Entering span: {:?}", scope); + /// } + /// } + /// + /// tracing::subscriber::with_default(tracing_subscriber::registry().with(PrintingLayer), || { + /// let _root = tracing::info_span!("root").entered(); + /// // Prints: Entering span: ["root"] + /// let _child = tracing::info_span!("child").entered(); + /// // Prints: Entering span: ["root", "child"] + /// let _leaf = tracing::info_span!("leaf").entered(); + /// // Prints: Entering span: ["root", "child", "leaf"] + /// }); + /// ``` + pub fn scope(&self) -> Scope<'a, R> { + Scope { + registry: self.registry, + next: Some(self.id()), + + #[cfg(feature = "registry")] + filter: self.filter, + } + } + + /// Returns a reference to this span's `Extensions`. + /// + /// The extensions may be used by `Layer`s to store additional data + /// describing the span. + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + pub fn extensions(&self) -> Extensions<'_> { + self.data.extensions() + } + + /// Returns a mutable reference to this span's `Extensions`. + /// + /// The extensions may be used by `Layer`s to store additional data + /// describing the span. + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + pub fn extensions_mut(&self) -> ExtensionsMut<'_> { + self.data.extensions_mut() + } + + #[cfg(all(feature = "registry", feature = "std"))] + pub(crate) fn try_with_filter(self, filter: FilterId) -> Option<Self> { + if self.is_enabled_for(filter) { + return Some(self.with_filter(filter)); + } + + None + } + + #[inline] + #[cfg(all(feature = "registry", feature = "std"))] + pub(crate) fn is_enabled_for(&self, filter: FilterId) -> bool { + self.data.is_enabled_for(filter) + } + + #[inline] + #[cfg(all(feature = "registry", feature = "std"))] + fn with_filter(self, filter: FilterId) -> Self { + Self { filter, ..self } + } +} + +#[cfg(all(test, feature = "registry", feature = "std"))] +mod tests { + use crate::{ + layer::{Context, Layer}, + prelude::*, + registry::LookupSpan, + }; + use std::sync::{Arc, Mutex}; + use tracing::{span, Subscriber}; + + #[test] + fn spanref_scope_iteration_order() { + let last_entered_scope = Arc::new(Mutex::new(Vec::new())); + + #[derive(Default)] + struct PrintingLayer { + last_entered_scope: Arc<Mutex<Vec<&'static str>>>, + } + + impl<S> Layer<S> for PrintingLayer + where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + { + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + let span = ctx.span(id).unwrap(); + let scope = span.scope().map(|span| span.name()).collect::<Vec<_>>(); + *self.last_entered_scope.lock().unwrap() = scope; + } + } + + let _guard = tracing::subscriber::set_default(crate::registry().with(PrintingLayer { + last_entered_scope: last_entered_scope.clone(), + })); + + let _root = tracing::info_span!("root").entered(); + assert_eq!(&*last_entered_scope.lock().unwrap(), &["root"]); + let _child = tracing::info_span!("child").entered(); + assert_eq!(&*last_entered_scope.lock().unwrap(), &["child", "root"]); + let _leaf = tracing::info_span!("leaf").entered(); + assert_eq!( + &*last_entered_scope.lock().unwrap(), + &["leaf", "child", "root"] + ); + } + + #[test] + fn spanref_scope_fromroot_iteration_order() { + let last_entered_scope = Arc::new(Mutex::new(Vec::new())); + + #[derive(Default)] + struct PrintingLayer { + last_entered_scope: Arc<Mutex<Vec<&'static str>>>, + } + + impl<S> Layer<S> for PrintingLayer + where + S: Subscriber + for<'lookup> LookupSpan<'lookup>, + { + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + let span = ctx.span(id).unwrap(); + let scope = span + .scope() + .from_root() + .map(|span| span.name()) + .collect::<Vec<_>>(); + *self.last_entered_scope.lock().unwrap() = scope; + } + } + + let _guard = tracing::subscriber::set_default(crate::registry().with(PrintingLayer { + last_entered_scope: last_entered_scope.clone(), + })); + + let _root = tracing::info_span!("root").entered(); + assert_eq!(&*last_entered_scope.lock().unwrap(), &["root"]); + let _child = tracing::info_span!("child").entered(); + assert_eq!(&*last_entered_scope.lock().unwrap(), &["root", "child",]); + let _leaf = tracing::info_span!("leaf").entered(); + assert_eq!( + &*last_entered_scope.lock().unwrap(), + &["root", "child", "leaf"] + ); + } +} diff --git a/vendor/tracing-subscriber/src/registry/sharded.rs b/vendor/tracing-subscriber/src/registry/sharded.rs new file mode 100644 index 000000000..b81d5fef8 --- /dev/null +++ b/vendor/tracing-subscriber/src/registry/sharded.rs @@ -0,0 +1,901 @@ +use sharded_slab::{pool::Ref, Clear, Pool}; +use thread_local::ThreadLocal; + +use super::stack::SpanStack; +use crate::{ + filter::{FilterId, FilterMap, FilterState}, + registry::{ + extensions::{Extensions, ExtensionsInner, ExtensionsMut}, + LookupSpan, SpanData, + }, + sync::RwLock, +}; +use std::{ + cell::{self, Cell, RefCell}, + sync::atomic::{fence, AtomicUsize, Ordering}, +}; +use tracing_core::{ + dispatcher::{self, Dispatch}, + span::{self, Current, Id}, + Event, Interest, Metadata, Subscriber, +}; + +/// A shared, reusable store for spans. +/// +/// A `Registry` is a [`Subscriber`] around which multiple [`Layer`]s +/// implementing various behaviors may be [added]. Unlike other types +/// implementing `Subscriber`, `Registry` does not actually record traces itself: +/// instead, it collects and stores span data that is exposed to any [`Layer`]s +/// wrapping it through implementations of the [`LookupSpan`] trait. +/// The `Registry` is responsible for storing span metadata, recording +/// relationships between spans, and tracking which spans are active and which +/// are closed. In addition, it provides a mechanism for [`Layer`]s to store +/// user-defined per-span data, called [extensions], in the registry. This +/// allows [`Layer`]-specific data to benefit from the `Registry`'s +/// high-performance concurrent storage. +/// +/// This registry is implemented using a [lock-free sharded slab][slab], and is +/// highly optimized for concurrent access. +/// +/// # Span ID Generation +/// +/// Span IDs are not globally unique, but the registry ensures that +/// no two currently active spans have the same ID within a process. +/// +/// One of the primary responsibilities of the registry is to generate [span +/// IDs]. Therefore, it's important for other code that interacts with the +/// registry, such as [`Layer`]s, to understand the guarantees of the +/// span IDs that are generated. +/// +/// The registry's span IDs are guaranteed to be unique **at a given point +/// in time**. This means that an active span will never be assigned the +/// same ID as another **currently active** span. However, the registry +/// **will** eventually reuse the IDs of [closed] spans, although an ID +/// will never be reassigned immediately after a span has closed. +/// +/// Spans are not [considered closed] by the `Registry` until *every* +/// [`Span`] reference with that ID has been dropped. +/// +/// Thus: span IDs generated by the registry should be considered unique +/// only at a given point in time, and only relative to other spans +/// generated by the same process. Two spans with the same ID will not exist +/// in the same process concurrently. However, if historical span data is +/// being stored, the same ID may occur for multiple spans times in that +/// data. If spans must be uniquely identified in historical data, the user +/// code storing this data must assign its own unique identifiers to those +/// spans. A counter is generally sufficient for this. +/// +/// Similarly, span IDs generated by the registry are not unique outside of +/// a given process. Distributed tracing systems may require identifiers +/// that are unique across multiple processes on multiple machines (for +/// example, [OpenTelemetry's `SpanId`s and `TraceId`s][ot]). `tracing` span +/// IDs generated by the registry should **not** be used for this purpose. +/// Instead, code which integrates with a distributed tracing system should +/// generate and propagate its own IDs according to the rules specified by +/// the distributed tracing system. These IDs can be associated with +/// `tracing` spans using [fields] and/or [stored span data]. +/// +/// [span IDs]: tracing_core::span::Id +/// [slab]: sharded_slab +/// [`Layer`]: crate::Layer +/// [added]: crate::layer::Layer#composing-layers +/// [extensions]: super::Extensions +/// [closed]: https://docs.rs/tracing/latest/tracing/span/index.html#closing-spans +/// [considered closed]: tracing_core::subscriber::Subscriber::try_close() +/// [`Span`]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html +/// [ot]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#spancontext +/// [fields]: tracing_core::field +/// [stored span data]: crate::registry::SpanData::extensions_mut +#[cfg(feature = "registry")] +#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))] +#[derive(Debug)] +pub struct Registry { + spans: Pool<DataInner>, + current_spans: ThreadLocal<RefCell<SpanStack>>, + next_filter_id: u8, +} + +/// Span data stored in a [`Registry`]. +/// +/// The registry stores well-known data defined by tracing: span relationships, +/// metadata and reference counts. Additional user-defined data provided by +/// [`Layer`s], such as formatted fields, metrics, or distributed traces should +/// be stored in the [extensions] typemap. +/// +/// [`Layer`s]: crate::layer::Layer +/// [extensions]: Extensions +#[cfg(feature = "registry")] +#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))] +#[derive(Debug)] +pub struct Data<'a> { + /// Immutable reference to the pooled `DataInner` entry. + inner: Ref<'a, DataInner>, +} + +/// Stored data associated with a span. +/// +/// This type is pooled using [`sharded_slab::Pool`]; when a span is +/// dropped, the `DataInner` entry at that span's slab index is cleared +/// in place and reused by a future span. Thus, the `Default` and +/// [`sharded_slab::Clear`] implementations for this type are +/// load-bearing. +#[derive(Debug)] +struct DataInner { + filter_map: FilterMap, + metadata: &'static Metadata<'static>, + parent: Option<Id>, + ref_count: AtomicUsize, + // The span's `Extensions` typemap. Allocations for the `HashMap` backing + // this are pooled and reused in place. + pub(crate) extensions: RwLock<ExtensionsInner>, +} + +// === impl Registry === + +impl Default for Registry { + fn default() -> Self { + Self { + spans: Pool::new(), + current_spans: ThreadLocal::new(), + next_filter_id: 0, + } + } +} + +#[inline] +fn idx_to_id(idx: usize) -> Id { + Id::from_u64(idx as u64 + 1) +} + +#[inline] +fn id_to_idx(id: &Id) -> usize { + id.into_u64() as usize - 1 +} + +/// A guard that tracks how many [`Registry`]-backed `Layer`s have +/// processed an `on_close` event. +/// +/// This is needed to enable a [`Registry`]-backed Layer to access span +/// data after the `Layer` has recieved the `on_close` callback. +/// +/// Once all `Layer`s have processed this event, the [`Registry`] knows +/// that is able to safely remove the span tracked by `id`. `CloseGuard` +/// accomplishes this through a two-step process: +/// 1. Whenever a [`Registry`]-backed `Layer::on_close` method is +/// called, `Registry::start_close` is closed. +/// `Registry::start_close` increments a thread-local `CLOSE_COUNT` +/// by 1 and returns a `CloseGuard`. +/// 2. The `CloseGuard` is dropped at the end of `Layer::on_close`. On +/// drop, `CloseGuard` checks thread-local `CLOSE_COUNT`. If +/// `CLOSE_COUNT` is 0, the `CloseGuard` removes the span with the +/// `id` from the registry, as all `Layers` that might have seen the +/// `on_close` notification have processed it. If `CLOSE_COUNT` is +/// greater than 0, `CloseGuard` decrements the counter by one and +/// _does not_ remove the span from the [`Registry`]. +/// +pub(crate) struct CloseGuard<'a> { + id: Id, + registry: &'a Registry, + is_closing: bool, +} + +impl Registry { + fn get(&self, id: &Id) -> Option<Ref<'_, DataInner>> { + self.spans.get(id_to_idx(id)) + } + + /// Returns a guard which tracks how many `Layer`s have + /// processed an `on_close` notification via the `CLOSE_COUNT` thread-local. + /// For additional details, see [`CloseGuard`]. + /// + pub(crate) fn start_close(&self, id: Id) -> CloseGuard<'_> { + CLOSE_COUNT.with(|count| { + let c = count.get(); + count.set(c + 1); + }); + CloseGuard { + id, + registry: self, + is_closing: false, + } + } + + pub(crate) fn has_per_layer_filters(&self) -> bool { + self.next_filter_id > 0 + } + + pub(crate) fn span_stack(&self) -> cell::Ref<'_, SpanStack> { + self.current_spans.get_or_default().borrow() + } +} + +thread_local! { + /// `CLOSE_COUNT` is the thread-local counter used by `CloseGuard` to + /// track how many layers have processed the close. + /// For additional details, see [`CloseGuard`]. + /// + static CLOSE_COUNT: Cell<usize> = Cell::new(0); +} + +impl Subscriber for Registry { + fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest { + if self.has_per_layer_filters() { + return FilterState::take_interest().unwrap_or_else(Interest::always); + } + + Interest::always() + } + + fn enabled(&self, _: &Metadata<'_>) -> bool { + if self.has_per_layer_filters() { + return FilterState::event_enabled(); + } + true + } + + #[inline] + fn new_span(&self, attrs: &span::Attributes<'_>) -> span::Id { + let parent = if attrs.is_root() { + None + } else if attrs.is_contextual() { + self.current_span().id().map(|id| self.clone_span(id)) + } else { + attrs.parent().map(|id| self.clone_span(id)) + }; + + let id = self + .spans + // Check out a `DataInner` entry from the pool for the new span. If + // there are free entries already allocated in the pool, this will + // preferentially reuse one; otherwise, a new `DataInner` is + // allocated and added to the pool. + .create_with(|data| { + data.metadata = attrs.metadata(); + data.parent = parent; + data.filter_map = crate::filter::FILTERING.with(|filtering| filtering.filter_map()); + #[cfg(debug_assertions)] + { + if data.filter_map != FilterMap::default() { + debug_assert!(self.has_per_layer_filters()); + } + } + + let refs = data.ref_count.get_mut(); + debug_assert_eq!(*refs, 0); + *refs = 1; + }) + .expect("Unable to allocate another span"); + idx_to_id(id) + } + + /// This is intentionally not implemented, as recording fields + /// on a span is the responsibility of layers atop of this registry. + #[inline] + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + + fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {} + + /// This is intentionally not implemented, as recording events + /// is the responsibility of layers atop of this registry. + fn event(&self, _: &Event<'_>) {} + + fn enter(&self, id: &span::Id) { + if self + .current_spans + .get_or_default() + .borrow_mut() + .push(id.clone()) + { + self.clone_span(id); + } + } + + fn exit(&self, id: &span::Id) { + if let Some(spans) = self.current_spans.get() { + if spans.borrow_mut().pop(id) { + dispatcher::get_default(|dispatch| dispatch.try_close(id.clone())); + } + } + } + + fn clone_span(&self, id: &span::Id) -> span::Id { + let span = self + .get(id) + .unwrap_or_else(|| panic!( + "tried to clone {:?}, but no span exists with that ID\n\ + This may be caused by consuming a parent span (`parent: span`) rather than borrowing it (`parent: &span`).", + id, + )); + // Like `std::sync::Arc`, adds to the ref count (on clone) don't require + // a strong ordering; if we call` clone_span`, the reference count must + // always at least 1. The only synchronization necessary is between + // calls to `try_close`: we have to ensure that all threads have + // dropped their refs to the span before the span is closed. + let refs = span.ref_count.fetch_add(1, Ordering::Relaxed); + assert_ne!( + refs, 0, + "tried to clone a span ({:?}) that already closed", + id + ); + id.clone() + } + + fn current_span(&self) -> Current { + self.current_spans + .get() + .and_then(|spans| { + let spans = spans.borrow(); + let id = spans.current()?; + let span = self.get(id)?; + Some(Current::new(id.clone(), span.metadata)) + }) + .unwrap_or_else(Current::none) + } + + /// Decrements the reference count of the span with the given `id`, and + /// removes the span if it is zero. + /// + /// The allocated span slot will be reused when a new span is created. + fn try_close(&self, id: span::Id) -> bool { + let span = match self.get(&id) { + Some(span) => span, + None if std::thread::panicking() => return false, + None => panic!("tried to drop a ref to {:?}, but no such span exists!", id), + }; + + let refs = span.ref_count.fetch_sub(1, Ordering::Release); + if !std::thread::panicking() { + assert!(refs < std::usize::MAX, "reference count overflow!"); + } + if refs > 1 { + return false; + } + + // Synchronize if we are actually removing the span (stolen + // from std::Arc); this ensures that all other `try_close` calls on + // other threads happen-before we actually remove the span. + fence(Ordering::Acquire); + true + } +} + +impl<'a> LookupSpan<'a> for Registry { + type Data = Data<'a>; + + fn span_data(&'a self, id: &Id) -> Option<Self::Data> { + let inner = self.get(id)?; + Some(Data { inner }) + } + + fn register_filter(&mut self) -> FilterId { + let id = FilterId::new(self.next_filter_id); + self.next_filter_id += 1; + id + } +} + +// === impl CloseGuard === + +impl<'a> CloseGuard<'a> { + pub(crate) fn set_closing(&mut self) { + self.is_closing = true; + } +} + +impl<'a> Drop for CloseGuard<'a> { + fn drop(&mut self) { + // If this returns with an error, we are already panicking. At + // this point, there's nothing we can really do to recover + // except by avoiding a double-panic. + let _ = CLOSE_COUNT.try_with(|count| { + let c = count.get(); + // Decrement the count to indicate that _this_ guard's + // `on_close` callback has completed. + // + // Note that we *must* do this before we actually remove the span + // from the registry, since dropping the `DataInner` may trigger a + // new close, if this span is the last reference to a parent span. + count.set(c - 1); + + // If the current close count is 1, this stack frame is the last + // `on_close` call. If the span is closing, it's okay to remove the + // span. + if c == 1 && self.is_closing { + self.registry.spans.clear(id_to_idx(&self.id)); + } + }); + } +} + +// === impl Data === + +impl<'a> SpanData<'a> for Data<'a> { + fn id(&self) -> Id { + idx_to_id(self.inner.key()) + } + + fn metadata(&self) -> &'static Metadata<'static> { + (*self).inner.metadata + } + + fn parent(&self) -> Option<&Id> { + self.inner.parent.as_ref() + } + + fn extensions(&self) -> Extensions<'_> { + Extensions::new(self.inner.extensions.read().expect("Mutex poisoned")) + } + + fn extensions_mut(&self) -> ExtensionsMut<'_> { + ExtensionsMut::new(self.inner.extensions.write().expect("Mutex poisoned")) + } + + #[inline] + fn is_enabled_for(&self, filter: FilterId) -> bool { + self.inner.filter_map.is_enabled(filter) + } +} + +// === impl DataInner === + +impl Default for DataInner { + fn default() -> Self { + // Since `DataInner` owns a `&'static Callsite` pointer, we need + // something to use as the initial default value for that callsite. + // Since we can't access a `DataInner` until it has had actual span data + // inserted into it, the null metadata will never actually be accessed. + struct NullCallsite; + impl tracing_core::callsite::Callsite for NullCallsite { + fn set_interest(&self, _: Interest) { + unreachable!( + "/!\\ Tried to register the null callsite /!\\\n \ + This should never have happened and is definitely a bug. \ + A `tracing` bug report would be appreciated." + ) + } + + fn metadata(&self) -> &Metadata<'_> { + unreachable!( + "/!\\ Tried to access the null callsite's metadata /!\\\n \ + This should never have happened and is definitely a bug. \ + A `tracing` bug report would be appreciated." + ) + } + } + + static NULL_CALLSITE: NullCallsite = NullCallsite; + static NULL_METADATA: Metadata<'static> = tracing_core::metadata! { + name: "", + target: "", + level: tracing_core::Level::TRACE, + fields: &[], + callsite: &NULL_CALLSITE, + kind: tracing_core::metadata::Kind::SPAN, + }; + + Self { + filter_map: FilterMap::default(), + metadata: &NULL_METADATA, + parent: None, + ref_count: AtomicUsize::new(0), + extensions: RwLock::new(ExtensionsInner::new()), + } + } +} + +impl Clear for DataInner { + /// Clears the span's data in place, dropping the parent's reference count. + fn clear(&mut self) { + // A span is not considered closed until all of its children have closed. + // Therefore, each span's `DataInner` holds a "reference" to the parent + // span, keeping the parent span open until all its children have closed. + // When we close a span, we must then decrement the parent's ref count + // (potentially, allowing it to close, if this child is the last reference + // to that span). + // We have to actually unpack the option inside the `get_default` + // closure, since it is a `FnMut`, but testing that there _is_ a value + // here lets us avoid the thread-local access if we don't need the + // dispatcher at all. + if self.parent.is_some() { + // Note that --- because `Layered::try_close` works by calling + // `try_close` on the inner subscriber and using the return value to + // determine whether to call the `Layer`'s `on_close` callback --- + // we must call `try_close` on the entire subscriber stack, rather + // than just on the registry. If the registry called `try_close` on + // itself directly, the layers wouldn't see the close notification. + let subscriber = dispatcher::get_default(Dispatch::clone); + if let Some(parent) = self.parent.take() { + let _ = subscriber.try_close(parent); + } + } + + // Clear (but do not deallocate!) the pooled `HashMap` for the span's extensions. + self.extensions + .get_mut() + .unwrap_or_else(|l| { + // This function can be called in a `Drop` impl, such as while + // panicking, so ignore lock poisoning. + l.into_inner() + }) + .clear(); + + self.filter_map = FilterMap::default(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{layer::Context, registry::LookupSpan, Layer}; + use std::{ + collections::HashMap, + sync::{Arc, Mutex, Weak}, + }; + use tracing::{self, subscriber::with_default}; + use tracing_core::{ + dispatcher, + span::{Attributes, Id}, + Subscriber, + }; + + #[derive(Debug)] + struct DoesNothing; + impl<S: Subscriber> Layer<S> for DoesNothing {} + + struct AssertionLayer; + impl<S> Layer<S> for AssertionLayer + where + S: Subscriber + for<'a> LookupSpan<'a>, + { + fn on_close(&self, id: Id, ctx: Context<'_, S>) { + dbg!(format_args!("closing {:?}", id)); + assert!(&ctx.span(&id).is_some()); + } + } + + #[test] + fn single_layer_can_access_closed_span() { + let subscriber = AssertionLayer.with_subscriber(Registry::default()); + + with_default(subscriber, || { + let span = tracing::debug_span!("span"); + drop(span); + }); + } + + #[test] + fn multiple_layers_can_access_closed_span() { + let subscriber = AssertionLayer + .and_then(AssertionLayer) + .with_subscriber(Registry::default()); + + with_default(subscriber, || { + let span = tracing::debug_span!("span"); + drop(span); + }); + } + + struct CloseLayer { + inner: Arc<Mutex<CloseState>>, + } + + struct CloseHandle { + state: Arc<Mutex<CloseState>>, + } + + #[derive(Default)] + struct CloseState { + open: HashMap<&'static str, Weak<()>>, + closed: Vec<(&'static str, Weak<()>)>, + } + + struct SetRemoved(Arc<()>); + + impl<S> Layer<S> for CloseLayer + where + S: Subscriber + for<'a> LookupSpan<'a>, + { + fn on_new_span(&self, _: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { + let span = ctx.span(id).expect("Missing span; this is a bug"); + let mut lock = self.inner.lock().unwrap(); + let is_removed = Arc::new(()); + assert!( + lock.open + .insert(span.name(), Arc::downgrade(&is_removed)) + .is_none(), + "test layer saw multiple spans with the same name, the test is probably messed up" + ); + let mut extensions = span.extensions_mut(); + extensions.insert(SetRemoved(is_removed)); + } + + fn on_close(&self, id: Id, ctx: Context<'_, S>) { + let span = if let Some(span) = ctx.span(&id) { + span + } else { + println!( + "span {:?} did not exist in `on_close`, are we panicking?", + id + ); + return; + }; + let name = span.name(); + println!("close {} ({:?})", name, id); + if let Ok(mut lock) = self.inner.lock() { + if let Some(is_removed) = lock.open.remove(name) { + assert!(is_removed.upgrade().is_some()); + lock.closed.push((name, is_removed)); + } + } + } + } + + impl CloseLayer { + fn new() -> (Self, CloseHandle) { + let state = Arc::new(Mutex::new(CloseState::default())); + ( + Self { + inner: state.clone(), + }, + CloseHandle { state }, + ) + } + } + + impl CloseState { + fn is_open(&self, span: &str) -> bool { + self.open.contains_key(span) + } + + fn is_closed(&self, span: &str) -> bool { + self.closed.iter().any(|(name, _)| name == &span) + } + } + + impl CloseHandle { + fn assert_closed(&self, span: &str) { + let lock = self.state.lock().unwrap(); + assert!( + lock.is_closed(span), + "expected {} to be closed{}", + span, + if lock.is_open(span) { + " (it was still open)" + } else { + ", but it never existed (is there a problem with the test?)" + } + ) + } + + fn assert_open(&self, span: &str) { + let lock = self.state.lock().unwrap(); + assert!( + lock.is_open(span), + "expected {} to be open{}", + span, + if lock.is_closed(span) { + " (it was still open)" + } else { + ", but it never existed (is there a problem with the test?)" + } + ) + } + + fn assert_removed(&self, span: &str) { + let lock = self.state.lock().unwrap(); + let is_removed = match lock.closed.iter().find(|(name, _)| name == &span) { + Some((_, is_removed)) => is_removed, + None => panic!( + "expected {} to be removed from the registry, but it was not closed {}", + span, + if lock.is_closed(span) { + " (it was still open)" + } else { + ", but it never existed (is there a problem with the test?)" + } + ), + }; + assert!( + is_removed.upgrade().is_none(), + "expected {} to have been removed from the registry", + span + ) + } + + fn assert_not_removed(&self, span: &str) { + let lock = self.state.lock().unwrap(); + let is_removed = match lock.closed.iter().find(|(name, _)| name == &span) { + Some((_, is_removed)) => is_removed, + None if lock.is_open(span) => return, + None => unreachable!(), + }; + assert!( + is_removed.upgrade().is_some(), + "expected {} to have been removed from the registry", + span + ) + } + + #[allow(unused)] // may want this for future tests + fn assert_last_closed(&self, span: Option<&str>) { + let lock = self.state.lock().unwrap(); + let last = lock.closed.last().map(|(span, _)| span); + assert_eq!( + last, + span.as_ref(), + "expected {:?} to have closed last", + span + ); + } + + fn assert_closed_in_order(&self, order: impl AsRef<[&'static str]>) { + let lock = self.state.lock().unwrap(); + let order = order.as_ref(); + for (i, name) in order.iter().enumerate() { + assert_eq!( + lock.closed.get(i).map(|(span, _)| span), + Some(name), + "expected close order: {:?}, actual: {:?}", + order, + lock.closed.iter().map(|(name, _)| name).collect::<Vec<_>>() + ); + } + } + } + + #[test] + fn spans_are_removed_from_registry() { + let (close_layer, state) = CloseLayer::new(); + let subscriber = AssertionLayer + .and_then(close_layer) + .with_subscriber(Registry::default()); + + // Create a `Dispatch` (which is internally reference counted) so that + // the subscriber lives to the end of the test. Otherwise, if we just + // passed the subscriber itself to `with_default`, we could see the span + // be dropped when the subscriber itself is dropped, destroying the + // registry. + let dispatch = dispatcher::Dispatch::new(subscriber); + + dispatcher::with_default(&dispatch, || { + let span = tracing::debug_span!("span1"); + drop(span); + let span = tracing::info_span!("span2"); + drop(span); + }); + + state.assert_removed("span1"); + state.assert_removed("span2"); + + // Ensure the registry itself outlives the span. + drop(dispatch); + } + + #[test] + fn spans_are_only_closed_when_the_last_ref_drops() { + let (close_layer, state) = CloseLayer::new(); + let subscriber = AssertionLayer + .and_then(close_layer) + .with_subscriber(Registry::default()); + + // Create a `Dispatch` (which is internally reference counted) so that + // the subscriber lives to the end of the test. Otherwise, if we just + // passed the subscriber itself to `with_default`, we could see the span + // be dropped when the subscriber itself is dropped, destroying the + // registry. + let dispatch = dispatcher::Dispatch::new(subscriber); + + let span2 = dispatcher::with_default(&dispatch, || { + let span = tracing::debug_span!("span1"); + drop(span); + let span2 = tracing::info_span!("span2"); + let span2_clone = span2.clone(); + drop(span2); + span2_clone + }); + + state.assert_removed("span1"); + state.assert_not_removed("span2"); + + drop(span2); + state.assert_removed("span1"); + + // Ensure the registry itself outlives the span. + drop(dispatch); + } + + #[test] + fn span_enter_guards_are_dropped_out_of_order() { + let (close_layer, state) = CloseLayer::new(); + let subscriber = AssertionLayer + .and_then(close_layer) + .with_subscriber(Registry::default()); + + // Create a `Dispatch` (which is internally reference counted) so that + // the subscriber lives to the end of the test. Otherwise, if we just + // passed the subscriber itself to `with_default`, we could see the span + // be dropped when the subscriber itself is dropped, destroying the + // registry. + let dispatch = dispatcher::Dispatch::new(subscriber); + + dispatcher::with_default(&dispatch, || { + let span1 = tracing::debug_span!("span1"); + let span2 = tracing::info_span!("span2"); + + let enter1 = span1.enter(); + let enter2 = span2.enter(); + + drop(enter1); + drop(span1); + + state.assert_removed("span1"); + state.assert_not_removed("span2"); + + drop(enter2); + state.assert_not_removed("span2"); + + drop(span2); + state.assert_removed("span1"); + state.assert_removed("span2"); + }); + } + + #[test] + fn child_closes_parent() { + // This test asserts that if a parent span's handle is dropped before + // a child span's handle, the parent will remain open until child + // closes, and will then be closed. + + let (close_layer, state) = CloseLayer::new(); + let subscriber = close_layer.with_subscriber(Registry::default()); + + let dispatch = dispatcher::Dispatch::new(subscriber); + + dispatcher::with_default(&dispatch, || { + let span1 = tracing::info_span!("parent"); + let span2 = tracing::info_span!(parent: &span1, "child"); + + state.assert_open("parent"); + state.assert_open("child"); + + drop(span1); + state.assert_open("parent"); + state.assert_open("child"); + + drop(span2); + state.assert_closed("parent"); + state.assert_closed("child"); + }); + } + + #[test] + fn child_closes_grandparent() { + // This test asserts that, when a span is kept open by a child which + // is *itself* kept open by a child, closing the grandchild will close + // both the parent *and* the grandparent. + let (close_layer, state) = CloseLayer::new(); + let subscriber = close_layer.with_subscriber(Registry::default()); + + let dispatch = dispatcher::Dispatch::new(subscriber); + + dispatcher::with_default(&dispatch, || { + let span1 = tracing::info_span!("grandparent"); + let span2 = tracing::info_span!(parent: &span1, "parent"); + let span3 = tracing::info_span!(parent: &span2, "child"); + + state.assert_open("grandparent"); + state.assert_open("parent"); + state.assert_open("child"); + + drop(span1); + drop(span2); + state.assert_open("grandparent"); + state.assert_open("parent"); + state.assert_open("child"); + + drop(span3); + + state.assert_closed_in_order(&["child", "parent", "grandparent"]); + }); + } +} diff --git a/vendor/tracing-subscriber/src/registry/stack.rs b/vendor/tracing-subscriber/src/registry/stack.rs new file mode 100644 index 000000000..4a3f7e59d --- /dev/null +++ b/vendor/tracing-subscriber/src/registry/stack.rs @@ -0,0 +1,77 @@ +pub(crate) use tracing_core::span::Id; + +#[derive(Debug)] +struct ContextId { + id: Id, + duplicate: bool, +} + +/// `SpanStack` tracks what spans are currently executing on a thread-local basis. +/// +/// A "separate current span" for each thread is a semantic choice, as each span +/// can be executing in a different thread. +#[derive(Debug, Default)] +pub(crate) struct SpanStack { + stack: Vec<ContextId>, +} + +impl SpanStack { + #[inline] + pub(super) fn push(&mut self, id: Id) -> bool { + let duplicate = self.stack.iter().any(|i| i.id == id); + self.stack.push(ContextId { id, duplicate }); + !duplicate + } + + #[inline] + pub(super) fn pop(&mut self, expected_id: &Id) -> bool { + if let Some((idx, _)) = self + .stack + .iter() + .enumerate() + .rev() + .find(|(_, ctx_id)| ctx_id.id == *expected_id) + { + let ContextId { id: _, duplicate } = self.stack.remove(idx); + return !duplicate; + } + false + } + + #[inline] + pub(crate) fn iter(&self) -> impl Iterator<Item = &Id> { + self.stack + .iter() + .rev() + .filter_map(|ContextId { id, duplicate }| if !*duplicate { Some(id) } else { None }) + } + + #[inline] + pub(crate) fn current(&self) -> Option<&Id> { + self.iter().next() + } +} + +#[cfg(test)] +mod tests { + use super::{Id, SpanStack}; + + #[test] + fn pop_last_span() { + let mut stack = SpanStack::default(); + let id = Id::from_u64(1); + stack.push(id.clone()); + + assert!(stack.pop(&id)); + } + + #[test] + fn pop_first_span() { + let mut stack = SpanStack::default(); + stack.push(Id::from_u64(1)); + stack.push(Id::from_u64(2)); + + let id = Id::from_u64(1); + assert!(stack.pop(&id)); + } +} diff --git a/vendor/tracing-subscriber/src/reload.rs b/vendor/tracing-subscriber/src/reload.rs new file mode 100644 index 000000000..3d1743440 --- /dev/null +++ b/vendor/tracing-subscriber/src/reload.rs @@ -0,0 +1,350 @@ +//! Wrapper for a `Layer` to allow it to be dynamically reloaded. +//! +//! This module provides a [`Layer` type] implementing the [`Layer` trait] or [`Filter` trait] +//! which wraps another type implementing the corresponding trait. This +//! allows the wrapped type to be replaced with another +//! instance of that type at runtime. +//! +//! This can be used in cases where a subset of `Layer` or `Filter` functionality +//! should be dynamically reconfigured, such as when filtering directives may +//! change at runtime. Note that this layer introduces a (relatively small) +//! amount of overhead, and should thus only be used as needed. +//! +//! # Examples +//! +//! Reloading a [global filtering](crate::layer#global-filtering) layer: +//! +//! ```rust +//! # use tracing::info; +//! use tracing_subscriber::{filter, fmt, reload, prelude::*}; +//! let filter = filter::LevelFilter::WARN; +//! let (filter, reload_handle) = reload::Layer::new(filter); +//! tracing_subscriber::registry() +//! .with(filter) +//! .with(fmt::Layer::default()) +//! .init(); +//! # +//! # // specifying the Registry type is required +//! # let _: &reload::Handle<filter::LevelFilter, tracing_subscriber::Registry> = &reload_handle; +//! # +//! info!("This will be ignored"); +//! reload_handle.modify(|filter| *filter = filter::LevelFilter::INFO); +//! info!("This will be logged"); +//! ``` +//! +//! Reloading a [`Filtered`](crate::filter::Filtered) layer: +//! +//! ```rust +//! # use tracing::info; +//! use tracing_subscriber::{filter, fmt, reload, prelude::*}; +//! let filtered_layer = fmt::Layer::default().with_filter(filter::LevelFilter::WARN); +//! let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer); +//! # +//! # // specifying the Registry type is required +//! # let _: &reload::Handle<filter::Filtered<fmt::Layer<tracing_subscriber::Registry>, +//! # filter::LevelFilter, tracing_subscriber::Registry>,tracing_subscriber::Registry> +//! # = &reload_handle; +//! # +//! tracing_subscriber::registry() +//! .with(filtered_layer) +//! .init(); +//! info!("This will be ignored"); +//! reload_handle.modify(|layer| *layer.filter_mut() = filter::LevelFilter::INFO); +//! info!("This will be logged"); +//! ``` +//! +//! ## Note +//! +//! The [`Layer`] implementation is unable to implement downcasting functionality, +//! so certain [`Layer`] will fail to downcast if wrapped in a `reload::Layer`. +//! +//! If you only want to be able to dynamically change the +//! `Filter` on a layer, prefer wrapping that `Filter` in the `reload::Layer`. +//! +//! [`Filter` trait]: crate::layer::Filter +//! [`Layer` type]: Layer +//! [`Layer` trait]: super::layer::Layer +use crate::layer; +use crate::sync::RwLock; + +use std::{ + error, fmt, + marker::PhantomData, + sync::{Arc, Weak}, +}; +use tracing_core::{ + callsite, span, + subscriber::{Interest, Subscriber}, + Event, Metadata, +}; + +/// Wraps a `Layer` or `Filter`, allowing it to be reloaded dynamically at runtime. +#[derive(Debug)] +pub struct Layer<L, S> { + // TODO(eliza): this once used a `crossbeam_util::ShardedRwLock`. We may + // eventually wish to replace it with a sharded lock implementation on top + // of our internal `RwLock` wrapper type. If possible, we should profile + // this first to determine if it's necessary. + inner: Arc<RwLock<L>>, + _s: PhantomData<fn(S)>, +} + +/// Allows reloading the state of an associated [`Layer`](crate::layer::Layer). +#[derive(Debug)] +pub struct Handle<L, S> { + inner: Weak<RwLock<L>>, + _s: PhantomData<fn(S)>, +} + +/// Indicates that an error occurred when reloading a layer. +#[derive(Debug)] +pub struct Error { + kind: ErrorKind, +} + +#[derive(Debug)] +enum ErrorKind { + SubscriberGone, + Poisoned, +} + +// ===== impl Layer ===== + +impl<L, S> crate::Layer<S> for Layer<L, S> +where + L: crate::Layer<S> + 'static, + S: Subscriber, +{ + fn on_layer(&mut self, subscriber: &mut S) { + try_lock!(self.inner.write(), else return).on_layer(subscriber); + } + + #[inline] + fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { + try_lock!(self.inner.read(), else return Interest::sometimes()).register_callsite(metadata) + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>, ctx: layer::Context<'_, S>) -> bool { + try_lock!(self.inner.read(), else return false).enabled(metadata, ctx) + } + + #[inline] + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_new_span(attrs, id, ctx) + } + + #[inline] + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_record(span, values, ctx) + } + + #[inline] + fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_follows_from(span, follows, ctx) + } + + #[inline] + fn event_enabled(&self, event: &Event<'_>, ctx: layer::Context<'_, S>) -> bool { + try_lock!(self.inner.read(), else return false).event_enabled(event, ctx) + } + + #[inline] + fn on_event(&self, event: &Event<'_>, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_event(event, ctx) + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_enter(id, ctx) + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_exit(id, ctx) + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_close(id, ctx) + } + + #[inline] + fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_id_change(old, new, ctx) + } +} + +// ===== impl Filter ===== + +#[cfg(all(feature = "registry", feature = "std"))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))] +impl<S, L> crate::layer::Filter<S> for Layer<L, S> +where + L: crate::layer::Filter<S> + 'static, + S: Subscriber, +{ + #[inline] + fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest { + try_lock!(self.inner.read(), else return Interest::sometimes()).callsite_enabled(metadata) + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>, ctx: &layer::Context<'_, S>) -> bool { + try_lock!(self.inner.read(), else return false).enabled(metadata, ctx) + } + + #[inline] + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_new_span(attrs, id, ctx) + } + + #[inline] + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_record(span, values, ctx) + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_enter(id, ctx) + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_exit(id, ctx) + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: layer::Context<'_, S>) { + try_lock!(self.inner.read()).on_close(id, ctx) + } +} + +impl<L, S> Layer<L, S> { + /// Wraps the given [`Layer`] or [`Filter`], returning a `reload::Layer` + /// and a `Handle` that allows the inner value to be modified at runtime. + /// + /// [`Layer`]: crate::layer::Layer + /// [`Filter`]: crate::layer::Filter + pub fn new(inner: L) -> (Self, Handle<L, S>) { + let this = Self { + inner: Arc::new(RwLock::new(inner)), + _s: PhantomData, + }; + let handle = this.handle(); + (this, handle) + } + + /// Returns a `Handle` that can be used to reload the wrapped [`Layer`] or [`Filter`]. + /// + /// [`Layer`]: crate::layer::Layer + /// [`Filter`]: crate::filter::Filter + pub fn handle(&self) -> Handle<L, S> { + Handle { + inner: Arc::downgrade(&self.inner), + _s: PhantomData, + } + } +} + +// ===== impl Handle ===== + +impl<L, S> Handle<L, S> { + /// Replace the current [`Layer`] or [`Filter`] with the provided `new_value`. + /// + /// [`Handle::reload`] cannot be used with the [`Filtered`] layer; use + /// [`Handle::modify`] instead (see [this issue] for additional details). + /// + /// However, if the _only_ the [`Filter`] needs to be modified, use + /// `reload::Layer` to wrap the `Filter` directly. + /// + /// [`Layer`]: crate::layer::Layer + /// [`Filter`]: crate::layer::Filter + /// [`Filtered`]: crate::filter::Filtered + /// + /// [this issue]: https://github.com/tokio-rs/tracing/issues/1629 + pub fn reload(&self, new_value: impl Into<L>) -> Result<(), Error> { + self.modify(|layer| { + *layer = new_value.into(); + }) + } + + /// Invokes a closure with a mutable reference to the current layer or filter, + /// allowing it to be modified in place. + pub fn modify(&self, f: impl FnOnce(&mut L)) -> Result<(), Error> { + let inner = self.inner.upgrade().ok_or(Error { + kind: ErrorKind::SubscriberGone, + })?; + + let mut lock = try_lock!(inner.write(), else return Err(Error::poisoned())); + f(&mut *lock); + // Release the lock before rebuilding the interest cache, as that + // function will lock the new layer. + drop(lock); + + callsite::rebuild_interest_cache(); + Ok(()) + } + + /// Returns a clone of the layer or filter's current value if it still exists. + /// Otherwise, if the subscriber has been dropped, returns `None`. + pub fn clone_current(&self) -> Option<L> + where + L: Clone, + { + self.with_current(L::clone).ok() + } + + /// Invokes a closure with a borrowed reference to the current layer or filter, + /// returning the result (or an error if the subscriber no longer exists). + pub fn with_current<T>(&self, f: impl FnOnce(&L) -> T) -> Result<T, Error> { + let inner = self.inner.upgrade().ok_or(Error { + kind: ErrorKind::SubscriberGone, + })?; + let inner = try_lock!(inner.read(), else return Err(Error::poisoned())); + Ok(f(&*inner)) + } +} + +impl<L, S> Clone for Handle<L, S> { + fn clone(&self) -> Self { + Handle { + inner: self.inner.clone(), + _s: PhantomData, + } + } +} + +// ===== impl Error ===== + +impl Error { + fn poisoned() -> Self { + Self { + kind: ErrorKind::Poisoned, + } + } + + /// Returns `true` if this error occurred because the layer was poisoned by + /// a panic on another thread. + pub fn is_poisoned(&self) -> bool { + matches!(self.kind, ErrorKind::Poisoned) + } + + /// Returns `true` if this error occurred because the `Subscriber` + /// containing the reloadable layer was dropped. + pub fn is_dropped(&self) -> bool { + matches!(self.kind, ErrorKind::SubscriberGone) + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let msg = match self.kind { + ErrorKind::SubscriberGone => "subscriber no longer exists", + ErrorKind::Poisoned => "lock poisoned", + }; + f.pad(msg) + } +} + +impl error::Error for Error {} diff --git a/vendor/tracing-subscriber/src/sync.rs b/vendor/tracing-subscriber/src/sync.rs new file mode 100644 index 000000000..ec42b834a --- /dev/null +++ b/vendor/tracing-subscriber/src/sync.rs @@ -0,0 +1,57 @@ +//! Abstracts over sync primitive implementations. +//! +//! Optionally, we allow the Rust standard library's `RwLock` to be replaced +//! with the `parking_lot` crate's implementation. This may provide improved +//! performance in some cases. However, the `parking_lot` dependency is an +//! opt-in feature flag. Because `parking_lot::RwLock` has a slightly different +//! API than `std::sync::RwLock` (it does not support poisoning on panics), we +//! wrap it with a type that provides the same method signatures. This allows us +//! to transparently swap `parking_lot` in without changing code at the callsite. +#[allow(unused_imports)] // may be used later; +pub(crate) use std::sync::{LockResult, PoisonError, TryLockResult}; + +#[cfg(not(feature = "parking_lot"))] +pub(crate) use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; + +#[cfg(feature = "parking_lot")] +pub(crate) use self::parking_lot_impl::*; + +#[cfg(feature = "parking_lot")] +mod parking_lot_impl { + pub(crate) use parking_lot::{RwLockReadGuard, RwLockWriteGuard}; + use std::sync::{LockResult, TryLockError, TryLockResult}; + + #[derive(Debug)] + pub(crate) struct RwLock<T> { + inner: parking_lot::RwLock<T>, + } + + impl<T> RwLock<T> { + pub(crate) fn new(val: T) -> Self { + Self { + inner: parking_lot::RwLock::new(val), + } + } + + #[inline] + pub(crate) fn get_mut(&mut self) -> LockResult<&mut T> { + Ok(self.inner.get_mut()) + } + + #[inline] + pub(crate) fn read(&self) -> LockResult<RwLockReadGuard<'_, T>> { + Ok(self.inner.read()) + } + + #[inline] + #[allow(dead_code)] // may be used later; + pub(crate) fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>> { + self.inner.try_read().ok_or(TryLockError::WouldBlock) + } + + #[inline] + pub(crate) fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>> { + Ok(self.inner.write()) + } + } +} diff --git a/vendor/tracing-subscriber/src/util.rs b/vendor/tracing-subscriber/src/util.rs new file mode 100644 index 000000000..1c98aa4d2 --- /dev/null +++ b/vendor/tracing-subscriber/src/util.rs @@ -0,0 +1,153 @@ +//! Extension traits and other utilities to make working with subscribers more +//! ergonomic. +use core::fmt; +#[cfg(feature = "std")] +use std::error::Error; +use tracing_core::dispatcher::{self, Dispatch}; +#[cfg(feature = "tracing-log")] +use tracing_log::AsLog; + +/// Extension trait adding utility methods for subscriber initialization. +/// +/// This trait provides extension methods to make configuring and setting a +/// [default subscriber] more ergonomic. It is automatically implemented for all +/// types that can be converted into a [trace dispatcher]. Since `Dispatch` +/// implements `From<T>` for all `T: Subscriber`, all `Subscriber` +/// implementations will implement this extension trait as well. Types which +/// can be converted into `Subscriber`s, such as builders that construct a +/// `Subscriber`, may implement `Into<Dispatch>`, and will also receive an +/// implementation of this trait. +/// +/// [default subscriber]: https://docs.rs/tracing/0.1.21/tracing/dispatcher/index.html#setting-the-default-subscriber +/// [trace dispatcher]: https://docs.rs/tracing/0.1.21/tracing/dispatcher/index.html +pub trait SubscriberInitExt +where + Self: Into<Dispatch>, +{ + /// Sets `self` as the [default subscriber] in the current scope, returning a + /// guard that will unset it when dropped. + /// + /// If the "tracing-log" feature flag is enabled, this will also initialize + /// a [`log`] compatibility layer. This allows the subscriber to consume + /// `log::Record`s as though they were `tracing` `Event`s. + /// + /// [default subscriber]: https://docs.rs/tracing/0.1.21/tracing/dispatcher/index.html#setting-the-default-subscriber + /// [`log`]: https://crates.io/log + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + fn set_default(self) -> dispatcher::DefaultGuard { + #[cfg(feature = "tracing-log")] + let _ = tracing_log::LogTracer::init(); + + dispatcher::set_default(&self.into()) + } + + /// Attempts to set `self` as the [global default subscriber] in the current + /// scope, returning an error if one is already set. + /// + /// If the "tracing-log" feature flag is enabled, this will also attempt to + /// initialize a [`log`] compatibility layer. This allows the subscriber to + /// consume `log::Record`s as though they were `tracing` `Event`s. + /// + /// This method returns an error if a global default subscriber has already + /// been set, or if a `log` logger has already been set (when the + /// "tracing-log" feature is enabled). + /// + /// [global default subscriber]: https://docs.rs/tracing/0.1.21/tracing/dispatcher/index.html#setting-the-default-subscriber + /// [`log`]: https://crates.io/log + fn try_init(self) -> Result<(), TryInitError> { + dispatcher::set_global_default(self.into()).map_err(TryInitError::new)?; + + // Since we are setting the global default subscriber, we can + // opportunistically go ahead and set its global max level hint as + // the max level for the `log` crate as well. This should make + // skipping `log` diagnostics much faster. + #[cfg(feature = "tracing-log")] + tracing_log::LogTracer::builder() + // Note that we must call this *after* setting the global default + // subscriber, so that we get its max level hint. + .with_max_level(tracing_core::LevelFilter::current().as_log()) + .init() + .map_err(TryInitError::new)?; + + Ok(()) + } + + /// Attempts to set `self` as the [global default subscriber] in the current + /// scope, panicking if this fails. + /// + /// If the "tracing-log" feature flag is enabled, this will also attempt to + /// initialize a [`log`] compatibility layer. This allows the subscriber to + /// consume `log::Record`s as though they were `tracing` `Event`s. + /// + /// This method panics if a global default subscriber has already been set, + /// or if a `log` logger has already been set (when the "tracing-log" + /// feature is enabled). + /// + /// [global default subscriber]: https://docs.rs/tracing/0.1.21/tracing/dispatcher/index.html#setting-the-default-subscriber + /// [`log`]: https://crates.io/log + fn init(self) { + self.try_init() + .expect("failed to set global default subscriber") + } +} + +impl<T> SubscriberInitExt for T where T: Into<Dispatch> {} + +/// Error returned by [`try_init`](SubscriberInitExt::try_init) if a global default subscriber could not be initialized. +pub struct TryInitError { + #[cfg(feature = "std")] + inner: Box<dyn Error + Send + Sync + 'static>, + + #[cfg(not(feature = "std"))] + _p: (), +} + +// ==== impl TryInitError ==== + +impl TryInitError { + #[cfg(feature = "std")] + fn new(e: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self { + Self { inner: e.into() } + } + + #[cfg(not(feature = "std"))] + fn new<T>(_: T) -> Self { + Self { _p: () } + } +} + +impl fmt::Debug for TryInitError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + #[cfg(feature = "std")] + { + fmt::Debug::fmt(&self.inner, f) + } + + #[cfg(not(feature = "std"))] + { + f.write_str("TryInitError(())") + } + } +} + +impl fmt::Display for TryInitError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + #[cfg(feature = "std")] + { + fmt::Display::fmt(&self.inner, f) + } + + #[cfg(not(feature = "std"))] + { + f.write_str("failed to set global default subscriber") + } + } +} + +#[cfg(feature = "std")] +impl Error for TryInitError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + self.inner.source() + } +} diff --git a/vendor/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs b/vendor/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs new file mode 100644 index 000000000..00e98a994 --- /dev/null +++ b/vendor/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs @@ -0,0 +1,123 @@ +#![cfg(feature = "registry")] +mod support; +use self::support::*; +use tracing::Level; +use tracing_subscriber::{filter::LevelFilter, prelude::*}; + +#[test] +fn layer_filters() { + let (unfiltered, unfiltered_handle) = unfiltered("unfiltered"); + let (filtered, filtered_handle) = filtered("filtered"); + + let _subscriber = tracing_subscriber::registry() + .with(unfiltered) + .with(filtered.with_filter(filter())) + .set_default(); + + events(); + + unfiltered_handle.assert_finished(); + filtered_handle.assert_finished(); +} + +#[test] +fn layered_layer_filters() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + let unfiltered = unfiltered1.and_then(unfiltered2); + + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + let filtered = filtered1 + .with_filter(filter()) + .and_then(filtered2.with_filter(filter())); + + let _subscriber = tracing_subscriber::registry() + .with(unfiltered) + .with(filtered) + .set_default(); + + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +#[test] +fn out_of_order() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + + let _subscriber = tracing_subscriber::registry() + .with(unfiltered1) + .with(filtered1.with_filter(filter())) + .with(unfiltered2) + .with(filtered2.with_filter(filter())) + .set_default(); + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +#[test] +fn mixed_layered() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + + let layered1 = filtered1.with_filter(filter()).and_then(unfiltered1); + let layered2 = unfiltered2.and_then(filtered2.with_filter(filter())); + + let _subscriber = tracing_subscriber::registry() + .with(layered1) + .with(layered2) + .set_default(); + + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +fn events() { + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); +} + +fn filter() -> LevelFilter { + LevelFilter::INFO +} + +fn unfiltered(name: &str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(name) + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle() +} + +fn filtered(name: &str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(name) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle() +} diff --git a/vendor/tracing-subscriber/tests/duplicate_spans.rs b/vendor/tracing-subscriber/tests/duplicate_spans.rs new file mode 100644 index 000000000..5d4dc6a85 --- /dev/null +++ b/vendor/tracing-subscriber/tests/duplicate_spans.rs @@ -0,0 +1,47 @@ +#![cfg(all(feature = "env-filter", feature = "fmt"))] +use tracing::{self, subscriber::with_default, Span}; +use tracing_subscriber::{filter::EnvFilter, FmtSubscriber}; + +#[test] +fn duplicate_spans() { + let subscriber = FmtSubscriber::builder() + .with_env_filter(EnvFilter::new("[root]=debug")) + .finish(); + + with_default(subscriber, || { + let root = tracing::debug_span!("root"); + root.in_scope(|| { + // root: + assert_eq!(root, Span::current(), "Current span must be 'root'"); + let leaf = tracing::debug_span!("leaf"); + leaf.in_scope(|| { + // root:leaf: + assert_eq!(leaf, Span::current(), "Current span must be 'leaf'"); + root.in_scope(|| { + // root:leaf: + assert_eq!( + leaf, + Span::current(), + "Current span must be 'leaf' after entering twice the 'root' span" + ); + }) + }); + // root: + assert_eq!( + root, + Span::current(), + "Current span must be root ('leaf' exited, nested 'root' exited)" + ); + + root.in_scope(|| { + assert_eq!(root, Span::current(), "Current span must be root"); + }); + // root: + assert_eq!( + root, + Span::current(), + "Current span must still be root after exiting nested 'root'" + ); + }); + }); +} diff --git a/vendor/tracing-subscriber/tests/env_filter/main.rs b/vendor/tracing-subscriber/tests/env_filter/main.rs new file mode 100644 index 000000000..3c3d4868b --- /dev/null +++ b/vendor/tracing-subscriber/tests/env_filter/main.rs @@ -0,0 +1,547 @@ +#![cfg(feature = "env-filter")] + +#[path = "../support.rs"] +mod support; +use self::support::*; + +mod per_layer; + +use tracing::{self, subscriber::with_default, Level}; +use tracing_subscriber::{ + filter::{EnvFilter, LevelFilter}, + prelude::*, +}; + +#[test] +fn level_filter_event() { + let filter: EnvFilter = "info".parse().expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "foo", "this should also be disabled"); + tracing::warn!(target: "foo", "this should be enabled"); + tracing::error!("this should be enabled too"); + }); + + finished.assert_finished(); +} + +#[test] +fn same_name_spans() { + let filter: EnvFilter = "[foo{bar}]=trace,[foo{baz}]=trace" + .parse() + .expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .new_span( + span::mock() + .named("foo") + .at_level(Level::TRACE) + .with_field(field::mock("bar")), + ) + .new_span( + span::mock() + .named("foo") + .at_level(Level::TRACE) + .with_field(field::mock("baz")), + ) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + with_default(subscriber, || { + tracing::trace_span!("foo", bar = 1); + tracing::trace_span!("foo", baz = 1); + }); + + finished.assert_finished(); +} + +#[test] +fn level_filter_event_with_target() { + let filter: EnvFilter = "info,stuff=debug".parse().expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) + .event(event::mock().at_level(Level::WARN).with_target("stuff")) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "stuff", "this should be enabled"); + tracing::debug!("but this shouldn't"); + tracing::trace!(target: "stuff", "and neither should this"); + tracing::warn!(target: "stuff", "this should be enabled"); + tracing::error!("this should be enabled too"); + tracing::error!(target: "stuff", "this should be enabled also"); + }); + + finished.assert_finished(); +} + +#[test] +fn level_filter_event_with_target_and_span_global() { + let filter: EnvFilter = "info,stuff[cool_span]=debug" + .parse() + .expect("filter should parse"); + + let cool_span = span::named("cool_span"); + let uncool_span = span::named("uncool_span"); + let (subscriber, handle) = subscriber::mock() + .enter(cool_span.clone()) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![cool_span.clone()]), + ) + .exit(cool_span) + .enter(uncool_span.clone()) + .exit(uncool_span) + .done() + .run_with_handle(); + + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + { + let _span = tracing::info_span!(target: "stuff", "cool_span").entered(); + tracing::debug!("this should be enabled"); + } + + tracing::debug!("should also be disabled"); + + { + let _span = tracing::info_span!("uncool_span").entered(); + tracing::debug!("this should be disabled"); + } + }); + + handle.assert_finished(); +} + +#[test] +fn not_order_dependent() { + // this test reproduces tokio-rs/tracing#623 + + let filter: EnvFilter = "stuff=debug,info".parse().expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) + .event(event::mock().at_level(Level::WARN).with_target("stuff")) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "stuff", "this should be enabled"); + tracing::debug!("but this shouldn't"); + tracing::trace!(target: "stuff", "and neither should this"); + tracing::warn!(target: "stuff", "this should be enabled"); + tracing::error!("this should be enabled too"); + tracing::error!(target: "stuff", "this should be enabled also"); + }); + + finished.assert_finished(); +} + +#[test] +fn add_directive_enables_event() { + // this test reproduces tokio-rs/tracing#591 + + // by default, use info level + let mut filter = EnvFilter::new(LevelFilter::INFO.to_string()); + + // overwrite with a more specific directive + filter = filter.add_directive("hello=trace".parse().expect("directive should parse")); + + let (subscriber, finished) = subscriber::mock() + .event(event::mock().at_level(Level::INFO).with_target("hello")) + .event(event::mock().at_level(Level::TRACE).with_target("hello")) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::info!(target: "hello", "hello info"); + tracing::trace!(target: "hello", "hello trace"); + }); + + finished.assert_finished(); +} + +#[test] +fn span_name_filter_is_dynamic() { + let filter: EnvFilter = "info,[cool_span]=debug" + .parse() + .expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event(event::mock().at_level(Level::INFO)) + .enter(span::named("cool_span")) + .event(event::mock().at_level(Level::DEBUG)) + .enter(span::named("uncool_span")) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::DEBUG)) + .exit(span::named("uncool_span")) + .exit(span::named("cool_span")) + .enter(span::named("uncool_span")) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .exit(span::named("uncool_span")) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + let cool_span = tracing::info_span!("cool_span"); + let uncool_span = tracing::info_span!("uncool_span"); + + { + let _enter = cool_span.enter(); + tracing::debug!("i'm a cool event"); + tracing::trace!("i'm cool, but not cool enough"); + let _enter2 = uncool_span.enter(); + tracing::warn!("warning: extremely cool!"); + tracing::debug!("i'm still cool"); + } + + let _enter = uncool_span.enter(); + tracing::warn!("warning: not that cool"); + tracing::trace!("im not cool enough"); + tracing::error!("uncool error"); + }); + + finished.assert_finished(); +} + +#[test] +fn method_name_resolution() { + #[allow(unused_imports)] + use tracing_subscriber::layer::{Filter, Layer}; + + let filter = EnvFilter::new("hello_world=info"); + filter.max_level_hint(); +} + +// contains the same tests as the first half of this file +// but using EnvFilter as a `Filter`, not as a `Layer` +mod per_layer_filter { + use super::*; + + #[test] + fn level_filter_event() { + let filter: EnvFilter = "info".parse().expect("filter should parse"); + let (layer, handle) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "foo", "this should also be disabled"); + tracing::warn!(target: "foo", "this should be enabled"); + tracing::error!("this should be enabled too"); + + handle.assert_finished(); + } + + #[test] + fn same_name_spans() { + let filter: EnvFilter = "[foo{bar}]=trace,[foo{baz}]=trace" + .parse() + .expect("filter should parse"); + let (layer, handle) = layer::mock() + .new_span( + span::mock() + .named("foo") + .at_level(Level::TRACE) + .with_field(field::mock("bar")), + ) + .new_span( + span::mock() + .named("foo") + .at_level(Level::TRACE) + .with_field(field::mock("baz")), + ) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace_span!("foo", bar = 1); + tracing::trace_span!("foo", baz = 1); + + handle.assert_finished(); + } + + #[test] + fn level_filter_event_with_target() { + let filter: EnvFilter = "info,stuff=debug".parse().expect("filter should parse"); + let (layer, handle) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) + .event(event::mock().at_level(Level::WARN).with_target("stuff")) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "stuff", "this should be enabled"); + tracing::debug!("but this shouldn't"); + tracing::trace!(target: "stuff", "and neither should this"); + tracing::warn!(target: "stuff", "this should be enabled"); + tracing::error!("this should be enabled too"); + tracing::error!(target: "stuff", "this should be enabled also"); + + handle.assert_finished(); + } + + #[test] + fn level_filter_event_with_target_and_span() { + let filter: EnvFilter = "stuff[cool_span]=debug" + .parse() + .expect("filter should parse"); + + let cool_span = span::named("cool_span"); + let (layer, handle) = layer::mock() + .enter(cool_span.clone()) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![cool_span.clone()]), + ) + .exit(cool_span) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + { + let _span = tracing::info_span!(target: "stuff", "cool_span").entered(); + tracing::debug!("this should be enabled"); + } + + tracing::debug!("should also be disabled"); + + { + let _span = tracing::info_span!("uncool_span").entered(); + tracing::debug!("this should be disabled"); + } + + handle.assert_finished(); + } + + #[test] + fn not_order_dependent() { + // this test reproduces tokio-rs/tracing#623 + + let filter: EnvFilter = "stuff=debug,info".parse().expect("filter should parse"); + let (layer, finished) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) + .event(event::mock().at_level(Level::WARN).with_target("stuff")) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "stuff", "this should be enabled"); + tracing::debug!("but this shouldn't"); + tracing::trace!(target: "stuff", "and neither should this"); + tracing::warn!(target: "stuff", "this should be enabled"); + tracing::error!("this should be enabled too"); + tracing::error!(target: "stuff", "this should be enabled also"); + + finished.assert_finished(); + } + + #[test] + fn add_directive_enables_event() { + // this test reproduces tokio-rs/tracing#591 + + // by default, use info level + let mut filter = EnvFilter::new(LevelFilter::INFO.to_string()); + + // overwrite with a more specific directive + filter = filter.add_directive("hello=trace".parse().expect("directive should parse")); + + let (layer, finished) = layer::mock() + .event(event::mock().at_level(Level::INFO).with_target("hello")) + .event(event::mock().at_level(Level::TRACE).with_target("hello")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::info!(target: "hello", "hello info"); + tracing::trace!(target: "hello", "hello trace"); + + finished.assert_finished(); + } + + #[test] + fn span_name_filter_is_dynamic() { + let filter: EnvFilter = "info,[cool_span]=debug" + .parse() + .expect("filter should parse"); + let cool_span = span::named("cool_span"); + let uncool_span = span::named("uncool_span"); + let (layer, finished) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .enter(cool_span.clone()) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![cool_span.clone()]), + ) + .enter(uncool_span.clone()) + .event( + event::mock() + .at_level(Level::WARN) + .in_scope(vec![uncool_span.clone()]), + ) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![uncool_span.clone()]), + ) + .exit(uncool_span.clone()) + .exit(cool_span) + .enter(uncool_span.clone()) + .event( + event::mock() + .at_level(Level::WARN) + .in_scope(vec![uncool_span.clone()]), + ) + .event( + event::mock() + .at_level(Level::ERROR) + .in_scope(vec![uncool_span.clone()]), + ) + .exit(uncool_span) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + let cool_span = tracing::info_span!("cool_span"); + let uncool_span = tracing::info_span!("uncool_span"); + + { + let _enter = cool_span.enter(); + tracing::debug!("i'm a cool event"); + tracing::trace!("i'm cool, but not cool enough"); + let _enter2 = uncool_span.enter(); + tracing::warn!("warning: extremely cool!"); + tracing::debug!("i'm still cool"); + } + + { + let _enter = uncool_span.enter(); + tracing::warn!("warning: not that cool"); + tracing::trace!("im not cool enough"); + tracing::error!("uncool error"); + } + + finished.assert_finished(); + } + + #[test] + fn multiple_dynamic_filters() { + // Test that multiple dynamic (span) filters only apply to the layers + // they're attached to. + let (layer1, handle1) = { + let span = span::named("span1"); + let filter: EnvFilter = "[span1]=debug".parse().expect("filter 1 should parse"); + let (layer, handle) = layer::named("layer1") + .enter(span.clone()) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![span.clone()]), + ) + .exit(span) + .done() + .run_with_handle(); + (layer.with_filter(filter), handle) + }; + + let (layer2, handle2) = { + let span = span::named("span2"); + let filter: EnvFilter = "[span2]=info".parse().expect("filter 2 should parse"); + let (layer, handle) = layer::named("layer2") + .enter(span.clone()) + .event( + event::mock() + .at_level(Level::INFO) + .in_scope(vec![span.clone()]), + ) + .exit(span) + .done() + .run_with_handle(); + (layer.with_filter(filter), handle) + }; + + let _subscriber = tracing_subscriber::registry() + .with(layer1) + .with(layer2) + .set_default(); + + tracing::info_span!("span1").in_scope(|| { + tracing::debug!("hello from span 1"); + tracing::trace!("not enabled"); + }); + + tracing::info_span!("span2").in_scope(|| { + tracing::info!("hello from span 2"); + tracing::debug!("not enabled"); + }); + + handle1.assert_finished(); + handle2.assert_finished(); + } +} diff --git a/vendor/tracing-subscriber/tests/env_filter/per_layer.rs b/vendor/tracing-subscriber/tests/env_filter/per_layer.rs new file mode 100644 index 000000000..8bf5698a4 --- /dev/null +++ b/vendor/tracing-subscriber/tests/env_filter/per_layer.rs @@ -0,0 +1,305 @@ +//! Tests for using `EnvFilter` as a per-layer filter (rather than a global +//! `Layer` filter). +#![cfg(feature = "registry")] +use super::*; + +#[test] +fn level_filter_event() { + let filter: EnvFilter = "info".parse().expect("filter should parse"); + let (layer, handle) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "foo", "this should also be disabled"); + tracing::warn!(target: "foo", "this should be enabled"); + tracing::error!("this should be enabled too"); + + handle.assert_finished(); +} + +#[test] +fn same_name_spans() { + let filter: EnvFilter = "[foo{bar}]=trace,[foo{baz}]=trace" + .parse() + .expect("filter should parse"); + let (layer, handle) = layer::mock() + .new_span( + span::mock() + .named("foo") + .at_level(Level::TRACE) + .with_field(field::mock("bar")), + ) + .new_span( + span::mock() + .named("foo") + .at_level(Level::TRACE) + .with_field(field::mock("baz")), + ) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace_span!("foo", bar = 1); + tracing::trace_span!("foo", baz = 1); + + handle.assert_finished(); +} + +#[test] +fn level_filter_event_with_target() { + let filter: EnvFilter = "info,stuff=debug".parse().expect("filter should parse"); + let (layer, handle) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) + .event(event::mock().at_level(Level::WARN).with_target("stuff")) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "stuff", "this should be enabled"); + tracing::debug!("but this shouldn't"); + tracing::trace!(target: "stuff", "and neither should this"); + tracing::warn!(target: "stuff", "this should be enabled"); + tracing::error!("this should be enabled too"); + tracing::error!(target: "stuff", "this should be enabled also"); + + handle.assert_finished(); +} + +#[test] +fn level_filter_event_with_target_and_span() { + let filter: EnvFilter = "stuff[cool_span]=debug" + .parse() + .expect("filter should parse"); + + let cool_span = span::named("cool_span"); + let (layer, handle) = layer::mock() + .enter(cool_span.clone()) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![cool_span.clone()]), + ) + .exit(cool_span) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + { + let _span = tracing::info_span!(target: "stuff", "cool_span").entered(); + tracing::debug!("this should be enabled"); + } + + tracing::debug!("should also be disabled"); + + { + let _span = tracing::info_span!("uncool_span").entered(); + tracing::debug!("this should be disabled"); + } + + handle.assert_finished(); +} + +#[test] +fn not_order_dependent() { + // this test reproduces tokio-rs/tracing#623 + + let filter: EnvFilter = "stuff=debug,info".parse().expect("filter should parse"); + let (layer, finished) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) + .event(event::mock().at_level(Level::WARN).with_target("stuff")) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + tracing::debug!(target: "stuff", "this should be enabled"); + tracing::debug!("but this shouldn't"); + tracing::trace!(target: "stuff", "and neither should this"); + tracing::warn!(target: "stuff", "this should be enabled"); + tracing::error!("this should be enabled too"); + tracing::error!(target: "stuff", "this should be enabled also"); + + finished.assert_finished(); +} + +#[test] +fn add_directive_enables_event() { + // this test reproduces tokio-rs/tracing#591 + + // by default, use info level + let mut filter = EnvFilter::new(LevelFilter::INFO.to_string()); + + // overwrite with a more specific directive + filter = filter.add_directive("hello=trace".parse().expect("directive should parse")); + + let (layer, finished) = layer::mock() + .event(event::mock().at_level(Level::INFO).with_target("hello")) + .event(event::mock().at_level(Level::TRACE).with_target("hello")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::info!(target: "hello", "hello info"); + tracing::trace!(target: "hello", "hello trace"); + + finished.assert_finished(); +} + +#[test] +fn span_name_filter_is_dynamic() { + let filter: EnvFilter = "info,[cool_span]=debug" + .parse() + .expect("filter should parse"); + let cool_span = span::named("cool_span"); + let uncool_span = span::named("uncool_span"); + let (layer, finished) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .enter(cool_span.clone()) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![cool_span.clone()]), + ) + .enter(uncool_span.clone()) + .event( + event::mock() + .at_level(Level::WARN) + .in_scope(vec![uncool_span.clone()]), + ) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![uncool_span.clone()]), + ) + .exit(uncool_span.clone()) + .exit(cool_span) + .enter(uncool_span.clone()) + .event( + event::mock() + .at_level(Level::WARN) + .in_scope(vec![uncool_span.clone()]), + ) + .event( + event::mock() + .at_level(Level::ERROR) + .in_scope(vec![uncool_span.clone()]), + ) + .exit(uncool_span) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + tracing::trace!("this should be disabled"); + tracing::info!("this shouldn't be"); + let cool_span = tracing::info_span!("cool_span"); + let uncool_span = tracing::info_span!("uncool_span"); + + { + let _enter = cool_span.enter(); + tracing::debug!("i'm a cool event"); + tracing::trace!("i'm cool, but not cool enough"); + let _enter2 = uncool_span.enter(); + tracing::warn!("warning: extremely cool!"); + tracing::debug!("i'm still cool"); + } + + { + let _enter = uncool_span.enter(); + tracing::warn!("warning: not that cool"); + tracing::trace!("im not cool enough"); + tracing::error!("uncool error"); + } + + finished.assert_finished(); +} + +#[test] +fn multiple_dynamic_filters() { + // Test that multiple dynamic (span) filters only apply to the layers + // they're attached to. + let (layer1, handle1) = { + let span = span::named("span1"); + let filter: EnvFilter = "[span1]=debug".parse().expect("filter 1 should parse"); + let (layer, handle) = layer::named("layer1") + .enter(span.clone()) + .event( + event::mock() + .at_level(Level::DEBUG) + .in_scope(vec![span.clone()]), + ) + .exit(span) + .done() + .run_with_handle(); + (layer.with_filter(filter), handle) + }; + + let (layer2, handle2) = { + let span = span::named("span2"); + let filter: EnvFilter = "[span2]=info".parse().expect("filter 2 should parse"); + let (layer, handle) = layer::named("layer2") + .enter(span.clone()) + .event( + event::mock() + .at_level(Level::INFO) + .in_scope(vec![span.clone()]), + ) + .exit(span) + .done() + .run_with_handle(); + (layer.with_filter(filter), handle) + }; + + let _subscriber = tracing_subscriber::registry() + .with(layer1) + .with(layer2) + .set_default(); + + tracing::info_span!("span1").in_scope(|| { + tracing::debug!("hello from span 1"); + tracing::trace!("not enabled"); + }); + + tracing::info_span!("span2").in_scope(|| { + tracing::info!("hello from span 2"); + tracing::debug!("not enabled"); + }); + + handle1.assert_finished(); + handle2.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/event_enabling.rs b/vendor/tracing-subscriber/tests/event_enabling.rs new file mode 100644 index 000000000..8f67cfcba --- /dev/null +++ b/vendor/tracing-subscriber/tests/event_enabling.rs @@ -0,0 +1,81 @@ +#![cfg(feature = "registry")] + +use std::sync::{Arc, Mutex}; +use tracing::{subscriber::with_default, Event, Metadata, Subscriber}; +use tracing_subscriber::{layer::Context, prelude::*, registry, Layer}; + +struct TrackingLayer { + enabled: bool, + event_enabled_count: Arc<Mutex<usize>>, + event_enabled: bool, + on_event_count: Arc<Mutex<usize>>, +} + +impl<C> Layer<C> for TrackingLayer +where + C: Subscriber + Send + Sync + 'static, +{ + fn enabled(&self, _metadata: &Metadata<'_>, _ctx: Context<'_, C>) -> bool { + self.enabled + } + + fn event_enabled(&self, _event: &Event<'_>, _ctx: Context<'_, C>) -> bool { + *self.event_enabled_count.lock().unwrap() += 1; + self.event_enabled + } + + fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, C>) { + *self.on_event_count.lock().unwrap() += 1; + } +} + +#[test] +fn event_enabled_is_only_called_once() { + let event_enabled_count = Arc::new(Mutex::default()); + let count = event_enabled_count.clone(); + let subscriber = registry().with(TrackingLayer { + enabled: true, + event_enabled_count, + event_enabled: true, + on_event_count: Arc::new(Mutex::default()), + }); + with_default(subscriber, || { + tracing::error!("hiya!"); + }); + + assert_eq!(1, *count.lock().unwrap()); +} + +#[test] +fn event_enabled_not_called_when_not_enabled() { + let event_enabled_count = Arc::new(Mutex::default()); + let count = event_enabled_count.clone(); + let subscriber = registry().with(TrackingLayer { + enabled: false, + event_enabled_count, + event_enabled: true, + on_event_count: Arc::new(Mutex::default()), + }); + with_default(subscriber, || { + tracing::error!("hiya!"); + }); + + assert_eq!(0, *count.lock().unwrap()); +} + +#[test] +fn event_disabled_does_disable_event() { + let on_event_count = Arc::new(Mutex::default()); + let count = on_event_count.clone(); + let subscriber = registry().with(TrackingLayer { + enabled: true, + event_enabled_count: Arc::new(Mutex::default()), + event_enabled: false, + on_event_count, + }); + with_default(subscriber, || { + tracing::error!("hiya!"); + }); + + assert_eq!(0, *count.lock().unwrap()); +} diff --git a/vendor/tracing-subscriber/tests/field_filter.rs b/vendor/tracing-subscriber/tests/field_filter.rs new file mode 100644 index 000000000..f14a0626d --- /dev/null +++ b/vendor/tracing-subscriber/tests/field_filter.rs @@ -0,0 +1,115 @@ +#![cfg(feature = "env-filter")] + +use tracing::{self, subscriber::with_default, Level}; +use tracing_mock::*; +use tracing_subscriber::{filter::EnvFilter, prelude::*}; + +#[test] +#[cfg_attr(not(flaky_tests), ignore)] +fn field_filter_events() { + let filter: EnvFilter = "[{thing}]=debug".parse().expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event( + event::mock() + .at_level(Level::INFO) + .with_fields(field::mock("thing")), + ) + .event( + event::mock() + .at_level(Level::DEBUG) + .with_fields(field::mock("thing")), + ) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::trace!(disabled = true); + tracing::info!("also disabled"); + tracing::info!(thing = 1); + tracing::debug!(thing = 2); + tracing::trace!(thing = 3); + }); + + finished.assert_finished(); +} + +#[test] +#[cfg_attr(not(flaky_tests), ignore)] +fn field_filter_spans() { + let filter: EnvFilter = "[{enabled=true}]=debug" + .parse() + .expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .enter(span::mock().named("span1")) + .event( + event::mock() + .at_level(Level::INFO) + .with_fields(field::mock("something")), + ) + .exit(span::mock().named("span1")) + .enter(span::mock().named("span2")) + .exit(span::mock().named("span2")) + .enter(span::mock().named("span3")) + .event( + event::mock() + .at_level(Level::DEBUG) + .with_fields(field::mock("something")), + ) + .exit(span::mock().named("span3")) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::trace!("disabled"); + tracing::info!("also disabled"); + tracing::info_span!("span1", enabled = true).in_scope(|| { + tracing::info!(something = 1); + }); + tracing::debug_span!("span2", enabled = false, foo = "hi").in_scope(|| { + tracing::warn!(something = 2); + }); + tracing::trace_span!("span3", enabled = true, answer = 42).in_scope(|| { + tracing::debug!(something = 2); + }); + }); + + finished.assert_finished(); +} + +#[test] +fn record_after_created() { + let filter: EnvFilter = "[{enabled=true}]=debug" + .parse() + .expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .enter(span::mock().named("span")) + .exit(span::mock().named("span")) + .record( + span::mock().named("span"), + field::mock("enabled").with_value(&true), + ) + .enter(span::mock().named("span")) + .event(event::mock().at_level(Level::DEBUG)) + .exit(span::mock().named("span")) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + let span = tracing::info_span!("span", enabled = false); + span.in_scope(|| { + tracing::debug!("i'm disabled!"); + }); + + span.record("enabled", &true); + span.in_scope(|| { + tracing::debug!("i'm enabled!"); + }); + + tracing::debug!("i'm also disabled"); + }); + + finished.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/filter_log.rs b/vendor/tracing-subscriber/tests/filter_log.rs new file mode 100644 index 000000000..8d57ed600 --- /dev/null +++ b/vendor/tracing-subscriber/tests/filter_log.rs @@ -0,0 +1,63 @@ +#![cfg(all(feature = "env-filter", feature = "tracing-log"))] + +use tracing::{self, Level}; +use tracing_mock::*; +use tracing_subscriber::{filter::EnvFilter, prelude::*}; + +mod my_module { + pub(crate) fn test_records() { + log::trace!("this should be disabled"); + log::info!("this shouldn't be"); + log::debug!("this should be disabled"); + log::warn!("this should be enabled"); + log::warn!(target: "something else", "this shouldn't be enabled"); + log::error!("this should be enabled too"); + } + + pub(crate) fn test_log_enabled() { + assert!( + log::log_enabled!(log::Level::Info), + "info should be enabled inside `my_module`" + ); + assert!( + !log::log_enabled!(log::Level::Debug), + "debug should not be enabled inside `my_module`" + ); + assert!( + log::log_enabled!(log::Level::Warn), + "warn should be enabled inside `my_module`" + ); + } +} + +#[test] +fn log_is_enabled() { + let filter: EnvFilter = "filter_log::my_module=info" + .parse() + .expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + // Note: we have to set the global default in order to set the `log` max + // level, which can only be set once. + subscriber.with(filter).init(); + + my_module::test_records(); + log::info!("this is disabled"); + + my_module::test_log_enabled(); + assert!( + !log::log_enabled!(log::Level::Info), + "info should not be enabled outside `my_module`" + ); + assert!( + !log::log_enabled!(log::Level::Warn), + "warn should not be enabled outside `my_module`" + ); + + finished.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/fmt_max_level_hint.rs b/vendor/tracing-subscriber/tests/fmt_max_level_hint.rs new file mode 100644 index 000000000..57a0f6e3f --- /dev/null +++ b/vendor/tracing-subscriber/tests/fmt_max_level_hint.rs @@ -0,0 +1,10 @@ +#![cfg(feature = "fmt")] +use tracing_subscriber::filter::LevelFilter; + +#[test] +fn fmt_sets_max_level_hint() { + tracing_subscriber::fmt() + .with_max_level(LevelFilter::DEBUG) + .init(); + assert_eq!(LevelFilter::current(), LevelFilter::DEBUG); +} diff --git a/vendor/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs b/vendor/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs new file mode 100644 index 000000000..897dae282 --- /dev/null +++ b/vendor/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs @@ -0,0 +1,131 @@ +#![cfg(feature = "registry")] +mod support; +use self::support::*; +use tracing::{Level, Metadata, Subscriber}; +use tracing_subscriber::{filter::DynFilterFn, layer::Context, prelude::*}; + +#[test] +fn layer_filters() { + let (unfiltered, unfiltered_handle) = unfiltered("unfiltered"); + let (filtered, filtered_handle) = filtered("filtered"); + + let subscriber = tracing_subscriber::registry() + .with(unfiltered) + .with(filtered.with_filter(filter())); + assert_eq!(subscriber.max_level_hint(), None); + let _subscriber = subscriber.set_default(); + + events(); + + unfiltered_handle.assert_finished(); + filtered_handle.assert_finished(); +} + +#[test] +fn layered_layer_filters() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + let unfiltered = unfiltered1.and_then(unfiltered2); + + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + let filtered = filtered1 + .with_filter(filter()) + .and_then(filtered2.with_filter(filter())); + + let subscriber = tracing_subscriber::registry() + .with(unfiltered) + .with(filtered); + assert_eq!(subscriber.max_level_hint(), None); + let _subscriber = subscriber.set_default(); + + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +#[test] +fn out_of_order() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + + let subscriber = tracing_subscriber::registry() + .with(unfiltered1) + .with(filtered1.with_filter(filter())) + .with(unfiltered2) + .with(filtered2.with_filter(filter())); + assert_eq!(subscriber.max_level_hint(), None); + let _subscriber = subscriber.set_default(); + + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +#[test] +fn mixed_layered() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + + let layered1 = filtered1.with_filter(filter()).and_then(unfiltered1); + let layered2 = unfiltered2.and_then(filtered2.with_filter(filter())); + + let subscriber = tracing_subscriber::registry().with(layered1).with(layered2); + assert_eq!(subscriber.max_level_hint(), None); + let _subscriber = subscriber.set_default(); + + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +fn events() { + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); +} + +fn filter<S>() -> DynFilterFn<S> { + DynFilterFn::new( + (|metadata: &Metadata<'_>, _: &tracing_subscriber::layer::Context<'_, S>| { + metadata.level() <= &Level::INFO + }) as fn(&Metadata<'_>, &Context<'_, S>) -> bool, + ) + .with_max_level_hint(Level::INFO) +} + +fn unfiltered(name: &str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(name) + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle() +} + +fn filtered(name: &str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(name) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle() +} diff --git a/vendor/tracing-subscriber/tests/layer_filter_interests_are_cached.rs b/vendor/tracing-subscriber/tests/layer_filter_interests_are_cached.rs new file mode 100644 index 000000000..d89d3bf17 --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filter_interests_are_cached.rs @@ -0,0 +1,69 @@ +#![cfg(feature = "registry")] +mod support; +use self::support::*; + +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, +}; +use tracing::{Level, Subscriber}; +use tracing_subscriber::{filter, prelude::*}; + +#[test] +fn layer_filter_interests_are_cached() { + let seen = Arc::new(Mutex::new(HashMap::new())); + let seen2 = seen.clone(); + let filter = filter::filter_fn(move |meta| { + *seen + .lock() + .unwrap() + .entry(meta.callsite()) + .or_insert(0usize) += 1; + meta.level() == &Level::INFO + }); + + let (expect, handle) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + + let subscriber = tracing_subscriber::registry().with(expect.with_filter(filter)); + assert!(subscriber.max_level_hint().is_none()); + + let _subscriber = subscriber.set_default(); + + fn events() { + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); + } + + events(); + { + let lock = seen2.lock().unwrap(); + for (callsite, &count) in lock.iter() { + assert_eq!( + count, 1, + "callsite {:?} should have been seen 1 time (after first set of events)", + callsite + ); + } + } + + events(); + { + let lock = seen2.lock().unwrap(); + for (callsite, &count) in lock.iter() { + assert_eq!( + count, 1, + "callsite {:?} should have been seen 1 time (after second set of events)", + callsite + ); + } + } + + handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/boxed.rs b/vendor/tracing-subscriber/tests/layer_filters/boxed.rs new file mode 100644 index 000000000..0fe37188e --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/boxed.rs @@ -0,0 +1,42 @@ +use super::*; +use tracing_subscriber::{filter, prelude::*, Layer}; + +fn layer() -> (ExpectLayer, subscriber::MockHandle) { + layer::mock().done().run_with_handle() +} + +fn filter<S>() -> filter::DynFilterFn<S> { + // Use dynamic filter fn to disable interest caching and max-level hints, + // allowing us to put all of these tests in the same file. + filter::dynamic_filter_fn(|_, _| false) +} + +/// reproduces https://github.com/tokio-rs/tracing/issues/1563#issuecomment-921363629 +#[test] +fn box_works() { + let (layer, handle) = layer(); + let layer = Box::new(layer.with_filter(filter())); + + let _guard = tracing_subscriber::registry().with(layer).set_default(); + + for i in 0..2 { + tracing::info!(i); + } + + handle.assert_finished(); +} + +/// the same as `box_works` but with a type-erased `Box`. +#[test] +fn dyn_box_works() { + let (layer, handle) = layer(); + let layer: Box<dyn Layer<_> + Send + Sync + 'static> = Box::new(layer.with_filter(filter())); + + let _guard = tracing_subscriber::registry().with(layer).set_default(); + + for i in 0..2 { + tracing::info!(i); + } + + handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/combinators.rs b/vendor/tracing-subscriber/tests/layer_filters/combinators.rs new file mode 100644 index 000000000..6052a2d00 --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/combinators.rs @@ -0,0 +1,42 @@ +use super::*; +use tracing_subscriber::{ + filter::{filter_fn, FilterExt, LevelFilter}, + prelude::*, +}; + +#[test] +fn and() { + let (layer, handle) = layer::mock() + .event( + event::msg("a very interesting event") + .at_level(tracing::Level::INFO) + .with_target("interesting_target"), + ) + .done() + .run_with_handle(); + + // Enables spans and events with targets starting with `interesting_target`: + let target_filter = filter::filter_fn(|meta| meta.target().starts_with("interesting_target")); + + // Enables spans and events with levels `INFO` and below: + let level_filter = LevelFilter::INFO; + + // Combine the two filters together, returning a filter that only enables + // spans and events that *both* filters will enable: + let filter = target_filter.and(level_filter); + + let _subscriber = tracing_subscriber::registry() + .with(layer.with_filter(filter)) + .set_default(); + + // This event will *not* be enabled: + tracing::info!("an event with an uninteresting target"); + + // This event *will* be enabled: + tracing::info!(target: "interesting_target", "a very interesting event"); + + // This event will *not* be enabled: + tracing::debug!(target: "interesting_target", "interesting debug event..."); + + handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/downcast_raw.rs b/vendor/tracing-subscriber/tests/layer_filters/downcast_raw.rs new file mode 100644 index 000000000..b5f7e35ce --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/downcast_raw.rs @@ -0,0 +1,68 @@ +use tracing::Subscriber; +use tracing_subscriber::filter::Targets; +use tracing_subscriber::prelude::*; +use tracing_subscriber::Layer; + +#[test] +fn downcast_ref_to_inner_layer_and_filter() { + // Test that a filtered layer gives downcast_ref access to + // both the layer and the filter. + + struct WrappedLayer; + + impl<S> Layer<S> for WrappedLayer + where + S: Subscriber, + S: for<'lookup> tracing_subscriber::registry::LookupSpan<'lookup>, + { + } + + let layer = WrappedLayer; + let filter = Targets::new().with_default(tracing::Level::INFO); + let registry = tracing_subscriber::registry().with(layer.with_filter(filter)); + let dispatch = tracing::dispatcher::Dispatch::new(registry); + + // The filter is available + assert!(dispatch.downcast_ref::<Targets>().is_some()); + // The wrapped layer is available + assert!(dispatch.downcast_ref::<WrappedLayer>().is_some()); +} + +#[test] +fn forward_downcast_raw_to_layer() { + // Test that a filtered layer still gives its wrapped layer a chance to + // return a custom struct from downcast_raw. + // https://github.com/tokio-rs/tracing/issues/1618 + + struct WrappedLayer { + with_context: WithContext, + } + + struct WithContext; + + impl<S> Layer<S> for WrappedLayer + where + S: Subscriber, + S: for<'lookup> tracing_subscriber::registry::LookupSpan<'lookup>, + { + unsafe fn downcast_raw(&self, id: std::any::TypeId) -> Option<*const ()> { + match id { + id if id == std::any::TypeId::of::<Self>() => Some(self as *const _ as *const ()), + id if id == std::any::TypeId::of::<WithContext>() => { + Some(&self.with_context as *const _ as *const ()) + } + _ => None, + } + } + } + + let layer = WrappedLayer { + with_context: WithContext, + }; + let filter = Targets::new().with_default(tracing::Level::INFO); + let registry = tracing_subscriber::registry().with(layer.with_filter(filter)); + let dispatch = tracing::dispatcher::Dispatch::new(registry); + + // Types from a custom implementation of `downcast_raw` are available + assert!(dispatch.downcast_ref::<WithContext>().is_some()); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/filter_scopes.rs b/vendor/tracing-subscriber/tests/layer_filters/filter_scopes.rs new file mode 100644 index 000000000..7fd7d843b --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/filter_scopes.rs @@ -0,0 +1,131 @@ +use super::*; + +#[test] +fn filters_span_scopes() { + let (debug_layer, debug_handle) = layer::named("debug") + .enter(span::mock().at_level(Level::DEBUG)) + .enter(span::mock().at_level(Level::INFO)) + .enter(span::mock().at_level(Level::WARN)) + .enter(span::mock().at_level(Level::ERROR)) + .event(event::msg("hello world").in_scope(vec![ + span::mock().at_level(Level::ERROR), + span::mock().at_level(Level::WARN), + span::mock().at_level(Level::INFO), + span::mock().at_level(Level::DEBUG), + ])) + .exit(span::mock().at_level(Level::ERROR)) + .exit(span::mock().at_level(Level::WARN)) + .exit(span::mock().at_level(Level::INFO)) + .exit(span::mock().at_level(Level::DEBUG)) + .done() + .run_with_handle(); + let (info_layer, info_handle) = layer::named("info") + .enter(span::mock().at_level(Level::INFO)) + .enter(span::mock().at_level(Level::WARN)) + .enter(span::mock().at_level(Level::ERROR)) + .event(event::msg("hello world").in_scope(vec![ + span::mock().at_level(Level::ERROR), + span::mock().at_level(Level::WARN), + span::mock().at_level(Level::INFO), + ])) + .exit(span::mock().at_level(Level::ERROR)) + .exit(span::mock().at_level(Level::WARN)) + .exit(span::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + let (warn_layer, warn_handle) = layer::named("warn") + .enter(span::mock().at_level(Level::WARN)) + .enter(span::mock().at_level(Level::ERROR)) + .event(event::msg("hello world").in_scope(vec![ + span::mock().at_level(Level::ERROR), + span::mock().at_level(Level::WARN), + ])) + .exit(span::mock().at_level(Level::ERROR)) + .exit(span::mock().at_level(Level::WARN)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(debug_layer.with_filter(LevelFilter::DEBUG)) + .with(info_layer.with_filter(LevelFilter::INFO)) + .with(warn_layer.with_filter(LevelFilter::WARN)) + .set_default(); + + { + let _trace = tracing::trace_span!("my_span").entered(); + let _debug = tracing::debug_span!("my_span").entered(); + let _info = tracing::info_span!("my_span").entered(); + let _warn = tracing::warn_span!("my_span").entered(); + let _error = tracing::error_span!("my_span").entered(); + tracing::error!("hello world"); + } + + debug_handle.assert_finished(); + info_handle.assert_finished(); + warn_handle.assert_finished(); +} + +#[test] +fn filters_interleaved_span_scopes() { + fn target_layer(target: &'static str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(format!("target_{}", target)) + .enter(span::mock().with_target(target)) + .enter(span::mock().with_target(target)) + .event(event::msg("hello world").in_scope(vec![ + span::mock().with_target(target), + span::mock().with_target(target), + ])) + .event( + event::msg("hello to my target") + .in_scope(vec![ + span::mock().with_target(target), + span::mock().with_target(target), + ]) + .with_target(target), + ) + .exit(span::mock().with_target(target)) + .exit(span::mock().with_target(target)) + .done() + .run_with_handle() + } + + let (a_layer, a_handle) = target_layer("a"); + let (b_layer, b_handle) = target_layer("b"); + let (all_layer, all_handle) = layer::named("all") + .enter(span::mock().with_target("b")) + .enter(span::mock().with_target("a")) + .event(event::msg("hello world").in_scope(vec![ + span::mock().with_target("a"), + span::mock().with_target("b"), + ])) + .exit(span::mock().with_target("a")) + .exit(span::mock().with_target("b")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(all_layer.with_filter(LevelFilter::INFO)) + .with(a_layer.with_filter(filter::filter_fn(|meta| { + let target = meta.target(); + target == "a" || target == module_path!() + }))) + .with(b_layer.with_filter(filter::filter_fn(|meta| { + let target = meta.target(); + target == "b" || target == module_path!() + }))) + .set_default(); + + { + let _a1 = tracing::trace_span!(target: "a", "a/trace").entered(); + let _b1 = tracing::info_span!(target: "b", "b/info").entered(); + let _a2 = tracing::info_span!(target: "a", "a/info").entered(); + let _b2 = tracing::trace_span!(target: "b", "b/trace").entered(); + tracing::info!("hello world"); + tracing::debug!(target: "a", "hello to my target"); + tracing::debug!(target: "b", "hello to my target"); + } + + a_handle.assert_finished(); + b_handle.assert_finished(); + all_handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/main.rs b/vendor/tracing-subscriber/tests/layer_filters/main.rs new file mode 100644 index 000000000..0233f063b --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/main.rs @@ -0,0 +1,189 @@ +#![cfg(feature = "registry")] +#[path = "../support.rs"] +mod support; +use self::support::*; +mod boxed; +mod downcast_raw; +mod filter_scopes; +mod targets; +mod trees; +mod vec; + +use tracing::{level_filters::LevelFilter, Level}; +use tracing_subscriber::{filter, prelude::*, Layer}; + +#[test] +fn basic_layer_filters() { + let (trace_layer, trace_handle) = layer::named("trace") + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + + let (debug_layer, debug_handle) = layer::named("debug") + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + + let (info_layer, info_handle) = layer::named("info") + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(trace_layer.with_filter(LevelFilter::TRACE)) + .with(debug_layer.with_filter(LevelFilter::DEBUG)) + .with(info_layer.with_filter(LevelFilter::INFO)) + .set_default(); + + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + + trace_handle.assert_finished(); + debug_handle.assert_finished(); + info_handle.assert_finished(); +} + +#[test] +fn basic_layer_filters_spans() { + let (trace_layer, trace_handle) = layer::named("trace") + .new_span(span::mock().at_level(Level::TRACE)) + .new_span(span::mock().at_level(Level::DEBUG)) + .new_span(span::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + + let (debug_layer, debug_handle) = layer::named("debug") + .new_span(span::mock().at_level(Level::DEBUG)) + .new_span(span::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + + let (info_layer, info_handle) = layer::named("info") + .new_span(span::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(trace_layer.with_filter(LevelFilter::TRACE)) + .with(debug_layer.with_filter(LevelFilter::DEBUG)) + .with(info_layer.with_filter(LevelFilter::INFO)) + .set_default(); + + tracing::trace_span!("hello trace"); + tracing::debug_span!("hello debug"); + tracing::info_span!("hello info"); + + trace_handle.assert_finished(); + debug_handle.assert_finished(); + info_handle.assert_finished(); +} + +#[test] +fn global_filters_layers_still_work() { + let (expect, handle) = layer::mock() + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(expect) + .with(LevelFilter::INFO) + .set_default(); + + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); + + handle.assert_finished(); +} + +#[test] +fn global_filter_interests_are_cached() { + let (expect, handle) = layer::mock() + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(expect.with_filter(filter::filter_fn(|meta| { + assert!( + meta.level() <= &Level::INFO, + "enabled should not be called for callsites disabled by the global filter" + ); + meta.level() <= &Level::WARN + }))) + .with(LevelFilter::INFO) + .set_default(); + + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); + + handle.assert_finished(); +} + +#[test] +fn global_filters_affect_layer_filters() { + let (expect, handle) = layer::named("debug") + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(expect.with_filter(LevelFilter::DEBUG)) + .with(LevelFilter::INFO) + .set_default(); + + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); + + handle.assert_finished(); +} + +#[test] +fn filter_fn() { + let (all, all_handle) = layer::named("all_targets") + .event(event::msg("hello foo")) + .event(event::msg("hello bar")) + .done() + .run_with_handle(); + + let (foo, foo_handle) = layer::named("foo_target") + .event(event::msg("hello foo")) + .done() + .run_with_handle(); + + let (bar, bar_handle) = layer::named("bar_target") + .event(event::msg("hello bar")) + .done() + .run_with_handle(); + + let _subscriber = tracing_subscriber::registry() + .with(all) + .with(foo.with_filter(filter::filter_fn(|meta| meta.target().starts_with("foo")))) + .with(bar.with_filter(filter::filter_fn(|meta| meta.target().starts_with("bar")))) + .set_default(); + + tracing::trace!(target: "foo", "hello foo"); + tracing::trace!(target: "bar", "hello bar"); + + foo_handle.assert_finished(); + bar_handle.assert_finished(); + all_handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/targets.rs b/vendor/tracing-subscriber/tests/layer_filters/targets.rs new file mode 100644 index 000000000..c8133044b --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/targets.rs @@ -0,0 +1,59 @@ +use super::*; +use tracing_subscriber::{ + filter::{filter_fn, Targets}, + prelude::*, +}; + +#[test] +#[cfg_attr(not(feature = "tracing-log"), ignore)] +fn log_events() { + // Reproduces https://github.com/tokio-rs/tracing/issues/1563 + mod inner { + pub(super) const MODULE_PATH: &str = module_path!(); + + #[tracing::instrument] + pub(super) fn logs() { + log::debug!("inner"); + } + } + + let filter = Targets::new() + .with_default(LevelFilter::DEBUG) + .with_target(inner::MODULE_PATH, LevelFilter::WARN); + + let layer = + tracing_subscriber::layer::Identity::new().with_filter(filter_fn(move |_meta| true)); + + let _guard = tracing_subscriber::registry() + .with(filter) + .with(layer) + .set_default(); + + inner::logs(); +} + +#[test] +fn inner_layer_short_circuits() { + // This test ensures that when a global filter short-circuits `Interest` + // evaluation, we aren't left with a "dirty" per-layer filter state. + + let (layer, handle) = layer::mock() + .event(event::msg("hello world")) + .done() + .run_with_handle(); + + let filter = Targets::new().with_target("magic_target", LevelFilter::DEBUG); + + let _guard = tracing_subscriber::registry() + // Note: we don't just use a `LevelFilter` for the global filter here, + // because it will just return a max level filter, and the chain of + // `register_callsite` calls that would trigger the bug never happens... + .with(filter::filter_fn(|meta| meta.level() <= &Level::INFO)) + .with(layer.with_filter(filter)) + .set_default(); + + tracing::debug!("skip me please!"); + tracing::info!(target: "magic_target", "hello world"); + + handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/trees.rs b/vendor/tracing-subscriber/tests/layer_filters/trees.rs new file mode 100644 index 000000000..18cdd8ccc --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/trees.rs @@ -0,0 +1,146 @@ +use super::*; + +#[test] +fn basic_trees() { + let (with_target, with_target_handle) = layer::named("info_with_target") + .event(event::mock().at_level(Level::INFO).with_target("my_target")) + .done() + .run_with_handle(); + + let (info, info_handle) = layer::named("info") + .event( + event::mock() + .at_level(Level::INFO) + .with_target(module_path!()), + ) + .event(event::mock().at_level(Level::INFO).with_target("my_target")) + .done() + .run_with_handle(); + + let (all, all_handle) = layer::named("all") + .event( + event::mock() + .at_level(Level::INFO) + .with_target(module_path!()), + ) + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::INFO).with_target("my_target")) + .event( + event::mock() + .at_level(Level::TRACE) + .with_target("my_target"), + ) + .done() + .run_with_handle(); + + let info_tree = info + .and_then( + with_target.with_filter(filter::filter_fn(|meta| dbg!(meta.target()) == "my_target")), + ) + .with_filter(LevelFilter::INFO); + + let subscriber = tracing_subscriber::registry().with(info_tree).with(all); + let _guard = dbg!(subscriber).set_default(); + + tracing::info!("hello world"); + tracing::trace!("hello trace"); + tracing::info!(target: "my_target", "hi to my target"); + tracing::trace!(target: "my_target", "hi to my target at trace"); + + all_handle.assert_finished(); + info_handle.assert_finished(); + with_target_handle.assert_finished(); +} + +#[test] +fn filter_span_scopes() { + fn target_layer(target: &'static str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(format!("target_{}", target)) + .enter(span::mock().with_target(target).at_level(Level::INFO)) + .event( + event::msg("hello world") + .in_scope(vec![span::mock().with_target(target).at_level(Level::INFO)]), + ) + .exit(span::mock().with_target(target).at_level(Level::INFO)) + .done() + .run_with_handle() + } + + let (a_layer, a_handle) = target_layer("a"); + let (b_layer, b_handle) = target_layer("b"); + let (info_layer, info_handle) = layer::named("info") + .enter(span::mock().with_target("b").at_level(Level::INFO)) + .enter(span::mock().with_target("a").at_level(Level::INFO)) + .event(event::msg("hello world").in_scope(vec![ + span::mock().with_target("a").at_level(Level::INFO), + span::mock().with_target("b").at_level(Level::INFO), + ])) + .exit(span::mock().with_target("a").at_level(Level::INFO)) + .exit(span::mock().with_target("b").at_level(Level::INFO)) + .done() + .run_with_handle(); + + let full_scope = vec![ + span::mock().with_target("b").at_level(Level::TRACE), + span::mock().with_target("a").at_level(Level::INFO), + span::mock().with_target("b").at_level(Level::INFO), + span::mock().with_target("a").at_level(Level::TRACE), + ]; + let (all_layer, all_handle) = layer::named("all") + .enter(span::mock().with_target("a").at_level(Level::TRACE)) + .enter(span::mock().with_target("b").at_level(Level::INFO)) + .enter(span::mock().with_target("a").at_level(Level::INFO)) + .enter(span::mock().with_target("b").at_level(Level::TRACE)) + .event(event::msg("hello world").in_scope(full_scope.clone())) + .event( + event::msg("hello to my target") + .with_target("a") + .in_scope(full_scope.clone()), + ) + .event( + event::msg("hello to my target") + .with_target("b") + .in_scope(full_scope), + ) + .exit(span::mock().with_target("b").at_level(Level::TRACE)) + .exit(span::mock().with_target("a").at_level(Level::INFO)) + .exit(span::mock().with_target("b").at_level(Level::INFO)) + .exit(span::mock().with_target("a").at_level(Level::TRACE)) + .done() + .run_with_handle(); + + let a_layer = a_layer.with_filter(filter::filter_fn(|meta| { + let target = meta.target(); + target == "a" || target == module_path!() + })); + + let b_layer = b_layer.with_filter(filter::filter_fn(|meta| { + let target = meta.target(); + target == "b" || target == module_path!() + })); + + let info_tree = info_layer + .and_then(a_layer) + .and_then(b_layer) + .with_filter(LevelFilter::INFO); + + let subscriber = tracing_subscriber::registry() + .with(info_tree) + .with(all_layer); + let _guard = dbg!(subscriber).set_default(); + + { + let _a1 = tracing::trace_span!(target: "a", "a/trace").entered(); + let _b1 = tracing::info_span!(target: "b", "b/info").entered(); + let _a2 = tracing::info_span!(target: "a", "a/info").entered(); + let _b2 = tracing::trace_span!(target: "b", "b/trace").entered(); + tracing::info!("hello world"); + tracing::debug!(target: "a", "hello to my target"); + tracing::debug!(target: "b", "hello to my target"); + } + + all_handle.assert_finished(); + info_handle.assert_finished(); + a_handle.assert_finished(); + b_handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/layer_filters/vec.rs b/vendor/tracing-subscriber/tests/layer_filters/vec.rs new file mode 100644 index 000000000..87244e4ab --- /dev/null +++ b/vendor/tracing-subscriber/tests/layer_filters/vec.rs @@ -0,0 +1,120 @@ +use super::*; +use tracing::Subscriber; + +#[test] +fn with_filters_unboxed() { + let (trace_layer, trace_handle) = layer::named("trace") + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + let trace_layer = trace_layer.with_filter(LevelFilter::TRACE); + + let (debug_layer, debug_handle) = layer::named("debug") + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG); + + let (info_layer, info_handle) = layer::named("info") + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + let info_layer = info_layer.with_filter(LevelFilter::INFO); + + let _subscriber = tracing_subscriber::registry() + .with(vec![trace_layer, debug_layer, info_layer]) + .set_default(); + + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + + trace_handle.assert_finished(); + debug_handle.assert_finished(); + info_handle.assert_finished(); +} + +#[test] +fn with_filters_boxed() { + let (unfiltered_layer, unfiltered_handle) = layer::named("unfiltered") + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + let unfiltered_layer = unfiltered_layer.boxed(); + + let (debug_layer, debug_handle) = layer::named("debug") + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG).boxed(); + + let (target_layer, target_handle) = layer::named("target") + .event(event::mock().at_level(Level::INFO)) + .done() + .run_with_handle(); + let target_layer = target_layer + .with_filter(filter::filter_fn(|meta| meta.target() == "my_target")) + .boxed(); + + let _subscriber = tracing_subscriber::registry() + .with(vec![unfiltered_layer, debug_layer, target_layer]) + .set_default(); + + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!(target: "my_target", "hello my target"); + + unfiltered_handle.assert_finished(); + debug_handle.assert_finished(); + target_handle.assert_finished(); +} + +#[test] +fn mixed_max_level_hint() { + let unfiltered = layer::named("unfiltered").run().boxed(); + let info = layer::named("info") + .run() + .with_filter(LevelFilter::INFO) + .boxed(); + let debug = layer::named("debug") + .run() + .with_filter(LevelFilter::DEBUG) + .boxed(); + + let subscriber = tracing_subscriber::registry().with(vec![unfiltered, info, debug]); + + assert_eq!(subscriber.max_level_hint(), None); +} + +#[test] +fn all_filtered_max_level_hint() { + let warn = layer::named("warn") + .run() + .with_filter(LevelFilter::WARN) + .boxed(); + let info = layer::named("info") + .run() + .with_filter(LevelFilter::INFO) + .boxed(); + let debug = layer::named("debug") + .run() + .with_filter(LevelFilter::DEBUG) + .boxed(); + + let subscriber = tracing_subscriber::registry().with(vec![warn, info, debug]); + + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG)); +} + +#[test] +fn empty_vec() { + // Just a None means everything is off + let subscriber = tracing_subscriber::registry().with(Vec::<ExpectLayer>::new()); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF)); +} diff --git a/vendor/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs b/vendor/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs new file mode 100644 index 000000000..5c25e7f03 --- /dev/null +++ b/vendor/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs @@ -0,0 +1,131 @@ +#![cfg(feature = "registry")] +mod support; +use self::support::*; + +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, +}; +use tracing::{Level, Subscriber}; +use tracing_subscriber::{filter, prelude::*}; + +#[test] +fn multiple_layer_filter_interests_are_cached() { + // This layer will return Interest::always for INFO and lower. + let seen_info = Arc::new(Mutex::new(HashMap::new())); + let seen_info2 = seen_info.clone(); + let filter = filter::filter_fn(move |meta| { + *seen_info + .lock() + .unwrap() + .entry(*meta.level()) + .or_insert(0usize) += 1; + meta.level() <= &Level::INFO + }); + let seen_info = seen_info2; + + let (info_layer, info_handle) = layer::named("info") + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + let info_layer = info_layer.with_filter(filter); + + // This layer will return Interest::always for WARN and lower. + let seen_warn = Arc::new(Mutex::new(HashMap::new())); + let seen_warn2 = seen_warn.clone(); + let filter = filter::filter_fn(move |meta| { + *seen_warn + .lock() + .unwrap() + .entry(*meta.level()) + .or_insert(0usize) += 1; + meta.level() <= &Level::WARN + }); + let seen_warn = seen_warn2; + + let (warn_layer, warn_handle) = layer::named("warn") + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + let warn_layer = warn_layer.with_filter(filter); + + let subscriber = tracing_subscriber::registry() + .with(warn_layer) + .with(info_layer); + assert!(subscriber.max_level_hint().is_none()); + + let _subscriber = subscriber.set_default(); + + fn events() { + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); + } + + events(); + { + let lock = seen_info.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the INFO layer (after first set of events)", + level + ); + } + + let lock = seen_warn.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the WARN layer (after first set of events)", + level + ); + } + } + + events(); + { + let lock = seen_info.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the INFO layer (after second set of events)", + level + ); + } + + let lock = seen_warn.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the WARN layer (after second set of events)", + level + ); + } + } + + info_handle.assert_finished(); + warn_handle.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/option.rs b/vendor/tracing-subscriber/tests/option.rs new file mode 100644 index 000000000..738cc0a6c --- /dev/null +++ b/vendor/tracing-subscriber/tests/option.rs @@ -0,0 +1,41 @@ +#![cfg(feature = "registry")] +use tracing::level_filters::LevelFilter; +use tracing::Subscriber; +use tracing_subscriber::prelude::*; + +// This test is just used to compare to the tests below +#[test] +fn just_layer() { + let subscriber = tracing_subscriber::registry().with(LevelFilter::INFO); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO)); +} + +#[test] +fn subscriber_and_option_some_layer() { + let subscriber = tracing_subscriber::registry() + .with(LevelFilter::INFO) + .with(Some(LevelFilter::DEBUG)); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG)); +} + +#[test] +fn subscriber_and_option_none_layer() { + // None means the other layer takes control + let subscriber = tracing_subscriber::registry() + .with(LevelFilter::ERROR) + .with(None::<LevelFilter>); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::ERROR)); +} + +#[test] +fn just_option_some_layer() { + // Just a None means everything is off + let subscriber = tracing_subscriber::registry().with(None::<LevelFilter>); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF)); +} + +#[test] +fn just_option_none_layer() { + let subscriber = tracing_subscriber::registry().with(Some(LevelFilter::ERROR)); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::ERROR)); +} diff --git a/vendor/tracing-subscriber/tests/registry_max_level_hint.rs b/vendor/tracing-subscriber/tests/registry_max_level_hint.rs new file mode 100644 index 000000000..f94c8a1fb --- /dev/null +++ b/vendor/tracing-subscriber/tests/registry_max_level_hint.rs @@ -0,0 +1,11 @@ +#![cfg(all(feature = "registry", feature = "fmt"))] +use tracing_subscriber::{filter::LevelFilter, prelude::*}; + +#[test] +fn registry_sets_max_level_hint() { + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer()) + .with(LevelFilter::DEBUG) + .init(); + assert_eq!(LevelFilter::current(), LevelFilter::DEBUG); +} diff --git a/vendor/tracing-subscriber/tests/registry_with_subscriber.rs b/vendor/tracing-subscriber/tests/registry_with_subscriber.rs new file mode 100644 index 000000000..50d2f551d --- /dev/null +++ b/vendor/tracing-subscriber/tests/registry_with_subscriber.rs @@ -0,0 +1,25 @@ +#![cfg(feature = "registry")] +use tracing_futures::{Instrument, WithSubscriber}; +use tracing_subscriber::prelude::*; + +#[tokio::test] +async fn future_with_subscriber() { + tracing_subscriber::registry().init(); + let span = tracing::info_span!("foo"); + let _e = span.enter(); + let span = tracing::info_span!("bar"); + let _e = span.enter(); + tokio::spawn( + async { + async { + let span = tracing::Span::current(); + println!("{:?}", span); + } + .instrument(tracing::info_span!("hi")) + .await + } + .with_subscriber(tracing_subscriber::registry()), + ) + .await + .unwrap(); +} diff --git a/vendor/tracing-subscriber/tests/reload.rs b/vendor/tracing-subscriber/tests/reload.rs new file mode 100644 index 000000000..b8b6c2b46 --- /dev/null +++ b/vendor/tracing-subscriber/tests/reload.rs @@ -0,0 +1,141 @@ +#![cfg(feature = "std")] +use std::sync::atomic::{AtomicUsize, Ordering}; +use tracing_core::{ + span::{Attributes, Id, Record}, + subscriber::Interest, + Event, Metadata, Subscriber, +}; +use tracing_subscriber::{layer, prelude::*, reload::*}; + +pub struct NopSubscriber; + +impl Subscriber for NopSubscriber { + fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest { + Interest::never() + } + + fn enabled(&self, _: &Metadata<'_>) -> bool { + false + } + + fn new_span(&self, _: &Attributes<'_>) -> Id { + Id::from_u64(1) + } + + fn record(&self, _: &Id, _: &Record<'_>) {} + fn record_follows_from(&self, _: &Id, _: &Id) {} + fn event(&self, _: &Event<'_>) {} + fn enter(&self, _: &Id) {} + fn exit(&self, _: &Id) {} +} + +#[test] +fn reload_handle() { + static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0); + static FILTER2_CALLS: AtomicUsize = AtomicUsize::new(0); + + enum Filter { + One, + Two, + } + + impl<S: Subscriber> tracing_subscriber::Layer<S> for Filter { + fn register_callsite(&self, m: &Metadata<'_>) -> Interest { + println!("REGISTER: {:?}", m); + Interest::sometimes() + } + + fn enabled(&self, m: &Metadata<'_>, _: layer::Context<'_, S>) -> bool { + println!("ENABLED: {:?}", m); + match self { + Filter::One => FILTER1_CALLS.fetch_add(1, Ordering::SeqCst), + Filter::Two => FILTER2_CALLS.fetch_add(1, Ordering::SeqCst), + }; + true + } + } + fn event() { + tracing::trace!("my event"); + } + + let (layer, handle) = Layer::new(Filter::One); + + let subscriber = tracing_core::dispatcher::Dispatch::new(layer.with_subscriber(NopSubscriber)); + + tracing_core::dispatcher::with_default(&subscriber, || { + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 0); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0); + + event(); + + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0); + + handle.reload(Filter::Two).expect("should reload"); + + event(); + + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 1); + }) +} + +#[test] +#[cfg(feature = "registry")] +fn reload_filter() { + struct NopLayer; + impl<S: Subscriber> tracing_subscriber::Layer<S> for NopLayer { + fn register_callsite(&self, _m: &Metadata<'_>) -> Interest { + Interest::sometimes() + } + + fn enabled(&self, _m: &Metadata<'_>, _: layer::Context<'_, S>) -> bool { + true + } + } + + static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0); + static FILTER2_CALLS: AtomicUsize = AtomicUsize::new(0); + + enum Filter { + One, + Two, + } + + impl<S: Subscriber> tracing_subscriber::layer::Filter<S> for Filter { + fn enabled(&self, m: &Metadata<'_>, _: &layer::Context<'_, S>) -> bool { + println!("ENABLED: {:?}", m); + match self { + Filter::One => FILTER1_CALLS.fetch_add(1, Ordering::SeqCst), + Filter::Two => FILTER2_CALLS.fetch_add(1, Ordering::SeqCst), + }; + true + } + } + fn event() { + tracing::trace!("my event"); + } + + let (filter, handle) = Layer::new(Filter::One); + + let dispatcher = tracing_core::Dispatch::new( + tracing_subscriber::registry().with(NopLayer.with_filter(filter)), + ); + + tracing_core::dispatcher::with_default(&dispatcher, || { + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 0); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0); + + event(); + + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0); + + handle.reload(Filter::Two).expect("should reload"); + + event(); + + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 1); + }) +} diff --git a/vendor/tracing-subscriber/tests/same_len_filters.rs b/vendor/tracing-subscriber/tests/same_len_filters.rs new file mode 100644 index 000000000..879e578d7 --- /dev/null +++ b/vendor/tracing-subscriber/tests/same_len_filters.rs @@ -0,0 +1,81 @@ +// These tests include field filters with no targets, so they have to go in a +// separate file. +#![cfg(feature = "env-filter")] + +use tracing::{self, subscriber::with_default, Level}; +use tracing_mock::*; +use tracing_subscriber::{filter::EnvFilter, prelude::*}; + +#[test] +fn same_length_targets() { + let filter: EnvFilter = "foo=trace,bar=trace".parse().expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::TRACE)) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + + with_default(subscriber, || { + tracing::trace!(target: "foo", "foo"); + tracing::trace!(target: "bar", "bar"); + }); + + finished.assert_finished(); +} + +#[test] +fn same_num_fields_event() { + let filter: EnvFilter = "[{foo}]=trace,[{bar}]=trace" + .parse() + .expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .event( + event::mock() + .at_level(Level::TRACE) + .with_fields(field::mock("foo")), + ) + .event( + event::mock() + .at_level(Level::TRACE) + .with_fields(field::mock("bar")), + ) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + with_default(subscriber, || { + tracing::trace!(foo = 1); + tracing::trace!(bar = 3); + }); + + finished.assert_finished(); +} + +#[test] +fn same_num_fields_and_name_len() { + let filter: EnvFilter = "[foo{bar=1}]=trace,[baz{boz=1}]=trace" + .parse() + .expect("filter should parse"); + let (subscriber, finished) = subscriber::mock() + .new_span( + span::mock() + .named("foo") + .at_level(Level::TRACE) + .with_field(field::mock("bar")), + ) + .new_span( + span::mock() + .named("baz") + .at_level(Level::TRACE) + .with_field(field::mock("boz")), + ) + .done() + .run_with_handle(); + let subscriber = subscriber.with(filter); + with_default(subscriber, || { + tracing::trace_span!("foo", bar = 1); + tracing::trace_span!("baz", boz = 1); + }); + + finished.assert_finished(); +} diff --git a/vendor/tracing-subscriber/tests/support.rs b/vendor/tracing-subscriber/tests/support.rs new file mode 100644 index 000000000..50e0e6669 --- /dev/null +++ b/vendor/tracing-subscriber/tests/support.rs @@ -0,0 +1,407 @@ +#![allow(missing_docs, dead_code)] +pub use tracing_mock::{event, field, span, subscriber}; + +use tracing_core::{ + span::{Attributes, Id, Record}, + Event, Subscriber, +}; +use tracing_mock::{ + event::MockEvent, + span::{MockSpan, NewSpan}, + subscriber::{Expect, MockHandle}, +}; +use tracing_subscriber::{ + layer::{Context, Layer}, + registry::{LookupSpan, SpanRef}, +}; + +use std::{ + collections::VecDeque, + fmt, + sync::{Arc, Mutex}, +}; + +pub mod layer { + use super::ExpectLayerBuilder; + + pub fn mock() -> ExpectLayerBuilder { + ExpectLayerBuilder { + expected: Default::default(), + name: std::thread::current() + .name() + .map(String::from) + .unwrap_or_default(), + } + } + + pub fn named(name: impl std::fmt::Display) -> ExpectLayerBuilder { + mock().named(name) + } +} + +pub struct ExpectLayerBuilder { + expected: VecDeque<Expect>, + name: String, +} + +pub struct ExpectLayer { + expected: Arc<Mutex<VecDeque<Expect>>>, + current: Mutex<Vec<Id>>, + name: String, +} + +impl ExpectLayerBuilder { + /// Overrides the name printed by the mock subscriber's debugging output. + /// + /// The debugging output is displayed if the test panics, or if the test is + /// run with `--nocapture`. + /// + /// By default, the mock subscriber's name is the name of the test + /// (*technically*, the name of the thread where it was created, which is + /// the name of the test unless tests are run with `--test-threads=1`). + /// When a test has only one mock subscriber, this is sufficient. However, + /// some tests may include multiple subscribers, in order to test + /// interactions between multiple subscribers. In that case, it can be + /// helpful to give each subscriber a separate name to distinguish where the + /// debugging output comes from. + pub fn named(mut self, name: impl fmt::Display) -> Self { + use std::fmt::Write; + if !self.name.is_empty() { + write!(&mut self.name, "::{}", name).unwrap(); + } else { + self.name = name.to_string(); + } + self + } + + pub fn enter(mut self, span: MockSpan) -> Self { + self.expected.push_back(Expect::Enter(span)); + self + } + + pub fn event(mut self, event: MockEvent) -> Self { + self.expected.push_back(Expect::Event(event)); + self + } + + pub fn exit(mut self, span: MockSpan) -> Self { + self.expected.push_back(Expect::Exit(span)); + self + } + + pub fn done(mut self) -> Self { + self.expected.push_back(Expect::Nothing); + self + } + + pub fn record<I>(mut self, span: MockSpan, fields: I) -> Self + where + I: Into<field::Expect>, + { + self.expected.push_back(Expect::Visit(span, fields.into())); + self + } + + pub fn new_span<I>(mut self, new_span: I) -> Self + where + I: Into<NewSpan>, + { + self.expected.push_back(Expect::NewSpan(new_span.into())); + self + } + + pub fn run(self) -> ExpectLayer { + ExpectLayer { + expected: Arc::new(Mutex::new(self.expected)), + name: self.name, + current: Mutex::new(Vec::new()), + } + } + + pub fn run_with_handle(self) -> (ExpectLayer, MockHandle) { + let expected = Arc::new(Mutex::new(self.expected)); + let handle = MockHandle::new(expected.clone(), self.name.clone()); + let layer = ExpectLayer { + expected, + name: self.name, + current: Mutex::new(Vec::new()), + }; + (layer, handle) + } +} + +impl ExpectLayer { + fn check_span_ref<'spans, S>( + &self, + expected: &MockSpan, + actual: &SpanRef<'spans, S>, + what_happened: impl fmt::Display, + ) where + S: LookupSpan<'spans>, + { + if let Some(exp_name) = expected.name() { + assert_eq!( + actual.name(), + exp_name, + "\n[{}] expected {} a span named {:?}\n\ + [{}] but it was named {:?} instead (span {} {:?})", + self.name, + what_happened, + exp_name, + self.name, + actual.name(), + actual.name(), + actual.id() + ); + } + + if let Some(exp_level) = expected.level() { + let actual_level = actual.metadata().level(); + assert_eq!( + actual_level, + &exp_level, + "\n[{}] expected {} a span at {:?}\n\ + [{}] but it was at {:?} instead (span {} {:?})", + self.name, + what_happened, + exp_level, + self.name, + actual_level, + actual.name(), + actual.id(), + ); + } + + if let Some(exp_target) = expected.target() { + let actual_target = actual.metadata().target(); + assert_eq!( + actual_target, + exp_target, + "\n[{}] expected {} a span with target {:?}\n\ + [{}] but it had the target {:?} instead (span {} {:?})", + self.name, + what_happened, + exp_target, + self.name, + actual_target, + actual.name(), + actual.id(), + ); + } + } +} + +impl<S> Layer<S> for ExpectLayer +where + S: Subscriber + for<'a> LookupSpan<'a>, +{ + fn register_callsite( + &self, + metadata: &'static tracing::Metadata<'static>, + ) -> tracing_core::Interest { + println!("[{}] register_callsite {:#?}", self.name, metadata); + tracing_core::Interest::always() + } + + fn on_record(&self, _: &Id, _: &Record<'_>, _: Context<'_, S>) { + unimplemented!( + "so far, we don't have any tests that need an `on_record` \ + implementation.\nif you just wrote one that does, feel free to \ + implement it!" + ); + } + + fn on_event(&self, event: &Event<'_>, cx: Context<'_, S>) { + let name = event.metadata().name(); + println!( + "[{}] event: {}; level: {}; target: {}", + self.name, + name, + event.metadata().level(), + event.metadata().target(), + ); + match self.expected.lock().unwrap().pop_front() { + None => {} + Some(Expect::Event(mut expected)) => { + let get_parent_name = || cx.event_span(event).map(|span| span.name().to_string()); + expected.check(event, get_parent_name, &self.name); + let mut current_scope = cx.event_scope(event).into_iter().flatten(); + let expected_scope = expected.scope_mut(); + let mut i = 0; + for (expected, actual) in expected_scope.iter_mut().zip(&mut current_scope) { + println!( + "[{}] event_scope[{}] actual={} ({:?}); expected={}", + self.name, + i, + actual.name(), + actual.id(), + expected + ); + self.check_span_ref( + expected, + &actual, + format_args!("the {}th span in the event's scope to be", i), + ); + i += 1; + } + let remaining_expected = &expected_scope[i..]; + assert!( + remaining_expected.is_empty(), + "\n[{}] did not observe all expected spans in event scope!\n[{}] missing: {:#?}", + self.name, + self.name, + remaining_expected, + ); + assert!( + current_scope.next().is_none(), + "\n[{}] did not expect all spans in the actual event scope!", + self.name, + ); + } + Some(ex) => ex.bad(&self.name, format_args!("observed event {:#?}", event)), + } + } + + fn on_follows_from(&self, _span: &Id, _follows: &Id, _: Context<'_, S>) { + // TODO: it should be possible to expect spans to follow from other spans + } + + fn on_new_span(&self, span: &Attributes<'_>, id: &Id, cx: Context<'_, S>) { + let meta = span.metadata(); + println!( + "[{}] new_span: name={:?}; target={:?}; id={:?};", + self.name, + meta.name(), + meta.target(), + id + ); + let mut expected = self.expected.lock().unwrap(); + let was_expected = matches!(expected.front(), Some(Expect::NewSpan(_))); + if was_expected { + if let Expect::NewSpan(mut expected) = expected.pop_front().unwrap() { + let get_parent_name = || { + span.parent() + .and_then(|id| cx.span(id)) + .or_else(|| cx.lookup_current()) + .map(|span| span.name().to_string()) + }; + expected.check(span, get_parent_name, &self.name); + } + } + } + + fn on_enter(&self, id: &Id, cx: Context<'_, S>) { + let span = cx + .span(id) + .unwrap_or_else(|| panic!("[{}] no span for ID {:?}", self.name, id)); + println!("[{}] enter: {}; id={:?};", self.name, span.name(), id); + match self.expected.lock().unwrap().pop_front() { + None => {} + Some(Expect::Enter(ref expected_span)) => { + self.check_span_ref(expected_span, &span, "to enter"); + } + Some(ex) => ex.bad(&self.name, format_args!("entered span {:?}", span.name())), + } + self.current.lock().unwrap().push(id.clone()); + } + + fn on_exit(&self, id: &Id, cx: Context<'_, S>) { + if std::thread::panicking() { + // `exit()` can be called in `drop` impls, so we must guard against + // double panics. + println!("[{}] exit {:?} while panicking", self.name, id); + return; + } + let span = cx + .span(id) + .unwrap_or_else(|| panic!("[{}] no span for ID {:?}", self.name, id)); + println!("[{}] exit: {}; id={:?};", self.name, span.name(), id); + match self.expected.lock().unwrap().pop_front() { + None => {} + Some(Expect::Exit(ref expected_span)) => { + self.check_span_ref(expected_span, &span, "to exit"); + let curr = self.current.lock().unwrap().pop(); + assert_eq!( + Some(id), + curr.as_ref(), + "[{}] exited span {:?}, but the current span was {:?}", + self.name, + span.name(), + curr.as_ref().and_then(|id| cx.span(id)).map(|s| s.name()) + ); + } + Some(ex) => ex.bad(&self.name, format_args!("exited span {:?}", span.name())), + }; + } + + fn on_close(&self, id: Id, cx: Context<'_, S>) { + if std::thread::panicking() { + // `try_close` can be called in `drop` impls, so we must guard against + // double panics. + println!("[{}] close {:?} while panicking", self.name, id); + return; + } + let span = cx.span(&id); + let name = span.as_ref().map(|span| { + println!("[{}] close_span: {}; id={:?};", self.name, span.name(), id,); + span.name() + }); + if name.is_none() { + println!("[{}] drop_span: id={:?}", self.name, id); + } + if let Ok(mut expected) = self.expected.try_lock() { + let was_expected = match expected.front() { + Some(Expect::DropSpan(ref expected_span)) => { + // Don't assert if this function was called while panicking, + // as failing the assertion can cause a double panic. + if !::std::thread::panicking() { + if let Some(ref span) = span { + self.check_span_ref(expected_span, span, "to close"); + } + } + true + } + Some(Expect::Event(_)) => { + if !::std::thread::panicking() { + panic!( + "[{}] expected an event, but dropped span {} (id={:?}) instead", + self.name, + name.unwrap_or("<unknown name>"), + id + ); + } + true + } + _ => false, + }; + if was_expected { + expected.pop_front(); + } + } + } + + fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>) { + panic!("well-behaved subscribers should never do this to us, lol"); + } +} + +impl fmt::Debug for ExpectLayer { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut s = f.debug_struct("ExpectLayer"); + s.field("name", &self.name); + + if let Ok(expected) = self.expected.try_lock() { + s.field("expected", &expected); + } else { + s.field("expected", &format_args!("<locked>")); + } + + if let Ok(current) = self.current.try_lock() { + s.field("current", &format_args!("{:?}", ¤t)); + } else { + s.field("current", &format_args!("<locked>")); + } + + s.finish() + } +} diff --git a/vendor/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs b/vendor/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs new file mode 100644 index 000000000..9fa5c6bd4 --- /dev/null +++ b/vendor/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs @@ -0,0 +1,123 @@ +#![cfg(feature = "registry")] +mod support; +use self::support::*; +use tracing::Level; +use tracing_subscriber::{filter::DynFilterFn, prelude::*}; + +#[test] +fn layer_filters() { + let (unfiltered, unfiltered_handle) = unfiltered("unfiltered"); + let (filtered, filtered_handle) = filtered("filtered"); + + let _subscriber = tracing_subscriber::registry() + .with(unfiltered) + .with(filtered.with_filter(filter())) + .set_default(); + + events(); + + unfiltered_handle.assert_finished(); + filtered_handle.assert_finished(); +} + +#[test] +fn layered_layer_filters() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + let unfiltered = unfiltered1.and_then(unfiltered2); + + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + let filtered = filtered1 + .with_filter(filter()) + .and_then(filtered2.with_filter(filter())); + + let _subscriber = tracing_subscriber::registry() + .with(unfiltered) + .with(filtered) + .set_default(); + + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +#[test] +fn out_of_order() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + + let _subscriber = tracing_subscriber::registry() + .with(unfiltered1) + .with(filtered1.with_filter(filter())) + .with(unfiltered2) + .with(filtered2.with_filter(filter())) + .set_default(); + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +#[test] +fn mixed_layered() { + let (unfiltered1, unfiltered1_handle) = unfiltered("unfiltered_1"); + let (unfiltered2, unfiltered2_handle) = unfiltered("unfiltered_2"); + let (filtered1, filtered1_handle) = filtered("filtered_1"); + let (filtered2, filtered2_handle) = filtered("filtered_2"); + + let layered1 = filtered1.with_filter(filter()).and_then(unfiltered1); + let layered2 = unfiltered2.and_then(filtered2.with_filter(filter())); + + let _subscriber = tracing_subscriber::registry() + .with(layered1) + .with(layered2) + .set_default(); + + events(); + + unfiltered1_handle.assert_finished(); + unfiltered2_handle.assert_finished(); + filtered1_handle.assert_finished(); + filtered2_handle.assert_finished(); +} + +fn events() { + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); +} + +fn filter<S>() -> DynFilterFn<S> { + DynFilterFn::new(|metadata, _| metadata.level() <= &Level::INFO) +} + +fn unfiltered(name: &str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(name) + .event(event::mock().at_level(Level::TRACE)) + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle() +} + +fn filtered(name: &str) -> (ExpectLayer, subscriber::MockHandle) { + layer::named(name) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle() +} diff --git a/vendor/tracing-subscriber/tests/utils.rs b/vendor/tracing-subscriber/tests/utils.rs new file mode 100644 index 000000000..e95868d5e --- /dev/null +++ b/vendor/tracing-subscriber/tests/utils.rs @@ -0,0 +1,39 @@ +#![cfg(feature = "std")] + +use tracing_mock::*; +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(); +} diff --git a/vendor/tracing-subscriber/tests/vec.rs b/vendor/tracing-subscriber/tests/vec.rs new file mode 100644 index 000000000..92abf0bff --- /dev/null +++ b/vendor/tracing-subscriber/tests/vec.rs @@ -0,0 +1,19 @@ +#![cfg(feature = "registry")] +use tracing::level_filters::LevelFilter; +use tracing::Subscriber; +use tracing_subscriber::prelude::*; + +#[test] +fn just_empty_vec() { + // Just a None means everything is off + let subscriber = tracing_subscriber::registry().with(Vec::<LevelFilter>::new()); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF)); +} + +#[test] +fn layer_and_empty_vec() { + let subscriber = tracing_subscriber::registry() + .with(LevelFilter::INFO) + .with(Vec::<LevelFilter>::new()); + assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO)); +} diff --git a/vendor/tracing-subscriber/tests/vec_subscriber_filter_interests_cached.rs b/vendor/tracing-subscriber/tests/vec_subscriber_filter_interests_cached.rs new file mode 100644 index 000000000..10467cb7d --- /dev/null +++ b/vendor/tracing-subscriber/tests/vec_subscriber_filter_interests_cached.rs @@ -0,0 +1,117 @@ +#![cfg(feature = "registry")] +mod support; +use self::support::*; + +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, +}; +use tracing::{Level, Subscriber}; +use tracing_subscriber::{filter, prelude::*}; + +#[test] +fn vec_layer_filter_interests_are_cached() { + let mk_filtered = |level: Level, subscriber: ExpectLayer| { + let seen = Arc::new(Mutex::new(HashMap::new())); + let filter = filter::filter_fn({ + let seen = seen.clone(); + move |meta| { + *seen.lock().unwrap().entry(*meta.level()).or_insert(0usize) += 1; + meta.level() <= &level + } + }); + (subscriber.with_filter(filter).boxed(), seen) + }; + + // This layer will return Interest::always for INFO and lower. + let (info_layer, info_handle) = layer::named("info") + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + let (info_layer, seen_info) = mk_filtered(Level::INFO, info_layer); + + // This layer will return Interest::always for WARN and lower. + let (warn_layer, warn_handle) = layer::named("warn") + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + let (warn_layer, seen_warn) = mk_filtered(Level::WARN, warn_layer); + + let subscriber = tracing_subscriber::registry().with(vec![warn_layer, info_layer]); + assert!(subscriber.max_level_hint().is_none()); + + let _subscriber = subscriber.set_default(); + + fn events() { + tracing::trace!("hello trace"); + tracing::debug!("hello debug"); + tracing::info!("hello info"); + tracing::warn!("hello warn"); + tracing::error!("hello error"); + } + + events(); + { + let lock = seen_info.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the INFO subscriber (after first set of events)", + level + ); + } + + let lock = seen_warn.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the WARN subscriber (after first set of events)", + level + ); + } + } + + events(); + { + let lock = seen_info.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the INFO subscriber (after second set of events)", + level + ); + } + + let lock = seen_warn.lock().unwrap(); + for (&level, &count) in lock.iter() { + if level == Level::INFO { + continue; + } + assert_eq!( + count, 1, + "level {:?} should have been seen 1 time by the WARN subscriber (after second set of events)", + level + ); + } + } + + info_handle.assert_finished(); + warn_handle.assert_finished(); +} diff --git a/vendor/tracing-tree/.cargo-checksum.json b/vendor/tracing-tree/.cargo-checksum.json new file mode 100644 index 000000000..c140e495f --- /dev/null +++ b/vendor/tracing-tree/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"e54b0301e8816a5e2d82ba08646102d89ee556d5e9150c571123a34013e6a79a","Cargo.toml":"734e1e9cf702e362c0661b968e92fd4599b732208a65634dd516d085449d45b2","LICENSE-APACHE":"c9c77d27bd737506eee1be90c11885fad935695714eafacd6725f41fff9ec8da","LICENSE-MIT":"d31139de68f7e19f9b32cd7de53dbb125b3c68522bbbb1d0c186c33112d34466","README.md":"589c2534836513b4edfa2811269767a9ac3c48da909c8ab936db3e646410dcf7","examples/basic.rs":"b56dad20aa283aae17a9c1364cc0326714cdc89d6a02a6329034e476b3597fd7","examples/basic.stdout":"49767873eb520f3e457b48d930fa86583d9ca5ef5571d67582b08dcb788ff6f2","examples/quiet.rs":"65a4cbb650cea4939ed274c2fd4d50c953151716fa6564b23393e1df6fb7afd7","examples/quiet.stdout":"d58a3240258279821b5d08e0d39ea60dee2dbd6a955f8639daf0d149ad0616fa","examples/stderr.rs":"ce0896f50be810908fb7083661bf39da6c9733ddef9188d40499313a40fe2c7a","examples/stderr.stderr":"8a0f6553fee903f159c6ea528dda6c5b193064ef9b58c765d1f3f60ecba5ac1f","examples/wraparound.rs":"7998f2e7b9e1f0dfc4b0cf11b1cdcf0f44c85aeaabbea9749113584ac7a3879f","examples/wraparound.stdout":"1c042231b3f8b20a843c18af063e43a51fcc852597dd1713f5878d4a8128271c","src/format.rs":"a986c0a259aec3135bcfd4908f6a80173dea03b3f57efe1dfa7c9d79f2b55257","src/lib.rs":"0a62652d641131ff67c31103331b977ca42ed268a1703aa7365180c70465f536","tests/ui.rs":"32a02ef41ae5cbabe13164f8d665b287b0bb764b2dc5bcb80443cb551df5289a"},"package":"d07e90b329c621ade432823988574e820212648aa40e7a2497777d58de0fb453"}
\ No newline at end of file diff --git a/vendor/tracing-tree/Cargo.lock b/vendor/tracing-tree/Cargo.lock new file mode 100644 index 000000000..82a94c9e0 --- /dev/null +++ b/vendor/tracing-tree/Cargo.lock @@ -0,0 +1,334 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "assert_cmd" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c98233c6673d8601ab23e77eb38f999c51100d46c5703b17288c57fddf3a1ffe" +dependencies = [ + "bstr", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "itertools" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3" +dependencies = [ + "either", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "once_cell" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" + +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + +[[package]] +name = "predicates" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5aab5be6e4732b473071984b3164dbbfb7a3674d30ea5ff44410b6bcd960c3c" +dependencies = [ + "difflib", + "itertools", + "predicates-core", +] + +[[package]] +name = "predicates-core" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb" + +[[package]] +name = "predicates-tree" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032" +dependencies = [ + "predicates-core", + "termtree", +] + +[[package]] +name = "proc-macro2" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "syn" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termtree" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b" + +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + +[[package]] +name = "tracing" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0ecdcb44a79f0fe9844f0c4f33a342cbcbb5117de8001e6ba0dc2351327d09" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6b8ad3567499f98a1db7a752b07a7c8c7c7c34c332ec00effb2b0027974b7c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f54c8ca710e81886d498c2fd3331b56c93aa248d49de2222ad2742247c60072f" +dependencies = [ + "lazy_static", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bc28f93baff38037f64e6f43d34cfa1605f27a49c34e8a04c5e78b0babf2596" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", +] + +[[package]] +name = "tracing-tree" +version = "0.2.1" +dependencies = [ + "ansi_term", + "assert_cmd", + "atty", + "glob", + "log", + "tracing", + "tracing-core", + "tracing-log", + "tracing-subscriber", +] + +[[package]] +name = "unicode-ident" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/vendor/tracing-tree/Cargo.toml b/vendor/tracing-tree/Cargo.toml new file mode 100644 index 000000000..32c47092f --- /dev/null +++ b/vendor/tracing-tree/Cargo.toml @@ -0,0 +1,64 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "tracing-tree" +version = "0.2.1" +authors = [ + "David Barsky <me@davidbarsky.com>", + "Nathan Whitaker", +] +description = "A Tracing Layer which prints a tree of spans and events." +readme = "README.md" +license = "MIT OR Apache-2.0" +repository = "https://github.com/davidbarsky/tracing-tree" + +[[test]] +name = "ui" +harness = false + +[dependencies.ansi_term] +version = "0.12" + +[dependencies.atty] +version = "0.2" + +[dependencies.tracing-core] +version = "0.1" + +[dependencies.tracing-log] +version = "0.1" +optional = true + +[dependencies.tracing-subscriber] +version = "0.3" +features = [ + "registry", + "fmt", + "std", +] +default-features = false + +[dev-dependencies.assert_cmd] +version = "1" + +[dev-dependencies.glob] +version = "0.3" + +[dev-dependencies.log] +version = "0.4" + +[dev-dependencies.tracing] +version = "0.1" + +[features] +default = ["tracing-log"] diff --git a/vendor/tracing-tree/LICENSE-APACHE b/vendor/tracing-tree/LICENSE-APACHE new file mode 100644 index 000000000..cb38739ee --- /dev/null +++ b/vendor/tracing-tree/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2021 tracing-tree contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/tracing-tree/LICENSE-MIT b/vendor/tracing-tree/LICENSE-MIT new file mode 100644 index 000000000..d0afab4a5 --- /dev/null +++ b/vendor/tracing-tree/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 tracing-tree contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/tracing-tree/README.md b/vendor/tracing-tree/README.md new file mode 100644 index 000000000..06751d4ec --- /dev/null +++ b/vendor/tracing-tree/README.md @@ -0,0 +1,38 @@ +# tracing-tree + +Instrument your application with [tracing](https://github.com/tokio-rs/tracing) +and get tree-structured summaries of your application activity with timing +information on the console: + +<pre> + <b>server</b>{host="localhost", port=8080<b>}</b> + 0ms <b> INFO</b> starting + 300ms <b> INFO</b> listening + <b>conn</b>{peer_addr="82.9.9.9", port=42381<b>}</b> + 0ms <b>DEBUG</b> connected + 300ms <b>DEBUG</b> message received, length=2 + <b>conn</b>{peer_addr="8.8.8.8", port=18230<b>}</b> + 300ms <b>DEBUG</b> connected + <b>conn</b>{peer_addr="82.9.9.9", port=42381<b>}</b> + 600ms <b> WARN</b> weak encryption requested, algo="xor" + 901ms <b>DEBUG</b> response sent, length=8 + 901ms <b>DEBUG</b> disconnected + <b>conn</b>{peer_addr="8.8.8.8", port=18230<b>}</b> + 600ms <b>DEBUG</b> message received, length=5 + 901ms <b>DEBUG</b> response sent, length=8 + 901ms <b>DEBUG</b> disconnected + 1502ms <b> WARN</b> internal error + 1502ms <b> INFO</b> exit +</pre> + +(Format inspired by [slog-term](https://github.com/slog-rs/slog#terminal-output-example)) + +## Setup + +After instrumenting your app with +[tracing](https://github.com/tokio-rs/tracing), add this subscriber like this: + +```rust +let subscriber = Registry::default().with(HierarchicalLayer::new(2)); +tracing::subscriber::set_global_default(subscriber).unwrap(); +```
\ No newline at end of file diff --git a/vendor/tracing-tree/examples/basic.rs b/vendor/tracing-tree/examples/basic.rs new file mode 100644 index 000000000..97f6263ff --- /dev/null +++ b/vendor/tracing-tree/examples/basic.rs @@ -0,0 +1,83 @@ +use tracing::{debug, error, info, instrument, span, warn, Level}; +use tracing_subscriber::{layer::SubscriberExt, registry::Registry}; +use tracing_tree::HierarchicalLayer; + +fn main() { + let layer = HierarchicalLayer::default() + .with_writer(std::io::stdout) + .with_indent_lines(true) + .with_indent_amount(2) + .with_thread_names(true) + .with_thread_ids(true) + .with_verbose_exit(true) + .with_verbose_entry(true) + .with_targets(true); + + let subscriber = Registry::default().with(layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); + #[cfg(feature = "tracing-log")] + tracing_log::LogTracer::init().unwrap(); + + let app_span = span!(Level::TRACE, "hierarchical-example", version = %0.1); + let _e = app_span.enter(); + + let server_span = span!(Level::TRACE, "server", host = "localhost", port = 8080); + let _e2 = server_span.enter(); + info!("starting"); + std::thread::sleep(std::time::Duration::from_millis(300)); + info!("listening"); + let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381); + peer1.in_scope(|| { + debug!("connected"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 2, "message received"); + }); + drop(peer1); + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); + peer2.in_scope(|| { + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!("connected"); + }); + drop(peer2); + let peer3 = span!( + Level::TRACE, + "foomp", + normal_var = 43, + "{} <- format string", + 42 + ); + peer3.in_scope(|| { + error!("hello"); + }); + drop(peer3); + let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381); + peer1.in_scope(|| { + warn!(algo = "xor", "weak encryption requested"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 8, "response sent"); + debug!("disconnected"); + }); + drop(peer1); + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); + peer2.in_scope(|| { + debug!(length = 5, "message received"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 8, "response sent"); + debug!("disconnected"); + }); + drop(peer2); + warn!("internal error"); + log::error!("this is a log message"); + info!("exit"); +} + +#[instrument] +fn call_a(name: &str) { + info!(name, "got a name"); + call_b(name) +} + +#[instrument] +fn call_b(name: &str) { + info!(name, "got a name"); +} diff --git a/vendor/tracing-tree/examples/basic.stdout b/vendor/tracing-tree/examples/basic.stdout new file mode 100644 index 000000000..4a2d83f11 --- /dev/null +++ b/vendor/tracing-tree/examples/basic.stdout @@ -0,0 +1,41 @@ +1:main┐basic::hierarchical-example version=0.1 +1:main├┐basic::hierarchical-example version=0.1 +1:main│└┐basic::server host="localhost", port=8080 +1:main│ ├─ms INFO basic starting +1:main│ ├─ms INFO basic listening +1:main│ ├┐basic::server host="localhost", port=8080 +1:main│ │└┐basic::conn peer_addr="82.9.9.9", port=42381 +1:main│ │ ├─ms DEBUG basic connected +1:main│ │ ├─ms DEBUG basic message received, length=2 +1:main│ │┌┘basic::conn peer_addr="82.9.9.9", port=42381 +1:main│ ├┘basic::server host="localhost", port=8080 +1:main│ ├┐basic::server host="localhost", port=8080 +1:main│ │└┐basic::conn peer_addr="8.8.8.8", port=18230 +1:main│ │ ├─ms DEBUG basic connected +1:main│ │┌┘basic::conn peer_addr="8.8.8.8", port=18230 +1:main│ ├┘basic::server host="localhost", port=8080 +1:main│ ├┐basic::server host="localhost", port=8080 +1:main│ │└┐basic::foomp 42 <- format string, normal_var=43 +1:main│ │ ├─ms ERROR basic hello +1:main│ │┌┘basic::foomp 42 <- format string, normal_var=43 +1:main│ ├┘basic::server host="localhost", port=8080 +1:main│ ├┐basic::server host="localhost", port=8080 +1:main│ │└┐basic::conn peer_addr="82.9.9.9", port=42381 +1:main│ │ ├─ms WARN basic weak encryption requested, algo="xor" +1:main│ │ ├─ms DEBUG basic response sent, length=8 +1:main│ │ ├─ms DEBUG basic disconnected +1:main│ │┌┘basic::conn peer_addr="82.9.9.9", port=42381 +1:main│ ├┘basic::server host="localhost", port=8080 +1:main│ ├┐basic::server host="localhost", port=8080 +1:main│ │└┐basic::conn peer_addr="8.8.8.8", port=18230 +1:main│ │ ├─ms DEBUG basic message received, length=5 +1:main│ │ ├─ms DEBUG basic response sent, length=8 +1:main│ │ ├─ms DEBUG basic disconnected +1:main│ │┌┘basic::conn peer_addr="8.8.8.8", port=18230 +1:main│ ├┘basic::server host="localhost", port=8080 +1:main│ ├─ms WARN basic internal error +1:main│ ├─ms ERROR basic this is a log message +1:main│ ├─ms INFO basic exit +1:main│┌┘basic::server host="localhost", port=8080 +1:main├┘basic::hierarchical-example version=0.1 +1:main┘basic::hierarchical-example version=0.1 diff --git a/vendor/tracing-tree/examples/quiet.rs b/vendor/tracing-tree/examples/quiet.rs new file mode 100644 index 000000000..81b0c9c2f --- /dev/null +++ b/vendor/tracing-tree/examples/quiet.rs @@ -0,0 +1,80 @@ +use tracing::{debug, error, info, instrument, span, warn, Level}; +use tracing_subscriber::{layer::SubscriberExt, registry::Registry}; +use tracing_tree::HierarchicalLayer; + +fn main() { + let layer = HierarchicalLayer::default() + .with_writer(std::io::stdout) + .with_indent_lines(true) + .with_indent_amount(2) + .with_thread_names(true) + .with_thread_ids(true) + .with_verbose_exit(false) + .with_verbose_entry(false) + .with_targets(true); + + let subscriber = Registry::default().with(layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); + + let app_span = span!(Level::TRACE, "hierarchical-example", version = %0.1); + let _e = app_span.enter(); + + let server_span = span!(Level::TRACE, "server", host = "localhost", port = 8080); + let _e2 = server_span.enter(); + info!("starting"); + std::thread::sleep(std::time::Duration::from_millis(300)); + info!("listening"); + let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381); + peer1.in_scope(|| { + debug!("connected"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 2, "message received"); + }); + drop(peer1); + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); + peer2.in_scope(|| { + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!("connected"); + }); + drop(peer2); + let peer3 = span!( + Level::TRACE, + "foomp", + normal_var = 43, + "{} <- format string", + 42 + ); + peer3.in_scope(|| { + error!("hello"); + }); + drop(peer3); + let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381); + peer1.in_scope(|| { + warn!(algo = "xor", "weak encryption requested"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 8, "response sent"); + debug!("disconnected"); + }); + drop(peer1); + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); + peer2.in_scope(|| { + debug!(length = 5, "message received"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 8, "response sent"); + debug!("disconnected"); + }); + drop(peer2); + warn!("internal error"); + info!("exit"); +} + +#[instrument] +fn call_a(name: &str) { + info!(name, "got a name"); + call_b(name) +} + +#[instrument] +fn call_b(name: &str) { + info!(name, "got a name"); +} diff --git a/vendor/tracing-tree/examples/quiet.stdout b/vendor/tracing-tree/examples/quiet.stdout new file mode 100644 index 000000000..96589655a --- /dev/null +++ b/vendor/tracing-tree/examples/quiet.stdout @@ -0,0 +1,28 @@ +1:main┐quiet::hierarchical-example version=0.1 +1:main├─┐quiet::server host="localhost", port=8080 +1:main│ ├─ms INFO quiet starting +1:main│ ├─ms INFO quiet listening +1:main│ ├─┐quiet::conn peer_addr="82.9.9.9", port=42381 +1:main│ │ ├─ms DEBUG quiet connected +1:main│ │ ├─ms DEBUG quiet message received, length=2 +1:main│ ├─┘ +1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230 +1:main│ │ ├─ms DEBUG quiet connected +1:main│ ├─┘ +1:main│ ├─┐quiet::foomp 42 <- format string, normal_var=43 +1:main│ │ ├─ms ERROR quiet hello +1:main│ ├─┘ +1:main│ ├─┐quiet::conn peer_addr="82.9.9.9", port=42381 +1:main│ │ ├─ms WARN quiet weak encryption requested, algo="xor" +1:main│ │ ├─ms DEBUG quiet response sent, length=8 +1:main│ │ ├─ms DEBUG quiet disconnected +1:main│ ├─┘ +1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230 +1:main│ │ ├─ms DEBUG quiet message received, length=5 +1:main│ │ ├─ms DEBUG quiet response sent, length=8 +1:main│ │ ├─ms DEBUG quiet disconnected +1:main│ ├─┘ +1:main│ ├─ms WARN quiet internal error +1:main│ ├─ms INFO quiet exit +1:main├─┘ +1:main┘ diff --git a/vendor/tracing-tree/examples/stderr.rs b/vendor/tracing-tree/examples/stderr.rs new file mode 100644 index 000000000..75880a376 --- /dev/null +++ b/vendor/tracing-tree/examples/stderr.rs @@ -0,0 +1,40 @@ +use tracing::{debug, info, instrument}; +use tracing_subscriber::{layer::SubscriberExt, registry::Registry}; +use tracing_tree::HierarchicalLayer; + +#[instrument] +fn nth_fibonacci(n: u64) -> u64 { + if n == 0 || n == 1 { + debug!("Base case"); + 1 + } else { + debug!("Recursing"); + nth_fibonacci(n - 1) + nth_fibonacci(n - 2) + } +} + +#[instrument] +fn fibonacci_seq(to: u64) -> Vec<u64> { + let mut sequence = vec![]; + + for n in 0..=to { + debug!("Pushing {n} fibonacci", n = n); + sequence.push(nth_fibonacci(n)); + } + + sequence +} + +fn main() { + let layer = HierarchicalLayer::default() + .with_indent_lines(true) + .with_indent_amount(2) + .with_bracketed_fields(true); + + let subscriber = Registry::default().with(layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); + + let n = 5; + let sequence = fibonacci_seq(n); + info!("The first {} fibonacci numbers are {:?}", n, sequence); +} diff --git a/vendor/tracing-tree/examples/stderr.stderr b/vendor/tracing-tree/examples/stderr.stderr new file mode 100644 index 000000000..0060f268f --- /dev/null +++ b/vendor/tracing-tree/examples/stderr.stderr @@ -0,0 +1,111 @@ +┐fibonacci_seq{to=5} +├─ms DEBUG Pushing 0 fibonacci +├─┐nth_fibonacci{n=0} +│ ├─ms DEBUG Base case +├─┘ +├─ms DEBUG Pushing 1 fibonacci +├─┐nth_fibonacci{n=1} +│ ├─ms DEBUG Base case +├─┘ +├─ms DEBUG Pushing 2 fibonacci +├─┐nth_fibonacci{n=2} +│ ├─ms DEBUG Recursing +│ ├─┐nth_fibonacci{n=1} +│ │ ├─ms DEBUG Base case +│ ├─┘ +│ ├─┐nth_fibonacci{n=0} +│ │ ├─ms DEBUG Base case +│ ├─┘ +├─┘ +├─ms DEBUG Pushing 3 fibonacci +├─┐nth_fibonacci{n=3} +│ ├─ms DEBUG Recursing +│ ├─┐nth_fibonacci{n=2} +│ │ ├─ms DEBUG Recursing +│ │ ├─┐nth_fibonacci{n=1} +│ │ │ ├─ms DEBUG Base case +│ │ ├─┘ +│ │ ├─┐nth_fibonacci{n=0} +│ │ │ ├─ms DEBUG Base case +│ │ ├─┘ +│ ├─┘ +│ ├─┐nth_fibonacci{n=1} +│ │ ├─ms DEBUG Base case +│ ├─┘ +├─┘ +├─ms DEBUG Pushing 4 fibonacci +├─┐nth_fibonacci{n=4} +│ ├─ms DEBUG Recursing +│ ├─┐nth_fibonacci{n=3} +│ │ ├─ms DEBUG Recursing +│ │ ├─┐nth_fibonacci{n=2} +│ │ │ ├─ms DEBUG Recursing +│ │ │ ├─┐nth_fibonacci{n=1} +│ │ │ │ ├─ms DEBUG Base case +│ │ │ ├─┘ +│ │ │ ├─┐nth_fibonacci{n=0} +│ │ │ │ ├─ms DEBUG Base case +│ │ │ ├─┘ +│ │ ├─┘ +│ │ ├─┐nth_fibonacci{n=1} +│ │ │ ├─ms DEBUG Base case +│ │ ├─┘ +│ ├─┘ +│ ├─┐nth_fibonacci{n=2} +│ │ ├─ms DEBUG Recursing +│ │ ├─┐nth_fibonacci{n=1} +│ │ │ ├─ms DEBUG Base case +│ │ ├─┘ +│ │ ├─┐nth_fibonacci{n=0} +│ │ │ ├─ms DEBUG Base case +│ │ ├─┘ +│ ├─┘ +├─┘ +├─ms DEBUG Pushing 5 fibonacci +├─┐nth_fibonacci{n=5} +│ ├─ms DEBUG Recursing +│ ├─┐nth_fibonacci{n=4} +│ │ ├─ms DEBUG Recursing +│ │ ├─┐nth_fibonacci{n=3} +│ │ │ ├─ms DEBUG Recursing +│ │ │ ├─┐nth_fibonacci{n=2} +│ │ │ │ ├─ms DEBUG Recursing +│ │ │ │ ├─┐nth_fibonacci{n=1} +│ │ │ │ │ ├─ms DEBUG Base case +│ │ │ │ ├─┘ +│ │ │ │ ├─┐nth_fibonacci{n=0} +│ │ │ │ │ ├─ms DEBUG Base case +│ │ │ │ ├─┘ +│ │ │ ├─┘ +│ │ │ ├─┐nth_fibonacci{n=1} +│ │ │ │ ├─ms DEBUG Base case +│ │ │ ├─┘ +│ │ ├─┘ +│ │ ├─┐nth_fibonacci{n=2} +│ │ │ ├─ms DEBUG Recursing +│ │ │ ├─┐nth_fibonacci{n=1} +│ │ │ │ ├─ms DEBUG Base case +│ │ │ ├─┘ +│ │ │ ├─┐nth_fibonacci{n=0} +│ │ │ │ ├─ms DEBUG Base case +│ │ │ ├─┘ +│ │ ├─┘ +│ ├─┘ +│ ├─┐nth_fibonacci{n=3} +│ │ ├─ms DEBUG Recursing +│ │ ├─┐nth_fibonacci{n=2} +│ │ │ ├─ms DEBUG Recursing +│ │ │ ├─┐nth_fibonacci{n=1} +│ │ │ │ ├─ms DEBUG Base case +│ │ │ ├─┘ +│ │ │ ├─┐nth_fibonacci{n=0} +│ │ │ │ ├─ms DEBUG Base case +│ │ │ ├─┘ +│ │ ├─┘ +│ │ ├─┐nth_fibonacci{n=1} +│ │ │ ├─ms DEBUG Base case +│ │ ├─┘ +│ ├─┘ +├─┘ +┘ +INFO The first 5 fibonacci numbers are [1, 1, 2, 3, 5, 8] diff --git a/vendor/tracing-tree/examples/wraparound.rs b/vendor/tracing-tree/examples/wraparound.rs new file mode 100644 index 000000000..e045eb9f2 --- /dev/null +++ b/vendor/tracing-tree/examples/wraparound.rs @@ -0,0 +1,31 @@ +use tracing::{instrument, warn}; +use tracing_subscriber::{layer::SubscriberExt, registry::Registry}; +use tracing_tree::HierarchicalLayer; + +fn main() { + let layer = HierarchicalLayer::default() + .with_writer(std::io::stdout) + .with_indent_lines(true) + .with_indent_amount(2) + .with_thread_names(true) + .with_thread_ids(true) + .with_targets(true) + .with_wraparound(5); + + let subscriber = Registry::default().with(layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); + + recurse(0); +} + +#[instrument] +fn recurse(i: usize) { + warn!("boop"); + if i > 20 { + warn!("bop"); + return; + } else { + recurse(i + 1); + } + warn!("bop"); +} diff --git a/vendor/tracing-tree/examples/wraparound.stdout b/vendor/tracing-tree/examples/wraparound.stdout new file mode 100644 index 000000000..bbf422545 --- /dev/null +++ b/vendor/tracing-tree/examples/wraparound.stdout @@ -0,0 +1,96 @@ +1:main┐wraparound::recurse i=0 +1:main├─ms WARN wraparound boop +1:main├─┐wraparound::recurse i=1 +1:main│ ├─ms WARN wraparound boop +1:main│ ├─┐wraparound::recurse i=2 +1:main│ │ ├─ms WARN wraparound boop +1:main│ │ ├─┐wraparound::recurse i=3 +1:main│ │ │ ├─ms WARN wraparound boop +1:main│ │ │ ├─┐wraparound::recurse i=4 +1:main────────┘ +1:mainms WARN wraparound boop +1:main┐wraparound::recurse i=5 +1:main├─ms WARN wraparound boop +1:main├─┐wraparound::recurse i=6 +1:main│ ├─ms WARN wraparound boop +1:main│ ├─┐wraparound::recurse i=7 +1:main│ │ ├─ms WARN wraparound boop +1:main│ │ ├─┐wraparound::recurse i=8 +1:main│ │ │ ├─ms WARN wraparound boop +1:main│ │ │ ├─┐wraparound::recurse i=9 +1:main────────┘ +1:mainms WARN wraparound boop +1:main┐wraparound::recurse i=10 +1:main├─ms WARN wraparound boop +1:main├─┐wraparound::recurse i=11 +1:main│ ├─ms WARN wraparound boop +1:main│ ├─┐wraparound::recurse i=12 +1:main│ │ ├─ms WARN wraparound boop +1:main│ │ ├─┐wraparound::recurse i=13 +1:main│ │ │ ├─ms WARN wraparound boop +1:main│ │ │ ├─┐wraparound::recurse i=14 +1:main────────┘ +1:mainms WARN wraparound boop +1:main┐wraparound::recurse i=15 +1:main├─ms WARN wraparound boop +1:main├─┐wraparound::recurse i=16 +1:main│ ├─ms WARN wraparound boop +1:main│ ├─┐wraparound::recurse i=17 +1:main│ │ ├─ms WARN wraparound boop +1:main│ │ ├─┐wraparound::recurse i=18 +1:main│ │ │ ├─ms WARN wraparound boop +1:main│ │ │ ├─┐wraparound::recurse i=19 +1:main────────┘ +1:mainms WARN wraparound boop +1:main┐wraparound::recurse i=20 +1:main├─ms WARN wraparound boop +1:main├─┐wraparound::recurse i=21 +1:main│ ├─ms WARN wraparound boop +1:main│ ├─ms WARN wraparound bop +1:main├─┘ +1:main├─ms WARN wraparound bop +1:main┘ +1:mainms WARN wraparound bop +1:main────────┐ +1:main│ │ │ ├─┘ +1:main│ │ │ ├─ms WARN wraparound bop +1:main│ │ ├─┘ +1:main│ │ ├─ms WARN wraparound bop +1:main│ ├─┘ +1:main│ ├─ms WARN wraparound bop +1:main├─┘ +1:main├─ms WARN wraparound bop +1:main┘ +1:mainms WARN wraparound bop +1:main────────┐ +1:main│ │ │ ├─┘ +1:main│ │ │ ├─ms WARN wraparound bop +1:main│ │ ├─┘ +1:main│ │ ├─ms WARN wraparound bop +1:main│ ├─┘ +1:main│ ├─ms WARN wraparound bop +1:main├─┘ +1:main├─ms WARN wraparound bop +1:main┘ +1:mainms WARN wraparound bop +1:main────────┐ +1:main│ │ │ ├─┘ +1:main│ │ │ ├─ms WARN wraparound bop +1:main│ │ ├─┘ +1:main│ │ ├─ms WARN wraparound bop +1:main│ ├─┘ +1:main│ ├─ms WARN wraparound bop +1:main├─┘ +1:main├─ms WARN wraparound bop +1:main┘ +1:mainms WARN wraparound bop +1:main────────┐ +1:main│ │ │ ├─┘ +1:main│ │ │ ├─ms WARN wraparound bop +1:main│ │ ├─┘ +1:main│ │ ├─ms WARN wraparound bop +1:main│ ├─┘ +1:main│ ├─ms WARN wraparound bop +1:main├─┘ +1:main├─ms WARN wraparound bop +1:main┘ diff --git a/vendor/tracing-tree/src/format.rs b/vendor/tracing-tree/src/format.rs new file mode 100644 index 000000000..067ea9771 --- /dev/null +++ b/vendor/tracing-tree/src/format.rs @@ -0,0 +1,425 @@ +use ansi_term::Color; +use std::{ + fmt::{self, Write as _}, + io, +}; +use tracing_core::{ + field::{Field, Visit}, + Level, +}; + +pub(crate) const LINE_VERT: &str = "│"; +const LINE_HORIZ: &str = "─"; +pub(crate) const LINE_BRANCH: &str = "├"; +pub(crate) const LINE_CLOSE: &str = "┘"; +pub(crate) const LINE_OPEN: &str = "┐"; + +#[derive(Copy, Clone)] +pub(crate) enum SpanMode { + PreOpen, + Open { verbose: bool }, + Close { verbose: bool }, + PostClose, + Event, +} + +#[derive(Debug)] +pub struct Config { + /// Whether to use colors. + pub ansi: bool, + /// Whether an ascii art tree is used or (if false) whether to just use whitespace indent + pub indent_lines: bool, + /// The amount of chars to indent. + pub indent_amount: usize, + /// Whether to show the module paths. + pub targets: bool, + /// Whether to show thread ids. + pub render_thread_ids: bool, + /// Whether to show thread names. + pub render_thread_names: bool, + /// Specifies after how many indentation levels we will wrap back around to zero + pub wraparound: usize, + /// Whether to print the current span before activating a new one + pub verbose_entry: bool, + /// Whether to print the current span before exiting it. + pub verbose_exit: bool, + /// Whether to print squiggly brackets (`{}`) around the list of fields in a span. + pub bracketed_fields: bool, +} + +impl Config { + pub fn with_ansi(self, ansi: bool) -> Self { + Self { ansi, ..self } + } + + pub fn with_indent_lines(self, indent_lines: bool) -> Self { + Self { + indent_lines, + ..self + } + } + + pub fn with_targets(self, targets: bool) -> Self { + Self { targets, ..self } + } + + pub fn with_thread_ids(self, render_thread_ids: bool) -> Self { + Self { + render_thread_ids, + ..self + } + } + + pub fn with_thread_names(self, render_thread_names: bool) -> Self { + Self { + render_thread_names, + ..self + } + } + + pub fn with_wraparound(self, wraparound: usize) -> Self { + Self { wraparound, ..self } + } + + pub fn with_verbose_entry(self, verbose_entry: bool) -> Self { + Self { + verbose_entry, + ..self + } + } + + pub fn with_verbose_exit(self, verbose_exit: bool) -> Self { + Self { + verbose_exit, + ..self + } + } + + pub fn with_bracketed_fields(self, bracketed_fields: bool) -> Self { + Self { + bracketed_fields, + ..self + } + } + + pub(crate) fn prefix(&self) -> String { + let mut buf = String::new(); + if self.render_thread_ids { + write!(buf, "{:?}", std::thread::current().id()).unwrap(); + if buf.ends_with(')') { + buf.truncate(buf.len() - 1); + } + if buf.starts_with("ThreadId(") { + buf.drain(0.."ThreadId(".len()); + } + } + if self.render_thread_names { + if let Some(name) = std::thread::current().name() { + if self.render_thread_ids { + buf.push(':'); + } + buf.push_str(name); + } + } + buf + } +} + +impl Default for Config { + fn default() -> Self { + Self { + ansi: true, + indent_lines: false, + indent_amount: 2, + targets: false, + render_thread_ids: false, + render_thread_names: false, + wraparound: usize::max_value(), + verbose_entry: false, + verbose_exit: false, + bracketed_fields: false, + } + } +} + +#[derive(Debug)] +pub struct Buffers { + pub current_buf: String, + pub indent_buf: String, +} + +impl Buffers { + pub fn new() -> Self { + Self { + current_buf: String::new(), + indent_buf: String::new(), + } + } + + pub fn flush_current_buf(&mut self, mut writer: impl io::Write) { + write!(writer, "{}", &self.current_buf).unwrap(); + self.current_buf.clear(); + } + + pub fn flush_indent_buf(&mut self) { + self.current_buf.push_str(&self.indent_buf); + self.indent_buf.clear(); + } + + pub(crate) fn indent_current(&mut self, indent: usize, config: &Config, style: SpanMode) { + self.current_buf.push('\n'); + let prefix = config.prefix(); + + // Render something when wraparound occurs so the user is aware of it + if config.indent_lines { + match style { + SpanMode::Close { .. } | SpanMode::PostClose => { + if indent > 0 && (indent + 1) % config.wraparound == 0 { + self.indent_buf.push_str(&prefix); + for _ in 0..(indent % config.wraparound * config.indent_amount) { + self.indent_buf.push_str(LINE_HORIZ); + } + self.indent_buf.push_str(LINE_OPEN); + self.indent_buf.push('\n'); + } + } + _ => {} + } + } + + indent_block( + &mut self.current_buf, + &mut self.indent_buf, + indent % config.wraparound, + config.indent_amount, + config.indent_lines, + &prefix, + style, + ); + self.current_buf.clear(); + self.flush_indent_buf(); + + // Render something when wraparound occurs so the user is aware of it + if config.indent_lines { + match style { + SpanMode::PreOpen | SpanMode::Open { .. } => { + if indent > 0 && (indent + 1) % config.wraparound == 0 { + self.current_buf.push_str(&prefix); + for _ in 0..(indent % config.wraparound * config.indent_amount) { + self.current_buf.push_str(LINE_HORIZ); + } + self.current_buf.push_str(LINE_CLOSE); + self.current_buf.push('\n'); + } + } + _ => {} + } + } + } +} + +pub struct FmtEvent<'a> { + pub bufs: &'a mut Buffers, + pub comma: bool, +} + +impl<'a> Visit for FmtEvent<'a> { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + let buf = &mut self.bufs.current_buf; + let comma = if self.comma { "," } else { "" }; + match field.name() { + "message" => { + write!(buf, "{} {:?}", comma, value).unwrap(); + self.comma = true; + } + // Skip fields that are actually log metadata that have already been handled + #[cfg(feature = "tracing-log")] + name if name.starts_with("log.") => {} + name => { + write!(buf, "{} {}={:?}", comma, name, value).unwrap(); + self.comma = true; + } + } + } +} + +pub struct ColorLevel<'a>(pub &'a Level); + +impl<'a> fmt::Display for ColorLevel<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self.0 { + Level::TRACE => Color::Purple.bold().paint("TRACE"), + Level::DEBUG => Color::Blue.bold().paint("DEBUG"), + Level::INFO => Color::Green.bold().paint(" INFO"), + Level::WARN => Color::RGB(252, 234, 160).bold().paint(" WARN"), // orange + Level::ERROR => Color::Red.bold().paint("ERROR"), + } + .fmt(f) + } +} + +fn indent_block_with_lines( + lines: &[&str], + buf: &mut String, + indent: usize, + indent_amount: usize, + prefix: &str, + style: SpanMode, +) { + let indent_spaces = indent * indent_amount; + if lines.is_empty() { + return; + } else if indent_spaces == 0 { + for line in lines { + buf.push_str(prefix); + // The first indent is special, we only need to print open/close and nothing else + if indent == 0 { + match style { + SpanMode::Open { .. } => buf.push_str(LINE_OPEN), + SpanMode::Close { .. } => buf.push_str(LINE_CLOSE), + SpanMode::PreOpen | SpanMode::PostClose => {} + SpanMode::Event => {} + } + } + buf.push_str(line); + buf.push('\n'); + } + return; + } + let mut s = String::with_capacity(indent_spaces + prefix.len()); + s.push_str(prefix); + + // instead of using all spaces to indent, draw a vertical line at every indent level + // up until the last indent + for i in 0..(indent_spaces - indent_amount) { + if i % indent_amount == 0 { + s.push_str(LINE_VERT); + } else { + s.push(' '); + } + } + + // draw branch + buf.push_str(&s); + + match style { + SpanMode::PreOpen => { + buf.push_str(LINE_BRANCH); + for _ in 1..(indent_amount / 2) { + buf.push_str(LINE_HORIZ); + } + buf.push_str(LINE_OPEN); + } + SpanMode::Open { verbose: false } => { + buf.push_str(LINE_BRANCH); + for _ in 1..indent_amount { + buf.push_str(LINE_HORIZ); + } + buf.push_str(LINE_OPEN); + } + SpanMode::Open { verbose: true } => { + buf.push_str(LINE_VERT); + for _ in 1..(indent_amount / 2) { + buf.push(' '); + } + // We don't have the space for fancy rendering at single space indent. + if indent_amount > 1 { + buf.push('└'); + } + for _ in (indent_amount / 2)..(indent_amount - 1) { + buf.push_str(LINE_HORIZ); + } + // We don't have the space for fancy rendering at single space indent. + if indent_amount > 1 { + buf.push_str(LINE_OPEN); + } else { + buf.push_str(LINE_VERT); + } + } + SpanMode::Close { verbose: false } => { + buf.push_str(LINE_BRANCH); + for _ in 1..indent_amount { + buf.push_str(LINE_HORIZ); + } + buf.push_str(LINE_CLOSE); + } + SpanMode::Close { verbose: true } => { + buf.push_str(LINE_VERT); + for _ in 1..(indent_amount / 2) { + buf.push(' '); + } + // We don't have the space for fancy rendering at single space indent. + if indent_amount > 1 { + buf.push('┌'); + } + for _ in (indent_amount / 2)..(indent_amount - 1) { + buf.push_str(LINE_HORIZ); + } + // We don't have the space for fancy rendering at single space indent. + if indent_amount > 1 { + buf.push_str(LINE_CLOSE); + } else { + buf.push_str(LINE_VERT); + } + } + SpanMode::PostClose => { + buf.push_str(LINE_BRANCH); + for _ in 1..(indent_amount / 2) { + buf.push_str(LINE_HORIZ); + } + buf.push_str(LINE_CLOSE); + } + SpanMode::Event => { + buf.push_str(LINE_BRANCH); + + // add `indent_amount - 1` horizontal lines before the span/event + for _ in 0..(indent_amount - 1) { + buf.push_str(LINE_HORIZ); + } + } + } + buf.push_str(lines[0]); + buf.push('\n'); + + // add the rest of the indentation, since we don't want to draw horizontal lines + // for subsequent lines + for i in 0..indent_amount { + if i % indent_amount == 0 { + s.push_str(LINE_VERT); + } else { + s.push(' '); + } + } + + // add all of the actual content, with each line preceded by the indent string + for line in &lines[1..] { + buf.push_str(&s); + buf.push_str(line); + buf.push('\n'); + } +} + +fn indent_block( + block: &mut String, + buf: &mut String, + indent: usize, + indent_amount: usize, + indent_lines: bool, + prefix: &str, + style: SpanMode, +) { + let lines: Vec<&str> = block.lines().collect(); + let indent_spaces = indent * indent_amount; + buf.reserve(block.len() + (lines.len() * indent_spaces)); + if indent_lines { + indent_block_with_lines(&lines, buf, indent, indent_amount, prefix, style); + } else { + let indent_str = String::from(" ").repeat(indent_spaces); + for line in lines { + buf.push_str(prefix); + buf.push_str(&indent_str); + buf.push_str(line); + buf.push('\n'); + } + } +} diff --git a/vendor/tracing-tree/src/lib.rs b/vendor/tracing-tree/src/lib.rs new file mode 100644 index 000000000..dbacaa57e --- /dev/null +++ b/vendor/tracing-tree/src/lib.rs @@ -0,0 +1,395 @@ +pub(crate) mod format; + +use ansi_term::{Color, Style}; +use format::{Buffers, ColorLevel, Config, FmtEvent, SpanMode}; +use std::{ + fmt::{self, Write as _}, + io, + sync::Mutex, + time::Instant, +}; +use tracing_core::{ + field::{Field, Visit}, + span::{Attributes, Id}, + Event, Subscriber, +}; +#[cfg(feature = "tracing-log")] +use tracing_log::NormalizeEvent; +use tracing_subscriber::{ + fmt::MakeWriter, + layer::{Context, Layer}, + registry::{self, LookupSpan}, +}; + +pub(crate) struct Data { + start: Instant, + kvs: Vec<(&'static str, String)>, +} + +impl Data { + pub fn new(attrs: &Attributes<'_>) -> Self { + let mut span = Self { + start: Instant::now(), + kvs: Vec::new(), + }; + attrs.record(&mut span); + span + } +} + +impl Visit for Data { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + self.kvs.push((field.name(), format!("{:?}", value))) + } +} +#[derive(Debug)] +pub struct HierarchicalLayer<W = fn() -> io::Stderr> +where + W: for<'writer> MakeWriter<'writer> + 'static, +{ + make_writer: W, + bufs: Mutex<Buffers>, + config: Config, +} + +impl Default for HierarchicalLayer { + fn default() -> Self { + Self::new(2) + } +} + +impl HierarchicalLayer<fn() -> io::Stderr> { + pub fn new(indent_amount: usize) -> Self { + let ansi = atty::is(atty::Stream::Stderr); + let config = Config { + ansi, + indent_amount, + ..Default::default() + }; + Self { + make_writer: io::stderr, + bufs: Mutex::new(Buffers::new()), + config, + } + } +} + +impl<W> HierarchicalLayer<W> +where + W: for<'writer> MakeWriter<'writer> + 'static, +{ + /// Enables terminal colors, boldness and italics. + pub fn with_ansi(self, ansi: bool) -> Self { + Self { + config: self.config.with_ansi(ansi), + ..self + } + } + + pub fn with_writer<W2>(self, make_writer: W2) -> HierarchicalLayer<W2> + where + W2: for<'writer> MakeWriter<'writer>, + { + HierarchicalLayer { + make_writer, + config: self.config, + bufs: self.bufs, + } + } + + pub fn with_indent_amount(self, indent_amount: usize) -> Self { + let config = Config { + indent_amount, + ..self.config + }; + Self { config, ..self } + } + + /// Renders an ascii art tree instead of just using whitespace indentation. + pub fn with_indent_lines(self, indent_lines: bool) -> Self { + Self { + config: self.config.with_indent_lines(indent_lines), + ..self + } + } + + /// Whether to render the event and span targets. Usually targets are the module path to the + /// event/span macro invocation. + pub fn with_targets(self, targets: bool) -> Self { + Self { + config: self.config.with_targets(targets), + ..self + } + } + + /// Whether to render the thread id in the beginning of every line. This is helpful to + /// untangle the tracing statements emitted by each thread. + pub fn with_thread_ids(self, thread_ids: bool) -> Self { + Self { + config: self.config.with_thread_ids(thread_ids), + ..self + } + } + + /// Whether to render the thread name in the beginning of every line. Not all threads have + /// names, but if they do, this may be more helpful than the generic thread ids. + pub fn with_thread_names(self, thread_names: bool) -> Self { + Self { + config: self.config.with_thread_names(thread_names), + ..self + } + } + + /// Resets the indentation to zero after `wraparound` indentation levels. + /// This is helpful if you expect very deeply nested spans as otherwise the indentation + /// just runs out of your screen. + pub fn with_wraparound(self, wraparound: usize) -> Self { + Self { + config: self.config.with_wraparound(wraparound), + ..self + } + } + + /// Whether to print the currently active span's message again before entering a new span. + /// This helps if the entry to the current span was quite a while back (and with scrolling + /// upwards in logs). + pub fn with_verbose_entry(self, verbose_entry: bool) -> Self { + Self { + config: self.config.with_verbose_entry(verbose_entry), + ..self + } + } + + /// Whether to print the currently active span's message again before dropping it. + /// This helps if the entry to the current span was quite a while back (and with scrolling + /// upwards in logs). + pub fn with_verbose_exit(self, verbose_exit: bool) -> Self { + Self { + config: self.config.with_verbose_exit(verbose_exit), + ..self + } + } + + /// Whether to print `{}` around the fields when printing a span. + /// This can help visually distinguish fields from the rest of the message. + pub fn with_bracketed_fields(self, bracketed_fields: bool) -> Self { + Self { + config: self.config.with_bracketed_fields(bracketed_fields), + ..self + } + } + + fn styled(&self, style: Style, text: impl AsRef<str>) -> String { + if self.config.ansi { + style.paint(text.as_ref()).to_string() + } else { + text.as_ref().to_string() + } + } + + fn print_kvs<'a, I, V>(&self, buf: &mut impl fmt::Write, kvs: I) -> fmt::Result + where + I: IntoIterator<Item = (&'a str, V)>, + V: fmt::Display + 'a, + { + let mut kvs = kvs.into_iter(); + if let Some((k, v)) = kvs.next() { + if k == "message" { + write!(buf, "{}", v)?; + } else { + write!(buf, "{}={}", k, v)?; + } + } + for (k, v) in kvs { + write!(buf, ", {}={}", k, v)?; + } + Ok(()) + } + + fn write_span_info<S>(&self, id: &Id, ctx: &Context<S>, style: SpanMode) + where + S: Subscriber + for<'span> LookupSpan<'span>, + { + let span = ctx + .span(id) + .expect("in on_enter/on_exit but span does not exist"); + let ext = span.extensions(); + let data = ext.get::<Data>().expect("span does not have data"); + + let mut guard = self.bufs.lock().unwrap(); + let bufs = &mut *guard; + let mut current_buf = &mut bufs.current_buf; + + let indent = ctx + .lookup_current() + .as_ref() + .map(registry::SpanRef::scope) + .map(registry::Scope::from_root) + .into_iter() + .flatten() + .count(); + + if self.config.verbose_entry || matches!(style, SpanMode::Open { .. } | SpanMode::Event) { + if self.config.targets { + let target = span.metadata().target(); + write!( + &mut current_buf, + "{}::", + self.styled(Style::new().dimmed(), target,), + ) + .expect("Unable to write to buffer"); + } + + write!( + current_buf, + "{name}", + name = self.styled(Style::new().fg(Color::Green).bold(), span.metadata().name()) + ) + .unwrap(); + if self.config.bracketed_fields { + write!( + current_buf, + "{}", + self.styled(Style::new().fg(Color::Green).bold(), "{") // Style::new().fg(Color::Green).dimmed().paint("{") + ) + .unwrap(); + } else { + write!(current_buf, " ").unwrap(); + } + self.print_kvs(&mut current_buf, data.kvs.iter().map(|(k, v)| (*k, v))) + .unwrap(); + if self.config.bracketed_fields { + write!( + current_buf, + "{}", + self.styled(Style::new().fg(Color::Green).bold(), "}") // Style::new().dimmed().paint("}") + ) + .unwrap(); + } + } + + bufs.indent_current(indent, &self.config, style); + let writer = self.make_writer.make_writer(); + bufs.flush_current_buf(writer) + } +} + +impl<S, W> Layer<S> for HierarchicalLayer<W> +where + S: Subscriber + for<'span> LookupSpan<'span>, + W: for<'writer> MakeWriter<'writer> + 'static, +{ + fn on_new_span(&self, attrs: &Attributes, id: &Id, ctx: Context<S>) { + let span = ctx.span(id).expect("in new_span but span does not exist"); + if span.extensions().get::<Data>().is_none() { + let data = Data::new(attrs); + span.extensions_mut().insert(data); + } + + if self.config.verbose_exit { + if let Some(span) = span.parent() { + self.write_span_info(&span.id(), &ctx, SpanMode::PreOpen); + } + } + + self.write_span_info( + id, + &ctx, + SpanMode::Open { + verbose: self.config.verbose_entry, + }, + ); + } + + fn on_event(&self, event: &Event<'_>, ctx: Context<S>) { + let mut guard = self.bufs.lock().unwrap(); + let bufs = &mut *guard; + let mut event_buf = &mut bufs.current_buf; + + // printing the indentation + let indent = if ctx.current_span().id().is_some() { + // size hint isn't implemented on Scope. + ctx.event_scope(event) + .expect("Unable to get span scope; this is a bug") + .count() + } else { + 0 + }; + + // check if this event occurred in the context of a span. + // if it has, get the start time of this span. + let start = match ctx.current_span().id() { + Some(id) => match ctx.span(id) { + // if the event is in a span, get the span's starting point. + Some(ctx) => { + let ext = ctx.extensions(); + let data = ext + .get::<Data>() + .expect("Data cannot be found in extensions"); + Some(data.start) + } + None => None, + }, + None => None, + }; + if let Some(start) = start { + let elapsed = start.elapsed(); + write!( + &mut event_buf, + "{timestamp}{unit} ", + timestamp = self.styled(Style::new().dimmed(), elapsed.as_millis().to_string()), + unit = self.styled(Style::new().dimmed(), "ms"), + ) + .expect("Unable to write to buffer"); + } + + #[cfg(feature = "tracing-log")] + let normalized_meta = event.normalized_metadata(); + #[cfg(feature = "tracing-log")] + let metadata = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); + #[cfg(not(feature = "tracing-log"))] + let metadata = event.metadata(); + + let level = metadata.level(); + let level = if self.config.ansi { + ColorLevel(level).to_string() + } else { + level.to_string() + }; + write!(&mut event_buf, "{level}", level = level).expect("Unable to write to buffer"); + + if self.config.targets { + let target = metadata.target(); + write!( + &mut event_buf, + " {}", + self.styled(Style::new().dimmed(), target,), + ) + .expect("Unable to write to buffer"); + } + + let mut visitor = FmtEvent { comma: false, bufs }; + event.record(&mut visitor); + visitor + .bufs + .indent_current(indent, &self.config, SpanMode::Event); + let writer = self.make_writer.make_writer(); + bufs.flush_current_buf(writer) + } + + fn on_close(&self, id: Id, ctx: Context<S>) { + self.write_span_info( + &id, + &ctx, + SpanMode::Close { + verbose: self.config.verbose_exit, + }, + ); + + if self.config.verbose_exit { + if let Some(span) = ctx.span(&id).and_then(|span| span.parent()) { + self.write_span_info(&span.id(), &ctx, SpanMode::PostClose); + } + } + } +} diff --git a/vendor/tracing-tree/tests/ui.rs b/vendor/tracing-tree/tests/ui.rs new file mode 100644 index 000000000..68c54e66e --- /dev/null +++ b/vendor/tracing-tree/tests/ui.rs @@ -0,0 +1,79 @@ +use assert_cmd::prelude::*; + +use std::process::Command; + +// Timings are flaky, so tests would spuriously fail. +// Thus we replace all `/([0-9]+)ms/` with underscores +fn replace_ms(data: &[u8]) -> Vec<u8> { + let mut skip = false; + let mut seen_s = false; + let mut v: Vec<u8> = data + .iter() + .rev() + .filter_map(|&b| match (b, skip, seen_s) { + (b'0'..=b'9', true, _) => None, + (_, true, _) => { + skip = false; + Some(b) + } + (b's', _, _) => { + seen_s = true; + Some(b) + } + (b'm', _, true) => { + seen_s = false; + skip = true; + Some(b) + } + _ => Some(b), + }) + .collect(); + v.reverse(); + v +} + +fn main() { + for entry in glob::glob("examples/*.rs").expect("Failed to read glob pattern") { + let entry = entry.unwrap(); + let mut cmd = Command::cargo_bin(entry.with_extension("").to_str().unwrap()).unwrap(); + let output = cmd.unwrap(); + let stderr = entry.with_extension("stderr"); + let stdout = entry.with_extension("stdout"); + + if std::env::args().any(|arg| arg == "--bless") { + if output.stderr.is_empty() { + let _ = std::fs::remove_file(stderr); + } else { + std::fs::write(stderr, replace_ms(&output.stderr)).unwrap(); + } + if output.stdout.is_empty() { + let _ = std::fs::remove_file(stdout); + } else { + std::fs::write(stdout, replace_ms(&output.stdout)).unwrap(); + } + } else { + if output.stderr.is_empty() { + assert!( + !stderr.exists(), + "{} exists but there was no stderr output", + stderr.display() + ); + } else { + assert!( + std::fs::read(&stderr).unwrap() == replace_ms(&output.stderr), + "{} is not the expected output, rerun the test with `cargo test -- -- --bless`", + stderr.display() + ); + } + if output.stdout.is_empty() { + assert!(!stdout.exists()); + } else { + assert!( + std::fs::read(&stdout).unwrap() == replace_ms(&output.stdout), + "{} is not the expected output, rerun the test with `cargo test -- -- --bless`", + stdout.display() + ); + } + } + } +} diff --git a/vendor/tracing/.cargo-checksum.json b/vendor/tracing/.cargo-checksum.json new file mode 100644 index 000000000..404d4f3c0 --- /dev/null +++ b/vendor/tracing/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"7d5517e1ac705d5d8574915b59a37bbd0ba5aa6e663fc272b75e5792eca572a9","Cargo.toml":"f6290faffd0bf079845dbd6c3c1ddb6c728bcf7225e3b56a4ba5a9478dc72a8b","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"a83e1a8287e3eca752383ae849872707dddfbd07b918806cc7c027a0824872c6","benches/global_subscriber.rs":"271213baed0e02054e506c1ec9c47b58696c78aaa46f0969a147f4c369f80e3d","benches/no_subscriber.rs":"495c9a91fb972ec61ced31ef8e19d2cca02ec8ffae4e98e3316e55f6a0074578","benches/subscriber.rs":"c609be119ed6e4d3fb79df77f15aa14effbd3e2f77c627a49229a50091d3ee6a","src/dispatcher.rs":"a8158e1901c50dc83d2f15b1773e6b9e31985d9af65e460be52dbf8be6874cd2","src/field.rs":"55c7a2798b9ad0269e7c738c3f15a5d0281bf34ac3a6196a3f0b15801e5278bd","src/instrument.rs":"1fe4de5c13b5ba048e9872d78d1fa4e85655f9f2ed10f79b72b5da881c9b8b45","src/level_filters.rs":"baae8e797897bae9cdd9ec64b8e9a3d71156e9c03261be17b5b18acba034e154","src/lib.rs":"a8ab6b578f93b5fb833942fc59642e67caca8d343670d64fb52d47d5507a67e4","src/macros.rs":"f1a83930cea322f1f2000548d91800c22a9e28d2daf3f178c7c7c3ac8da5a02d","src/span.rs":"96b6c86be09c97b15b2ab5c607013abc5b388c2870eb648bee2cfc07837fa6e9","src/stdlib.rs":"248514a9bae6106e436358aee44c92abf8e7f79022895c4a25136ddef211d198","src/subscriber.rs":"ae879c373be7ee4935f7b02a345f92ccbeb7879d61c5d37e3cc1277b3d51ddb2","tests/enabled.rs":"1333339aace87ea9d701f2f76a1985820cc513a75013a7ed89669f7a2c635479","tests/event.rs":"c4ec3ac338475e9e61675551eb99df1d8a7cbefb05a0d60203994f5c1df7c784","tests/filter_caching_is_lexically_scoped.rs":"5487a37db5fbdf3d57020ab1f01185d928c45d967d99d723ffc434540459d8dc","tests/filters_are_not_reevaluated_for_the_same_span.rs":"251abbc000dddd298448958a1f0e5be71da527ac6c1a368d57837c83a5467329","tests/filters_are_reevaluated_for_different_call_sites.rs":"e0fdd8e930c043674702831b4d96f331e63aba824576bbac50b3f53bb0241cc7","tests/filters_dont_leak.rs":"d594266818a3461886da33bfcc76937d89a433ed6980226fc428706b216c093c","tests/future_send.rs":"3e9c9193219d12e342c18bbedb2f6ec940334202feb3cffba91601d6001b8575","tests/macro_imports.rs":"d5de857162185d4a2384f3cb644bfcf76c7f5c1a3b5f72bfa0d2620ac6e3873c","tests/macros.rs":"fa83397181d73d2cae09c16d9647a63d1e3bad0f2dbc5b3280f69f3d0180c488","tests/macros_incompatible_concat.rs":"5f3bcbb65e4ae39db1cfc2def62fc913c20bab0fb769c8f731504e2615585ee5","tests/macros_redefined_core.rs":"a6eac60522f71fe6c9a040b8b869d596f7eb9e907f5b49f4be4413a40c387676","tests/max_level_hint.rs":"9b366591d947ca0202fa0bdf797e1bb14534d3c896cf8b9674660cd2807c32ef","tests/multiple_max_level_hints.rs":"4d9ef0de9cccc787da8f5e3f6c233ac9db42a2a99cfe5e39997e1f4aa9df0c00","tests/no_subscriber.rs":"2f8f2ada5089d8e2e503394dfe8206598a11895907c53bf940b892f1e6afdd2f","tests/register_callsite_deadlock.rs":"c0b3142543e7a10065c7583a8ee0b6bc978ea4f3979599651101c5a28966e7c8","tests/scoped_clobbers_default.rs":"806480a74c15e4d68bb7576050662b1e53ee765fd583d003f8b349f17ea63a4b","tests/span.rs":"f84ead5b1dad9b91e5cec9d8378ab932a942936374ba928fb381e67fab52cda0","tests/subscriber.rs":"1617c098f4fa6abed174fe062111444c7b67fa0f377d2b342176998e572480e3"},"package":"a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160"}
\ No newline at end of file diff --git a/vendor/tracing/CHANGELOG.md b/vendor/tracing/CHANGELOG.md new file mode 100644 index 000000000..1c527f2de --- /dev/null +++ b/vendor/tracing/CHANGELOG.md @@ -0,0 +1,724 @@ +# 0.1.35 (June 8, 2022) + +This release reduces the overhead of callsite registration by using new +`tracing-core` APIs. + +### Added + +- Use `DefaultCallsite` to reduce callsite registration overhead ([#2083]) + +### Changed + +- `tracing-core`: updated to [0.1.27][core-0.1.27] + +[core-0.1.27]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.27 +[#2088]: https://github.com/tokio-rs/tracing/pull/2083 + +# 0.1.34 (April 14, 2022) + +This release includes bug fixes for the "log" support feature and for the use of +both scoped and global default dispatchers in the same program. + +### Fixed + +- Failure to use the global default dispatcher when a thread sets a local + default dispatcher before the global default is set ([#2065]) +- **log**: Compilation errors due to `async` block/fn futures becoming `!Send` + when the "log" feature flag is enabled ([#2073]) +- Broken links in documentation ([#2068]) + +Thanks to @ben0x539 for contributing to this release! + +[#2065]: https://github.com/tokio-rs/tracing/pull/2065 +[#2073]: https://github.com/tokio-rs/tracing/pull/2073 +[#2068]: https://github.com/tokio-rs/tracing/pull/2068 + +# 0.1.33 (April 9, 2022) + +This release adds new `span_enabled!` and `event_enabled!` variants of the +`enabled!` macro, for testing whether a subscriber would specifically enable a +span or an event. + +### Added + +- `span_enabled!` and `event_enabled!` macros ([#1900]) +- Several documentation improvements ([#2010], [#2012]) + +### Fixed + +- Compilation warning when compiling for <=32-bit targets (including `wasm32`) + ([#2060]) + +Thanks to @guswynn, @arifd, @hrxi, @CAD97, and @name1e5s for contributing to +this release! + +[#1900]: https://github.com/tokio-rs/tracing/pull/1900 +[#2010]: https://github.com/tokio-rs/tracing/pull/2010 +[#2012]: https://github.com/tokio-rs/tracing/pull/2012 +[#2060]: https://github.com/tokio-rs/tracing/pull/2060 + +# 0.1.32 (March 8th, 2022) + +This release reduces the overhead of creating and dropping disabled +spans significantly, which should improve performance when no `tracing` +subscriber is in use or when spans are disabled by a filter. + +### Fixed + +- **attributes**: Compilation failure with `--minimal-versions` due to a + too-permissive `syn` dependency ([#1960]) + +### Changed + +- Reduced `Drop` overhead for disabled spans ([#1974]) +- `tracing-attributes`: updated to [0.1.20][attributes-0.1.20] + +[#1974]: https://github.com/tokio-rs/tracing/pull/1974 +[#1960]: https://github.com/tokio-rs/tracing/pull/1960 +[attributes-0.1.20]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.20 + +# 0.1.31 (February 17th, 2022) + +This release increases the minimum supported Rust version (MSRV) to 1.49.0. In +addition, it fixes some relatively rare macro bugs. + +### Added + +- Added `tracing-forest` to the list of related crates ([#1935]) + +### Changed + +- Updated minimum supported Rust version (MSRV) to 1.49.0 ([#1913]) + +### Fixed + +- Fixed the `warn!` macro incorrectly generating an event with the `TRACE` level + ([#1930]) +- Fixed macro hygiene issues when used in a crate that defines its own `concat!` + macro, for real this time ([#1918]) + +Thanks to @QnnOkabayashi, @nicolaasg, and @teohhanhui for contributing to this +release! + +[#1935]: https://github.com/tokio-rs/tracing/pull/1935 +[#1913]: https://github.com/tokio-rs/tracing/pull/1913 +[#1930]: https://github.com/tokio-rs/tracing/pull/1930 +[#1918]: https://github.com/tokio-rs/tracing/pull/1918 + +# 0.1.30 (February 3rd, 2022) + +This release adds *experimental* support for recording structured field +values using the [`valuable`] crate. See [this blog post][post] for +details on `valuable`. + +Note that `valuable` support currently requires `--cfg tracing_unstable`. See +the documentation for details. + +This release also adds a new `enabled!` macro for testing if a span or event +would be enabled. + +### Added + +- **field**: Experimental support for recording field values using the + [`valuable`] crate ([#1608], [#1888], [#1887]) +- `enabled!` macro for testing if a span or event is enabled ([#1882]) + +### Changed + +- `tracing-core`: updated to [0.1.22][core-0.1.22] +- `tracing-attributes`: updated to [0.1.19][attributes-0.1.19] + +### Fixed + +- **log**: Fixed "use of moved value" compiler error when the "log" feature is + enabled ([#1823]) +- Fixed macro hygiene issues when used in a crate that defines its own `concat!` + macro ([#1842]) +- A very large number of documentation fixes and improvements. + +Thanks to @@Vlad-Scherbina, @Skepfyr, @Swatinem, @guswynn, @teohhanhui, +@xd009642, @tobz, @d-e-s-o@0b01, and @nickelc for contributing to this release! + +[`valuable`]: https://crates.io/crates/valuable +[post]: https://tokio.rs/blog/2021-05-valuable +[core-0.1.22]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.22 +[attributes-0.1.19]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.19 +[#1608]: https://github.com/tokio-rs/tracing/pull/1608 +[#1888]: https://github.com/tokio-rs/tracing/pull/1888 +[#1887]: https://github.com/tokio-rs/tracing/pull/1887 +[#1882]: https://github.com/tokio-rs/tracing/pull/1882 +[#1823]: https://github.com/tokio-rs/tracing/pull/1823 +[#1842]: https://github.com/tokio-rs/tracing/pull/1842 + +# 0.1.29 (October 5th, 2021) + +This release adds support for recording `Option<T> where T: Value` as typed +`tracing` field values. It also includes significant performance improvements +for functions annotated with the `#[instrument]` attribute when the generated +span is disabled. + +### Changed + +- `tracing-core`: updated to v0.1.21 +- `tracing-attributes`: updated to v0.1.18 + +### Added + +- **field**: `Value` impl for `Option<T> where T: Value` ([#1585]) +- **attributes**: - improved performance when skipping `#[instrument]`-generated + spans below the max level ([#1600], [#1605], [#1614], [#1616], [#1617]) + +### Fixed + +- **instrument**: added missing `Future` implementation for `WithSubscriber`, + making the `WithDispatch` extension trait actually useable ([#1602]) +- Documentation fixes and improvements ([#1595], [#1601], [#1597]) + +Thanks to @brianburgers, @mattiast, @DCjanus, @oli-obk, and @matklad for +contributing to this release! + +[#1585]: https://github.com/tokio-rs/tracing/pull/1585 +[#1595]: https://github.com/tokio-rs/tracing/pull/1596 +[#1597]: https://github.com/tokio-rs/tracing/pull/1597 +[#1600]: https://github.com/tokio-rs/tracing/pull/1600 +[#1601]: https://github.com/tokio-rs/tracing/pull/1601 +[#1602]: https://github.com/tokio-rs/tracing/pull/1602 +[#1605]: https://github.com/tokio-rs/tracing/pull/1605 +[#1614]: https://github.com/tokio-rs/tracing/pull/1614 +[#1616]: https://github.com/tokio-rs/tracing/pull/1616 +[#1617]: https://github.com/tokio-rs/tracing/pull/1617 + +# 0.1.28 (September 17th, 2021) + +This release fixes an issue where the RustDoc documentation was rendered +incorrectly. It doesn't include any actual code changes, and is very boring and +can be ignored. + +### Fixed + +- **docs**: Incorrect documentation rendering due to unclosed `<div>` tag + ([#1572]) + +[#1572]: https://github.com/tokio-rs/tracing/pull/1572 + +# 0.1.27 (September 13, 2021) + +This release adds a new [`Span::or_current`] method to aid in efficiently +propagating span contexts to spawned threads or tasks. Additionally, it updates +the [`tracing-core`] version to [0.1.20] and the [`tracing-attributes`] version to +[0.1.16], ensuring that a number of new features in those crates are present. + +### Fixed + +- **instrument**: Added missing `WithSubscriber` implementations for futures and + other types ([#1424]) + +### Added + +- `Span::or_current` method, to help with efficient span context propagation + ([#1538]) +- **attributes**: add `skip_all` option to `#[instrument]` ([#1548]) +- **attributes**: record primitive types as primitive values rather than as + `fmt::Debug` ([#1378]) +- **core**: `NoSubscriber`, a no-op `Subscriber` implementation + ([#1549]) +- **core**: Added `Visit::record_f64` and support for recording floating-point + values ([#1507], [#1522]) +- A large number of documentation improvements and fixes ([#1369], [#1398], + [#1435], [#1442], [#1524], [#1556]) + +Thanks to new contributors @dzvon and @mbergkvist, as well as @teozkr, +@maxburke, @LukeMathWalker, and @jsgf, for contributing to this +release! + +[`Span::or_current`]: https://docs.rs/tracing/0.1.27/tracing/struct.Span.html#method.or_current +[`tracing-core`]: https://crates.io/crates/tracing-core +[`tracing-attributes`]: https://crates.io/crates/tracing-attributes +[`tracing-core`]: https://crates.io/crates/tracing-core +[0.1.20]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.20 +[0.1.16]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.16 +[#1424]: https://github.com/tokio-rs/tracing/pull/1424 +[#1538]: https://github.com/tokio-rs/tracing/pull/1538 +[#1548]: https://github.com/tokio-rs/tracing/pull/1548 +[#1378]: https://github.com/tokio-rs/tracing/pull/1378 +[#1507]: https://github.com/tokio-rs/tracing/pull/1507 +[#1522]: https://github.com/tokio-rs/tracing/pull/1522 +[#1369]: https://github.com/tokio-rs/tracing/pull/1369 +[#1398]: https://github.com/tokio-rs/tracing/pull/1398 +[#1435]: https://github.com/tokio-rs/tracing/pull/1435 +[#1442]: https://github.com/tokio-rs/tracing/pull/1442 +[#1524]: https://github.com/tokio-rs/tracing/pull/1524 +[#1556]: https://github.com/tokio-rs/tracing/pull/1556 + +# 0.1.26 (April 30, 2021) + +### Fixed + +- **attributes**: Compatibility between `#[instrument]` and `async-trait` + v0.1.43 and newer ([#1228]) +- Several documentation fixes ([#1305], [#1344]) +### Added + +- `Subscriber` impl for `Box<dyn Subscriber + Send + Sync + 'static>` ([#1358]) +- `Subscriber` impl for `Arc<dyn Subscriber + Send + Sync + 'static>` ([#1374]) +- Symmetric `From` impls for existing `Into` impls on `span::Current`, `Span`, + and `Option<Id>` ([#1335], [#1338]) +- `From<EnteredSpan>` implementation for `Option<Id>`, allowing `EnteredSpan` to + be used in a `span!` macro's `parent:` field ([#1325]) +- `Attributes::fields` accessor that returns the set of fields defined on a + span's `Attributes` ([#1331]) + + +Thanks to @Folyd, @nightmared, and new contributors @rmsc and @Fishrock123 for +contributing to this release! + +[#1227]: https://github.com/tokio-rs/tracing/pull/1228 +[#1305]: https://github.com/tokio-rs/tracing/pull/1305 +[#1325]: https://github.com/tokio-rs/tracing/pull/1325 +[#1338]: https://github.com/tokio-rs/tracing/pull/1338 +[#1344]: https://github.com/tokio-rs/tracing/pull/1344 +[#1358]: https://github.com/tokio-rs/tracing/pull/1358 +[#1374]: https://github.com/tokio-rs/tracing/pull/1374 +[#1335]: https://github.com/tokio-rs/tracing/pull/1335 +[#1331]: https://github.com/tokio-rs/tracing/pull/1331 + +# 0.1.25 (February 23, 2021) + +### Added + +- `Span::entered` method for entering a span and moving it into a guard by value + rather than borrowing it ([#1252]) + +Thanks to @matklad for contributing to this release! + +[#1252]: https://github.com/tokio-rs/tracing/pull/1252 + +# 0.1.24 (February 17, 2021) + +### Fixed + +- **attributes**: Compiler error when using `#[instrument(err)]` on functions + which return `impl Trait` ([#1236]) +- Fixed broken match arms in event macros ([#1239]) +- Documentation improvements ([#1232]) + +Thanks to @bkchr and @lfranke for contributing to this release! + +[#1236]: https://github.com/tokio-rs/tracing/pull/1236 +[#1239]: https://github.com/tokio-rs/tracing/pull/1239 +[#1232]: https://github.com/tokio-rs/tracing/pull/1232 + +# 0.1.23 (February 4, 2021) + +### Fixed + +- **attributes**: Compiler error when using `#[instrument(err)]` on functions + with mutable parameters ([#1167]) +- **attributes**: Missing function visibility modifier when using + `#[instrument]` with `async-trait` ([#977]) +- **attributes** Removed unused `syn` features ([#928]) +- **log**: Fixed an issue where the `tracing` macros would generate code for + events whose levels are disabled statically by the `log` crate's + `static_max_level_XXX` features ([#1175]) +- Fixed deprecations and clippy lints ([#1195]) +- Several documentation fixes and improvements ([#941], [#965], [#981], [#1146], + [#1215]) + +### Changed + +- **attributes**: `tracing-futures` dependency is no longer required when using + `#[instrument]` on async functions ([#808]) +- **attributes**: Updated `tracing-attributes` minimum dependency to v0.1.12 + ([#1222]) + +Thanks to @nagisa, @Txuritan, @TaKO8Ki, @okready, and @krojew for contributing +to this release! + +[#1167]: https://github.com/tokio-rs/tracing/pull/1167 +[#977]: https://github.com/tokio-rs/tracing/pull/977 +[#965]: https://github.com/tokio-rs/tracing/pull/965 +[#981]: https://github.com/tokio-rs/tracing/pull/981 +[#1215]: https://github.com/tokio-rs/tracing/pull/1215 +[#808]: https://github.com/tokio-rs/tracing/pull/808 +[#941]: https://github.com/tokio-rs/tracing/pull/941 +[#1146]: https://github.com/tokio-rs/tracing/pull/1146 +[#1175]: https://github.com/tokio-rs/tracing/pull/1175 +[#1195]: https://github.com/tokio-rs/tracing/pull/1195 +[#1222]: https://github.com/tokio-rs/tracing/pull/1222 + +# 0.1.22 (November 23, 2020) + +### Changed + +- Updated `pin-project-lite` dependency to 0.2 ([#1108]) + +[#1108]: https://github.com/tokio-rs/tracing/pull/1108 + +# 0.1.21 (September 28, 2020) + +### Fixed + +- Incorrect inlining of `Span::new`, `Span::new_root`, and `Span::new_child_of`, + which could result in `dispatcher::get_default` being inlined at the callsite + ([#994]) +- Regression where using a struct field as a span or event field when other + fields on that struct are borrowed mutably would fail to compile ([#987]) + +### Changed + +- Updated `tracing-core` to 0.1.17 ([#992]) + +### Added + +- `Instrument` trait and `Instrumented` type for attaching a `Span` to a + `Future` ([#808]) +- `Copy` implementations for `Level` and `LevelFilter` ([#992]) +- Multiple documentation fixes and improvements ([#964], [#980], [#981]) + +Thanks to @nagisa, and new contributors @SecurityInsanity, @froydnj, @jyn514 and +@TaKO8Ki for contributing to this release! + +[#994]: https://github.com/tokio-rs/tracing/pull/994 +[#992]: https://github.com/tokio-rs/tracing/pull/992 +[#987]: https://github.com/tokio-rs/tracing/pull/987 +[#980]: https://github.com/tokio-rs/tracing/pull/980 +[#981]: https://github.com/tokio-rs/tracing/pull/981 +[#964]: https://github.com/tokio-rs/tracing/pull/964 +[#808]: https://github.com/tokio-rs/tracing/pull/808 + +# 0.1.20 (August 24, 2020) + +### Changed + +- Significantly reduced assembly generated by macro invocations (#943) +- Updated `tracing-core` to 0.1.15 (#943) + +### Added + +- Documented minimum supported Rust version policy (#941) + +# 0.1.19 (August 10, 2020) + +### Fixed + +- Updated `tracing-core` to fix incorrect calculation of the global max level + filter (#908) + +### Added + +- **attributes**: Support for using `self` in field expressions when + instrumenting `async-trait` functions (#875) +- Several documentation improvements (#832, #881, #896, #897, #911, #913) + +Thanks to @anton-dutov, @nightmared, @mystor, and @toshokan for contributing to +this release! + +# 0.1.18 (July 31, 2020) + +### Fixed + +- Fixed a bug where `LevelFilter::OFF` (and thus also the `static_max_level_off` + feature flag) would enable *all* traces, rather than *none* (#853) +- **log**: Fixed `tracing` macros and `Span`s not checking `log::max_level` + before emitting `log` records (#870) + +### Changed + +- **macros**: Macros now check the global max level (`LevelFilter::current`) + before the per-callsite cache when determining if a span or event is enabled. + This significantly improves performance in some use cases (#853) +- **macros**: Simplified the code generated by macro expansion significantly, + which may improve compile times and/or `rustc` optimizatation of surrounding + code (#869, #869) +- **macros**: Macros now check the static max level before checking any runtime + filtering, improving performance when a span or event is disabled by a + `static_max_level_XXX` feature flag (#868) +- `LevelFilter` is now a re-export of the `tracing_core::LevelFilter` type, it + can now be used interchangably with the versions in `tracing-core` and + `tracing-subscriber` (#853) +- Significant performance improvements when comparing `LevelFilter`s and + `Level`s (#853) +- Updated the minimum `tracing-core` dependency to 0.1.12 (#853) + +### Added + +- **macros**: Quoted string literals may now be used as field names, to allow + fields whose names are not valid Rust identifiers (#790) +- **docs**: Several documentation improvements (#850, #857, #841) +- `LevelFilter::current()` function, which returns the highest level that any + subscriber will enable (#853) +- `Subscriber::max_level_hint` optional trait method, for setting the value + returned by `LevelFilter::current()` (#853) + +Thanks to new contributors @cuviper, @ethanboxx, @ben0x539, @dignati, +@colelawrence, and @rbtcollins for helping out with this release! + +# 0.1.17 (July 22, 2020) + +### Changed + +- **log**: Moved verbose span enter/exit log records to "tracing::span::active" + target, allowing them to be filtered separately (#833) +- **log**: All span lifecycle log records without fields now have the `Trace` + log filter, to guard against `log` users enabling them by default with blanket + level filtering (#833) + +### Fixed + +- **log**/**macros**: Fixed missing implicit imports of the + `tracing::field::debug` and `tracing::field::display` functions inside the + macros when the "log" feature is enabled (#835) + +# 0.1.16 (July 8, 2020) + +### Added + +- **attributes**: Support for arbitrary expressions as fields in `#[instrument]` (#672) +- **attributes**: `#[instrument]` now emits a compiler warning when ignoring unrecognized + input (#672, #786) +- Improved documentation on using `tracing` in async code (#769) + +### Changed + +- Updated `tracing-core` dependency to 0.1.11 + +### Fixed + +- **macros**: Excessive monomorphization in macros, which could lead to + longer compilation times (#787) +- **log**: Compiler warnings in macros when `log` or `log-always` features + are enabled (#753) +- Compiler error when `tracing-core/std` feature is enabled but `tracing/std` is + not (#760) + +Thanks to @nagisa for contributing to this release! + +# 0.1.15 (June 2, 2020) + +### Changed + +- **macros**: Replaced use of legacy `local_inner_macros` with `$crate::` (#740) + +### Added + +- Docs fixes and improvements (#742, #731, #730) + +Thanks to @bnjjj, @blaenk, and @LukeMathWalker for contributing to this release! + +# 0.1.14 (May 14, 2020) + +### Added + +- **log**: When using the [`log`] compatibility feature alongside a `tracing` + `Subscriber`, log records for spans now include span IDs (#613) +- **attributes**: Support for using `#[instrument]` on methods that are part of + [`async-trait`] trait implementations (#711) +- **attributes**: Optional `#[instrument(err)]` argument to automatically emit + an event if an instrumented function returns `Err` (#637) +- Added `#[must_use]` attribute to the guard returned by + `subscriber::set_default` (#685) + +### Changed + +- **log**: Made [`log`] records emitted by spans much less noisy when span IDs are + not available (#613) + +### Fixed + +- Several typos in the documentation (#656, #710, #715) + +Thanks to @FintanH, @shepmaster, @inanna-malick, @zekisharif, @bkchr, @majecty, +@ilana and @nightmared for contributing to this release! + +[`async-trait`]: https://crates.io/crates/async-trait +[`log`]: https://crates.io/crates/log + +# 0.1.13 (February 26, 2019) + +### Added + +- **field**: `field::Empty` type for declaring empty fields whose values will be + recorded later (#548) +- **field**: `field::Value` implementations for `Wrapping` and `NonZero*` + numbers (#538) +- **attributes**: Support for adding arbitrary literal fields to spans generated + by `#[instrument]` (#569) +- **attributes**: `#[instrument]` now emits a helpful compiler error when + attempting to skip a function parameter (#600) + +### Changed + +- **attributes**: The `#[instrument]` attribute was placed under an on-by-default + feature flag "attributes" (#603) + +### Fixed + +- Broken and unresolvable links in RustDoc (#595) + +Thanks to @oli-cosmian and @Kobzol for contributing to this release! + +# 0.1.12 (January 11, 2019) + +### Added + +- `Span::with_subscriber` method to access the subscriber that tracks a `Span` + (#503) +- API documentation now shows which features are required by feature-flagged + items (#523) +- Improved README examples (#496) +- Documentation links to related crates (#507) + +# 0.1.11 (December 20, 2019) + +### Added + +- `Span::is_none` method (#475) +- `LevelFilter::into_level` method (#470) +- `LevelFilter::from_level` function and `From<Level>` impl (#471) +- Documented minimum supported Rust version (#482) + +### Fixed + +- Incorrect parameter type to `Span::follows_from` that made it impossible to + call (#467) +- Missing whitespace in `log` records generated when enabling the `log` feature + flag (#484) +- Typos and missing links in documentation (#405, #423, #439) + +# 0.1.10 (October 23, 2019) + +### Added + +- Support for destructuring in arguments to `#[instrument]`ed functions (#397) +- Generated field for `self` parameters when `#[instrument]`ing methods (#397) +- Optional `skip` argument to `#[instrument]` for excluding function parameters + from generated spans (#359) +- Added `dispatcher::set_default` and `subscriber::set_default` APIs, which + return a drop guard (#388) + +### Fixed + +- Some minor documentation errors (#356, #370) + +# 0.1.9 (September 13, 2019) + +### Fixed + +- Fixed `#[instrument]`ed async functions not compiling on `nightly-2019-09-11` + or newer (#342) + +### Changed + +- Significantly reduced performance impact of skipped spans and events when a + `Subscriber` is not in use (#326) +- The `log` feature will now only cause `tracing` spans and events to emit log + records when a `Subscriber` is not in use (#346) + +### Added + +- Added support for overriding the name of the span generated by `#[instrument]` + (#330) +- `log-always` feature flag to emit log records even when a `Subscriber` is set + (#346) + +# 0.1.8 (September 3, 2019) + +### Changed + +- Reorganized and improved API documentation (#317) + +### Removed + +- Dev-dependencies on `ansi_term` and `humantime` crates, which were used only + for examples (#316) + +# 0.1.7 (August 30, 2019) + +### Changed + +- New (curly-brace free) event message syntax to place the message in the first + field rather than the last (#309) + +### Fixed + +- Fixed a regression causing macro stack exhaustion when the `log` feature flag + is enabled (#304) + +# 0.1.6 (August 20, 2019) + +### Added + +- `std::error::Error` as a new primitive type (#277) +- Support for mixing key-value fields and `format_args` messages without curly + braces as delimiters (#288) + +### Changed + +- `tracing-core` dependency to 0.1.5 (#294) +- `tracing-attributes` dependency to 0.1.2 (#297) + +# 0.1.5 (August 9, 2019) + +### Added + +- Support for `no-std` + `liballoc` (#263) + +### Changed + +- Using the `#[instrument]` attribute on `async fn`s no longer requires a + feature flag (#258) + +### Fixed + +- The `#[instrument]` macro now works on generic functions (#262) + +# 0.1.4 (August 8, 2019) + +### Added + +- `#[instrument]` attribute for automatically adding spans to functions (#253) + +# 0.1.3 (July 11, 2019) + +### Added + +- Log messages when a subscriber indicates that a span has closed, when the + `log` feature flag is enabled (#180). + +### Changed + +- `tracing-core` minimum dependency version to 0.1.2 (#174). + +### Fixed + +- Fixed an issue where event macro invocations with a single field, using local + variable shorthand, would recur infinitely (#166). +- Fixed uses of deprecated `tracing-core` APIs (#174). + +# 0.1.2 (July 6, 2019) + +### Added + +- `Span::none()` constructor, which does not require metadata and + returns a completely empty span (#147). +- `Span::current()` function, returning the current span if it is + known to the subscriber (#148). + +### Fixed + +- Broken macro imports when used prefixed with `tracing::` (#152). + +# 0.1.1 (July 3, 2019) + +### Changed + +- `cfg_if` dependency to 0.1.9. + +### Fixed + +- Compilation errors when the `log` feature is enabled (#131). +- Unclear wording and typos in documentation (#124, #128, #142). + +# 0.1.0 (June 27, 2019) + +- Initial release diff --git a/vendor/tracing/Cargo.toml b/vendor/tracing/Cargo.toml new file mode 100644 index 000000000..dd6f1fb47 --- /dev/null +++ b/vendor/tracing/Cargo.toml @@ -0,0 +1,114 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +rust-version = "1.49.0" +name = "tracing" +version = "0.1.35" +authors = [ + "Eliza Weisman <eliza@buoyant.io>", + "Tokio Contributors <team@tokio.rs>", +] +description = """ +Application-level tracing for Rust. +""" +homepage = "https://tokio.rs" +readme = "README.md" +keywords = [ + "logging", + "tracing", + "metrics", + "async", +] +categories = [ + "development-tools::debugging", + "development-tools::profiling", + "asynchronous", + "no-std", +] +license = "MIT" +repository = "https://github.com/tokio-rs/tracing" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = [ + "--cfg", + "docsrs", + "--cfg", + "tracing_unstable", +] +rustc-args = [ + "--cfg", + "tracing_unstable", +] + +[[bench]] +name = "subscriber" +harness = false + +[[bench]] +name = "no_subscriber" +harness = false + +[dependencies.cfg-if] +version = "1.0.0" + +[dependencies.log] +version = "0.4" +optional = true + +[dependencies.pin-project-lite] +version = "0.2" + +[dependencies.tracing-attributes] +version = "0.1.20" +optional = true + +[dependencies.tracing-core] +version = "0.1.27" +default-features = false + +[dev-dependencies.criterion] +version = "0.3" +default_features = false + +[dev-dependencies.log] +version = "0.4" + +[features] +async-await = [] +attributes = ["tracing-attributes"] +default = [ + "std", + "attributes", +] +log-always = ["log"] +max_level_debug = [] +max_level_error = [] +max_level_info = [] +max_level_off = [] +max_level_trace = [] +max_level_warn = [] +release_max_level_debug = [] +release_max_level_error = [] +release_max_level_info = [] +release_max_level_off = [] +release_max_level_trace = [] +release_max_level_warn = [] +std = ["tracing-core/std"] +valuable = ["tracing-core/valuable"] + +[target."cfg(target_arch = \"wasm32\")".dev-dependencies.wasm-bindgen-test] +version = "^0.3" + +[badges.maintenance] +status = "actively-developed" diff --git a/vendor/tracing/LICENSE b/vendor/tracing/LICENSE new file mode 100644 index 000000000..cdb28b4b5 --- /dev/null +++ b/vendor/tracing/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2019 Tokio Contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/tracing/README.md b/vendor/tracing/README.md new file mode 100644 index 000000000..5da141a3a --- /dev/null +++ b/vendor/tracing/README.md @@ -0,0 +1,463 @@ +![Tracing — Structured, application-level diagnostics][splash] + +[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg + +# tracing + +Application-level tracing for Rust. + +[![Crates.io][crates-badge]][crates-url] +[![Documentation][docs-badge]][docs-url] +[![Documentation (master)][docs-master-badge]][docs-master-url] +[![MIT licensed][mit-badge]][mit-url] +[![Build Status][actions-badge]][actions-url] +[![Discord chat][discord-badge]][discord-url] + +[Documentation][docs-url] | [Chat][discord-url] + +[crates-badge]: https://img.shields.io/crates/v/tracing.svg +[crates-url]: https://crates.io/crates/tracing/0.1.35 +[docs-badge]: https://docs.rs/tracing/badge.svg +[docs-url]: https://docs.rs/tracing/0.1.35 +[docs-master-badge]: https://img.shields.io/badge/docs-master-blue +[docs-master-url]: https://tracing-rs.netlify.com/tracing +[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg +[mit-url]: LICENSE +[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg +[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI +[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white +[discord-url]: https://discord.gg/EeF3cQw + +## Overview + +`tracing` is a framework for instrumenting Rust programs to collect +structured, event-based diagnostic information. + +In asynchronous systems like Tokio, interpreting traditional log messages can +often be quite challenging. Since individual tasks are multiplexed on the same +thread, associated events and log lines are intermixed making it difficult to +trace the logic flow. `tracing` expands upon logging-style diagnostics by +allowing libraries and applications to record structured events with additional +information about *temporality* and *causality* — unlike a log message, a span +in `tracing` has a beginning and end time, may be entered and exited by the +flow of execution, and may exist within a nested tree of similar spans. In +addition, `tracing` spans are *structured*, with the ability to record typed +data as well as textual messages. + +The `tracing` crate provides the APIs necessary for instrumenting libraries +and applications to emit trace data. + +*Compiler support: [requires `rustc` 1.49+][msrv]* + +[msrv]: #supported-rust-versions + +## Usage + +(The examples below are borrowed from the `log` crate's yak-shaving +[example](https://docs.rs/log/0.4.10/log/index.html#examples), modified to +idiomatic `tracing`.) + +### In Applications + +In order to record trace events, executables have to use a `Subscriber` +implementation compatible with `tracing`. A `Subscriber` implements a way of +collecting trace data, such as by logging it to standard output. [`tracing_subscriber`](https://docs.rs/tracing-subscriber/)'s +[`fmt` module](https://docs.rs/tracing-subscriber/0.3/tracing_subscriber/fmt/index.html) provides reasonable defaults. +Additionally, `tracing-subscriber` is able to consume messages emitted by `log`-instrumented libraries and modules. + +The simplest way to use a subscriber is to call the `set_global_default` function. + +```rust +use tracing::{info, Level}; +use tracing_subscriber::FmtSubscriber; + +fn main() { + // a builder for `FmtSubscriber`. + let subscriber = FmtSubscriber::builder() + // all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.) + // will be written to stdout. + .with_max_level(Level::TRACE) + // completes the builder. + .finish(); + + tracing::subscriber::set_global_default(subscriber) + .expect("setting default subscriber failed"); + + let number_of_yaks = 3; + // this creates a new event, outside of any spans. + info!(number_of_yaks, "preparing to shave yaks"); + + let number_shaved = yak_shave::shave_all(number_of_yaks); + info!( + all_yaks_shaved = number_shaved == number_of_yaks, + "yak shaving completed." + ); +} +``` + +```toml +[dependencies] +tracing = "0.1" +tracing-subscriber = "0.2.0" +``` + +This subscriber will be used as the default in all threads for the remainder of the duration +of the program, similar to how loggers work in the `log` crate. + +In addition, you can locally override the default subscriber. For example: + +```rust +use tracing::{info, Level}; +use tracing_subscriber::FmtSubscriber; + +fn main() { + let subscriber = tracing_subscriber::FmtSubscriber::builder() + // all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.) + // will be written to stdout. + .with_max_level(Level::TRACE) + // builds the subscriber. + .finish(); + + tracing::subscriber::with_default(subscriber, || { + info!("This will be logged to stdout"); + }); + info!("This will _not_ be logged to stdout"); +} +``` + +This approach allows trace data to be collected by multiple subscribers +within different contexts in the program. Note that the override only applies to the +currently executing thread; other threads will not see the change from with_default. + +Any trace events generated outside the context of a subscriber will not be collected. + +Once a subscriber has been set, instrumentation points may be added to the +executable using the `tracing` crate's macros. + +### In Libraries + +Libraries should only rely on the `tracing` crate and use the provided macros +and types to collect whatever information might be useful to downstream consumers. + +```rust +use std::{error::Error, io}; +use tracing::{debug, error, info, span, warn, Level}; + +// the `#[tracing::instrument]` attribute creates and enters a span +// every time the instrumented function is called. The span is named after the +// the function or method. Paramaters passed to the function are recorded as fields. +#[tracing::instrument] +pub fn shave(yak: usize) -> Result<(), Box<dyn Error + 'static>> { + // this creates an event at the DEBUG level with two fields: + // - `excitement`, with the key "excitement" and the value "yay!" + // - `message`, with the key "message" and the value "hello! I'm gonna shave a yak." + // + // unlike other fields, `message`'s shorthand initialization is just the string itself. + debug!(excitement = "yay!", "hello! I'm gonna shave a yak."); + if yak == 3 { + warn!("could not locate yak!"); + // note that this is intended to demonstrate `tracing`'s features, not idiomatic + // error handling! in a library or application, you should consider returning + // a dedicated `YakError`. libraries like snafu or thiserror make this easy. + return Err(io::Error::new(io::ErrorKind::Other, "shaving yak failed!").into()); + } else { + debug!("yak shaved successfully"); + } + Ok(()) +} + +pub fn shave_all(yaks: usize) -> usize { + // Constructs a new span named "shaving_yaks" at the TRACE level, + // and a field whose key is "yaks". This is equivalent to writing: + // + // let span = span!(Level::TRACE, "shaving_yaks", yaks = yaks); + // + // local variables (`yaks`) can be used as field values + // without an assignment, similar to struct initializers. + let _span_ = span!(Level::TRACE, "shaving_yaks", yaks).entered(); + + info!("shaving yaks"); + + let mut yaks_shaved = 0; + for yak in 1..=yaks { + let res = shave(yak); + debug!(yak, shaved = res.is_ok()); + + if let Err(ref error) = res { + // Like spans, events can also use the field initialization shorthand. + // In this instance, `yak` is the field being initalized. + error!(yak, error = error.as_ref(), "failed to shave yak!"); + } else { + yaks_shaved += 1; + } + debug!(yaks_shaved); + } + + yaks_shaved +} +``` + +```toml +[dependencies] +tracing = "0.1" +``` + +Note: Libraries should *NOT* call `set_global_default()`, as this will cause +conflicts when executables try to set the default later. + +### In Asynchronous Code + +If you are instrumenting code that make use of +[`std::future::Future`](https://doc.rust-lang.org/stable/std/future/trait.Future.html) +or async/await, avoid using the `Span::enter` method. The following example +_will not_ work: + +```rust +async { + let _s = span.enter(); + // ... +} +``` +```rust +async { + let _s = tracing::span!(...).entered(); + // ... +} +``` + +The span guard `_s` will not exit until the future generated by the `async` block is complete. +Since futures and spans can be entered and exited _multiple_ times without them completing, +the span remains entered for as long as the future exists, rather than being entered only when +it is polled, leading to very confusing and incorrect output. +For more details, see [the documentation on closing spans](https://tracing.rs/tracing/span/index.html#closing-spans). + +There are two ways to instrument asynchronous code. The first is through the +[`Future::instrument`](https://docs.rs/tracing/latest/tracing/trait.Instrument.html#method.instrument) combinator: + +```rust +use tracing::Instrument; + +let my_future = async { + // ... +}; + +my_future + .instrument(tracing::info_span!("my_future")) + .await +``` + +`Future::instrument` attaches a span to the future, ensuring that the span's lifetime +is as long as the future's. + +The second, and preferred, option is through the +[`#[instrument]`](https://docs.rs/tracing/0.1.35/tracing/attr.instrument.html) +attribute: + +```rust +use tracing::{info, instrument}; +use tokio::{io::AsyncWriteExt, net::TcpStream}; +use std::io; + +#[instrument] +async fn write(stream: &mut TcpStream) -> io::Result<usize> { + let result = stream.write(b"hello world\n").await; + info!("wrote to stream; success={:?}", result.is_ok()); + result +} +``` + +Under the hood, the `#[instrument]` macro performs the same explicit span +attachment that `Future::instrument` does. + +### Concepts + +This crate provides macros for creating `Span`s and `Event`s, which represent +periods of time and momentary events within the execution of a program, +respectively. + +As a rule of thumb, _spans_ should be used to represent discrete units of work +(e.g., a given request's lifetime in a server) or periods of time spent in a +given context (e.g., time spent interacting with an instance of an external +system, such as a database). In contrast, _events_ should be used to represent +points in time within a span — a request returned with a given status code, +_n_ new items were taken from a queue, and so on. + +`Span`s are constructed using the `span!` macro, and then _entered_ +to indicate that some code takes place within the context of that `Span`: + +```rust +use tracing::{span, Level}; + +// Construct a new span named "my span". +let mut span = span!(Level::INFO, "my span"); +span.in_scope(|| { + // Any trace events in this closure or code called by it will occur within + // the span. +}); +// Dropping the span will close it, indicating that it has ended. +``` + +The [`#[instrument]`](https://docs.rs/tracing/0.1.35/tracing/attr.instrument.html) attribute macro +can reduce some of this boilerplate: + +```rust +use tracing::{instrument}; + +#[instrument] +pub fn my_function(my_arg: usize) { + // This event will be recorded inside a span named `my_function` with the + // field `my_arg`. + tracing::info!("inside my_function!"); + // ... +} +``` + +The `Event` type represent an event that occurs instantaneously, and is +essentially a `Span` that cannot be entered. They are created using the `event!` +macro: + +```rust +use tracing::{event, Level}; + +event!(Level::INFO, "something has happened!"); +``` + +Users of the [`log`] crate should note that `tracing` exposes a set of macros for +creating `Event`s (`trace!`, `debug!`, `info!`, `warn!`, and `error!`) which may +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 + +In addition to `tracing` and `tracing-core`, the [`tokio-rs/tracing`] repository +contains several additional crates designed to be used with the `tracing` ecosystem. +This includes a collection of `Subscriber` implementations, as well as utility +and adapter crates to assist in writing `Subscriber`s and instrumenting +applications. + +In particular, the following crates are likely to be of interest: + +- [`tracing-futures`] provides a compatibility layer with the `futures` + crate, allowing spans to be attached to `Future`s, `Stream`s, and `Executor`s. +- [`tracing-subscriber`] provides `Subscriber` implementations and + utilities for working with `Subscriber`s. This includes a [`FmtSubscriber`] + `FmtSubscriber` for logging formatted trace data to stdout, with similar + filtering and formatting to the [`env_logger`] crate. +- [`tracing-log`] provides a compatibility layer with the [`log`] crate, + allowing log messages to be recorded as `tracing` `Event`s within the + trace tree. This is useful when a project using `tracing` have + dependencies which use `log`. Note that if you're using + `tracing-subscriber`'s `FmtSubscriber`, you don't need to depend on + `tracing-log` directly. + +Additionally, there are also several third-party crates which are not +maintained by the `tokio` project. These include: + +- [`tracing-timing`] implements inter-event timing metrics on top of `tracing`. + It provides a subscriber that records the time elapsed between pairs of + `tracing` events and generates histograms. +- [`tracing-opentelemetry`] provides a subscriber for emitting traces to + [OpenTelemetry]-compatible distributed tracing systems. +- [`tracing-honeycomb`] Provides a layer that reports traces spanning multiple machines to [honeycomb.io]. Backed by [`tracing-distributed`]. +- [`tracing-distributed`] Provides a generic implementation of a layer that reports traces spanning multiple machines to some backend. +- [`tracing-actix`] provides `tracing` integration for the `actix` actor + framework. +- [`tracing-gelf`] implements a subscriber for exporting traces in Greylog + GELF format. +- [`tracing-coz`] provides integration with the [coz] causal profiler + (Linux-only). +- [`test-log`] takes care of initializing `tracing` for tests, based on + environment variables with an `env_logger` compatible syntax. +- [`tracing-unwrap`] provides convenience methods to report failed unwraps on `Result` or `Option` types to a `Subscriber`. +- [`diesel-tracing`] provides integration with [`diesel`] database connections. +- [`tracing-tracy`] provides a way to collect [Tracy] profiles in instrumented + applications. +- [`tracing-elastic-apm`] provides a layer for reporting traces to [Elastic APM]. +- [`tracing-etw`] provides a layer for emitting Windows [ETW] events. +- [`tracing-fluent-assertions`] provides a fluent assertions-style testing + framework for validating the behavior of `tracing` spans. +- [`sentry-tracing`] provides a layer for reporting events and traces to [Sentry]. +- [`tracing-loki`] provides a layer for shipping logs to [Grafana Loki]. + +If you're the maintainer of a `tracing` ecosystem crate not listed above, +please let us know! We'd love to add your project to the list! + +[`tracing-timing`]: https://crates.io/crates/tracing-timing +[`tracing-opentelemetry`]: https://crates.io/crates/tracing-opentelemetry +[OpenTelemetry]: https://opentelemetry.io/ +[`tracing-honeycomb`]: https://crates.io/crates/tracing-honeycomb +[`tracing-distributed`]: https://crates.io/crates/tracing-distributed +[honeycomb.io]: https://www.honeycomb.io/ +[`tracing-actix`]: https://crates.io/crates/tracing-actix +[`tracing-gelf`]: https://crates.io/crates/tracing-gelf +[`tracing-coz`]: https://crates.io/crates/tracing-coz +[coz]: https://github.com/plasma-umass/coz +[`test-log`]: https://crates.io/crates/test-log +[`tracing-unwrap`]: https://docs.rs/tracing-unwrap +[`diesel`]: https://crates.io/crates/diesel +[`diesel-tracing`]: https://crates.io/crates/diesel-tracing +[`tracing-tracy`]: https://crates.io/crates/tracing-tracy +[Tracy]: https://github.com/wolfpld/tracy +[`tracing-elastic-apm`]: https://crates.io/crates/tracing-elastic-apm +[Elastic APM]: https://www.elastic.co/apm +[`tracing-etw`]: https://github.com/microsoft/tracing-etw +[ETW]: https://docs.microsoft.com/en-us/windows/win32/etw/about-event-tracing +[`tracing-fluent-assertions`]: https://crates.io/crates/tracing-fluent-assertions +[`sentry-tracing`]: https://crates.io/crates/sentry-tracing +[Sentry]: https://sentry.io/welcome/ +[`tracing-loki`]: https://crates.io/crates/tracing-loki +[Grafana Loki]: https://grafana.com/oss/loki/ + +**Note:** that some of the ecosystem crates are currently unreleased and +undergoing active development. They may be less stable than `tracing` and +`tracing-core`. + +[`log`]: https://docs.rs/log/0.4.6/log/ +[`tokio-rs/tracing`]: https://github.com/tokio-rs/tracing +[`tracing-futures`]: https://github.com/tokio-rs/tracing/tree/master/tracing-futures +[`tracing-subscriber`]: https://github.com/tokio-rs/tracing/tree/master/tracing-subscriber +[`tracing-log`]: https://github.com/tokio-rs/tracing/tree/master/tracing-log +[`env_logger`]: https://crates.io/crates/env_logger +[`FmtSubscriber`]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/struct.Subscriber.html +[`examples`]: https://github.com/tokio-rs/tracing/tree/master/examples + +## Supported Rust Versions + +Tracing is built against the latest stable release. The minimum supported +version is 1.49. 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. + +## License + +This project is licensed under the [MIT license](LICENSE). + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in Tokio by you, shall be licensed as MIT, without any additional +terms or conditions. diff --git a/vendor/tracing/benches/global_subscriber.rs b/vendor/tracing/benches/global_subscriber.rs new file mode 100644 index 000000000..83519610a --- /dev/null +++ b/vendor/tracing/benches/global_subscriber.rs @@ -0,0 +1,136 @@ +use std::fmt::Write; + +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tracing::Level; + +use tracing::{span, Event, Id, Metadata}; + +/// A subscriber that is enabled but otherwise does nothing. +struct EnabledSubscriber; + +impl tracing::Subscriber for EnabledSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let _ = span; + Id::from_u64(0xDEAD_FACE) + } + + fn event(&self, event: &Event<'_>) { + let _ = event; + } + + fn record(&self, span: &Id, values: &span::Record<'_>) { + let _ = (span, values); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +const NOP_LOGGER: NopLogger = NopLogger; + +struct NopLogger; + +impl log::Log for NopLogger { + fn enabled(&self, _metadata: &log::Metadata) -> bool { + true + } + + fn log(&self, record: &log::Record) { + if self.enabled(record.metadata()) { + let mut this = self; + let _ = write!(this, "{}", record.args()); + } + } + + fn flush(&self) {} +} + +impl Write for &NopLogger { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + black_box(s); + Ok(()) + } +} + +const N_SPANS: usize = 100; + +fn criterion_benchmark(c: &mut Criterion) { + let mut c = c.benchmark_group("global/subscriber"); + let _ = tracing::subscriber::set_global_default(EnabledSubscriber); + let _ = log::set_logger(&NOP_LOGGER); + log::set_max_level(log::LevelFilter::Trace); + c.bench_function("span_no_fields", |b| b.iter(|| span!(Level::TRACE, "span"))); + + c.bench_function("event", |b| { + b.iter(|| { + tracing::event!(Level::TRACE, "hello"); + }) + }); + + c.bench_function("enter_span", |b| { + let span = span!(Level::TRACE, "span"); + #[allow(clippy::unit_arg)] + b.iter(|| black_box(span.in_scope(|| {}))) + }); + + c.bench_function("span_repeatedly", |b| { + #[inline] + fn mk_span(i: u64) -> tracing::Span { + span!(Level::TRACE, "span", i = i) + } + + let n = black_box(N_SPANS); + b.iter(|| (0..n).fold(mk_span(0), |_, i| mk_span(i as u64))) + }); + + c.bench_function("span_with_fields", |b| { + b.iter(|| { + span!( + Level::TRACE, + "span", + foo = "foo", + bar = "bar", + baz = 3, + quuux = tracing::field::debug(0.99) + ) + }); + }); +} + +fn bench_dispatch(c: &mut Criterion) { + let mut group = c.benchmark_group("global/dispatch"); + let _ = tracing::subscriber::set_global_default(EnabledSubscriber); + let _ = log::set_logger(&NOP_LOGGER); + log::set_max_level(log::LevelFilter::Trace); + group.bench_function("get_ref", |b| { + b.iter(|| { + tracing::dispatcher::get_default(|current| { + black_box(¤t); + }) + }) + }); + group.bench_function("get_clone", |b| { + b.iter(|| { + let current = tracing::dispatcher::get_default(|current| current.clone()); + black_box(current); + }) + }); + group.finish(); +} + +criterion_group!(benches, criterion_benchmark, bench_dispatch); +criterion_main!(benches); diff --git a/vendor/tracing/benches/no_subscriber.rs b/vendor/tracing/benches/no_subscriber.rs new file mode 100644 index 000000000..e0f82b56a --- /dev/null +++ b/vendor/tracing/benches/no_subscriber.rs @@ -0,0 +1,101 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tracing::Level; + +struct FakeEmptySpan { + inner: Option<(usize, std::sync::Arc<()>)>, + meta: Option<&'static ()>, +} + +impl FakeEmptySpan { + fn new() -> Self { + Self { + inner: None, + meta: None, + } + } +} + +impl Drop for FakeEmptySpan { + fn drop(&mut self) { + black_box(&self.inner); + black_box(&self.meta); + } +} + +fn bench_no_subscriber(c: &mut Criterion) { + use std::sync::atomic::{AtomicUsize, Ordering}; + + let mut group = c.benchmark_group("no_subscriber"); + + group.bench_function("span", |b| { + b.iter(|| { + let span = tracing::span!(Level::TRACE, "span"); + black_box(&span); + }) + }); + group.bench_function("span_enter", |b| { + b.iter(|| { + let span = tracing::span!(Level::TRACE, "span"); + let _e = span.enter(); + }) + }); + group.bench_function("empty_span", |b| { + b.iter(|| { + let span = tracing::span::Span::none(); + black_box(&span); + }); + }); + group.bench_function("empty_struct", |b| { + b.iter(|| { + let span = FakeEmptySpan::new(); + black_box(&span); + }) + }); + group.bench_function("event", |b| { + b.iter(|| { + tracing::event!(Level::TRACE, "hello"); + }) + }); + group.bench_function("relaxed_load", |b| { + let foo = AtomicUsize::new(1); + b.iter(|| black_box(foo.load(Ordering::Relaxed))); + }); + group.bench_function("acquire_load", |b| { + let foo = AtomicUsize::new(1); + b.iter(|| black_box(foo.load(Ordering::Acquire))) + }); + group.bench_function("log", |b| { + b.iter(|| { + log::log!(log::Level::Info, "log"); + }) + }); + group.finish(); +} + +fn bench_fields(c: &mut Criterion) { + let mut group = c.benchmark_group("no_subscriber_field"); + group.bench_function("span", |b| { + b.iter(|| { + black_box(tracing::span!( + Level::TRACE, + "span", + foo = tracing::field::display(format!("bar {:?}", 2)) + )); + }) + }); + group.bench_function("event", |b| { + b.iter(|| { + tracing::event!( + Level::TRACE, + foo = tracing::field::display(format!("bar {:?}", 2)) + ); + }) + }); + group.bench_function("log", |b| { + b.iter(|| log::log!(log::Level::Trace, "{}", format!("bar {:?}", 2))) + }); + group.finish(); +} + +criterion_group!(benches, bench_no_subscriber, bench_fields); +criterion_main!(benches); diff --git a/vendor/tracing/benches/subscriber.rs b/vendor/tracing/benches/subscriber.rs new file mode 100644 index 000000000..c6418010f --- /dev/null +++ b/vendor/tracing/benches/subscriber.rs @@ -0,0 +1,189 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tracing::Level; + +use std::{ + fmt, + sync::{Mutex, MutexGuard}, +}; +use tracing::{field, span, Event, Id, Metadata}; + +/// A subscriber that is enabled but otherwise does nothing. +struct EnabledSubscriber; + +impl tracing::Subscriber for EnabledSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let _ = span; + Id::from_u64(0xDEAD_FACE) + } + + fn event(&self, event: &Event<'_>) { + let _ = event; + } + + fn record(&self, span: &Id, values: &span::Record<'_>) { + let _ = (span, values); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +/// Simulates a subscriber that records span data. +struct VisitingSubscriber(Mutex<String>); + +struct Visitor<'a>(MutexGuard<'a, String>); + +impl<'a> field::Visit for Visitor<'a> { + fn record_debug(&mut self, _field: &field::Field, value: &dyn fmt::Debug) { + use std::fmt::Write; + let _ = write!(&mut *self.0, "{:?}", value); + } +} + +impl tracing::Subscriber for VisitingSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let mut visitor = Visitor(self.0.lock().unwrap()); + span.record(&mut visitor); + Id::from_u64(0xDEAD_FACE) + } + + fn record(&self, _span: &Id, values: &span::Record<'_>) { + let mut visitor = Visitor(self.0.lock().unwrap()); + values.record(&mut visitor); + } + + fn event(&self, event: &Event<'_>) { + let mut visitor = Visitor(self.0.lock().unwrap()); + event.record(&mut visitor); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +const N_SPANS: usize = 100; + +fn criterion_benchmark(c: &mut Criterion) { + c.bench_function("span_no_fields", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| span!(Level::TRACE, "span")) + }); + }); + + c.bench_function("enter_span", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + let span = span!(Level::TRACE, "span"); + #[allow(clippy::unit_arg)] + b.iter(|| black_box(span.in_scope(|| {}))) + }); + }); + + c.bench_function("span_repeatedly", |b| { + #[inline] + fn mk_span(i: u64) -> tracing::Span { + span!(Level::TRACE, "span", i = i) + } + + let n = black_box(N_SPANS); + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| (0..n).fold(mk_span(0), |_, i| mk_span(i as u64))) + }); + }); + + c.bench_function("span_with_fields", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + span!( + Level::TRACE, + "span", + foo = "foo", + bar = "bar", + baz = 3, + quuux = tracing::field::debug(0.99) + ) + }) + }); + }); + + c.bench_function("span_with_fields_record", |b| { + let subscriber = VisitingSubscriber(Mutex::new(String::from(""))); + tracing::subscriber::with_default(subscriber, || { + b.iter(|| { + span!( + Level::TRACE, + "span", + foo = "foo", + bar = "bar", + baz = 3, + quuux = tracing::field::debug(0.99) + ) + }) + }); + }); +} + +fn bench_dispatch(c: &mut Criterion) { + let mut group = c.benchmark_group("dispatch"); + group.bench_function("no_dispatch_get_ref", |b| { + b.iter(|| { + tracing::dispatcher::get_default(|current| { + black_box(¤t); + }) + }) + }); + group.bench_function("no_dispatch_get_clone", |b| { + b.iter(|| { + let current = tracing::dispatcher::get_default(|current| current.clone()); + black_box(current); + }) + }); + group.bench_function("get_ref", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + tracing::dispatcher::get_default(|current| { + black_box(¤t); + }) + }) + }) + }); + group.bench_function("get_clone", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + let current = tracing::dispatcher::get_default(|current| current.clone()); + black_box(current); + }) + }) + }); + group.finish(); +} + +criterion_group!(benches, criterion_benchmark, bench_dispatch); +criterion_main!(benches); diff --git a/vendor/tracing/src/dispatcher.rs b/vendor/tracing/src/dispatcher.rs new file mode 100644 index 000000000..8817ac033 --- /dev/null +++ b/vendor/tracing/src/dispatcher.rs @@ -0,0 +1,145 @@ +//! Dispatches trace events to [`Subscriber`]s. +//! +//! The _dispatcher_ is the component of the tracing system which is responsible +//! for forwarding trace data from the instrumentation points that generate it +//! to the subscriber that collects it. +//! +//! # Using the Trace Dispatcher +//! +//! Every thread in a program using `tracing` has a _default subscriber_. When +//! events occur, or spans are created, they are dispatched to the thread's +//! current subscriber. +//! +//! ## Setting the Default Subscriber +//! +//! By default, the current subscriber is an empty implementation that does +//! nothing. To use a subscriber implementation, it must be set as the default. +//! There are two methods for doing so: [`with_default`] and +//! [`set_global_default`]. `with_default` sets the default subscriber for the +//! duration of a scope, while `set_global_default` sets a default subscriber +//! for the entire process. +//! +//! To use either of these functions, we must first wrap our subscriber in a +//! [`Dispatch`], a cloneable, type-erased reference to a subscriber. For +//! example: +//! ```rust +//! # pub struct FooSubscriber; +//! # use tracing_core::{ +//! # dispatcher, Event, Metadata, +//! # span::{Attributes, Id, Record} +//! # }; +//! # impl tracing_core::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { fn new() -> Self { FooSubscriber } } +//! use dispatcher::Dispatch; +//! +//! let my_subscriber = FooSubscriber::new(); +//! let my_dispatch = Dispatch::new(my_subscriber); +//! ``` +//! Then, we can use [`with_default`] to set our `Dispatch` as the default for +//! the duration of a block: +//! ```rust +//! # pub struct FooSubscriber; +//! # use tracing_core::{ +//! # dispatcher, Event, Metadata, +//! # span::{Attributes, Id, Record} +//! # }; +//! # impl tracing_core::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { fn new() -> Self { FooSubscriber } } +//! # let my_subscriber = FooSubscriber::new(); +//! # let my_dispatch = dispatcher::Dispatch::new(my_subscriber); +//! // no default subscriber +//! +//! # #[cfg(feature = "std")] +//! dispatcher::with_default(&my_dispatch, || { +//! // my_subscriber is the default +//! }); +//! +//! // no default subscriber again +//! ``` +//! It's important to note that `with_default` will not propagate the current +//! thread's default subscriber to any threads spawned within the `with_default` +//! block. To propagate the default subscriber to new threads, either use +//! `with_default` from the new thread, or use `set_global_default`. +//! +//! As an alternative to `with_default`, we can use [`set_global_default`] to +//! set a `Dispatch` as the default for all threads, for the lifetime of the +//! program. For example: +//! ```rust +//! # pub struct FooSubscriber; +//! # use tracing_core::{ +//! # dispatcher, Event, Metadata, +//! # span::{Attributes, Id, Record} +//! # }; +//! # impl tracing_core::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { fn new() -> Self { FooSubscriber } } +//! # let my_subscriber = FooSubscriber::new(); +//! # let my_dispatch = dispatcher::Dispatch::new(my_subscriber); +//! // no default subscriber +//! +//! dispatcher::set_global_default(my_dispatch) +//! // `set_global_default` will return an error if the global default +//! // subscriber has already been set. +//! .expect("global default was already set!"); +//! +//! // `my_subscriber` is now the default +//! ``` +//! +//! <pre class="ignore" style="white-space:normal;font:inherit;"> +//! <strong>Note</strong>: The thread-local scoped dispatcher (<code>with_default</code>) +//! requires the Rust standard library. <code>no_std</code> users should +//! use <a href="fn.set_global_default.html"><code>set_global_default</code></a> +//! instead. +//! </pre> +//! +//! ## Accessing the Default Subscriber +//! +//! A thread's current default subscriber can be accessed using the +//! [`get_default`] function, which executes a closure with a reference to the +//! currently default `Dispatch`. This is used primarily by `tracing` +//! instrumentation. +//! +//! [`Subscriber`]: crate::Subscriber +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub use tracing_core::dispatcher::set_default; +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub use tracing_core::dispatcher::with_default; +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub use tracing_core::dispatcher::DefaultGuard; +pub use tracing_core::dispatcher::{ + get_default, set_global_default, Dispatch, SetGlobalDefaultError, +}; + +/// Private API for internal use by tracing's macros. +/// +/// This function is *not* considered part of `tracing`'s public API, and has no +/// stability guarantees. If you use it, and it breaks or disappears entirely, +/// don't say we didn;'t warn you. +#[doc(hidden)] +pub use tracing_core::dispatcher::has_been_set; diff --git a/vendor/tracing/src/field.rs b/vendor/tracing/src/field.rs new file mode 100644 index 000000000..b3f9fbdfc --- /dev/null +++ b/vendor/tracing/src/field.rs @@ -0,0 +1,170 @@ +//! `Span` and `Event` key-value data. +//! +//! Spans and events may be annotated with key-value data, referred to as known +//! as _fields_. These fields consist of a mapping from a key (corresponding to +//! a `&str` but represented internally as an array index) to a [`Value`]. +//! +//! # `Value`s and `Subscriber`s +//! +//! `Subscriber`s consume `Value`s as fields attached to [span]s or [`Event`]s. +//! The set of field keys on a given span or is defined on its [`Metadata`]. +//! When a span is created, it provides [`Attributes`] to the `Subscriber`'s +//! [`new_span`] method, containing any fields whose values were provided when +//! the span was created; and may call the `Subscriber`'s [`record`] method +//! with additional [`Record`]s if values are added for more of its fields. +//! Similarly, the [`Event`] type passed to the subscriber's [`event`] method +//! will contain any fields attached to each event. +//! +//! `tracing` represents values as either one of a set of Rust primitives +//! (`i64`, `u64`, `f64`, `bool`, and `&str`) or using a `fmt::Display` or +//! `fmt::Debug` implementation. `Subscriber`s are provided these primitive +//! value types as `dyn Value` trait objects. +//! +//! These trait objects can be formatted using `fmt::Debug`, but may also be +//! recorded as typed data by calling the [`Value::record`] method on these +//! trait objects with a _visitor_ implementing the [`Visit`] trait. This trait +//! represents the behavior used to record values of various types. For example, +//! an implementation of `Visit` might record integers by incrementing counters +//! for their field names rather than printing them. +//! +//! +//! # Using `valuable` +//! +//! `tracing`'s [`Value`] trait is intentionally minimalist: it supports only a small +//! number of Rust primitives as typed values, and only permits recording +//! user-defined types with their [`fmt::Debug`] or [`fmt::Display`] +//! implementations. However, there are some cases where it may be useful to record +//! nested values (such as arrays, `Vec`s, or `HashMap`s containing values), or +//! user-defined `struct` and `enum` types without having to format them as +//! unstructured text. +//! +//! To address `Value`'s limitations, `tracing` offers experimental support for +//! the [`valuable`] crate, which provides object-safe inspection of structured +//! values. User-defined types can implement the [`valuable::Valuable`] trait, +//! and be recorded as a `tracing` field by calling their [`as_value`] method. +//! If the [`Subscriber`] also supports the `valuable` crate, it can +//! then visit those types fields as structured values using `valuable`. +//! +//! <pre class="ignore" style="white-space:normal;font:inherit;"> +//! <strong>Note</strong>: <code>valuable</code> support is an +//! <a href = "../index.html#unstable-features">unstable feature</a>. See +//! the documentation on unstable features for details on how to enable it. +//! </pre> +//! +//! For example: +//! ```ignore +//! // Derive `Valuable` for our types: +//! use valuable::Valuable; +//! +//! #[derive(Clone, Debug, Valuable)] +//! struct User { +//! name: String, +//! age: u32, +//! address: Address, +//! } +//! +//! #[derive(Clone, Debug, Valuable)] +//! struct Address { +//! country: String, +//! city: String, +//! street: String, +//! } +//! +//! let user = User { +//! name: "Arwen Undomiel".to_string(), +//! age: 3000, +//! address: Address { +//! country: "Middle Earth".to_string(), +//! city: "Rivendell".to_string(), +//! street: "leafy lane".to_string(), +//! }, +//! }; +//! +//! // Recording `user` as a `valuable::Value` will allow the `tracing` subscriber +//! // to traverse its fields as a nested, typed structure: +//! tracing::info!(current_user = user.as_value()); +//! ``` +//! +//! Alternatively, the [`valuable()`] function may be used to convert a type +//! implementing [`Valuable`] into a `tracing` field value. +//! +//! When the `valuable` feature is enabled, the [`Visit`] trait will include an +//! optional [`record_value`] method. `Visit` implementations that wish to +//! record `valuable` values can implement this method with custom behavior. +//! If a visitor does not implement `record_value`, the [`valuable::Value`] will +//! be forwarded to the visitor's [`record_debug`] method. +//! +//! [`fmt::Debug`]: std::fmt::Debug +//! [`fmt::Display`]: std::fmt::Debug +//! [`valuable`]: https://crates.io/crates/valuable +//! [`valuable::Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html +//! [`as_value`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html#tymethod.as_value +//! [`valuable::Value`]: https://docs.rs/valuable/latest/valuable/enum.Value.html +//! [`Subscriber`]: crate::Subscriber +//! [`record_value`]: Visit::record_value +//! [`record_debug`]: Visit::record_debug +//! [span]: mod@crate::span +//! [`Event`]: crate::event::Event +//! [`Metadata`]: crate::Metadata +//! [`Attributes`]: crate::span::Attributes +//! [`Record`]: crate::span::Record +//! [`new_span`]: crate::Subscriber::new_span +//! [`record`]: crate::Subscriber::record +//! [`event`]: crate::Subscriber::event +pub use tracing_core::field::*; + +use crate::Metadata; + +/// Trait implemented to allow a type to be used as a field key. +/// +/// <pre class="ignore" style="white-space:normal;font:inherit;"> +/// <strong>Note</strong>: Although this is implemented for both the +/// <a href="./struct.Field.html"><code>Field</code></a> type <em>and</em> any +/// type that can be borrowed as an <code>&str</code>, only <code>Field</code> +/// allows <em>O</em>(1) access. +/// Indexing a field with a string results in an iterative search that performs +/// string comparisons. Thus, if possible, once the key for a field is known, it +/// should be used whenever possible. +/// </pre> +pub trait AsField: crate::sealed::Sealed { + /// Attempts to convert `&self` into a `Field` with the specified `metadata`. + /// + /// If `metadata` defines this field, then the field is returned. Otherwise, + /// this returns `None`. + fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field>; +} + +// ===== impl AsField ===== + +impl AsField for Field { + #[inline] + fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field> { + if self.callsite() == metadata.callsite() { + Some(self.clone()) + } else { + None + } + } +} + +impl<'a> AsField for &'a Field { + #[inline] + fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field> { + if self.callsite() == metadata.callsite() { + Some((*self).clone()) + } else { + None + } + } +} + +impl AsField for str { + #[inline] + fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field> { + metadata.fields().field(&self) + } +} + +impl crate::sealed::Sealed for Field {} +impl<'a> crate::sealed::Sealed for &'a Field {} +impl crate::sealed::Sealed for str {} diff --git a/vendor/tracing/src/instrument.rs b/vendor/tracing/src/instrument.rs new file mode 100644 index 000000000..46e5f579c --- /dev/null +++ b/vendor/tracing/src/instrument.rs @@ -0,0 +1,370 @@ +use crate::stdlib::pin::Pin; +use crate::stdlib::task::{Context, Poll}; +use crate::stdlib::{future::Future, marker::Sized}; +use crate::{ + dispatcher::{self, Dispatch}, + span::Span, +}; +use pin_project_lite::pin_project; + +/// Attaches spans to a [`std::future::Future`]. +/// +/// Extension trait allowing futures to be +/// instrumented with a `tracing` [span]. +/// +/// [span]: super::Span +pub trait Instrument: Sized { + /// Instruments this type with the provided [`Span`], returning an + /// `Instrumented` wrapper. + /// + /// The attached [`Span`] will be [entered] every time the instrumented + /// [`Future`] is polled. + /// + /// # Examples + /// + /// Instrumenting a future: + /// + /// ```rust + /// use tracing::Instrument; + /// + /// # async fn doc() { + /// let my_future = async { + /// // ... + /// }; + /// + /// my_future + /// .instrument(tracing::info_span!("my_future")) + /// .await + /// # } + /// ``` + /// + /// The [`Span::or_current`] combinator can be used in combination with + /// `instrument` to ensure that the [current span] is attached to the + /// future if the span passed to `instrument` is [disabled]: + /// + /// ``` + /// use tracing::Instrument; + /// # mod tokio { + /// # pub(super) fn spawn(_: impl std::future::Future) {} + /// # } + /// + /// let my_future = async { + /// // ... + /// }; + /// + /// let outer_span = tracing::info_span!("outer").entered(); + /// + /// // If the "my_future" span is enabled, then the spawned task will + /// // be within both "my_future" *and* "outer", since "outer" is + /// // "my_future"'s parent. However, if "my_future" is disabled, + /// // the spawned task will *not* be in any span. + /// tokio::spawn( + /// my_future + /// .instrument(tracing::debug_span!("my_future")) + /// ); + /// + /// // Using `Span::or_current` ensures the spawned task is instrumented + /// // with the current span, if the new span passed to `instrument` is + /// // not enabled. This means that if the "my_future" span is disabled, + /// // the spawned task will still be instrumented with the "outer" span: + /// # let my_future = async {}; + /// tokio::spawn( + /// my_future + /// .instrument(tracing::debug_span!("my_future").or_current()) + /// ); + /// ``` + /// + /// [entered]: super::Span::enter() + /// [`Span::or_current`]: super::Span::or_current() + /// [current span]: super::Span::current() + /// [disabled]: super::Span::is_disabled() + /// [`Future`]: std::future::Future + fn instrument(self, span: Span) -> Instrumented<Self> { + Instrumented { inner: self, span } + } + + /// Instruments this type with the [current] [`Span`], returning an + /// `Instrumented` wrapper. + /// + /// The attached [`Span`] will be [entered] every time the instrumented + /// [`Future`] is polled. + /// + /// This can be used to propagate the current span when spawning a new future. + /// + /// # Examples + /// + /// ```rust + /// use tracing::Instrument; + /// + /// # mod tokio { + /// # pub(super) fn spawn(_: impl std::future::Future) {} + /// # } + /// # async fn doc() { + /// let span = tracing::info_span!("my_span"); + /// let _enter = span.enter(); + /// + /// // ... + /// + /// let future = async { + /// tracing::debug!("this event will occur inside `my_span`"); + /// // ... + /// }; + /// tokio::spawn(future.in_current_span()); + /// # } + /// ``` + /// + /// [current]: super::Span::current() + /// [entered]: super::Span::enter() + /// [`Span`]: crate::Span + /// [`Future`]: std::future::Future + #[inline] + fn in_current_span(self) -> Instrumented<Self> { + self.instrument(Span::current()) + } +} + +/// Extension trait allowing futures to be instrumented with +/// a `tracing` [`Subscriber`](crate::Subscriber). +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub trait WithSubscriber: Sized { + /// Attaches the provided [`Subscriber`] to this type, returning a + /// [`WithDispatch`] wrapper. + /// + /// The attached [`Subscriber`] will be set as the [default] when the returned + /// [`Future`] is polled. + /// + /// # Examples + /// + /// ``` + /// # use tracing::subscriber::NoSubscriber as MySubscriber; + /// # use tracing::subscriber::NoSubscriber as MyOtherSubscriber; + /// # async fn docs() { + /// use tracing::instrument::WithSubscriber; + /// + /// // Set the default `Subscriber` + /// let _default = tracing::subscriber::set_default(MySubscriber::default()); + /// + /// tracing::info!("this event will be recorded by the default `Subscriber`"); + /// + /// // Create a different `Subscriber` and attach it to a future. + /// let other_subscriber = MyOtherSubscriber::default(); + /// let future = async { + /// tracing::info!("this event will be recorded by the other `Subscriber`"); + /// // ... + /// }; + /// + /// future + /// // Attach the other `Subscriber` to the future before awaiting it + /// .with_subscriber(other_subscriber) + /// .await; + /// + /// // Once the future has completed, we return to the default `Subscriber`. + /// tracing::info!("this event will be recorded by the default `Subscriber`"); + /// # } + /// ``` + /// + /// [`Subscriber`]: super::Subscriber + /// [default]: crate::dispatcher#setting-the-default-subscriber + /// [`Future`]: std::future::Future + fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> + where + S: Into<Dispatch>, + { + WithDispatch { + inner: self, + dispatcher: subscriber.into(), + } + } + + /// Attaches the current [default] [`Subscriber`] to this type, returning a + /// [`WithDispatch`] wrapper. + /// + /// The attached `Subscriber` will be set as the [default] when the returned + /// [`Future`] is polled. + /// + /// This can be used to propagate the current dispatcher context when + /// spawning a new future that may run on a different thread. + /// + /// # Examples + /// + /// ``` + /// # mod tokio { + /// # pub(super) fn spawn(_: impl std::future::Future) {} + /// # } + /// # use tracing::subscriber::NoSubscriber as MySubscriber; + /// # async fn docs() { + /// use tracing::instrument::WithSubscriber; + /// + /// // Using `set_default` (rather than `set_global_default`) sets the + /// // default `Subscriber` for *this* thread only. + /// let _default = tracing::subscriber::set_default(MySubscriber::default()); + /// + /// let future = async { + /// // ... + /// }; + /// + /// // If a multi-threaded async runtime is in use, this spawned task may + /// // run on a different thread, in a different default `Subscriber`'s context. + /// tokio::spawn(future); + /// + /// // However, calling `with_current_subscriber` on the future before + /// // spawning it, ensures that the current thread's default `Subscriber` is + /// // propagated to the spawned task, regardless of where it executes: + /// # let future = async { }; + /// tokio::spawn(future.with_current_subscriber()); + /// # } + /// ``` + /// [`Subscriber`]: super::Subscriber + /// [default]: crate::dispatcher#setting-the-default-subscriber + /// [`Future`]: std::future::Future + #[inline] + fn with_current_subscriber(self) -> WithDispatch<Self> { + WithDispatch { + inner: self, + dispatcher: crate::dispatcher::get_default(|default| default.clone()), + } + } +} + +pin_project! { + /// A [`Future`] that has been instrumented with a `tracing` [`Subscriber`]. + /// + /// This type is returned by the [`WithSubscriber`] extension trait. See that + /// trait's documentation for details. + /// + /// [`Future`]: std::future::Future + /// [`Subscriber`]: crate::Subscriber + #[derive(Clone, Debug)] + #[must_use = "futures do nothing unless you `.await` or poll them"] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + pub struct WithDispatch<T> { + #[pin] + inner: T, + dispatcher: Dispatch, + } +} + +pin_project! { + /// A [`Future`] that has been instrumented with a `tracing` [`Span`]. + /// + /// This type is returned by the [`Instrument`] extension trait. See that + /// trait's documentation for details. + /// + /// [`Future`]: std::future::Future + /// [`Span`]: crate::Span + #[derive(Debug, Clone)] + #[must_use = "futures do nothing unless you `.await` or poll them"] + pub struct Instrumented<T> { + #[pin] + inner: T, + span: Span, + } +} + +// === impl Instrumented === + +impl<T: Future> Future for Instrumented<T> { + type Output = T::Output; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { + let this = self.project(); + let _enter = this.span.enter(); + this.inner.poll(cx) + } +} + +impl<T: Sized> Instrument for T {} + +impl<T> Instrumented<T> { + /// Borrows the `Span` that this type is instrumented by. + pub fn span(&self) -> &Span { + &self.span + } + + /// Mutably borrows the `Span` that this type is instrumented by. + pub fn span_mut(&mut self) -> &mut Span { + &mut self.span + } + + /// Borrows the wrapped type. + pub fn inner(&self) -> &T { + &self.inner + } + + /// Mutably borrows the wrapped type. + pub fn inner_mut(&mut self) -> &mut T { + &mut self.inner + } + + /// Get a pinned reference to the wrapped type. + pub fn inner_pin_ref(self: Pin<&Self>) -> Pin<&T> { + self.project_ref().inner + } + + /// Get a pinned mutable reference to the wrapped type. + pub fn inner_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> { + self.project().inner + } + + /// Consumes the `Instrumented`, returning the wrapped type. + /// + /// Note that this drops the span. + pub fn into_inner(self) -> T { + self.inner + } +} + +// === impl WithDispatch === + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl<T: Future> Future for WithDispatch<T> { + type Output = T::Output; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { + let this = self.project(); + let dispatcher = this.dispatcher; + let future = this.inner; + let _default = dispatcher::set_default(dispatcher); + future.poll(cx) + } +} + +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl<T: Sized> WithSubscriber for T {} + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +impl<T> WithDispatch<T> { + /// Borrows the [`Dispatch`] that is entered when this type is polled. + pub fn dispatcher(&self) -> &Dispatch { + &self.dispatcher + } + + /// Borrows the wrapped type. + pub fn inner(&self) -> &T { + &self.inner + } + + /// Mutably borrows the wrapped type. + pub fn inner_mut(&mut self) -> &mut T { + &mut self.inner + } + + /// Get a pinned reference to the wrapped type. + pub fn inner_pin_ref(self: Pin<&Self>) -> Pin<&T> { + self.project_ref().inner + } + + /// Get a pinned mutable reference to the wrapped type. + pub fn inner_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> { + self.project().inner + } + + /// Consumes the `Instrumented`, returning the wrapped type. + /// + /// Note that this drops the span. + pub fn into_inner(self) -> T { + self.inner + } +} diff --git a/vendor/tracing/src/level_filters.rs b/vendor/tracing/src/level_filters.rs new file mode 100644 index 000000000..44f5e5f57 --- /dev/null +++ b/vendor/tracing/src/level_filters.rs @@ -0,0 +1,94 @@ +//! Trace verbosity level filtering. +//! +//! # Compile time filters +//! +//! Trace verbosity levels can be statically disabled at compile time via Cargo +//! features, similar to the [`log` crate]. Trace instrumentation at disabled +//! levels will be skipped and will not even be present in the resulting binary +//! unless the verbosity level is specified dynamically. This level is +//! configured separately for release and debug builds. The features are: +//! +//! * `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` +//! +//! These features control the value of the `STATIC_MAX_LEVEL` constant. The +//! instrumentation macros macros check this value before recording an event or +//! constructing a span. By default, no levels are disabled. +//! +//! For example, a crate can disable trace level instrumentation in debug builds +//! and trace, debug, and info level instrumentation in release builds with the +//! following configuration: +//! +//! ```toml +//! [dependencies] +//! tracing = { version = "0.1", features = ["max_level_debug", "release_max_level_warn"] } +//! ``` +//! ## Notes +//! +//! Please note that `tracing`'s static max level features do *not* control the +//! [`log`] records that may be emitted when [`tracing`'s "log" feature flag][f] is +//! enabled. This is to allow `tracing` to be disabled entirely at compile time +//! while still emitting `log` records --- such as when a library using +//! `tracing` is used by an application using `log` that doesn't want to +//! generate any `tracing`-related code, but does want to collect `log` records. +//! +//! This means that if the "log" feature is in use, some code may be generated +//! for `log` records emitted by disabled `tracing` events. If this is not +//! desirable, `log` records may be disabled separately using [`log`'s static +//! max level features][`log` crate]. +//! +//! [`log`]: https://docs.rs/log/ +//! [`log` crate]: https://docs.rs/log/latest/log/#compile-time-filters +//! [f]: https://docs.rs/tracing/latest/tracing/#emitting-log-records +pub use tracing_core::{metadata::ParseLevelFilterError, LevelFilter}; + +/// The statically configured maximum trace level. +/// +/// See the [module-level documentation] for information on how to configure +/// this. +/// +/// This value is checked by the `event!` and `span!` macros. Code that +/// manually constructs events or spans via the `Event::record` function or +/// `Span` constructors should compare the level against this value to +/// determine if those spans or events are enabled. +/// +/// [module-level documentation]: super#compile-time-filters +pub const STATIC_MAX_LEVEL: LevelFilter = MAX_LEVEL; + +cfg_if::cfg_if! { + if #[cfg(all(not(debug_assertions), feature = "release_max_level_off"))] { + const MAX_LEVEL: LevelFilter = LevelFilter::OFF; + } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_error"))] { + const MAX_LEVEL: LevelFilter = LevelFilter::ERROR; + } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_warn"))] { + const MAX_LEVEL: LevelFilter = LevelFilter::WARN; + } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_info"))] { + const MAX_LEVEL: LevelFilter = LevelFilter::INFO; + } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_debug"))] { + const MAX_LEVEL: LevelFilter = LevelFilter::DEBUG; + } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_trace"))] { + const MAX_LEVEL: LevelFilter = LevelFilter::TRACE; + } else if #[cfg(feature = "max_level_off")] { + const MAX_LEVEL: LevelFilter = LevelFilter::OFF; + } else if #[cfg(feature = "max_level_error")] { + const MAX_LEVEL: LevelFilter = LevelFilter::ERROR; + } else if #[cfg(feature = "max_level_warn")] { + const MAX_LEVEL: LevelFilter = LevelFilter::WARN; + } else if #[cfg(feature = "max_level_info")] { + const MAX_LEVEL: LevelFilter = LevelFilter::INFO; + } else if #[cfg(feature = "max_level_debug")] { + const MAX_LEVEL: LevelFilter = LevelFilter::DEBUG; + } else { + const MAX_LEVEL: LevelFilter = LevelFilter::TRACE; + } +} diff --git a/vendor/tracing/src/lib.rs b/vendor/tracing/src/lib.rs new file mode 100644 index 000000000..4743eba20 --- /dev/null +++ b/vendor/tracing/src/lib.rs @@ -0,0 +1,1107 @@ +//! A scoped, structured logging and diagnostics system. +//! +//! # Overview +//! +//! `tracing` is a framework for instrumenting Rust programs to collect +//! structured, event-based diagnostic information. +//! +//! In asynchronous systems like Tokio, interpreting traditional log messages can +//! often be quite challenging. Since individual tasks are multiplexed on the same +//! thread, associated events and log lines are intermixed making it difficult to +//! trace the logic flow. `tracing` expands upon logging-style diagnostics by +//! allowing libraries and applications to record structured events with additional +//! information about *temporality* and *causality* — unlike a log message, a span +//! in `tracing` has a beginning and end time, may be entered and exited by the +//! flow of execution, and may exist within a nested tree of similar spans. In +//! addition, `tracing` spans are *structured*, with the ability to record typed +//! data as well as textual messages. +//! +//! The `tracing` crate provides the APIs necessary for instrumenting libraries +//! and applications to emit trace data. +//! +//! *Compiler support: [requires `rustc` 1.49+][msrv]* +//! +//! [msrv]: #supported-rust-versions +//! # Core Concepts +//! +//! The core of `tracing`'s API is composed of _spans_, _events_ and +//! _subscribers_. We'll cover these in turn. +//! +//! ## Spans +//! +//! To record the flow of execution through a program, `tracing` introduces the +//! concept of [spans]. Unlike a log line that represents a _moment in +//! time_, a span represents a _period of time_ with a beginning and an end. When a +//! program begins executing in a context or performing a unit of work, it +//! _enters_ that context's span, and when it stops executing in that context, +//! it _exits_ the span. The span in which a thread is currently executing is +//! referred to as that thread's _current_ span. +//! +//! For example: +//! ``` +//! use tracing::{span, Level}; +//! # fn main() { +//! let span = span!(Level::TRACE, "my_span"); +//! // `enter` returns a RAII guard which, when dropped, exits the span. this +//! // indicates that we are in the span for the current lexical scope. +//! let _enter = span.enter(); +//! // perform some work in the context of `my_span`... +//! # } +//!``` +//! +//! The [`span` module][span]'s documentation provides further details on how to +//! use spans. +//! +//! <div class="example-wrap" style="display:inline-block"><pre class="compile_fail" style="white-space:normal;font:inherit;"> +//! +//! **Warning**: In asynchronous code that uses async/await syntax, +//! `Span::enter` may produce incorrect traces if the returned drop +//! guard is held across an await point. See +//! [the method documentation][Span#in-asynchronous-code] for details. +//! +//! </pre></div> +//! +//! ## Events +//! +//! An [`Event`] represents a _moment_ in time. It signifies something that +//! happened while a trace was being recorded. `Event`s are comparable to the log +//! records emitted by unstructured logging code, but unlike a typical log line, +//! an `Event` may occur within the context of a span. +//! +//! For example: +//! ``` +//! use tracing::{event, span, Level}; +//! +//! # fn main() { +//! // records an event outside of any span context: +//! event!(Level::INFO, "something happened"); +//! +//! let span = span!(Level::INFO, "my_span"); +//! let _guard = span.enter(); +//! +//! // records an event within "my_span". +//! event!(Level::DEBUG, "something happened inside my_span"); +//! # } +//!``` +//! +//! In general, events should be used to represent points in time _within_ a +//! span — a request returned with a given status code, _n_ new items were +//! taken from a queue, and so on. +//! +//! The [`Event` struct][`Event`] documentation provides further details on using +//! events. +//! +//! ## Subscribers +//! +//! As `Span`s and `Event`s occur, they are recorded or aggregated by +//! implementations of the [`Subscriber`] trait. `Subscriber`s are notified +//! when an `Event` takes place and when a `Span` is entered or exited. These +//! notifications are represented by the following `Subscriber` trait methods: +//! +//! + [`event`][Subscriber::event], called when an `Event` takes place, +//! + [`enter`], called when execution enters a `Span`, +//! + [`exit`], called when execution exits a `Span` +//! +//! In addition, subscribers may implement the [`enabled`] function to _filter_ +//! the notifications they receive based on [metadata] describing each `Span` +//! or `Event`. If a call to `Subscriber::enabled` returns `false` for a given +//! set of metadata, that `Subscriber` will *not* be notified about the +//! corresponding `Span` or `Event`. For performance reasons, if no currently +//! active subscribers express interest in a given set of metadata by returning +//! `true`, then the corresponding `Span` or `Event` will never be constructed. +//! +//! # Usage +//! +//! First, add this to your `Cargo.toml`: +//! +//! ```toml +//! [dependencies] +//! tracing = "0.1" +//! ``` +//! +//! ## Recording Spans and Events +//! +//! Spans and events are recorded using macros. +//! +//! ### Spans +//! +//! The [`span!`] macro expands to a [`Span` struct][`Span`] which is used to +//! record a span. The [`Span::enter`] method on that struct records that the +//! span has been entered, and returns a [RAII] guard object, which will exit +//! the span when dropped. +//! +//! For example: +//! +//! ```rust +//! use tracing::{span, Level}; +//! # fn main() { +//! // Construct a new span named "my span" with trace log level. +//! let span = span!(Level::TRACE, "my span"); +//! +//! // Enter the span, returning a guard object. +//! let _enter = span.enter(); +//! +//! // Any trace events that occur before the guard is dropped will occur +//! // within the span. +//! +//! // Dropping the guard will exit the span. +//! # } +//! ``` +//! +//! The [`#[instrument]`][instrument] attribute provides an easy way to +//! add `tracing` spans to functions. A function annotated with `#[instrument]` +//! will create and enter a span with that function's name every time the +//! function is called, with arguments to that function will be recorded as +//! fields using `fmt::Debug`. +//! +//! For example: +//! ```ignore +//! # // this doctest is ignored because we don't have a way to say +//! # // that it should only be run with cfg(feature = "attributes") +//! use tracing::{Level, event, instrument}; +//! +//! #[instrument] +//! pub fn my_function(my_arg: usize) { +//! // This event will be recorded inside a span named `my_function` with the +//! // field `my_arg`. +//! event!(Level::INFO, "inside my_function!"); +//! // ... +//! } +//! # fn main() {} +//! ``` +//! +//! For functions which don't have built-in tracing support and can't have +//! the `#[instrument]` attribute applied (such as from an external crate), +//! the [`Span` struct][`Span`] has a [`in_scope()` method][`in_scope`] +//! which can be used to easily wrap synchonous code in a span. +//! +//! For example: +//! ```rust +//! use tracing::info_span; +//! +//! # fn doc() -> Result<(), ()> { +//! # mod serde_json { +//! # pub(crate) fn from_slice(buf: &[u8]) -> Result<(), ()> { Ok(()) } +//! # } +//! # let buf: [u8; 0] = []; +//! let json = info_span!("json.parse").in_scope(|| serde_json::from_slice(&buf))?; +//! # let _ = json; // suppress unused variable warning +//! # Ok(()) +//! # } +//! ``` +//! +//! You can find more examples showing how to use this crate [here][examples]. +//! +//! [RAII]: https://github.com/rust-unofficial/patterns/blob/master/patterns/behavioural/RAII.md +//! [examples]: https://github.com/tokio-rs/tracing/tree/master/examples +//! +//! ### Events +//! +//! [`Event`]s are recorded using the [`event!`] macro: +//! +//! ```rust +//! # fn main() { +//! use tracing::{event, Level}; +//! event!(Level::INFO, "something has happened!"); +//! # } +//! ``` +//! +//! ## Using the Macros +//! +//! The [`span!`] and [`event!`] macros as well as the `#[instrument]` attribute +//! use fairly similar syntax, with some exceptions. +//! +//! ### Configuring Attributes +//! +//! Both macros require a [`Level`] specifying the verbosity of the span or +//! event. Optionally, the [target] and [parent span] may be overridden. If the +//! target and parent span are not overridden, they will default to the +//! module path where the macro was invoked and the current span (as determined +//! by the subscriber), respectively. +//! +//! For example: +//! +//! ``` +//! # use tracing::{span, event, Level}; +//! # fn main() { +//! span!(target: "app_spans", Level::TRACE, "my span"); +//! event!(target: "app_events", Level::INFO, "something has happened!"); +//! # } +//! ``` +//! ``` +//! # use tracing::{span, event, Level}; +//! # fn main() { +//! let span = span!(Level::TRACE, "my span"); +//! event!(parent: &span, Level::INFO, "something has happened!"); +//! # } +//! ``` +//! +//! The span macros also take a string literal after the level, to set the name +//! of the span. +//! +//! ### Recording Fields +//! +//! Structured fields on spans and events are specified using the syntax +//! `field_name = field_value`. Fields are separated by commas. +//! +//! ``` +//! # use tracing::{event, Level}; +//! # fn main() { +//! // records an event with two fields: +//! // - "answer", with the value 42 +//! // - "question", with the value "life, the universe and everything" +//! event!(Level::INFO, answer = 42, question = "life, the universe, and everything"); +//! # } +//! ``` +//! +//! As shorthand, local variables may be used as field values without an +//! assignment, similar to [struct initializers]. For example: +//! +//! ``` +//! # use tracing::{span, Level}; +//! # fn main() { +//! let user = "ferris"; +//! +//! span!(Level::TRACE, "login", user); +//! // is equivalent to: +//! span!(Level::TRACE, "login", user = user); +//! # } +//!``` +//! +//! Field names can include dots, but should not be terminated by them: +//! ``` +//! # use tracing::{span, Level}; +//! # fn main() { +//! let user = "ferris"; +//! let email = "ferris@rust-lang.org"; +//! span!(Level::TRACE, "login", user, user.email = email); +//! # } +//!``` +//! +//! Since field names can include dots, fields on local structs can be used +//! using the local variable shorthand: +//! ``` +//! # use tracing::{span, Level}; +//! # fn main() { +//! # struct User { +//! # name: &'static str, +//! # email: &'static str, +//! # } +//! let user = User { +//! name: "ferris", +//! email: "ferris@rust-lang.org", +//! }; +//! // the span will have the fields `user.name = "ferris"` and +//! // `user.email = "ferris@rust-lang.org"`. +//! span!(Level::TRACE, "login", user.name, user.email); +//! # } +//!``` +//! +//! Fields with names that are not Rust identifiers, or with names that are Rust reserved words, +//! may be created using quoted string literals. However, this may not be used with the local +//! variable shorthand. +//! ``` +//! # use tracing::{span, Level}; +//! # fn main() { +//! // records an event with fields whose names are not Rust identifiers +//! // - "guid:x-request-id", containing a `:`, with the value "abcdef" +//! // - "type", which is a reserved word, with the value "request" +//! span!(Level::TRACE, "api", "guid:x-request-id" = "abcdef", "type" = "request"); +//! # } +//!``` +//! +//! The `?` sigil is shorthand that specifies a field should be recorded using +//! its [`fmt::Debug`] implementation: +//! ``` +//! # use tracing::{event, Level}; +//! # fn main() { +//! #[derive(Debug)] +//! struct MyStruct { +//! field: &'static str, +//! } +//! +//! let my_struct = MyStruct { +//! field: "Hello world!" +//! }; +//! +//! // `my_struct` will be recorded using its `fmt::Debug` implementation. +//! event!(Level::TRACE, greeting = ?my_struct); +//! // is equivalent to: +//! event!(Level::TRACE, greeting = tracing::field::debug(&my_struct)); +//! # } +//! ``` +//! +//! The `%` sigil operates similarly, but indicates that the value should be +//! recorded using its [`fmt::Display`] implementation: +//! ``` +//! # use tracing::{event, Level}; +//! # fn main() { +//! # #[derive(Debug)] +//! # struct MyStruct { +//! # field: &'static str, +//! # } +//! # +//! # let my_struct = MyStruct { +//! # field: "Hello world!" +//! # }; +//! // `my_struct.field` will be recorded using its `fmt::Display` implementation. +//! event!(Level::TRACE, greeting = %my_struct.field); +//! // is equivalent to: +//! event!(Level::TRACE, greeting = tracing::field::display(&my_struct.field)); +//! # } +//! ``` +//! +//! The `%` and `?` sigils may also be used with local variable shorthand: +//! +//! ``` +//! # use tracing::{event, Level}; +//! # fn main() { +//! # #[derive(Debug)] +//! # struct MyStruct { +//! # field: &'static str, +//! # } +//! # +//! # let my_struct = MyStruct { +//! # field: "Hello world!" +//! # }; +//! // `my_struct.field` will be recorded using its `fmt::Display` implementation. +//! event!(Level::TRACE, %my_struct.field); +//! # } +//! ``` +//! +//! Additionally, a span may declare fields with the special value [`Empty`], +//! which indicates that that the value for that field does not currently exist +//! but may be recorded later. For example: +//! +//! ``` +//! use tracing::{trace_span, field}; +//! +//! // Create a span with two fields: `greeting`, with the value "hello world", and +//! // `parting`, without a value. +//! let span = trace_span!("my_span", greeting = "hello world", parting = field::Empty); +//! +//! // ... +//! +//! // Now, record a value for parting as well. +//! span.record("parting", &"goodbye world!"); +//! ``` +//! +//! Note that a span may have up to 32 fields. The following will not compile: +//! +//! ```rust,compile_fail +//! # use tracing::Level; +//! # fn main() { +//! let bad_span = span!( +//! Level::TRACE, +//! "too many fields!", +//! a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9, +//! j = 10, k = 11, l = 12, m = 13, n = 14, o = 15, p = 16, q = 17, +//! r = 18, s = 19, t = 20, u = 21, v = 22, w = 23, x = 24, y = 25, +//! z = 26, aa = 27, bb = 28, cc = 29, dd = 30, ee = 31, ff = 32, gg = 33 +//! ); +//! # } +//! ``` +//! +//! Finally, events may also include human-readable messages, in the form of a +//! [format string][fmt] and (optional) arguments, **after** the event's +//! key-value fields. If a format string and arguments are provided, +//! they will implicitly create a new field named `message` whose value is the +//! provided set of format arguments. +//! +//! For example: +//! +//! ``` +//! # use tracing::{event, Level}; +//! # fn main() { +//! let question = "the ultimate question of life, the universe, and everything"; +//! let answer = 42; +//! // records an event with the following fields: +//! // - `question.answer` with the value 42, +//! // - `question.tricky` with the value `true`, +//! // - "message", with the value "the answer to the ultimate question of life, the +//! // universe, and everything is 42." +//! event!( +//! Level::DEBUG, +//! question.answer = answer, +//! question.tricky = true, +//! "the answer to {} is {}.", question, answer +//! ); +//! # } +//! ``` +//! +//! Specifying a formatted message in this manner does not allocate by default. +//! +//! [struct initializers]: https://doc.rust-lang.org/book/ch05-01-defining-structs.html#using-the-field-init-shorthand-when-variables-and-fields-have-the-same-name +//! [target]: Metadata::target +//! [parent span]: span::Attributes::parent +//! [determined contextually]: span::Attributes::is_contextual +//! [`fmt::Debug`]: std::fmt::Debug +//! [`fmt::Display`]: std::fmt::Display +//! [fmt]: std::fmt#usage +//! [`Empty`]: field::Empty +//! +//! ### Shorthand Macros +//! +//! `tracing` also offers a number of macros with preset verbosity levels. +//! The [`trace!`], [`debug!`], [`info!`], [`warn!`], and [`error!`] behave +//! similarly to the [`event!`] macro, but with the [`Level`] argument already +//! specified, while the corresponding [`trace_span!`], [`debug_span!`], +//! [`info_span!`], [`warn_span!`], and [`error_span!`] macros are the same, +//! but for the [`span!`] macro. +//! +//! These are intended both as a shorthand, and for compatibility with the [`log`] +//! crate (see the next section). +//! +//! [`span!`]: span! +//! [`event!`]: event! +//! [`trace!`]: trace! +//! [`debug!`]: debug! +//! [`info!`]: info! +//! [`warn!`]: warn! +//! [`error!`]: error! +//! [`trace_span!`]: trace_span! +//! [`debug_span!`]: debug_span! +//! [`info_span!`]: info_span! +//! [`warn_span!`]: warn_span! +//! [`error_span!`]: error_span! +//! +//! ### For `log` Users +//! +//! Users of the [`log`] crate should note that `tracing` exposes a set of +//! macros for creating `Event`s (`trace!`, `debug!`, `info!`, `warn!`, and +//! `error!`) which may 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. +//! +//! Let's consider the `log` crate's yak-shaving example: +//! +//! ```rust,ignore +//! use std::{error::Error, io}; +//! use tracing::{debug, error, info, span, warn, Level}; +//! +//! // the `#[tracing::instrument]` attribute creates and enters a span +//! // every time the instrumented function is called. The span is named after the +//! // the function or method. Parameters passed to the function are recorded as fields. +//! #[tracing::instrument] +//! pub fn shave(yak: usize) -> Result<(), Box<dyn Error + 'static>> { +//! // this creates an event at the DEBUG level with two fields: +//! // - `excitement`, with the key "excitement" and the value "yay!" +//! // - `message`, with the key "message" and the value "hello! I'm gonna shave a yak." +//! // +//! // unlike other fields, `message`'s shorthand initialization is just the string itself. +//! debug!(excitement = "yay!", "hello! I'm gonna shave a yak."); +//! if yak == 3 { +//! warn!("could not locate yak!"); +//! // note that this is intended to demonstrate `tracing`'s features, not idiomatic +//! // error handling! in a library or application, you should consider returning +//! // a dedicated `YakError`. libraries like snafu or thiserror make this easy. +//! return Err(io::Error::new(io::ErrorKind::Other, "shaving yak failed!").into()); +//! } else { +//! debug!("yak shaved successfully"); +//! } +//! Ok(()) +//! } +//! +//! pub fn shave_all(yaks: usize) -> usize { +//! // Constructs a new span named "shaving_yaks" at the TRACE level, +//! // and a field whose key is "yaks". This is equivalent to writing: +//! // +//! // let span = span!(Level::TRACE, "shaving_yaks", yaks = yaks); +//! // +//! // local variables (`yaks`) can be used as field values +//! // without an assignment, similar to struct initializers. +//! let _span = span!(Level::TRACE, "shaving_yaks", yaks).entered(); +//! +//! info!("shaving yaks"); +//! +//! let mut yaks_shaved = 0; +//! for yak in 1..=yaks { +//! let res = shave(yak); +//! debug!(yak, shaved = res.is_ok()); +//! +//! if let Err(ref error) = res { +//! // Like spans, events can also use the field initialization shorthand. +//! // In this instance, `yak` is the field being initalized. +//! error!(yak, error = error.as_ref(), "failed to shave yak!"); +//! } else { +//! yaks_shaved += 1; +//! } +//! debug!(yaks_shaved); +//! } +//! +//! yaks_shaved +//! } +//! ``` +//! +//! ## In libraries +//! +//! Libraries should link only to the `tracing` crate, and use the provided +//! macros to record whatever information will be useful to downstream +//! consumers. +//! +//! ## In executables +//! +//! In order to record trace events, executables have to use a `Subscriber` +//! implementation compatible with `tracing`. A `Subscriber` implements a +//! way of collecting trace data, such as by logging it to standard output. +//! +//! This library does not contain any `Subscriber` implementations; these are +//! provided by [other crates](#related-crates). +//! +//! The simplest way to use a subscriber is to call the [`set_global_default`] +//! function: +//! +//! ``` +//! extern crate tracing; +//! # pub struct FooSubscriber; +//! # use tracing::{span::{Id, Attributes, Record}, Metadata}; +//! # impl tracing::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &tracing::Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { +//! # fn new() -> Self { FooSubscriber } +//! # } +//! # fn main() { +//! +//! let my_subscriber = FooSubscriber::new(); +//! tracing::subscriber::set_global_default(my_subscriber) +//! .expect("setting tracing default failed"); +//! # } +//! ``` +//! +//! <pre class="compile_fail" style="white-space:normal;font:inherit;"> +//! <strong>Warning</strong>: In general, libraries should <em>not</em> call +//! <code>set_global_default()</code>! Doing so will cause conflicts when +//! executables that depend on the library try to set the default later. +//! </pre> +//! +//! This subscriber will be used as the default in all threads for the +//! remainder of the duration of the program, similar to setting the logger +//! in the `log` crate. +//! +//! In addition, the default subscriber can be set through using the +//! [`with_default`] function. This follows the `tokio` pattern of using +//! closures to represent executing code in a context that is exited at the end +//! of the closure. For example: +//! +//! ```rust +//! # pub struct FooSubscriber; +//! # use tracing::{span::{Id, Attributes, Record}, Metadata}; +//! # impl tracing::Subscriber for FooSubscriber { +//! # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) } +//! # fn record(&self, _: &Id, _: &Record) {} +//! # fn event(&self, _: &tracing::Event) {} +//! # fn record_follows_from(&self, _: &Id, _: &Id) {} +//! # fn enabled(&self, _: &Metadata) -> bool { false } +//! # fn enter(&self, _: &Id) {} +//! # fn exit(&self, _: &Id) {} +//! # } +//! # impl FooSubscriber { +//! # fn new() -> Self { FooSubscriber } +//! # } +//! # fn main() { +//! +//! let my_subscriber = FooSubscriber::new(); +//! # #[cfg(feature = "std")] +//! tracing::subscriber::with_default(my_subscriber, || { +//! // Any trace events generated in this closure or by functions it calls +//! // will be collected by `my_subscriber`. +//! }) +//! # } +//! ``` +//! +//! This approach allows trace data to be collected by multiple subscribers +//! within different contexts in the program. Note that the override only applies to the +//! currently executing thread; other threads will not see the change from with_default. +//! +//! Any trace events generated outside the context of a subscriber will not be collected. +//! +//! Once a subscriber has been set, instrumentation points may be added to the +//! executable using the `tracing` crate's macros. +//! +//! ## `log` Compatibility +//! +//! The [`log`] crate provides a simple, lightweight logging facade for Rust. +//! While `tracing` builds upon `log`'s foundation with richer structured +//! diagnostic data, `log`'s simplicity and ubiquity make it the "lowest common +//! denominator" for text-based logging in Rust — a vast majority of Rust +//! libraries and applications either emit or consume `log` records. Therefore, +//! `tracing` provides multiple forms of interoperability with `log`: `tracing` +//! instrumentation can emit `log` records, and a compatibility layer enables +//! `tracing` [`Subscriber`]s to consume `log` records as `tracing` [`Event`]s. +//! +//! ### Emitting `log` Records +//! +//! This crate provides two feature flags, "log" and "log-always", which will +//! cause [spans] and [events] to emit `log` records. When the "log" feature is +//! enabled, if no `tracing` `Subscriber` is active, invoking an event macro or +//! creating a span with fields will emit a `log` record. This is intended +//! primarily for use in libraries which wish to emit diagnostics that can be +//! consumed by applications using `tracing` *or* `log`, without paying the +//! additional overhead of emitting both forms of diagnostics when `tracing` is +//! in use. +//! +//! Enabling the "log-always" feature will cause `log` records to be emitted +//! even if a `tracing` `Subscriber` _is_ set. This is intended to be used in +//! applications where a `log` `Logger` is being used to record a textual log, +//! and `tracing` is used only to record other forms of diagnostics (such as +//! metrics, profiling, or distributed tracing data). Unlike the "log" feature, +//! libraries generally should **not** enable the "log-always" feature, as doing +//! so will prevent applications from being able to opt out of the `log` records. +//! +//! See [here][flags] for more details on this crate's feature flags. +//! +//! The generated `log` records' messages will be a string representation of the +//! span or event's fields, and all additional information recorded by `log` +//! (target, verbosity level, module path, file, and line number) will also be +//! populated. Additionally, `log` records are also generated when spans are +//! entered, exited, and closed. Since these additional span lifecycle logs have +//! the potential to be very verbose, and don't include additional fields, they +//! will always be emitted at the `Trace` level, rather than inheriting the +//! level of the span that generated them. Furthermore, they are are categorized +//! under a separate `log` target, "tracing::span" (and its sub-target, +//! "tracing::span::active", for the logs on entering and exiting a span), which +//! may be enabled or disabled separately from other `log` records emitted by +//! `tracing`. +//! +//! ### Consuming `log` Records +//! +//! The [`tracing-log`] crate provides a compatibility layer which +//! allows a `tracing` [`Subscriber`] to consume `log` records as though they +//! were `tracing` [events]. This allows applications using `tracing` to record +//! the logs emitted by dependencies using `log` as events within the context of +//! the application's trace tree. See [that crate's documentation][log-tracer] +//! for details. +//! +//! [log-tracer]: https://docs.rs/tracing-log/latest/tracing_log/#convert-log-records-to-tracing-events +//! +//! ## Related Crates +//! +//! In addition to `tracing` and `tracing-core`, the [`tokio-rs/tracing`] repository +//! contains several additional crates designed to be used with the `tracing` ecosystem. +//! This includes a collection of `Subscriber` implementations, as well as utility +//! and adapter crates to assist in writing `Subscriber`s and instrumenting +//! applications. +//! +//! In particular, the following crates are likely to be of interest: +//! +//! - [`tracing-futures`] provides a compatibility layer with the `futures` +//! crate, allowing spans to be attached to `Future`s, `Stream`s, and `Executor`s. +//! - [`tracing-subscriber`] provides `Subscriber` implementations and +//! utilities for working with `Subscriber`s. This includes a [`FmtSubscriber`] +//! `FmtSubscriber` for logging formatted trace data to stdout, with similar +//! filtering and formatting to the [`env_logger`] crate. +//! - [`tracing-log`] provides a compatibility layer with the [`log`] crate, +//! allowing log messages to be recorded as `tracing` `Event`s within the +//! trace tree. This is useful when a project using `tracing` have +//! dependencies which use `log`. Note that if you're using +//! `tracing-subscriber`'s `FmtSubscriber`, you don't need to depend on +//! `tracing-log` directly. +//! - [`tracing-appender`] provides utilities for outputting tracing data, +//! including a file appender and non blocking writer. +//! +//! Additionally, there are also several third-party crates which are not +//! maintained by the `tokio` project. These include: +//! +//! - [`tracing-timing`] implements inter-event timing metrics on top of `tracing`. +//! It provides a subscriber that records the time elapsed between pairs of +//! `tracing` events and generates histograms. +//! - [`tracing-opentelemetry`] provides a subscriber for emitting traces to +//! [OpenTelemetry]-compatible distributed tracing systems. +//! - [`tracing-honeycomb`] Provides a layer that reports traces spanning multiple machines to [honeycomb.io]. Backed by [`tracing-distributed`]. +//! - [`tracing-distributed`] Provides a generic implementation of a layer that reports traces spanning multiple machines to some backend. +//! - [`tracing-actix-web`] provides `tracing` integration for the `actix-web` web framework. +//! - [`tracing-actix`] provides `tracing` integration for the `actix` actor +//! framework. +//! - [`tracing-gelf`] implements a subscriber for exporting traces in Greylog +//! GELF format. +//! - [`tracing-coz`] provides integration with the [coz] causal profiler +//! (Linux-only). +//! - [`tracing-bunyan-formatter`] provides a layer implementation that reports events and spans +//! in [bunyan] format, enriched with timing information. +//! - [`tracing-wasm`] provides a `Subscriber`/`Layer` implementation that reports +//! events and spans via browser `console.log` and [User Timing API (`window.performance`)]. +//! - [`tide-tracing`] provides a [tide] middleware to trace all incoming requests and responses. +//! - [`test-log`] takes care of initializing `tracing` for tests, based on +//! environment variables with an `env_logger` compatible syntax. +//! - [`tracing-unwrap`] provides convenience methods to report failed unwraps +//! on `Result` or `Option` types to a `Subscriber`. +//! - [`diesel-tracing`] provides integration with [`diesel`] database connections. +//! - [`tracing-tracy`] provides a way to collect [Tracy] profiles in instrumented +//! applications. +//! - [`tracing-elastic-apm`] provides a layer for reporting traces to [Elastic APM]. +//! - [`tracing-etw`] provides a layer for emitting Windows [ETW] events. +//! - [`tracing-fluent-assertions`] provides a fluent assertions-style testing +//! framework for validating the behavior of `tracing` spans. +//! - [`sentry-tracing`] provides a layer for reporting events and traces to [Sentry]. +//! - [`tracing-forest`] provides a subscriber that preserves contextual coherence by +//! grouping together logs from the same spans during writing. +//! - [`tracing-loki`] provides a layer for shipping logs to [Grafana Loki]. +//! +//! If you're the maintainer of a `tracing` ecosystem crate not listed above, +//! please let us know! We'd love to add your project to the list! +//! +//! [`tracing-opentelemetry`]: https://crates.io/crates/tracing-opentelemetry +//! [OpenTelemetry]: https://opentelemetry.io/ +//! [`tracing-honeycomb`]: https://crates.io/crates/tracing-honeycomb +//! [`tracing-distributed`]: https://crates.io/crates/tracing-distributed +//! [honeycomb.io]: https://www.honeycomb.io/ +//! [`tracing-actix-web`]: https://crates.io/crates/tracing-actix-web +//! [`tracing-actix`]: https://crates.io/crates/tracing-actix +//! [`tracing-gelf`]: https://crates.io/crates/tracing-gelf +//! [`tracing-coz`]: https://crates.io/crates/tracing-coz +//! [coz]: https://github.com/plasma-umass/coz +//! [`tracing-bunyan-formatter`]: https://crates.io/crates/tracing-bunyan-formatter +//! [bunyan]: https://github.com/trentm/node-bunyan +//! [`tracing-wasm`]: https://docs.rs/tracing-wasm +//! [User Timing API (`window.performance`)]: https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API +//! [`tide-tracing`]: https://crates.io/crates/tide-tracing +//! [tide]: https://crates.io/crates/tide +//! [`test-log`]: https://crates.io/crates/test-log +//! [`tracing-unwrap`]: https://docs.rs/tracing-unwrap +//! [`diesel`]: https://crates.io/crates/diesel +//! [`diesel-tracing`]: https://crates.io/crates/diesel-tracing +//! [`tracing-tracy`]: https://crates.io/crates/tracing-tracy +//! [Tracy]: https://github.com/wolfpld/tracy +//! [`tracing-elastic-apm`]: https://crates.io/crates/tracing-elastic-apm +//! [Elastic APM]: https://www.elastic.co/apm +//! [`tracing-etw`]: https://github.com/microsoft/tracing-etw +//! [ETW]: https://docs.microsoft.com/en-us/windows/win32/etw/about-event-tracing +//! [`tracing-fluent-assertions`]: https://crates.io/crates/tracing-fluent-assertions +//! [`sentry-tracing`]: https://crates.io/crates/sentry-tracing +//! [Sentry]: https://sentry.io/welcome/ +//! [`tracing-forest`]: https://crates.io/crates/tracing-forest +//! [`tracing-loki`]: https://crates.io/crates/tracing-loki +//! [Grafana Loki]: https://grafana.com/oss/loki/ +//! +//! <pre class="ignore" style="white-space:normal;font:inherit;"> +//! <strong>Note</strong>: Some of these ecosystem crates are currently +//! unreleased and/or in earlier stages of development. They may be less stable +//! than <code>tracing</code> and <code>tracing-core</code>. +//! </pre> +//! +//! ## Crate Feature Flags +//! +//! The following crate [feature flags] are available: +//! +//! * A set of features controlling the [static verbosity level]. +//! * `log`: causes trace instrumentation points to emit [`log`] records as well +//! as trace events, if a default `tracing` subscriber has not been set. This +//! is intended for use in libraries whose users may be using either `tracing` +//! or `log`. +//! * `log-always`: Emit `log` records from all `tracing` spans and events, even +//! if a `tracing` subscriber has been set. This should be set only by +//! applications which intend to collect traces and logs separately; if an +//! adapter is used to convert `log` records into `tracing` events, this will +//! cause duplicate events to occur. +//! * `attributes`: Includes support for the `#[instrument]` attribute. +//! This is on by default, but does bring in the `syn` crate as a dependency, +//! which may add to the compile time of crates that do not already use it. +//! * `std`: Depend on the Rust standard library (enabled by default). +//! +//! `no_std` users may disable this feature with `default-features = false`: +//! +//! ```toml +//! [dependencies] +//! tracing = { version = "0.1.35", default-features = false } +//! ``` +//! +//! <pre class="ignore" style="white-space:normal;font:inherit;"> +//! <strong>Note</strong>: <code>tracing</code>'s <code>no_std</code> support +//! requires <code>liballoc</code>. +//! </pre> +//! +//! ### Unstable Features +//! +//! These feature flags enable **unstable** features. The public API may break in 0.1.x +//! releases. To enable these features, the `--cfg tracing_unstable` must be passed to +//! `rustc` when compiling. +//! +//! The following unstable feature flags are currently available: +//! +//! * `valuable`: Enables support for recording [field values] using the +//! [`valuable`] crate. +//! +//! #### Enabling Unstable Features +//! +//! The easiest way to set the `tracing_unstable` cfg is to use the `RUSTFLAGS` +//! env variable when running `cargo` commands: +//! +//! ```shell +//! RUSTFLAGS="--cfg tracing_unstable" cargo build +//! ``` +//! Alternatively, the following can be added to the `.cargo/config` file in a +//! project to automatically enable the cfg flag for that project: +//! +//! ```toml +//! [build] +//! rustflags = ["--cfg", "tracing_unstable"] +//! ``` +//! +//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section +//! [field values]: crate::field +//! [`valuable`]: https://crates.io/crates/valuable +//! +//! ## Supported Rust Versions +//! +//! Tracing is built against the latest stable release. The minimum supported +//! version is 1.49. 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. +//! +//! [`log`]: https://docs.rs/log/0.4.6/log/ +//! [span]: mod@span +//! [spans]: mod@span +//! [`Span`]: span::Span +//! [`in_scope`]: span::Span::in_scope +//! [event]: Event +//! [events]: Event +//! [`Subscriber`]: subscriber::Subscriber +//! [Subscriber::event]: subscriber::Subscriber::event +//! [`enter`]: subscriber::Subscriber::enter +//! [`exit`]: subscriber::Subscriber::exit +//! [`enabled`]: subscriber::Subscriber::enabled +//! [metadata]: Metadata +//! [`field::display`]: field::display +//! [`field::debug`]: field::debug +//! [`set_global_default`]: subscriber::set_global_default +//! [`with_default`]: subscriber::with_default +//! [`tokio-rs/tracing`]: https://github.com/tokio-rs/tracing +//! [`tracing-futures`]: https://crates.io/crates/tracing-futures +//! [`tracing-subscriber`]: https://crates.io/crates/tracing-subscriber +//! [`tracing-log`]: https://crates.io/crates/tracing-log +//! [`tracing-timing`]: https://crates.io/crates/tracing-timing +//! [`tracing-appender`]: https://crates.io/crates/tracing-appender +//! [`env_logger`]: https://crates.io/crates/env_logger +//! [`FmtSubscriber`]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/struct.Subscriber.html +//! [static verbosity level]: level_filters#compile-time-filters +//! [instrument]: https://docs.rs/tracing-attributes/latest/tracing_attributes/attr.instrument.html +//! [flags]: #crate-feature-flags +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::broken_intra_doc_links))] +#![doc(html_root_url = "https://docs.rs/tracing/0.1.35")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png", + issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" +)] +#![warn( + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + unreachable_pub, + bad_style, + const_err, + dead_code, + improper_ctypes, + non_shorthand_field_patterns, + no_mangle_generic_items, + overflowing_literals, + path_statements, + patterns_in_fns_without_body, + private_in_public, + unconditional_recursion, + unused, + unused_allocation, + unused_comparisons, + unused_parens, + while_true +)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + +// Somehow this `use` statement is necessary for us to re-export the `core` +// macros on Rust 1.26.0. I'm not sure how this makes it work, but it does. +#[allow(unused_imports)] +#[doc(hidden)] +use tracing_core::*; + +#[doc(inline)] +pub use self::instrument::Instrument; +pub use self::{dispatcher::Dispatch, event::Event, field::Value, subscriber::Subscriber}; + +#[doc(hidden)] +pub use self::span::Id; + +#[doc(hidden)] +pub use tracing_core::{ + callsite::{self, Callsite}, + metadata, +}; +pub use tracing_core::{event, Level, Metadata}; + +#[doc(inline)] +pub use self::span::Span; +#[cfg(feature = "attributes")] +#[cfg_attr(docsrs, doc(cfg(feature = "attributes")))] +#[doc(inline)] +pub use tracing_attributes::instrument; + +#[macro_use] +mod macros; + +pub mod dispatcher; +pub mod field; +/// Attach a span to a `std::future::Future`. +pub mod instrument; +pub mod level_filters; +pub mod span; +pub(crate) mod stdlib; +pub mod subscriber; + +#[doc(hidden)] +pub mod __macro_support { + pub use crate::callsite::Callsite; + use crate::{subscriber::Interest, Metadata}; + pub use core::concat; + + /// Callsite implementation used by macro-generated code. + /// + /// /!\ WARNING: This is *not* a stable API! /!\ + /// This type, and all code contained in the `__macro_support` module, is + /// a *private* API of `tracing`. It is exposed publicly because it is used + /// by the `tracing` macros, but it is not part of the stable versioned API. + /// Breaking changes to this module may occur in small-numbered versions + /// without warning. + pub use tracing_core::callsite::DefaultCallsite as MacroCallsite; + + /// /!\ WARNING: This is *not* a stable API! /!\ + /// This function, and all code contained in the `__macro_support` module, is + /// a *private* API of `tracing`. It is exposed publicly because it is used + /// by the `tracing` macros, but it is not part of the stable versioned API. + /// Breaking changes to this module may occur in small-numbered versions + /// without warning. + pub fn __is_enabled(meta: &Metadata<'static>, interest: Interest) -> bool { + interest.is_always() || crate::dispatcher::get_default(|default| default.enabled(meta)) + } + + /// /!\ WARNING: This is *not* a stable API! /!\ + /// This function, and all code contained in the `__macro_support` module, is + /// a *private* API of `tracing`. It is exposed publicly because it is used + /// by the `tracing` macros, but it is not part of the stable versioned API. + /// Breaking changes to this module may occur in small-numbered versions + /// without warning. + #[inline] + #[cfg(feature = "log")] + pub fn __disabled_span(meta: &'static Metadata<'static>) -> crate::Span { + crate::Span::new_disabled(meta) + } + + /// /!\ WARNING: This is *not* a stable API! /!\ + /// This function, and all code contained in the `__macro_support` module, is + /// a *private* API of `tracing`. It is exposed publicly because it is used + /// by the `tracing` macros, but it is not part of the stable versioned API. + /// Breaking changes to this module may occur in small-numbered versions + /// without warning. + #[inline] + #[cfg(not(feature = "log"))] + pub fn __disabled_span(_: &'static Metadata<'static>) -> crate::Span { + crate::Span::none() + } + + /// /!\ WARNING: This is *not* a stable API! /!\ + /// This function, and all code contained in the `__macro_support` module, is + /// a *private* API of `tracing`. It is exposed publicly because it is used + /// by the `tracing` macros, but it is not part of the stable versioned API. + /// Breaking changes to this module may occur in small-numbered versions + /// without warning. + #[cfg(feature = "log")] + pub fn __tracing_log( + meta: &Metadata<'static>, + logger: &'static dyn log::Log, + log_meta: log::Metadata<'_>, + values: &tracing_core::field::ValueSet<'_>, + ) { + logger.log( + &crate::log::Record::builder() + .file(meta.file()) + .module_path(meta.module_path()) + .line(meta.line()) + .metadata(log_meta) + .args(format_args!( + "{}", + crate::log::LogValueSet { + values, + is_first: true + } + )) + .build(), + ); + } +} + +#[cfg(feature = "log")] +#[doc(hidden)] +pub mod log { + use core::fmt; + pub use log::*; + use tracing_core::field::{Field, ValueSet, Visit}; + + /// Utility to format [`ValueSet`]s for logging. + pub(crate) struct LogValueSet<'a> { + pub(crate) values: &'a ValueSet<'a>, + pub(crate) is_first: bool, + } + + impl<'a> fmt::Display for LogValueSet<'a> { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + struct LogVisitor<'a, 'b> { + f: &'a mut fmt::Formatter<'b>, + is_first: bool, + result: fmt::Result, + } + + impl Visit for LogVisitor<'_, '_> { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + let res = if self.is_first { + self.is_first = false; + if field.name() == "message" { + write!(self.f, "{:?}", value) + } else { + write!(self.f, "{}={:?}", field.name(), value) + } + } else { + write!(self.f, " {}={:?}", field.name(), value) + }; + if let Err(err) = res { + self.result = self.result.and(Err(err)); + } + } + + fn record_str(&mut self, field: &Field, value: &str) { + if field.name() == "message" { + self.record_debug(field, &format_args!("{}", value)) + } else { + self.record_debug(field, &value) + } + } + } + + let mut visit = LogVisitor { + f, + is_first: self.is_first, + result: Ok(()), + }; + self.values.record(&mut visit); + visit.result + } + } +} + +mod sealed { + pub trait Sealed {} +} diff --git a/vendor/tracing/src/macros.rs b/vendor/tracing/src/macros.rs new file mode 100644 index 000000000..b134af6e8 --- /dev/null +++ b/vendor/tracing/src/macros.rs @@ -0,0 +1,2504 @@ +/// Constructs a new span. +/// +/// See [the top-level documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [lib]: crate#using-the-macros +/// +/// # Examples +/// +/// Creating a new span: +/// ``` +/// # use tracing::{span, Level}; +/// # fn main() { +/// let span = span!(Level::TRACE, "my span"); +/// let _enter = span.enter(); +/// // do work inside the span... +/// # } +/// ``` +#[macro_export] +macro_rules! span { + (target: $target:expr, parent: $parent:expr, $lvl:expr, $name:expr) => { + $crate::span!(target: $target, parent: $parent, $lvl, $name,) + }; + (target: $target:expr, parent: $parent:expr, $lvl:expr, $name:expr, $($fields:tt)*) => { + { + use $crate::__macro_support::Callsite as _; + static CALLSITE: $crate::callsite::DefaultCallsite = $crate::callsite2! { + name: $name, + kind: $crate::metadata::Kind::SPAN, + target: $target, + level: $lvl, + fields: $($fields)* + }; + let mut interest = $crate::subscriber::Interest::never(); + if $crate::level_enabled!($lvl) + && { interest = CALLSITE.interest(); !interest.is_never() } + && $crate::__macro_support::__is_enabled(CALLSITE.metadata(), interest) + { + let meta = CALLSITE.metadata(); + // span with explicit parent + $crate::Span::child_of( + $parent, + meta, + &$crate::valueset!(meta.fields(), $($fields)*), + ) + } else { + let span = $crate::__macro_support::__disabled_span(CALLSITE.metadata()); + $crate::if_log_enabled! { $lvl, { + span.record_all(&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)); + }}; + span + } + } + }; + (target: $target:expr, $lvl:expr, $name:expr, $($fields:tt)*) => { + { + use $crate::__macro_support::Callsite as _; + static CALLSITE: $crate::callsite::DefaultCallsite = $crate::callsite2! { + name: $name, + kind: $crate::metadata::Kind::SPAN, + target: $target, + level: $lvl, + fields: $($fields)* + }; + let mut interest = $crate::subscriber::Interest::never(); + if $crate::level_enabled!($lvl) + && { interest = CALLSITE.interest(); !interest.is_never() } + && $crate::__macro_support::__is_enabled(CALLSITE.metadata(), interest) + { + let meta = CALLSITE.metadata(); + // span with contextual parent + $crate::Span::new( + meta, + &$crate::valueset!(meta.fields(), $($fields)*), + ) + } else { + let span = $crate::__macro_support::__disabled_span(CALLSITE.metadata()); + $crate::if_log_enabled! { $lvl, { + span.record_all(&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)); + }}; + span + } + } + }; + (target: $target:expr, parent: $parent:expr, $lvl:expr, $name:expr) => { + $crate::span!(target: $target, parent: $parent, $lvl, $name,) + }; + (parent: $parent:expr, $lvl:expr, $name:expr, $($fields:tt)*) => { + $crate::span!( + target: module_path!(), + parent: $parent, + $lvl, + $name, + $($fields)* + ) + }; + (parent: $parent:expr, $lvl:expr, $name:expr) => { + $crate::span!( + target: module_path!(), + parent: $parent, + $lvl, + $name, + ) + }; + (target: $target:expr, $lvl:expr, $name:expr, $($fields:tt)*) => { + $crate::span!( + target: $target, + $lvl, + $name, + $($fields)* + ) + }; + (target: $target:expr, $lvl:expr, $name:expr) => { + $crate::span!(target: $target, $lvl, $name,) + }; + ($lvl:expr, $name:expr, $($fields:tt)*) => { + $crate::span!( + target: module_path!(), + $lvl, + $name, + $($fields)* + ) + }; + ($lvl:expr, $name:expr) => { + $crate::span!( + target: module_path!(), + $lvl, + $name, + ) + }; +} + +/// Constructs a span at the trace level. +/// +/// [Fields] and [attributes] are set using the same syntax as the [`span!`] +/// macro. +/// +/// See [the top-level documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [lib]: crate#using-the-macros +/// [attributes]: crate#configuring-attributes +/// [Fields]: crate#recording-fields +/// [`span!`]: crate::span! +/// +/// # Examples +/// +/// ```rust +/// # use tracing::{trace_span, span, Level}; +/// # fn main() { +/// trace_span!("my_span"); +/// // is equivalent to: +/// span!(Level::TRACE, "my_span"); +/// # } +/// ``` +/// +/// ```rust +/// # use tracing::{trace_span, span, Level}; +/// # fn main() { +/// let span = trace_span!("my span"); +/// span.in_scope(|| { +/// // do work inside the span... +/// }); +/// # } +/// ``` +#[macro_export] +macro_rules! trace_span { + (target: $target:expr, parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + parent: $parent, + $crate::Level::TRACE, + $name, + $($field)* + ) + }; + (target: $target:expr, parent: $parent:expr, $name:expr) => { + $crate::trace_span!(target: $target, parent: $parent, $name,) + }; + (parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + $name, + $($field)* + ) + }; + (parent: $parent:expr, $name:expr) => { + $crate::trace_span!(parent: $parent, $name,) + }; + (target: $target:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + $crate::Level::TRACE, + $name, + $($field)* + ) + }; + (target: $target:expr, $name:expr) => { + $crate::trace_span!(target: $target, $name,) + }; + ($name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + $crate::Level::TRACE, + $name, + $($field)* + ) + }; + ($name:expr) => { $crate::trace_span!($name,) }; +} + +/// Constructs a span at the debug level. +/// +/// [Fields] and [attributes] are set using the same syntax as the [`span!`] +/// macro. +/// +/// See [the top-level documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [lib]: crate#using-the-macros +/// [attributes]: crate#configuring-attributes +/// [Fields]: crate#recording-fields +/// [`span!`]: crate::span! +/// +/// # Examples +/// +/// ```rust +/// # use tracing::{debug_span, span, Level}; +/// # fn main() { +/// debug_span!("my_span"); +/// // is equivalent to: +/// span!(Level::DEBUG, "my_span"); +/// # } +/// ``` +/// +/// ```rust +/// # use tracing::debug_span; +/// # fn main() { +/// let span = debug_span!("my span"); +/// span.in_scope(|| { +/// // do work inside the span... +/// }); +/// # } +/// ``` +#[macro_export] +macro_rules! debug_span { + (target: $target:expr, parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + parent: $parent, + $crate::Level::DEBUG, + $name, + $($field)* + ) + }; + (target: $target:expr, parent: $parent:expr, $name:expr) => { + $crate::debug_span!(target: $target, parent: $parent, $name,) + }; + (parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + $name, + $($field)* + ) + }; + (parent: $parent:expr, $name:expr) => { + $crate::debug_span!(parent: $parent, $name,) + }; + (target: $target:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + $crate::Level::DEBUG, + $name, + $($field)* + ) + }; + (target: $target:expr, $name:expr) => { + $crate::debug_span!(target: $target, $name,) + }; + ($name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + $crate::Level::DEBUG, + $name, + $($field)* + ) + }; + ($name:expr) => {$crate::debug_span!($name,)}; +} + +/// Constructs a span at the info level. +/// +/// [Fields] and [attributes] are set using the same syntax as the [`span!`] +/// macro. +/// +/// See [the top-level documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [lib]: crate#using-the-macros +/// [attributes]: crate#configuring-attributes +/// [Fields]: crate#recording-fields +/// [`span!`]: crate::span! +/// +/// # Examples +/// +/// ```rust +/// # use tracing::{span, info_span, Level}; +/// # fn main() { +/// info_span!("my_span"); +/// // is equivalent to: +/// span!(Level::INFO, "my_span"); +/// # } +/// ``` +/// +/// ```rust +/// # use tracing::info_span; +/// # fn main() { +/// let span = info_span!("my span"); +/// span.in_scope(|| { +/// // do work inside the span... +/// }); +/// # } +/// ``` +#[macro_export] +macro_rules! info_span { + (target: $target:expr, parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + parent: $parent, + $crate::Level::INFO, + $name, + $($field)* + ) + }; + (target: $target:expr, parent: $parent:expr, $name:expr) => { + $crate::info_span!(target: $target, parent: $parent, $name,) + }; + (parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + $name, + $($field)* + ) + }; + (parent: $parent:expr, $name:expr) => { + $crate::info_span!(parent: $parent, $name,) + }; + (target: $target:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + $crate::Level::INFO, + $name, + $($field)* + ) + }; + (target: $target:expr, $name:expr) => { + $crate::info_span!(target: $target, $name,) + }; + ($name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + $crate::Level::INFO, + $name, + $($field)* + ) + }; + ($name:expr) => {$crate::info_span!($name,)}; +} + +/// Constructs a span at the warn level. +/// +/// [Fields] and [attributes] are set using the same syntax as the [`span!`] +/// macro. +/// +/// See [the top-level documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [lib]: crate#using-the-macros +/// [attributes]: crate#configuring-attributes +/// [Fields]: crate#recording-fields +/// [`span!`]: crate::span! +/// +/// # Examples +/// +/// ```rust +/// # use tracing::{warn_span, span, Level}; +/// # fn main() { +/// warn_span!("my_span"); +/// // is equivalent to: +/// span!(Level::WARN, "my_span"); +/// # } +/// ``` +/// +/// ```rust +/// use tracing::warn_span; +/// # fn main() { +/// let span = warn_span!("my span"); +/// span.in_scope(|| { +/// // do work inside the span... +/// }); +/// # } +/// ``` +#[macro_export] +macro_rules! warn_span { + (target: $target:expr, parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + parent: $parent, + $crate::Level::WARN, + $name, + $($field)* + ) + }; + (target: $target:expr, parent: $parent:expr, $name:expr) => { + $crate::warn_span!(target: $target, parent: $parent, $name,) + }; + (parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + $name, + $($field)* + ) + }; + (parent: $parent:expr, $name:expr) => { + $crate::warn_span!(parent: $parent, $name,) + }; + (target: $target:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + $crate::Level::WARN, + $name, + $($field)* + ) + }; + (target: $target:expr, $name:expr) => { + $crate::warn_span!(target: $target, $name,) + }; + ($name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + $crate::Level::WARN, + $name, + $($field)* + ) + }; + ($name:expr) => {$crate::warn_span!($name,)}; +} +/// Constructs a span at the error level. +/// +/// [Fields] and [attributes] are set using the same syntax as the [`span!`] +/// macro. +/// +/// See [the top-level documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [lib]: crate#using-the-macros +/// [attributes]: crate#configuring-attributes +/// [Fields]: crate#recording-fields +/// [`span!`]: crate::span! +/// +/// # Examples +/// +/// ```rust +/// # use tracing::{span, error_span, Level}; +/// # fn main() { +/// error_span!("my_span"); +/// // is equivalent to: +/// span!(Level::ERROR, "my_span"); +/// # } +/// ``` +/// +/// ```rust +/// # use tracing::error_span; +/// # fn main() { +/// let span = error_span!("my span"); +/// span.in_scope(|| { +/// // do work inside the span... +/// }); +/// # } +/// ``` +#[macro_export] +macro_rules! error_span { + (target: $target:expr, parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + parent: $parent, + $crate::Level::ERROR, + $name, + $($field)* + ) + }; + (target: $target:expr, parent: $parent:expr, $name:expr) => { + $crate::error_span!(target: $target, parent: $parent, $name,) + }; + (parent: $parent:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + $name, + $($field)* + ) + }; + (parent: $parent:expr, $name:expr) => { + $crate::error_span!(parent: $parent, $name,) + }; + (target: $target:expr, $name:expr, $($field:tt)*) => { + $crate::span!( + target: $target, + $crate::Level::ERROR, + $name, + $($field)* + ) + }; + (target: $target:expr, $name:expr) => { + $crate::error_span!(target: $target, $name,) + }; + ($name:expr, $($field:tt)*) => { + $crate::span!( + target: module_path!(), + $crate::Level::ERROR, + $name, + $($field)* + ) + }; + ($name:expr) => {$crate::error_span!($name,)}; +} + +/// Constructs a new `Event`. +/// +/// The event macro is invoked with a `Level` and up to 32 key-value fields. +/// Optionally, a format string and arguments may follow the fields; this will +/// be used to construct an implicit field named "message". +/// +/// See [the top-level documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [lib]: crate#using-the-macros +/// +/// # Examples +/// +/// ```rust +/// use tracing::{event, Level}; +/// +/// # fn main() { +/// let data = (42, "forty-two"); +/// let private_data = "private"; +/// let error = "a bad error"; +/// +/// event!(Level::ERROR, %error, "Received error"); +/// event!( +/// target: "app_events", +/// Level::WARN, +/// private_data, +/// ?data, +/// "App warning: {}", +/// error +/// ); +/// event!(Level::INFO, the_answer = data.0); +/// # } +/// ``` +/// +// /// Note that *unlike `span!`*, `event!` requires a value for all fields. As +// /// events are recorded immediately when the macro is invoked, there is no +// /// opportunity for fields to be recorded later. A trailing comma on the final +// /// field is valid. +// /// +// /// For example, the following does not compile: +// /// ```rust,compile_fail +// /// # use tracing::{Level, event}; +// /// # fn main() { +// /// event!(Level::INFO, foo = 5, bad_field, bar = "hello") +// /// #} +// /// ``` +#[macro_export] +macro_rules! event { + (target: $target:expr, parent: $parent:expr, $lvl:expr, { $($fields:tt)* } )=> ({ + use $crate::__macro_support::Callsite as _; + static CALLSITE: $crate::callsite::DefaultCallsite = $crate::callsite2! { + name: $crate::__macro_support::concat!( + "event ", + file!(), + ":", + line!() + ), + kind: $crate::metadata::Kind::EVENT, + target: $target, + level: $lvl, + fields: $($fields)* + }; + + let enabled = $crate::level_enabled!($lvl) && { + let interest = CALLSITE.interest(); + !interest.is_never() && $crate::__macro_support::__is_enabled(CALLSITE.metadata(), interest) + }; + if enabled { + (|value_set: $crate::field::ValueSet| { + $crate::__tracing_log!( + $lvl, + CALLSITE, + &value_set + ); + let meta = CALLSITE.metadata(); + // event with explicit parent + $crate::Event::child_of( + $parent, + meta, + &value_set + ); + })($crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)); + } else { + $crate::__tracing_log!( + $lvl, + CALLSITE, + &$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*) + ); + } + }); + + (target: $target:expr, parent: $parent:expr, $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => ( + $crate::event!( + target: $target, + parent: $parent, + $lvl, + { message = format_args!($($arg)+), $($fields)* } + ) + ); + (target: $target:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => ( + $crate::event!(target: $target, parent: $parent, $lvl, { $($k).+ = $($fields)* }) + ); + (target: $target:expr, parent: $parent:expr, $lvl:expr, $($arg:tt)+) => ( + $crate::event!(target: $target, parent: $parent, $lvl, { $($arg)+ }) + ); + (target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({ + use $crate::__macro_support::Callsite as _; + static CALLSITE: $crate::callsite::DefaultCallsite = $crate::callsite2! { + name: $crate::__macro_support::concat!( + "event ", + file!(), + ":", + line!() + ), + kind: $crate::metadata::Kind::EVENT, + target: $target, + level: $lvl, + fields: $($fields)* + }; + let enabled = $crate::level_enabled!($lvl) && { + let interest = CALLSITE.interest(); + !interest.is_never() && $crate::__macro_support::__is_enabled(CALLSITE.metadata(), interest) + }; + if enabled { + (|value_set: $crate::field::ValueSet| { + let meta = CALLSITE.metadata(); + // event with contextual parent + $crate::Event::dispatch( + meta, + &value_set + ); + $crate::__tracing_log!( + $lvl, + CALLSITE, + &value_set + ); + })($crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)); + } else { + $crate::__tracing_log!( + $lvl, + CALLSITE, + &$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*) + ); + } + }); + (target: $target:expr, $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => ( + $crate::event!( + target: $target, + $lvl, + { message = format_args!($($arg)+), $($fields)* } + ) + ); + (target: $target:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => ( + $crate::event!(target: $target, $lvl, { $($k).+ = $($fields)* }) + ); + (target: $target:expr, $lvl:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, $lvl, { $($arg)+ }) + ); + (parent: $parent:expr, $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $lvl, + { message = format_args!($($arg)+), $($fields)* } + ) + ); + (parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $lvl, + { $($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $lvl:expr, ?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $lvl, + { ?$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $lvl:expr, %$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $lvl, + { %$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $lvl:expr, $($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $lvl, + { $($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $lvl:expr, %$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $lvl, + { %$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $lvl:expr, ?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $lvl, + { ?$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $lvl:expr, $($arg:tt)+ ) => ( + $crate::event!(target: module_path!(), parent: $parent, $lvl, { $($arg)+ }) + ); + ( $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + $lvl, + { message = format_args!($($arg)+), $($fields)* } + ) + ); + ( $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + $lvl, + { message = format_args!($($arg)+), $($fields)* } + ) + ); + ($lvl:expr, $($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $lvl, + { $($k).+ = $($field)*} + ) + ); + ($lvl:expr, $($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $lvl, + { $($k).+, $($field)*} + ) + ); + ($lvl:expr, ?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $lvl, + { ?$($k).+, $($field)*} + ) + ); + ($lvl:expr, %$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $lvl, + { %$($k).+, $($field)*} + ) + ); + ($lvl:expr, ?$($k:ident).+) => ( + $crate::event!($lvl, ?$($k).+,) + ); + ($lvl:expr, %$($k:ident).+) => ( + $crate::event!($lvl, %$($k).+,) + ); + ($lvl:expr, $($k:ident).+) => ( + $crate::event!($lvl, $($k).+,) + ); + ( $lvl:expr, $($arg:tt)+ ) => ( + $crate::event!(target: module_path!(), $lvl, { $($arg)+ }) + ); +} + +/// Tests whether an event with the specified level and target would be enabled. +/// +/// This is similar to [`enabled!`], but queries the current subscriber specifically for +/// an event, whereas [`enabled!`] queries for an event _or_ span. +/// +/// See the documentation for [`enabled!]` for more details on using this macro. +/// See also [`span_enabled!`]. +/// +/// # Examples +/// +/// ```rust +/// # use tracing::{event_enabled, Level}; +/// if event_enabled!(target: "my_crate", Level::DEBUG) { +/// // some expensive work... +/// } +/// // simpler +/// if event_enabled!(Level::DEBUG) { +/// // some expensive work... +/// } +/// // with fields +/// if event_enabled!(Level::DEBUG, foo_field) { +/// // some expensive work... +/// } +/// ``` +/// +/// [`enabled!`]: crate::enabled +/// [`span_enabled!`]: crate::span_enabled +#[macro_export] +macro_rules! event_enabled { + ($($rest:tt)*)=> ( + $crate::enabled!(kind: $crate::metadata::Kind::EVENT, $($rest)*) + ) +} + +/// Tests whether a span with the specified level and target would be enabled. +/// +/// This is similar to [`enabled!`], but queries the current subscriber specifically for +/// an event, whereas [`enabled!`] queries for an event _or_ span. +/// +/// See the documentation for [`enabled!]` for more details on using this macro. +/// See also [`span_enabled!`]. +/// +/// # Examples +/// +/// ```rust +/// # use tracing::{span_enabled, Level}; +/// if span_enabled!(target: "my_crate", Level::DEBUG) { +/// // some expensive work... +/// } +/// // simpler +/// if span_enabled!(Level::DEBUG) { +/// // some expensive work... +/// } +/// // with fields +/// if span_enabled!(Level::DEBUG, foo_field) { +/// // some expensive work... +/// } +/// ``` +/// +/// [`enabled!`]: crate::enabled +/// [`span_enabled!`]: crate::span_enabled +#[macro_export] +macro_rules! span_enabled { + ($($rest:tt)*)=> ( + $crate::enabled!(kind: $crate::metadata::Kind::SPAN, $($rest)*) + ) +} + +/// Checks whether a span or event is [enabled] based on the provided [metadata]. +/// +/// [enabled]: crate::Subscriber::enabled +/// [metadata]: crate::Metadata +/// +/// This macro is a specialized tool: it is intended to be used prior +/// to an expensive computation required *just* for that event, but +/// *cannot* be done as part of an argument to that event, such as +/// when multiple events are emitted (e.g., iterating over a collection +/// and emitting an event for each item). +/// +/// # Usage +/// +/// [Subscribers] can make filtering decisions based all the data included in a +/// span or event's [`Metadata`]. This means that it is possible for `enabled!` +/// to return a _false positive_ (indicating that something would be enabled +/// when it actually would not be) or a _false negative_ (indicating that +/// something would be disabled when it would actually be enabled). +/// +/// [Subscribers]: crate::subscriber::Subscriber +/// [`Metadata`]: crate::metadata::Metadata +/// +/// This occurs when a subscriber is using a _more specific_ filter than the +/// metadata provided to the `enabled!` macro. Some situations that can result +/// in false positives or false negatives include: +/// +/// - If a subscriber is using a filter which may enable a span or event based +/// on field names, but `enabled!` is invoked without listing field names, +/// `enabled!` may return a false negative if a specific field name would +/// cause the subscriber to enable something that would otherwise be disabled. +/// - If a subscriber is using a filter which enables or disables specific events by +/// file path and line number, a particular event may be enabled/disabled +/// even if an `enabled!` invocation with the same level, target, and fields +/// indicated otherwise. +/// - The subscriber can choose to enable _only_ spans or _only_ events, which `enabled` +/// will not reflect. +/// +/// `enabled!()` requires a [level](crate::Level) argument, an optional `target:` +/// argument, and an optional set of field names. If the fields are not provided, +/// they are considered to be unknown. `enabled!` attempts to match the +/// syntax of `event!()` as closely as possible, which can be seen in the +/// examples below. +/// +/// # Examples +/// +/// If the current subscriber is interested in recording `DEBUG`-level spans and +/// events in the current file and module path, this will evaluate to true: +/// ```rust +/// use tracing::{enabled, Level}; +/// +/// if enabled!(Level::DEBUG) { +/// // some expensive work... +/// } +/// ``` +/// +/// If the current subscriber is interested in recording spans and events +/// in the current file and module path, with the target "my_crate", and at the +/// level `DEBUG`, this will evaluate to true: +/// ```rust +/// # use tracing::{enabled, Level}; +/// if enabled!(target: "my_crate", Level::DEBUG) { +/// // some expensive work... +/// } +/// ``` +/// +/// If the current subscriber is interested in recording spans and events +/// in the current file and module path, with the target "my_crate", at +/// the level `DEBUG`, and with a field named "hello", this will evaluate +/// to true: +/// +/// ```rust +/// # use tracing::{enabled, Level}; +/// if enabled!(target: "my_crate", Level::DEBUG, hello) { +/// // some expensive work... +/// } +/// ``` +/// +/// # Alternatives +/// +/// `enabled!` queries subscribers with [`Metadata`] where +/// [`is_event`] and [`is_span`] both return `false`. Alternatively, +/// use [`event_enabled!`] or [`span_enabled!`] to ensure one of these +/// returns true. +/// +/// +/// [`Metadata`]: crate::Metadata +/// [`is_event`]: crate::Metadata::is_event +/// [`is_span`]: crate::Metadata::is_span +/// [`enabled!`]: crate::enabled +/// [`span_enabled!`]: crate::span_enabled +#[macro_export] +macro_rules! enabled { + (kind: $kind:expr, target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({ + if $crate::level_enabled!($lvl) { + use $crate::__macro_support::Callsite as _; + static CALLSITE: $crate::callsite::DefaultCallsite = $crate::callsite2! { + name: $crate::__macro_support::concat!( + "enabled ", + file!(), + ":", + line!() + ), + kind: $kind.hint(), + target: $target, + level: $lvl, + fields: $($fields)* + }; + let interest = CALLSITE.interest(); + if !interest.is_never() && $crate::__macro_support::__is_enabled(CALLSITE.metadata(), interest) { + let meta = CALLSITE.metadata(); + $crate::dispatcher::get_default(|current| current.enabled(meta)) + } else { + false + } + } else { + false + } + }); + // Just target and level + (kind: $kind:expr, target: $target:expr, $lvl:expr ) => ( + $crate::enabled!(kind: $kind, target: $target, $lvl, { }) + ); + (target: $target:expr, $lvl:expr ) => ( + $crate::enabled!(kind: $crate::metadata::Kind::HINT, target: $target, $lvl, { }) + ); + + // These four cases handle fields with no values + (kind: $kind:expr, target: $target:expr, $lvl:expr, $($field:tt)*) => ( + $crate::enabled!( + kind: $kind, + target: $target, + $lvl, + { $($field)*} + ) + ); + (target: $target:expr, $lvl:expr, $($field:tt)*) => ( + $crate::enabled!( + kind: $crate::metadata::Kind::HINT, + target: $target, + $lvl, + { $($field)*} + ) + ); + + // Level and field case + (kind: $kind:expr, $lvl:expr, $($field:tt)*) => ( + $crate::enabled!( + kind: $kind, + target: module_path!(), + $lvl, + { $($field)*} + ) + ); + + // Simplest `enabled!` case + (kind: $kind:expr, $lvl:expr) => ( + $crate::enabled!(kind: $kind, target: module_path!(), $lvl, { }) + ); + ($lvl:expr) => ( + $crate::enabled!(kind: $crate::metadata::Kind::HINT, target: module_path!(), $lvl, { }) + ); + + // Fallthrough from above + ($lvl:expr, $($field:tt)*) => ( + $crate::enabled!( + kind: $crate::metadata::Kind::HINT, + target: module_path!(), + $lvl, + { $($field)*} + ) + ); +} + +/// Constructs an event at the trace level. +/// +/// This functions similarly to the [`event!`] macro. See [the top-level +/// documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [`event!`]: crate::event! +/// [lib]: crate#using-the-macros +/// +/// # Examples +/// +/// ```rust +/// use tracing::trace; +/// # #[derive(Debug, Copy, Clone)] struct Position { x: f32, y: f32 } +/// # impl Position { +/// # const ORIGIN: Self = Self { x: 0.0, y: 0.0 }; +/// # fn dist(&self, other: Position) -> f32 { +/// # let x = (other.x - self.x).exp2(); let y = (self.y - other.y).exp2(); +/// # (x + y).sqrt() +/// # } +/// # } +/// # fn main() { +/// let pos = Position { x: 3.234, y: -1.223 }; +/// let origin_dist = pos.dist(Position::ORIGIN); +/// +/// trace!(position = ?pos, ?origin_dist); +/// trace!( +/// target: "app_events", +/// position = ?pos, +/// "x is {} and y is {}", +/// if pos.x >= 0.0 { "positive" } else { "negative" }, +/// if pos.y >= 0.0 { "positive" } else { "negative" } +/// ); +/// # } +/// ``` +#[macro_export] +macro_rules! trace { + (target: $target:expr, parent: $parent:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::TRACE, { $($field)* }, $($arg)*) + ); + (target: $target:expr, parent: $parent:expr, $($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::TRACE, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, ?$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::TRACE, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, %$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::TRACE, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::TRACE, {}, $($arg)+) + ); + (parent: $parent:expr, { $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + { $($field)+ }, + $($arg)+ + ) + ); + (parent: $parent:expr, $($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + { $($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + { ?$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + { %$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + { $($k).+, $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + { ?$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + { %$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::TRACE, + {}, + $($arg)+ + ) + ); + (target: $target:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::TRACE, { $($field)* }, $($arg)*) + ); + (target: $target:expr, $($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::TRACE, { $($k).+ $($field)* }) + ); + (target: $target:expr, ?$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::TRACE, { ?$($k).+ $($field)* }) + ); + (target: $target:expr, %$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::TRACE, { %$($k).+ $($field)* }) + ); + (target: $target:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, $crate::Level::TRACE, {}, $($arg)+) + ); + ({ $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { $($field)+ }, + $($arg)+ + ) + ); + ($($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { $($k).+ = $($field)*} + ) + ); + ($($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { $($k).+, $($field)*} + ) + ); + (?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { ?$($k).+, $($field)*} + ) + ); + (%$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { %$($k).+, $($field)*} + ) + ); + (?$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { ?$($k).+ } + ) + ); + (%$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { %$($k).+ } + ) + ); + ($($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + { $($k).+ } + ) + ); + ($($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::TRACE, + {}, + $($arg)+ + ) + ); +} + +/// Constructs an event at the debug level. +/// +/// This functions similarly to the [`event!`] macro. See [the top-level +/// documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [`event!`]: crate::event! +/// [lib]: crate#using-the-macros +/// +/// # Examples +/// +/// ```rust +/// use tracing::debug; +/// # fn main() { +/// # #[derive(Debug)] struct Position { x: f32, y: f32 } +/// +/// let pos = Position { x: 3.234, y: -1.223 }; +/// +/// debug!(?pos.x, ?pos.y); +/// debug!(target: "app_events", position = ?pos, "New position"); +/// # } +/// ``` +#[macro_export] +macro_rules! debug { + (target: $target:expr, parent: $parent:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::DEBUG, { $($field)* }, $($arg)*) + ); + (target: $target:expr, parent: $parent:expr, $($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::DEBUG, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, ?$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::DEBUG, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, %$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::DEBUG, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::DEBUG, {}, $($arg)+) + ); + (parent: $parent:expr, { $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + { $($field)+ }, + $($arg)+ + ) + ); + (parent: $parent:expr, $($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + { $($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + { ?$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + { %$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + { $($k).+, $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + { ?$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + { %$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::DEBUG, + {}, + $($arg)+ + ) + ); + (target: $target:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::DEBUG, { $($field)* }, $($arg)*) + ); + (target: $target:expr, $($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::DEBUG, { $($k).+ $($field)* }) + ); + (target: $target:expr, ?$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::DEBUG, { ?$($k).+ $($field)* }) + ); + (target: $target:expr, %$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::DEBUG, { %$($k).+ $($field)* }) + ); + (target: $target:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, $crate::Level::DEBUG, {}, $($arg)+) + ); + ({ $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { $($field)+ }, + $($arg)+ + ) + ); + ($($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { $($k).+ = $($field)*} + ) + ); + (?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { ?$($k).+ = $($field)*} + ) + ); + (%$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { %$($k).+ = $($field)*} + ) + ); + ($($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { $($k).+, $($field)*} + ) + ); + (?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { ?$($k).+, $($field)*} + ) + ); + (%$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { %$($k).+, $($field)*} + ) + ); + (?$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { ?$($k).+ } + ) + ); + (%$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { %$($k).+ } + ) + ); + ($($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + { $($k).+ } + ) + ); + ($($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::DEBUG, + {}, + $($arg)+ + ) + ); +} + +/// Constructs an event at the info level. +/// +/// This functions similarly to the [`event!`] macro. See [the top-level +/// documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [`event!`]: crate::event! +/// [lib]: crate#using-the-macros +/// +/// # Examples +/// +/// ```rust +/// use tracing::info; +/// # // this is so the test will still work in no-std mode +/// # #[derive(Debug)] +/// # pub struct Ipv4Addr; +/// # impl Ipv4Addr { fn new(o1: u8, o2: u8, o3: u8, o4: u8) -> Self { Self } } +/// # fn main() { +/// # struct Connection { port: u32, speed: f32 } +/// use tracing::field; +/// +/// let addr = Ipv4Addr::new(127, 0, 0, 1); +/// let conn = Connection { port: 40, speed: 3.20 }; +/// +/// info!(conn.port, "connected to {:?}", addr); +/// info!( +/// target: "connection_events", +/// ip = ?addr, +/// conn.port, +/// ?conn.speed, +/// ); +/// # } +/// ``` +#[macro_export] +macro_rules! info { + (target: $target:expr, parent: $parent:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::INFO, { $($field)* }, $($arg)*) + ); + (target: $target:expr, parent: $parent:expr, $($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::INFO, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, ?$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::INFO, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, %$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::INFO, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::INFO, {}, $($arg)+) + ); + (parent: $parent:expr, { $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + { $($field)+ }, + $($arg)+ + ) + ); + (parent: $parent:expr, $($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + { $($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + { ?$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + { %$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + { $($k).+, $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + { ?$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + { %$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::INFO, + {}, + $($arg)+ + ) + ); + (target: $target:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::INFO, { $($field)* }, $($arg)*) + ); + (target: $target:expr, $($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::INFO, { $($k).+ $($field)* }) + ); + (target: $target:expr, ?$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::INFO, { ?$($k).+ $($field)* }) + ); + (target: $target:expr, %$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::INFO, { $($k).+ $($field)* }) + ); + (target: $target:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, $crate::Level::INFO, {}, $($arg)+) + ); + ({ $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { $($field)+ }, + $($arg)+ + ) + ); + ($($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { $($k).+ = $($field)*} + ) + ); + (?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { ?$($k).+ = $($field)*} + ) + ); + (%$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { %$($k).+ = $($field)*} + ) + ); + ($($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { $($k).+, $($field)*} + ) + ); + (?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { ?$($k).+, $($field)*} + ) + ); + (%$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { %$($k).+, $($field)*} + ) + ); + (?$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { ?$($k).+ } + ) + ); + (%$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { %$($k).+ } + ) + ); + ($($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + { $($k).+ } + ) + ); + ($($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::INFO, + {}, + $($arg)+ + ) + ); +} + +/// Constructs an event at the warn level. +/// +/// This functions similarly to the [`event!`] macro. See [the top-level +/// documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [`event!`]: crate::event! +/// [lib]: crate#using-the-macros +/// +/// # Examples +/// +/// ```rust +/// use tracing::warn; +/// # fn main() { +/// +/// let warn_description = "Invalid Input"; +/// let input = &[0x27, 0x45]; +/// +/// warn!(?input, warning = warn_description); +/// warn!( +/// target: "input_events", +/// warning = warn_description, +/// "Received warning for input: {:?}", input, +/// ); +/// # } +/// ``` +#[macro_export] +macro_rules! warn { + (target: $target:expr, parent: $parent:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::WARN, { $($field)* }, $($arg)*) + ); + (target: $target:expr, parent: $parent:expr, $($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::WARN, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, ?$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::WARN, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, %$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::WARN, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::WARN, {}, $($arg)+) + ); + (parent: $parent:expr, { $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + { $($field)+ }, + $($arg)+ + ) + ); + (parent: $parent:expr, $($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + { $($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + { ?$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + { %$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + { $($k).+, $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + { ?$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + { %$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::WARN, + {}, + $($arg)+ + ) + ); + (target: $target:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::WARN, { $($field)* }, $($arg)*) + ); + (target: $target:expr, $($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::WARN, { $($k).+ $($field)* }) + ); + (target: $target:expr, ?$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::WARN, { ?$($k).+ $($field)* }) + ); + (target: $target:expr, %$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::WARN, { %$($k).+ $($field)* }) + ); + (target: $target:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, $crate::Level::WARN, {}, $($arg)+) + ); + ({ $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { $($field)+ }, + $($arg)+ + ) + ); + ($($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { $($k).+ = $($field)*} + ) + ); + (?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { ?$($k).+ = $($field)*} + ) + ); + (%$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { %$($k).+ = $($field)*} + ) + ); + ($($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { $($k).+, $($field)*} + ) + ); + (?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { ?$($k).+, $($field)*} + ) + ); + (%$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { %$($k).+, $($field)*} + ) + ); + (?$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { ?$($k).+ } + ) + ); + (%$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { %$($k).+ } + ) + ); + ($($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + { $($k).+ } + ) + ); + ($($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::WARN, + {}, + $($arg)+ + ) + ); +} + +/// Constructs an event at the error level. +/// +/// This functions similarly to the [`event!`] macro. See [the top-level +/// documentation][lib] for details on the syntax accepted by +/// this macro. +/// +/// [`event!`]: crate::event! +/// [lib]: crate#using-the-macros +/// +/// # Examples +/// +/// ```rust +/// use tracing::error; +/// # fn main() { +/// +/// let (err_info, port) = ("No connection", 22); +/// +/// error!(port, error = %err_info); +/// error!(target: "app_events", "App Error: {}", err_info); +/// error!({ info = err_info }, "error on port: {}", port); +/// # } +/// ``` +#[macro_export] +macro_rules! error { + (target: $target:expr, parent: $parent:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::ERROR, { $($field)* }, $($arg)*) + ); + (target: $target:expr, parent: $parent:expr, $($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::ERROR, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, ?$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::ERROR, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, %$($k:ident).+ $($field:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::ERROR, { $($k).+ $($field)+ }) + ); + (target: $target:expr, parent: $parent:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, parent: $parent, $crate::Level::ERROR, {}, $($arg)+) + ); + (parent: $parent:expr, { $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + { $($field)+ }, + $($arg)+ + ) + ); + (parent: $parent:expr, $($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + { $($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + { ?$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + { %$($k).+ = $($field)*} + ) + ); + (parent: $parent:expr, $($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + { $($k).+, $($field)*} + ) + ); + (parent: $parent:expr, ?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + { ?$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, %$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + { %$($k).+, $($field)*} + ) + ); + (parent: $parent:expr, $($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + parent: $parent, + $crate::Level::ERROR, + {}, + $($arg)+ + ) + ); + (target: $target:expr, { $($field:tt)* }, $($arg:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::ERROR, { $($field)* }, $($arg)*) + ); + (target: $target:expr, $($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::ERROR, { $($k).+ $($field)* }) + ); + (target: $target:expr, ?$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::ERROR, { ?$($k).+ $($field)* }) + ); + (target: $target:expr, %$($k:ident).+ $($field:tt)* ) => ( + $crate::event!(target: $target, $crate::Level::ERROR, { %$($k).+ $($field)* }) + ); + (target: $target:expr, $($arg:tt)+ ) => ( + $crate::event!(target: $target, $crate::Level::ERROR, {}, $($arg)+) + ); + ({ $($field:tt)+ }, $($arg:tt)+ ) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { $($field)+ }, + $($arg)+ + ) + ); + ($($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { $($k).+ = $($field)*} + ) + ); + (?$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { ?$($k).+ = $($field)*} + ) + ); + (%$($k:ident).+ = $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { %$($k).+ = $($field)*} + ) + ); + ($($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { $($k).+, $($field)*} + ) + ); + (?$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { ?$($k).+, $($field)*} + ) + ); + (%$($k:ident).+, $($field:tt)*) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { %$($k).+, $($field)*} + ) + ); + (?$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { ?$($k).+ } + ) + ); + (%$($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { %$($k).+ } + ) + ); + ($($k:ident).+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + { $($k).+ } + ) + ); + ($($arg:tt)+) => ( + $crate::event!( + target: module_path!(), + $crate::Level::ERROR, + {}, + $($arg)+ + ) + ); +} + +/// Constructs a new static callsite for a span or event. +#[doc(hidden)] +#[macro_export] +macro_rules! callsite { + (name: $name:expr, kind: $kind:expr, fields: $($fields:tt)*) => {{ + $crate::callsite! { + name: $name, + kind: $kind, + target: module_path!(), + level: $crate::Level::TRACE, + fields: $($fields)* + } + }}; + ( + name: $name:expr, + kind: $kind:expr, + level: $lvl:expr, + fields: $($fields:tt)* + ) => {{ + $crate::callsite! { + name: $name, + kind: $kind, + target: module_path!(), + level: $lvl, + fields: $($fields)* + } + }}; + ( + name: $name:expr, + kind: $kind:expr, + target: $target:expr, + level: $lvl:expr, + fields: $($fields:tt)* + ) => {{ + static META: $crate::Metadata<'static> = { + $crate::metadata! { + name: $name, + target: $target, + level: $lvl, + fields: $crate::fieldset!( $($fields)* ), + callsite: &CALLSITE, + kind: $kind, + } + }; + static CALLSITE: $crate::callsite::DefaultCallsite = $crate::callsite::DefaultCallsite::new(&META); + CALLSITE.register(); + &CALLSITE + }}; +} + +/// Constructs a new static callsite for a span or event. +#[doc(hidden)] +#[macro_export] +macro_rules! callsite2 { + (name: $name:expr, kind: $kind:expr, fields: $($fields:tt)*) => {{ + $crate::callsite2! { + name: $name, + kind: $kind, + target: module_path!(), + level: $crate::Level::TRACE, + fields: $($fields)* + } + }}; + ( + name: $name:expr, + kind: $kind:expr, + level: $lvl:expr, + fields: $($fields:tt)* + ) => {{ + $crate::callsite2! { + name: $name, + kind: $kind, + target: module_path!(), + level: $lvl, + fields: $($fields)* + } + }}; + ( + name: $name:expr, + kind: $kind:expr, + target: $target:expr, + level: $lvl:expr, + fields: $($fields:tt)* + ) => {{ + static META: $crate::Metadata<'static> = { + $crate::metadata! { + name: $name, + target: $target, + level: $lvl, + fields: $crate::fieldset!( $($fields)* ), + callsite: &CALLSITE, + kind: $kind, + } + }; + $crate::callsite::DefaultCallsite::new(&META) + }}; +} + +#[macro_export] +// TODO: determine if this ought to be public API?` +#[doc(hidden)] +macro_rules! level_enabled { + ($lvl:expr) => { + $lvl <= $crate::level_filters::STATIC_MAX_LEVEL + && $lvl <= $crate::level_filters::LevelFilter::current() + }; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! valueset { + + // === base case === + (@ { $(,)* $($val:expr),* $(,)* }, $next:expr $(,)*) => { + &[ $($val),* ] + }; + + // === recursive case (more tts) === + + // TODO(#1138): determine a new syntax for uninitialized span fields, and + // re-enable this. + // (@{ $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = _, $($rest:tt)*) => { + // $crate::valueset!(@ { $($out),*, (&$next, None) }, $next, $($rest)*) + // }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = ?$val:expr, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = %$val:expr, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = $val:expr, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&$val as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&$($k).+ as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, ?$($k:ident).+, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&debug(&$($k).+) as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, %$($k:ident).+, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&display(&$($k).+) as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = ?$val:expr) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + $next, + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = %$val:expr) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + $next, + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = $val:expr) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&$val as &Value)) }, + $next, + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&$($k).+ as &Value)) }, + $next, + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, ?$($k:ident).+) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&debug(&$($k).+) as &Value)) }, + $next, + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, %$($k:ident).+) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&display(&$($k).+) as &Value)) }, + $next, + ) + }; + + // Handle literal names + (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = ?$val:expr, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = %$val:expr, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = $val:expr, $($rest:tt)*) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&$val as &Value)) }, + $next, + $($rest)* + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = ?$val:expr) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + $next, + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = %$val:expr) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + $next, + ) + }; + (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = $val:expr) => { + $crate::valueset!( + @ { $($out),*, (&$next, Some(&$val as &Value)) }, + $next, + ) + }; + + // Remainder is unparseable, but exists --- must be format args! + (@ { $(,)* $($out:expr),* }, $next:expr, $($rest:tt)+) => { + $crate::valueset!(@ { (&$next, Some(&format_args!($($rest)+) as &Value)), $($out),* }, $next, ) + }; + + // === entry === + ($fields:expr, $($kvs:tt)+) => { + { + #[allow(unused_imports)] + use $crate::field::{debug, display, Value}; + let mut iter = $fields.iter(); + $fields.value_set($crate::valueset!( + @ { }, + iter.next().expect("FieldSet corrupted (this is a bug)"), + $($kvs)+ + )) + } + }; + ($fields:expr,) => { + { + $fields.value_set(&[]) + } + }; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! fieldset { + // == base case == + (@ { $(,)* $($out:expr),* $(,)* } $(,)*) => { + &[ $($out),* ] + }; + + // == recursive cases (more tts) == + (@ { $(,)* $($out:expr),* } $($k:ident).+ = ?$val:expr, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $crate::__tracing_stringify!($($k).+) } $($rest)*) + }; + (@ { $(,)* $($out:expr),* } $($k:ident).+ = %$val:expr, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $crate::__tracing_stringify!($($k).+) } $($rest)*) + }; + (@ { $(,)* $($out:expr),* } $($k:ident).+ = $val:expr, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $crate::__tracing_stringify!($($k).+) } $($rest)*) + }; + // TODO(#1138): determine a new syntax for uninitialized span fields, and + // re-enable this. + // (@ { $($out:expr),* } $($k:ident).+ = _, $($rest:tt)*) => { + // $crate::fieldset!(@ { $($out),*, $crate::__tracing_stringify!($($k).+) } $($rest)*) + // }; + (@ { $(,)* $($out:expr),* } ?$($k:ident).+, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $crate::__tracing_stringify!($($k).+) } $($rest)*) + }; + (@ { $(,)* $($out:expr),* } %$($k:ident).+, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $crate::__tracing_stringify!($($k).+) } $($rest)*) + }; + (@ { $(,)* $($out:expr),* } $($k:ident).+, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $crate::__tracing_stringify!($($k).+) } $($rest)*) + }; + + // Handle literal names + (@ { $(,)* $($out:expr),* } $k:literal = ?$val:expr, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $k } $($rest)*) + }; + (@ { $(,)* $($out:expr),* } $k:literal = %$val:expr, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $k } $($rest)*) + }; + (@ { $(,)* $($out:expr),* } $k:literal = $val:expr, $($rest:tt)*) => { + $crate::fieldset!(@ { $($out),*, $k } $($rest)*) + }; + + // Remainder is unparseable, but exists --- must be format args! + (@ { $(,)* $($out:expr),* } $($rest:tt)+) => { + $crate::fieldset!(@ { "message", $($out),*, }) + }; + + // == entry == + ($($args:tt)*) => { + $crate::fieldset!(@ { } $($args)*,) + }; + +} + +#[cfg(feature = "log")] +#[doc(hidden)] +#[macro_export] +macro_rules! level_to_log { + ($level:expr) => { + match $level { + $crate::Level::ERROR => $crate::log::Level::Error, + $crate::Level::WARN => $crate::log::Level::Warn, + $crate::Level::INFO => $crate::log::Level::Info, + $crate::Level::DEBUG => $crate::log::Level::Debug, + _ => $crate::log::Level::Trace, + } + }; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! __tracing_stringify { + ($s:expr) => { + stringify!($s) + }; +} + +#[cfg(not(feature = "log"))] +#[doc(hidden)] +#[macro_export] +macro_rules! __tracing_log { + ($level:expr, $callsite:expr, $value_set:expr) => {}; +} + +#[cfg(feature = "log")] +#[doc(hidden)] +#[macro_export] +macro_rules! __tracing_log { + ($level:expr, $callsite:expr, $value_set:expr) => { + $crate::if_log_enabled! { $level, { + use $crate::log; + let level = $crate::level_to_log!($level); + if level <= log::max_level() { + let meta = $callsite.metadata(); + let log_meta = log::Metadata::builder() + .level(level) + .target(meta.target()) + .build(); + let logger = log::logger(); + if logger.enabled(&log_meta) { + $crate::__macro_support::__tracing_log(meta, logger, log_meta, $value_set) + } + } + }} + }; +} + +#[cfg(not(feature = "log"))] +#[doc(hidden)] +#[macro_export] +macro_rules! if_log_enabled { + ($lvl:expr, $e:expr;) => { + $crate::if_log_enabled! { $lvl, $e } + }; + ($lvl:expr, $if_log:block) => { + $crate::if_log_enabled! { $lvl, $if_log else {} } + }; + ($lvl:expr, $if_log:block else $else_block:block) => { + $else_block + }; +} + +#[cfg(all(feature = "log", not(feature = "log-always")))] +#[doc(hidden)] +#[macro_export] +macro_rules! if_log_enabled { + ($lvl:expr, $e:expr;) => { + $crate::if_log_enabled! { $lvl, $e } + }; + ($lvl:expr, $if_log:block) => { + $crate::if_log_enabled! { $lvl, $if_log else {} } + }; + ($lvl:expr, $if_log:block else $else_block:block) => { + if $crate::level_to_log!($lvl) <= $crate::log::STATIC_MAX_LEVEL { + if !$crate::dispatcher::has_been_set() { + $if_log + } else { + $else_block + } + } else { + $else_block + } + }; +} + +#[cfg(all(feature = "log", feature = "log-always"))] +#[doc(hidden)] +#[macro_export] +macro_rules! if_log_enabled { + ($lvl:expr, $e:expr;) => { + $crate::if_log_enabled! { $lvl, $e } + }; + ($lvl:expr, $if_log:block) => { + $crate::if_log_enabled! { $lvl, $if_log else {} } + }; + ($lvl:expr, $if_log:block else $else_block:block) => { + if $crate::level_to_log!($lvl) <= $crate::log::STATIC_MAX_LEVEL { + #[allow(unused_braces)] + $if_log + } else { + $else_block + } + }; +} diff --git a/vendor/tracing/src/span.rs b/vendor/tracing/src/span.rs new file mode 100644 index 000000000..21358eb27 --- /dev/null +++ b/vendor/tracing/src/span.rs @@ -0,0 +1,1617 @@ +//! Spans represent periods of time in which a program was executing in a +//! particular context. +//! +//! A span consists of [fields], user-defined key-value pairs of arbitrary data +//! that describe the context the span represents, and a set of fixed attributes +//! that describe all `tracing` spans and events. Attributes describing spans +//! include: +//! +//! - An [`Id`] assigned by the subscriber that uniquely identifies it in relation +//! to other spans. +//! - The span's [parent] in the trace tree. +//! - [Metadata] that describes static characteristics of all spans +//! originating from that callsite, such as its name, source code location, +//! [verbosity level], and the names of its fields. +//! +//! # Creating Spans +//! +//! Spans are created using the [`span!`] macro. This macro is invoked with the +//! following arguments, in order: +//! +//! - The [`target`] and/or [`parent`][parent] attributes, if the user wishes to +//! override their default values. +//! - The span's [verbosity level] +//! - A string literal providing the span's name. +//! - Finally, between zero and 32 arbitrary key/value fields. +//! +//! [`target`]: super::Metadata::target +//! +//! For example: +//! ```rust +//! use tracing::{span, Level}; +//! +//! /// Construct a new span at the `INFO` level named "my_span", with a single +//! /// field named answer , with the value `42`. +//! let my_span = span!(Level::INFO, "my_span", answer = 42); +//! ``` +//! +//! The documentation for the [`span!`] macro provides additional examples of +//! the various options that exist when creating spans. +//! +//! The [`trace_span!`], [`debug_span!`], [`info_span!`], [`warn_span!`], and +//! [`error_span!`] exist as shorthand for constructing spans at various +//! verbosity levels. +//! +//! ## Recording Span Creation +//! +//! The [`Attributes`] type contains data associated with a span, and is +//! provided to the [`Subscriber`] when a new span is created. It contains +//! the span's metadata, the ID of [the span's parent][parent] if one was +//! explicitly set, and any fields whose values were recorded when the span was +//! constructed. The subscriber, which is responsible for recording `tracing` +//! data, can then store or record these values. +//! +//! # The Span Lifecycle +//! +//! ## Entering a Span +//! +//! A thread of execution is said to _enter_ a span when it begins executing, +//! and _exit_ the span when it switches to another context. Spans may be +//! entered through the [`enter`], [`entered`], and [`in_scope`] methods. +//! +//! The [`enter`] method enters a span, returning a [guard] that exits the span +//! when dropped +//! ``` +//! # use tracing::{span, Level}; +//! let my_var: u64 = 5; +//! let my_span = span!(Level::TRACE, "my_span", my_var); +//! +//! // `my_span` exists but has not been entered. +//! +//! // Enter `my_span`... +//! let _enter = my_span.enter(); +//! +//! // Perform some work inside of the context of `my_span`... +//! // Dropping the `_enter` guard will exit the span. +//!``` +//! +//! <div class="example-wrap" style="display:inline-block"><pre class="compile_fail" style="white-space:normal;font:inherit;"> +//! <strong>Warning</strong>: In asynchronous code that uses async/await syntax, +//! <code>Span::enter</code> may produce incorrect traces if the returned drop +//! guard is held across an await point. See +//! <a href="struct.Span.html#in-asynchronous-code">the method documentation</a> +//! for details. +//! </pre></div> +//! +//! The [`entered`] method is analogous to [`enter`], but moves the span into +//! the returned guard, rather than borrowing it. This allows creating and +//! entering a span in a single expression: +//! +//! ``` +//! # use tracing::{span, Level}; +//! // Create a span and enter it, returning a guard: +//! let span = span!(Level::INFO, "my_span").entered(); +//! +//! // We are now inside the span! Like `enter()`, the guard returned by +//! // `entered()` will exit the span when it is dropped... +//! +//! // ...but, it can also be exited explicitly, returning the `Span` +//! // struct: +//! let span = span.exit(); +//! ``` +//! +//! Finally, [`in_scope`] takes a closure or function pointer and executes it +//! inside the span: +//! +//! ``` +//! # use tracing::{span, Level}; +//! let my_var: u64 = 5; +//! let my_span = span!(Level::TRACE, "my_span", my_var = &my_var); +//! +//! my_span.in_scope(|| { +//! // perform some work in the context of `my_span`... +//! }); +//! +//! // Perform some work outside of the context of `my_span`... +//! +//! my_span.in_scope(|| { +//! // Perform some more work in the context of `my_span`. +//! }); +//! ``` +//! +//! <pre class="ignore" style="white-space:normal;font:inherit;"> +//! <strong>Note</strong>: Since entering a span takes <code>&self</code>, and +//! <code>Span</code>s are <code>Clone</code>, <code>Send</code>, and +//! <code>Sync</code>, it is entirely valid for multiple threads to enter the +//! same span concurrently. +//! </pre> +//! +//! ## Span Relationships +//! +//! Spans form a tree structure — unless it is a root span, all spans have a +//! _parent_, and may have one or more _children_. When a new span is created, +//! the current span becomes the new span's parent. The total execution time of +//! a span consists of the time spent in that span and in the entire subtree +//! represented by its children. Thus, a parent span always lasts for at least +//! as long as the longest-executing span in its subtree. +//! +//! ``` +//! # use tracing::{Level, span}; +//! // this span is considered the "root" of a new trace tree: +//! span!(Level::INFO, "root").in_scope(|| { +//! // since we are now inside "root", this span is considered a child +//! // of "root": +//! span!(Level::DEBUG, "outer_child").in_scope(|| { +//! // this span is a child of "outer_child", which is in turn a +//! // child of "root": +//! span!(Level::TRACE, "inner_child").in_scope(|| { +//! // and so on... +//! }); +//! }); +//! // another span created here would also be a child of "root". +//! }); +//!``` +//! +//! In addition, the parent of a span may be explicitly specified in +//! the `span!` macro. For example: +//! +//! ```rust +//! # use tracing::{Level, span}; +//! // Create, but do not enter, a span called "foo". +//! let foo = span!(Level::INFO, "foo"); +//! +//! // Create and enter a span called "bar". +//! let bar = span!(Level::INFO, "bar"); +//! let _enter = bar.enter(); +//! +//! // Although we have currently entered "bar", "baz"'s parent span +//! // will be "foo". +//! let baz = span!(parent: &foo, Level::INFO, "baz"); +//! ``` +//! +//! A child span should typically be considered _part_ of its parent. For +//! example, if a subscriber is recording the length of time spent in various +//! spans, it should generally include the time spent in a span's children as +//! part of that span's duration. +//! +//! In addition to having zero or one parent, a span may also _follow from_ any +//! number of other spans. This indicates a causal relationship between the span +//! and the spans that it follows from, but a follower is *not* typically +//! considered part of the duration of the span it follows. Unlike the parent, a +//! span may record that it follows from another span after it is created, using +//! the [`follows_from`] method. +//! +//! As an example, consider a listener task in a server. As the listener accepts +//! incoming connections, it spawns new tasks that handle those connections. We +//! might want to have a span representing the listener, and instrument each +//! spawned handler task with its own span. We would want our instrumentation to +//! record that the handler tasks were spawned as a result of the listener task. +//! However, we might not consider the handler tasks to be _part_ of the time +//! spent in the listener task, so we would not consider those spans children of +//! the listener span. Instead, we would record that the handler tasks follow +//! from the listener, recording the causal relationship but treating the spans +//! as separate durations. +//! +//! ## Closing Spans +//! +//! Execution may enter and exit a span multiple times before that span is +//! _closed_. Consider, for example, a future which has an associated +//! span and enters that span every time it is polled: +//! ```rust +//! # use std::future::Future; +//! # use std::task::{Context, Poll}; +//! # use std::pin::Pin; +//! struct MyFuture { +//! // data +//! span: tracing::Span, +//! } +//! +//! impl Future for MyFuture { +//! type Output = (); +//! +//! fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { +//! let _enter = self.span.enter(); +//! // Do actual future work... +//! # Poll::Ready(()) +//! } +//! } +//! ``` +//! +//! If this future was spawned on an executor, it might yield one or more times +//! before `poll` returns [`Poll::Ready`]. If the future were to yield, then +//! the executor would move on to poll the next future, which may _also_ enter +//! an associated span or series of spans. Therefore, it is valid for a span to +//! be entered repeatedly before it completes. Only the time when that span or +//! one of its children was the current span is considered to be time spent in +//! that span. A span which is not executing and has not yet been closed is said +//! to be _idle_. +//! +//! Because spans may be entered and exited multiple times before they close, +//! [`Subscriber`]s have separate trait methods which are called to notify them +//! of span exits and when span handles are dropped. When execution exits a +//! span, [`exit`] will always be called with that span's ID to notify the +//! subscriber that the span has been exited. When span handles are dropped, the +//! [`drop_span`] method is called with that span's ID. The subscriber may use +//! this to determine whether or not the span will be entered again. +//! +//! If there is only a single handle with the capacity to exit a span, dropping +//! that handle "closes" the span, since the capacity to enter it no longer +//! exists. For example: +//! ``` +//! # use tracing::{Level, span}; +//! { +//! span!(Level::TRACE, "my_span").in_scope(|| { +//! // perform some work in the context of `my_span`... +//! }); // --> Subscriber::exit(my_span) +//! +//! // The handle to `my_span` only lives inside of this block; when it is +//! // dropped, the subscriber will be informed via `drop_span`. +//! +//! } // --> Subscriber::drop_span(my_span) +//! ``` +//! +//! However, if multiple handles exist, the span can still be re-entered even if +//! one or more is dropped. For determining when _all_ handles to a span have +//! been dropped, `Subscriber`s have a [`clone_span`] method, which is called +//! every time a span handle is cloned. Combined with `drop_span`, this may be +//! used to track the number of handles to a given span — if `drop_span` has +//! been called one more time than the number of calls to `clone_span` for a +//! given ID, then no more handles to the span with that ID exist. The +//! subscriber may then treat it as closed. +//! +//! # When to use spans +//! +//! As a rule of thumb, spans should be used to represent discrete units of work +//! (e.g., a given request's lifetime in a server) or periods of time spent in a +//! given context (e.g., time spent interacting with an instance of an external +//! system, such as a database). +//! +//! Which scopes in a program correspond to new spans depend somewhat on user +//! intent. For example, consider the case of a loop in a program. Should we +//! construct one span and perform the entire loop inside of that span, like: +//! +//! ```rust +//! # use tracing::{Level, span}; +//! # let n = 1; +//! let span = span!(Level::TRACE, "my_loop"); +//! let _enter = span.enter(); +//! for i in 0..n { +//! # let _ = i; +//! // ... +//! } +//! ``` +//! Or, should we create a new span for each iteration of the loop, as in: +//! ```rust +//! # use tracing::{Level, span}; +//! # let n = 1u64; +//! for i in 0..n { +//! let span = span!(Level::TRACE, "my_loop", iteration = i); +//! let _enter = span.enter(); +//! // ... +//! } +//! ``` +//! +//! Depending on the circumstances, we might want to do either, or both. For +//! example, if we want to know how long was spent in the loop overall, we would +//! create a single span around the entire loop; whereas if we wanted to know how +//! much time was spent in each individual iteration, we would enter a new span +//! on every iteration. +//! +//! [fields]: super::field +//! [Metadata]: super::Metadata +//! [verbosity level]: super::Level +//! [`Poll::Ready`]: std::task::Poll::Ready +//! [`span!`]: super::span! +//! [`trace_span!`]: super::trace_span! +//! [`debug_span!`]: super::debug_span! +//! [`info_span!`]: super::info_span! +//! [`warn_span!`]: super::warn_span! +//! [`error_span!`]: super::error_span! +//! [`clone_span`]: super::subscriber::Subscriber::clone_span() +//! [`drop_span`]: super::subscriber::Subscriber::drop_span() +//! [`exit`]: super::subscriber::Subscriber::exit +//! [`Subscriber`]: super::subscriber::Subscriber +//! [`enter`]: Span::enter() +//! [`entered`]: Span::entered() +//! [`in_scope`]: Span::in_scope() +//! [`follows_from`]: Span::follows_from() +//! [guard]: Entered +//! [parent]: #span-relationships +pub use tracing_core::span::{Attributes, Id, Record}; + +use crate::stdlib::{ + cmp, fmt, + hash::{Hash, Hasher}, + marker::PhantomData, + mem, + ops::Deref, +}; +use crate::{ + dispatcher::{self, Dispatch}, + field, Metadata, +}; + +/// Trait implemented by types which have a span `Id`. +pub trait AsId: crate::sealed::Sealed { + /// Returns the `Id` of the span that `self` corresponds to, or `None` if + /// this corresponds to a disabled span. + fn as_id(&self) -> Option<&Id>; +} + +/// A handle representing a span, with the capability to enter the span if it +/// exists. +/// +/// If the span was rejected by the current `Subscriber`'s filter, entering the +/// span will silently do nothing. Thus, the handle can be used in the same +/// manner regardless of whether or not the trace is currently being collected. +#[derive(Clone)] +pub struct Span { + /// A handle used to enter the span when it is not executing. + /// + /// If this is `None`, then the span has either closed or was never enabled. + inner: Option<Inner>, + /// Metadata describing the span. + /// + /// This might be `Some` even if `inner` is `None`, in the case that the + /// span is disabled but the metadata is needed for `log` support. + meta: Option<&'static Metadata<'static>>, +} + +/// A handle representing the capacity to enter a span which is known to exist. +/// +/// Unlike `Span`, this type is only constructed for spans which _have_ been +/// enabled by the current filter. This type is primarily used for implementing +/// span handles; users should typically not need to interact with it directly. +#[derive(Debug)] +pub(crate) struct Inner { + /// The span's ID, as provided by `subscriber`. + id: Id, + + /// The subscriber that will receive events relating to this span. + /// + /// This should be the same subscriber that provided this span with its + /// `id`. + subscriber: Dispatch, +} + +/// A guard representing a span which has been entered and is currently +/// executing. +/// +/// When the guard is dropped, the span will be exited. +/// +/// This is returned by the [`Span::enter`] function. +/// +/// [`Span::enter`]: super::Span::enter +#[derive(Debug)] +#[must_use = "once a span has been entered, it should be exited"] +pub struct Entered<'a> { + span: &'a Span, +} + +/// An owned version of [`Entered`], a guard representing a span which has been +/// entered and is currently executing. +/// +/// When the guard is dropped, the span will be exited. +/// +/// This is returned by the [`Span::entered`] function. +/// +/// [`Span::entered`]: super::Span::entered() +#[derive(Debug)] +#[must_use = "once a span has been entered, it should be exited"] +pub struct EnteredSpan { + span: Span, + + /// ```compile_fail + /// use tracing::span::*; + /// trait AssertSend: Send {} + /// + /// impl AssertSend for EnteredSpan {} + /// ``` + _not_send: PhantomNotSend, +} + +/// `log` target for all span lifecycle (creation/enter/exit/close) records. +#[cfg(feature = "log")] +const LIFECYCLE_LOG_TARGET: &str = "tracing::span"; +/// `log` target for span activity (enter/exit) records. +#[cfg(feature = "log")] +const ACTIVITY_LOG_TARGET: &str = "tracing::span::active"; + +// ===== impl Span ===== + +impl Span { + /// Constructs a new `Span` with the given [metadata] and set of + /// [field values]. + /// + /// The new span will be constructed by the currently-active [`Subscriber`], + /// with the current span as its parent (if one exists). + /// + /// After the span is constructed, [field values] and/or [`follows_from`] + /// annotations may be added to it. + /// + /// [metadata]: super::Metadata + /// [`Subscriber`]: super::subscriber::Subscriber + /// [field values]: super::field::ValueSet + /// [`follows_from`]: super::Span::follows_from + pub fn new(meta: &'static Metadata<'static>, values: &field::ValueSet<'_>) -> Span { + dispatcher::get_default(|dispatch| Self::new_with(meta, values, dispatch)) + } + + #[inline] + #[doc(hidden)] + pub fn new_with( + meta: &'static Metadata<'static>, + values: &field::ValueSet<'_>, + dispatch: &Dispatch, + ) -> Span { + let new_span = Attributes::new(meta, values); + Self::make_with(meta, new_span, dispatch) + } + + /// Constructs a new `Span` as the root of its own trace tree, with the + /// given [metadata] and set of [field values]. + /// + /// After the span is constructed, [field values] and/or [`follows_from`] + /// annotations may be added to it. + /// + /// [metadata]: super::Metadata + /// [field values]: super::field::ValueSet + /// [`follows_from`]: super::Span::follows_from + pub fn new_root(meta: &'static Metadata<'static>, values: &field::ValueSet<'_>) -> Span { + dispatcher::get_default(|dispatch| Self::new_root_with(meta, values, dispatch)) + } + + #[inline] + #[doc(hidden)] + pub fn new_root_with( + meta: &'static Metadata<'static>, + values: &field::ValueSet<'_>, + dispatch: &Dispatch, + ) -> Span { + let new_span = Attributes::new_root(meta, values); + Self::make_with(meta, new_span, dispatch) + } + + /// Constructs a new `Span` as child of the given parent span, with the + /// given [metadata] and set of [field values]. + /// + /// After the span is constructed, [field values] and/or [`follows_from`] + /// annotations may be added to it. + /// + /// [metadata]: super::Metadata + /// [field values]: super::field::ValueSet + /// [`follows_from`]: super::Span::follows_from + pub fn child_of( + parent: impl Into<Option<Id>>, + meta: &'static Metadata<'static>, + values: &field::ValueSet<'_>, + ) -> Span { + let mut parent = parent.into(); + dispatcher::get_default(move |dispatch| { + Self::child_of_with(Option::take(&mut parent), meta, values, dispatch) + }) + } + + #[inline] + #[doc(hidden)] + pub fn child_of_with( + parent: impl Into<Option<Id>>, + meta: &'static Metadata<'static>, + values: &field::ValueSet<'_>, + dispatch: &Dispatch, + ) -> Span { + let new_span = match parent.into() { + Some(parent) => Attributes::child_of(parent, meta, values), + None => Attributes::new_root(meta, values), + }; + Self::make_with(meta, new_span, dispatch) + } + + /// Constructs a new disabled span with the given `Metadata`. + /// + /// This should be used when a span is constructed from a known callsite, + /// but the subscriber indicates that it is disabled. + /// + /// Entering, exiting, and recording values on this span will not notify the + /// `Subscriber` but _may_ record log messages if the `log` feature flag is + /// enabled. + #[inline(always)] + pub fn new_disabled(meta: &'static Metadata<'static>) -> Span { + Self { + inner: None, + meta: Some(meta), + } + } + + /// Constructs a new span that is *completely disabled*. + /// + /// This can be used rather than `Option<Span>` to represent cases where a + /// span is not present. + /// + /// Entering, exiting, and recording values on this span will do nothing. + #[inline(always)] + pub const fn none() -> Span { + Self { + inner: None, + meta: None, + } + } + + /// Returns a handle to the span [considered by the `Subscriber`] to be the + /// current span. + /// + /// If the subscriber indicates that it does not track the current span, or + /// that the thread from which this function is called is not currently + /// inside a span, the returned span will be disabled. + /// + /// [considered by the `Subscriber`]: + /// super::subscriber::Subscriber::current_span + pub fn current() -> Span { + dispatcher::get_default(|dispatch| { + if let Some((id, meta)) = dispatch.current_span().into_inner() { + let id = dispatch.clone_span(&id); + Self { + inner: Some(Inner::new(id, dispatch)), + meta: Some(meta), + } + } else { + Self::none() + } + }) + } + + fn make_with( + meta: &'static Metadata<'static>, + new_span: Attributes<'_>, + dispatch: &Dispatch, + ) -> Span { + let attrs = &new_span; + let id = dispatch.new_span(attrs); + let inner = Some(Inner::new(id, dispatch)); + + let span = Self { + inner, + meta: Some(meta), + }; + + if_log_enabled! { *meta.level(), { + let target = if attrs.is_empty() { + LIFECYCLE_LOG_TARGET + } else { + meta.target() + }; + let values = attrs.values(); + span.log( + target, + level_to_log!(*meta.level()), + format_args!("++ {};{}", meta.name(), crate::log::LogValueSet { values, is_first: false }), + ); + }} + + span + } + + /// Enters this span, returning a guard that will exit the span when dropped. + /// + /// If this span is enabled by the current subscriber, then this function will + /// call [`Subscriber::enter`] with the span's [`Id`], and dropping the guard + /// will call [`Subscriber::exit`]. If the span is disabled, this does + /// nothing. + /// + /// # In Asynchronous Code + /// + /// **Warning**: in asynchronous code that uses [async/await syntax][syntax], + /// `Span::enter` should be used very carefully or avoided entirely. Holding + /// the drop guard returned by `Span::enter` across `.await` points will + /// result in incorrect traces. For example, + /// + /// ``` + /// # use tracing::info_span; + /// # async fn some_other_async_function() {} + /// async fn my_async_function() { + /// let span = info_span!("my_async_function"); + /// + /// // WARNING: This span will remain entered until this + /// // guard is dropped... + /// let _enter = span.enter(); + /// // ...but the `await` keyword may yield, causing the + /// // runtime to switch to another task, while remaining in + /// // this span! + /// some_other_async_function().await + /// + /// // ... + /// } + /// ``` + /// + /// The drop guard returned by `Span::enter` exits the span when it is + /// dropped. When an async function or async block yields at an `.await` + /// point, the current scope is _exited_, but values in that scope are + /// **not** dropped (because the async block will eventually resume + /// execution from that await point). This means that _another_ task will + /// begin executing while _remaining_ in the entered span. This results in + /// an incorrect trace. + /// + /// Instead of using `Span::enter` in asynchronous code, prefer the + /// following: + /// + /// * To enter a span for a synchronous section of code within an async + /// block or function, prefer [`Span::in_scope`]. Since `in_scope` takes a + /// synchronous closure and exits the span when the closure returns, the + /// span will always be exited before the next await point. For example: + /// ``` + /// # use tracing::info_span; + /// # async fn some_other_async_function(_: ()) {} + /// async fn my_async_function() { + /// let span = info_span!("my_async_function"); + /// + /// let some_value = span.in_scope(|| { + /// // run some synchronous code inside the span... + /// }); + /// + /// // This is okay! The span has already been exited before we reach + /// // the await point. + /// some_other_async_function(some_value).await; + /// + /// // ... + /// } + /// ``` + /// * For instrumenting asynchronous code, `tracing` provides the + /// [`Future::instrument` combinator][instrument] for + /// attaching a span to a future (async function or block). This will + /// enter the span _every_ time the future is polled, and exit it whenever + /// the future yields. + /// + /// `Instrument` can be used with an async block inside an async function: + /// ```ignore + /// # use tracing::info_span; + /// use tracing::Instrument; + /// + /// # async fn some_other_async_function() {} + /// async fn my_async_function() { + /// let span = info_span!("my_async_function"); + /// async move { + /// // This is correct! If we yield here, the span will be exited, + /// // and re-entered when we resume. + /// some_other_async_function().await; + /// + /// //more asynchronous code inside the span... + /// + /// } + /// // instrument the async block with the span... + /// .instrument(span) + /// // ...and await it. + /// .await + /// } + /// ``` + /// + /// It can also be used to instrument calls to async functions at the + /// callsite: + /// ```ignore + /// # use tracing::debug_span; + /// use tracing::Instrument; + /// + /// # async fn some_other_async_function() {} + /// async fn my_async_function() { + /// let some_value = some_other_async_function() + /// .instrument(debug_span!("some_other_async_function")) + /// .await; + /// + /// // ... + /// } + /// ``` + /// + /// * The [`#[instrument]` attribute macro][attr] can automatically generate + /// correct code when used on an async function: + /// + /// ```ignore + /// # async fn some_other_async_function() {} + /// #[tracing::instrument(level = "info")] + /// async fn my_async_function() { + /// + /// // This is correct! If we yield here, the span will be exited, + /// // and re-entered when we resume. + /// some_other_async_function().await; + /// + /// // ... + /// + /// } + /// ``` + /// + /// [syntax]: https://rust-lang.github.io/async-book/01_getting_started/04_async_await_primer.html + /// [`Span::in_scope`]: Span::in_scope() + /// [instrument]: crate::Instrument + /// [attr]: macro@crate::instrument + /// + /// # Examples + /// + /// ``` + /// # use tracing::{span, Level}; + /// let span = span!(Level::INFO, "my_span"); + /// let guard = span.enter(); + /// + /// // code here is within the span + /// + /// drop(guard); + /// + /// // code here is no longer within the span + /// + /// ``` + /// + /// Guards need not be explicitly dropped: + /// + /// ``` + /// # use tracing::trace_span; + /// fn my_function() -> String { + /// // enter a span for the duration of this function. + /// let span = trace_span!("my_function"); + /// let _enter = span.enter(); + /// + /// // anything happening in functions we call is still inside the span... + /// my_other_function(); + /// + /// // returning from the function drops the guard, exiting the span. + /// return "Hello world".to_owned(); + /// } + /// + /// fn my_other_function() { + /// // ... + /// } + /// ``` + /// + /// Sub-scopes may be created to limit the duration for which the span is + /// entered: + /// + /// ``` + /// # use tracing::{info, info_span}; + /// let span = info_span!("my_great_span"); + /// + /// { + /// let _enter = span.enter(); + /// + /// // this event occurs inside the span. + /// info!("i'm in the span!"); + /// + /// // exiting the scope drops the guard, exiting the span. + /// } + /// + /// // this event is not inside the span. + /// info!("i'm outside the span!") + /// ``` + /// + /// [`Subscriber::enter`]: super::subscriber::Subscriber::enter() + /// [`Subscriber::exit`]: super::subscriber::Subscriber::exit() + /// [`Id`]: super::Id + #[inline(always)] + pub fn enter(&self) -> Entered<'_> { + self.do_enter(); + Entered { span: self } + } + + /// Enters this span, consuming it and returning a [guard][`EnteredSpan`] + /// that will exit the span when dropped. + /// + /// <pre class="compile_fail" style="white-space:normal;font:inherit;"> + /// <strong>Warning</strong>: In asynchronous code that uses async/await syntax, + /// <code>Span::entered</code> may produce incorrect traces if the returned drop + /// guard is held across an await point. See <a href="#in-asynchronous-code">the + /// <code>Span::enter</code> documentation</a> for details. + /// </pre> + /// + /// + /// If this span is enabled by the current subscriber, then this function will + /// call [`Subscriber::enter`] with the span's [`Id`], and dropping the guard + /// will call [`Subscriber::exit`]. If the span is disabled, this does + /// nothing. + /// + /// This is similar to the [`Span::enter`] method, except that it moves the + /// span by value into the returned guard, rather than borrowing it. + /// Therefore, this method can be used to create and enter a span in a + /// single expression, without requiring a `let`-binding. For example: + /// + /// ``` + /// # use tracing::info_span; + /// let _span = info_span!("something_interesting").entered(); + /// ``` + /// rather than: + /// ``` + /// # use tracing::info_span; + /// let span = info_span!("something_interesting"); + /// let _e = span.enter(); + /// ``` + /// + /// Furthermore, `entered` may be used when the span must be stored in some + /// other struct or be passed to a function while remaining entered. + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: The returned <a href="../struct.EnteredSpan.html"> + /// <code>EnteredSpan</a></code> guard does not implement <code>Send</code>. + /// Dropping the guard will exit <em>this</em> span, and if the guard is sent + /// to another thread and dropped there, that thread may never have entered + /// this span. Thus, <code>EnteredSpan</code>s should not be sent between threads. + /// </pre> + /// + /// [syntax]: https://rust-lang.github.io/async-book/01_getting_started/04_async_await_primer.html + /// + /// # Examples + /// + /// The returned guard can be [explicitly exited][EnteredSpan::exit], + /// returning the un-entered span: + /// + /// ``` + /// # use tracing::{Level, span}; + /// let span = span!(Level::INFO, "doing_something").entered(); + /// + /// // code here is within the span + /// + /// // explicitly exit the span, returning it + /// let span = span.exit(); + /// + /// // code here is no longer within the span + /// + /// // enter the span again + /// let span = span.entered(); + /// + /// // now we are inside the span once again + /// ``` + /// + /// Guards need not be explicitly dropped: + /// + /// ``` + /// # use tracing::trace_span; + /// fn my_function() -> String { + /// // enter a span for the duration of this function. + /// let span = trace_span!("my_function").entered(); + /// + /// // anything happening in functions we call is still inside the span... + /// my_other_function(); + /// + /// // returning from the function drops the guard, exiting the span. + /// return "Hello world".to_owned(); + /// } + /// + /// fn my_other_function() { + /// // ... + /// } + /// ``` + /// + /// Since the [`EnteredSpan`] guard can dereference to the [`Span`] itself, + /// the span may still be accessed while entered. For example: + /// + /// ```rust + /// # use tracing::info_span; + /// use tracing::field; + /// + /// // create the span with an empty field, and enter it. + /// let span = info_span!("my_span", some_field = field::Empty).entered(); + /// + /// // we can still record a value for the field while the span is entered. + /// span.record("some_field", &"hello world!"); + /// ``` + /// + + /// [`Subscriber::enter`]: super::subscriber::Subscriber::enter() + /// [`Subscriber::exit`]: super::subscriber::Subscriber::exit() + /// [`Id`]: super::Id + #[inline(always)] + pub fn entered(self) -> EnteredSpan { + self.do_enter(); + EnteredSpan { + span: self, + _not_send: PhantomNotSend, + } + } + + /// Returns this span, if it was [enabled] by the current [`Subscriber`], or + /// the [current span] (whose lexical distance may be further than expected), + /// if this span [is disabled]. + /// + /// This method can be useful when propagating spans to spawned threads or + /// [async tasks]. Consider the following: + /// + /// ``` + /// let _parent_span = tracing::info_span!("parent").entered(); + /// + /// // ... + /// + /// let child_span = tracing::debug_span!("child"); + /// + /// std::thread::spawn(move || { + /// let _entered = child_span.entered(); + /// + /// tracing::info!("spawned a thread!"); + /// + /// // ... + /// }); + /// ``` + /// + /// If the current [`Subscriber`] enables the [`DEBUG`] level, then both + /// the "parent" and "child" spans will be enabled. Thus, when the "spawaned + /// a thread!" event occurs, it will be inside of the "child" span. Because + /// "parent" is the parent of "child", the event will _also_ be inside of + /// "parent". + /// + /// However, if the [`Subscriber`] only enables the [`INFO`] level, the "child" + /// span will be disabled. When the thread is spawned, the + /// `child_span.entered()` call will do nothing, since "child" is not + /// enabled. In this case, the "spawned a thread!" event occurs outside of + /// *any* span, since the "child" span was responsible for propagating its + /// parent to the spawned thread. + /// + /// If this is not the desired behavior, `Span::or_current` can be used to + /// ensure that the "parent" span is propagated in both cases, either as a + /// parent of "child" _or_ directly. For example: + /// + /// ``` + /// let _parent_span = tracing::info_span!("parent").entered(); + /// + /// // ... + /// + /// // If DEBUG is enabled, then "child" will be enabled, and `or_current` + /// // returns "child". Otherwise, if DEBUG is not enabled, "child" will be + /// // disabled, and `or_current` returns "parent". + /// let child_span = tracing::debug_span!("child").or_current(); + /// + /// std::thread::spawn(move || { + /// let _entered = child_span.entered(); + /// + /// tracing::info!("spawned a thread!"); + /// + /// // ... + /// }); + /// ``` + /// + /// When spawning [asynchronous tasks][async tasks], `Span::or_current` can + /// be used similarly, in combination with [`instrument`]: + /// + /// ``` + /// use tracing::Instrument; + /// # // lol + /// # mod tokio { + /// # pub(super) fn spawn(_: impl std::future::Future) {} + /// # } + /// + /// let _parent_span = tracing::info_span!("parent").entered(); + /// + /// // ... + /// + /// let child_span = tracing::debug_span!("child"); + /// + /// tokio::spawn( + /// async { + /// tracing::info!("spawned a task!"); + /// + /// // ... + /// + /// }.instrument(child_span.or_current()) + /// ); + /// ``` + /// + /// In general, `or_current` should be preferred over nesting an + /// [`instrument`] call inside of an [`in_current_span`] call, as using + /// `or_current` will be more efficient. + /// + /// ``` + /// use tracing::Instrument; + /// # // lol + /// # mod tokio { + /// # pub(super) fn spawn(_: impl std::future::Future) {} + /// # } + /// async fn my_async_fn() { + /// // ... + /// } + /// + /// let _parent_span = tracing::info_span!("parent").entered(); + /// + /// // Do this: + /// tokio::spawn( + /// my_async_fn().instrument(tracing::debug_span!("child").or_current()) + /// ); + /// + /// // ...rather than this: + /// tokio::spawn( + /// my_async_fn() + /// .instrument(tracing::debug_span!("child")) + /// .in_current_span() + /// ); + /// ``` + /// + /// [enabled]: crate::Subscriber::enabled + /// [`Subscriber`]: crate::Subscriber + /// [current span]: Span::current + /// [is disabled]: Span::is_disabled + /// [`INFO`]: crate::Level::INFO + /// [`DEBUG`]: crate::Level::DEBUG + /// [async tasks]: std::task + /// [`instrument`]: crate::instrument::Instrument::instrument + /// [`in_current_span`]: crate::instrument::Instrument::in_current_span + pub fn or_current(self) -> Self { + if self.is_disabled() { + return Self::current(); + } + self + } + + #[inline(always)] + fn do_enter(&self) { + if let Some(inner) = self.inner.as_ref() { + inner.subscriber.enter(&inner.id); + } + + if_log_enabled! { crate::Level::TRACE, { + if let Some(_meta) = self.meta { + self.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("-> {};", _meta.name())); + } + }} + } + + // Called from [`Entered`] and [`EnteredSpan`] drops. + // + // Running this behaviour on drop rather than with an explicit function + // call means that spans may still be exited when unwinding. + #[inline(always)] + fn do_exit(&self) { + if let Some(inner) = self.inner.as_ref() { + inner.subscriber.exit(&inner.id); + } + + if_log_enabled! { crate::Level::TRACE, { + if let Some(_meta) = self.meta { + self.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("<- {};", _meta.name())); + } + }} + } + + /// Executes the given function in the context of this span. + /// + /// If this span is enabled, then this function enters the span, invokes `f` + /// and then exits the span. If the span is disabled, `f` will still be + /// invoked, but in the context of the currently-executing span (if there is + /// one). + /// + /// Returns the result of evaluating `f`. + /// + /// # Examples + /// + /// ``` + /// # use tracing::{trace, span, Level}; + /// let my_span = span!(Level::TRACE, "my_span"); + /// + /// my_span.in_scope(|| { + /// // this event occurs within the span. + /// trace!("i'm in the span!"); + /// }); + /// + /// // this event occurs outside the span. + /// trace!("i'm not in the span!"); + /// ``` + /// + /// Calling a function and returning the result: + /// ``` + /// # use tracing::{info_span, Level}; + /// fn hello_world() -> String { + /// "Hello world!".to_owned() + /// } + /// + /// let span = info_span!("hello_world"); + /// // the span will be entered for the duration of the call to + /// // `hello_world`. + /// let a_string = span.in_scope(hello_world); + /// + pub fn in_scope<F: FnOnce() -> T, T>(&self, f: F) -> T { + let _enter = self.enter(); + f() + } + + /// Returns a [`Field`][super::field::Field] for the field with the + /// given `name`, if one exists, + pub fn field<Q: ?Sized>(&self, field: &Q) -> Option<field::Field> + where + Q: field::AsField, + { + self.metadata().and_then(|meta| field.as_field(meta)) + } + + /// Returns true if this `Span` has a field for the given + /// [`Field`][super::field::Field] or field name. + #[inline] + pub fn has_field<Q: ?Sized>(&self, field: &Q) -> bool + where + Q: field::AsField, + { + self.field(field).is_some() + } + + /// Records that the field described by `field` has the value `value`. + /// + /// This may be used with [`field::Empty`] to declare fields whose values + /// are not known when the span is created, and record them later: + /// ``` + /// use tracing::{trace_span, field}; + /// + /// // Create a span with two fields: `greeting`, with the value "hello world", and + /// // `parting`, without a value. + /// let span = trace_span!("my_span", greeting = "hello world", parting = field::Empty); + /// + /// // ... + /// + /// // Now, record a value for parting as well. + /// // (note that the field name is passed as a string slice) + /// span.record("parting", &"goodbye world!"); + /// ``` + /// However, it may also be used to record a _new_ value for a field whose + /// value was already recorded: + /// ``` + /// use tracing::info_span; + /// # fn do_something() -> Result<(), ()> { Err(()) } + /// + /// // Initially, let's assume that our attempt to do something is going okay... + /// let span = info_span!("doing_something", is_okay = true); + /// let _e = span.enter(); + /// + /// match do_something() { + /// Ok(something) => { + /// // ... + /// } + /// Err(_) => { + /// // Things are no longer okay! + /// span.record("is_okay", &false); + /// } + /// } + /// ``` + /// + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// <strong>Note</strong>: The fields associated with a span are part + /// of its <a href="../struct.Metadata.html"><code>Metadata</code></a>. + /// The <a href="../struct.Metadata.html"><code>Metadata</code></a> + /// describing a particular span is constructed statically when the span + /// is created and cannot be extended later to add new fields. Therefore, + /// you cannot record a value for a field that was not specified when the + /// span was created: + /// </pre> + /// + /// ``` + /// use tracing::{trace_span, field}; + /// + /// // Create a span with two fields: `greeting`, with the value "hello world", and + /// // `parting`, without a value. + /// let span = trace_span!("my_span", greeting = "hello world", parting = field::Empty); + /// + /// // ... + /// + /// // Now, you try to record a value for a new field, `new_field`, which was not + /// // declared as `Empty` or populated when you created `span`. + /// // You won't get any error, but the assignment will have no effect! + /// span.record("new_field", &"interesting_value_you_really_need"); + /// + /// // Instead, all fields that may be recorded after span creation should be declared up front, + /// // using field::Empty when a value is not known, as we did for `parting`. + /// // This `record` call will indeed replace field::Empty with "you will be remembered". + /// span.record("parting", &"you will be remembered"); + /// ``` + /// + /// [`field::Empty`]: super::field::Empty + /// [`Metadata`]: super::Metadata + pub fn record<Q: ?Sized, V>(&self, field: &Q, value: &V) -> &Self + where + Q: field::AsField, + V: field::Value, + { + if let Some(meta) = self.meta { + if let Some(field) = field.as_field(meta) { + self.record_all( + &meta + .fields() + .value_set(&[(&field, Some(value as &dyn field::Value))]), + ); + } + } + + self + } + + /// Records all the fields in the provided `ValueSet`. + pub fn record_all(&self, values: &field::ValueSet<'_>) -> &Self { + let record = Record::new(values); + if let Some(ref inner) = self.inner { + inner.record(&record); + } + + if let Some(_meta) = self.meta { + if_log_enabled! { *_meta.level(), { + let target = if record.is_empty() { + LIFECYCLE_LOG_TARGET + } else { + _meta.target() + }; + self.log( + target, + level_to_log!(*_meta.level()), + format_args!("{};{}", _meta.name(), crate::log::LogValueSet { values, is_first: false }), + ); + }} + } + + self + } + + /// Returns `true` if this span was disabled by the subscriber and does not + /// exist. + /// + /// See also [`is_none`]. + /// + /// [`is_none`]: Span::is_none() + #[inline] + pub fn is_disabled(&self) -> bool { + self.inner.is_none() + } + + /// Returns `true` if this span was constructed by [`Span::none`] and is + /// empty. + /// + /// If `is_none` returns `true` for a given span, then [`is_disabled`] will + /// also return `true`. However, when a span is disabled by the subscriber + /// rather than constructed by `Span::none`, this method will return + /// `false`, while `is_disabled` will return `true`. + /// + /// [`Span::none`]: Span::none() + /// [`is_disabled`]: Span::is_disabled() + #[inline] + pub fn is_none(&self) -> bool { + self.is_disabled() && self.meta.is_none() + } + + /// Indicates that the span with the given ID has an indirect causal + /// relationship with this span. + /// + /// This relationship differs somewhat from the parent-child relationship: a + /// span may have any number of prior spans, rather than a single one; and + /// spans are not considered to be executing _inside_ of the spans they + /// follow from. This means that a span may close even if subsequent spans + /// that follow from it are still open, and time spent inside of a + /// subsequent span should not be included in the time its precedents were + /// executing. This is used to model causal relationships such as when a + /// single future spawns several related background tasks, et cetera. + /// + /// If this span is disabled, or the resulting follows-from relationship + /// would be invalid, this function will do nothing. + /// + /// # Examples + /// + /// Setting a `follows_from` relationship with a `Span`: + /// ``` + /// # use tracing::{span, Id, Level, Span}; + /// let span1 = span!(Level::INFO, "span_1"); + /// let span2 = span!(Level::DEBUG, "span_2"); + /// span2.follows_from(span1); + /// ``` + /// + /// Setting a `follows_from` relationship with the current span: + /// ``` + /// # use tracing::{span, Id, Level, Span}; + /// let span = span!(Level::INFO, "hello!"); + /// span.follows_from(Span::current()); + /// ``` + /// + /// Setting a `follows_from` relationship with a `Span` reference: + /// ``` + /// # use tracing::{span, Id, Level, Span}; + /// let span = span!(Level::INFO, "hello!"); + /// let curr = Span::current(); + /// span.follows_from(&curr); + /// ``` + /// + /// Setting a `follows_from` relationship with an `Id`: + /// ``` + /// # use tracing::{span, Id, Level, Span}; + /// let span = span!(Level::INFO, "hello!"); + /// let id = span.id(); + /// span.follows_from(id); + /// ``` + pub fn follows_from(&self, from: impl Into<Option<Id>>) -> &Self { + if let Some(ref inner) = self.inner { + if let Some(from) = from.into() { + inner.follows_from(&from); + } + } + self + } + + /// Returns this span's `Id`, if it is enabled. + pub fn id(&self) -> Option<Id> { + self.inner.as_ref().map(Inner::id) + } + + /// Returns this span's `Metadata`, if it is enabled. + pub fn metadata(&self) -> Option<&'static Metadata<'static>> { + self.meta + } + + #[cfg(feature = "log")] + #[inline] + fn log(&self, target: &str, level: log::Level, message: fmt::Arguments<'_>) { + if let Some(meta) = self.meta { + if level_to_log!(*meta.level()) <= log::max_level() { + let logger = log::logger(); + let log_meta = log::Metadata::builder().level(level).target(target).build(); + if logger.enabled(&log_meta) { + if let Some(ref inner) = self.inner { + logger.log( + &log::Record::builder() + .metadata(log_meta) + .module_path(meta.module_path()) + .file(meta.file()) + .line(meta.line()) + .args(format_args!("{} span={}", message, inner.id.into_u64())) + .build(), + ); + } else { + logger.log( + &log::Record::builder() + .metadata(log_meta) + .module_path(meta.module_path()) + .file(meta.file()) + .line(meta.line()) + .args(message) + .build(), + ); + } + } + } + } + } + + /// Invokes a function with a reference to this span's ID and subscriber. + /// + /// if this span is enabled, the provided function is called, and the result is returned. + /// If the span is disabled, the function is not called, and this method returns `None` + /// instead. + pub fn with_subscriber<T>(&self, f: impl FnOnce((&Id, &Dispatch)) -> T) -> Option<T> { + self.inner + .as_ref() + .map(|inner| f((&inner.id, &inner.subscriber))) + } +} + +impl cmp::PartialEq for Span { + fn eq(&self, other: &Self) -> bool { + match (&self.meta, &other.meta) { + (Some(this), Some(that)) => { + this.callsite() == that.callsite() && self.inner == other.inner + } + _ => false, + } + } +} + +impl Hash for Span { + fn hash<H: Hasher>(&self, hasher: &mut H) { + self.inner.hash(hasher); + } +} + +impl fmt::Debug for Span { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut span = f.debug_struct("Span"); + if let Some(meta) = self.meta { + span.field("name", &meta.name()) + .field("level", &meta.level()) + .field("target", &meta.target()); + + if let Some(ref inner) = self.inner { + span.field("id", &inner.id()); + } else { + span.field("disabled", &true); + } + + if let Some(ref path) = meta.module_path() { + span.field("module_path", &path); + } + + if let Some(ref line) = meta.line() { + span.field("line", &line); + } + + if let Some(ref file) = meta.file() { + span.field("file", &file); + } + } else { + span.field("none", &true); + } + + span.finish() + } +} + +impl<'a> From<&'a Span> for Option<&'a Id> { + fn from(span: &'a Span) -> Self { + span.inner.as_ref().map(|inner| &inner.id) + } +} + +impl<'a> From<&'a Span> for Option<Id> { + fn from(span: &'a Span) -> Self { + span.inner.as_ref().map(Inner::id) + } +} + +impl From<Span> for Option<Id> { + fn from(span: Span) -> Self { + span.inner.as_ref().map(Inner::id) + } +} + +impl<'a> From<&'a EnteredSpan> for Option<&'a Id> { + fn from(span: &'a EnteredSpan) -> Self { + span.inner.as_ref().map(|inner| &inner.id) + } +} + +impl<'a> From<&'a EnteredSpan> for Option<Id> { + fn from(span: &'a EnteredSpan) -> Self { + span.inner.as_ref().map(Inner::id) + } +} + +impl Drop for Span { + #[inline(always)] + fn drop(&mut self) { + if let Some(Inner { + ref id, + ref subscriber, + }) = self.inner + { + subscriber.try_close(id.clone()); + } + + if_log_enabled! { crate::Level::TRACE, { + if let Some(meta) = self.meta { + self.log( + LIFECYCLE_LOG_TARGET, + log::Level::Trace, + format_args!("-- {};", meta.name()), + ); + } + }} + } +} + +// ===== impl Inner ===== + +impl Inner { + /// Indicates that the span with the given ID has an indirect causal + /// relationship with this span. + /// + /// This relationship differs somewhat from the parent-child relationship: a + /// span may have any number of prior spans, rather than a single one; and + /// spans are not considered to be executing _inside_ of the spans they + /// follow from. This means that a span may close even if subsequent spans + /// that follow from it are still open, and time spent inside of a + /// subsequent span should not be included in the time its precedents were + /// executing. This is used to model causal relationships such as when a + /// single future spawns several related background tasks, et cetera. + /// + /// If this span is disabled, this function will do nothing. Otherwise, it + /// returns `Ok(())` if the other span was added as a precedent of this + /// span, or an error if this was not possible. + fn follows_from(&self, from: &Id) { + self.subscriber.record_follows_from(&self.id, from) + } + + /// Returns the span's ID. + fn id(&self) -> Id { + self.id.clone() + } + + fn record(&self, values: &Record<'_>) { + self.subscriber.record(&self.id, values) + } + + fn new(id: Id, subscriber: &Dispatch) -> Self { + Inner { + id, + subscriber: subscriber.clone(), + } + } +} + +impl cmp::PartialEq for Inner { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl Hash for Inner { + fn hash<H: Hasher>(&self, state: &mut H) { + self.id.hash(state); + } +} + +impl Clone for Inner { + fn clone(&self) -> Self { + Inner { + id: self.subscriber.clone_span(&self.id), + subscriber: self.subscriber.clone(), + } + } +} + +// ===== impl Entered ===== + +impl EnteredSpan { + /// Returns this span's `Id`, if it is enabled. + pub fn id(&self) -> Option<Id> { + self.inner.as_ref().map(Inner::id) + } + + /// Exits this span, returning the underlying [`Span`]. + #[inline] + pub fn exit(mut self) -> Span { + // One does not simply move out of a struct with `Drop`. + let span = mem::replace(&mut self.span, Span::none()); + span.do_exit(); + span + } +} + +impl Deref for EnteredSpan { + type Target = Span; + + #[inline] + fn deref(&self) -> &Span { + &self.span + } +} + +impl<'a> Drop for Entered<'a> { + #[inline(always)] + fn drop(&mut self) { + self.span.do_exit() + } +} + +impl Drop for EnteredSpan { + #[inline(always)] + fn drop(&mut self) { + self.span.do_exit() + } +} + +/// Technically, `EnteredSpan` _can_ implement both `Send` *and* +/// `Sync` safely. It doesn't, because it has a `PhantomNotSend` field, +/// specifically added in order to make it `!Send`. +/// +/// Sending an `EnteredSpan` guard between threads cannot cause memory unsafety. +/// However, it *would* result in incorrect behavior, so we add a +/// `PhantomNotSend` to prevent it from being sent between threads. This is +/// because it must be *dropped* on the same thread that it was created; +/// otherwise, the span will never be exited on the thread where it was entered, +/// and it will attempt to exit the span on a thread that may never have entered +/// it. However, we still want them to be `Sync` so that a struct holding an +/// `Entered` guard can be `Sync`. +/// +/// Thus, this is totally safe. +#[derive(Debug)] +struct PhantomNotSend { + ghost: PhantomData<*mut ()>, +} + +#[allow(non_upper_case_globals)] +const PhantomNotSend: PhantomNotSend = PhantomNotSend { ghost: PhantomData }; + +/// # Safety +/// +/// Trivially safe, as `PhantomNotSend` doesn't have any API. +unsafe impl Sync for PhantomNotSend {} + +#[cfg(test)] +mod test { + use super::*; + + trait AssertSend: Send {} + impl AssertSend for Span {} + + trait AssertSync: Sync {} + impl AssertSync for Span {} + impl AssertSync for Entered<'_> {} + impl AssertSync for EnteredSpan {} +} diff --git a/vendor/tracing/src/stdlib.rs b/vendor/tracing/src/stdlib.rs new file mode 100644 index 000000000..12b54084d --- /dev/null +++ b/vendor/tracing/src/stdlib.rs @@ -0,0 +1,55 @@ +//! Re-exports either the Rust `std` library or `core` and `alloc` when `std` is +//! disabled. +//! +//! `crate::stdlib::...` should be used rather than `std::` when adding code that +//! will be available with the standard library disabled. +//! +//! Note that this module is called `stdlib` rather than `std`, as Rust 1.34.0 +//! does not permit redefining the name `stdlib` (although this works on the +//! latest stable Rust). +#[cfg(feature = "std")] +pub(crate) use std::*; + +#[cfg(not(feature = "std"))] +pub(crate) use self::no_std::*; + +#[cfg(not(feature = "std"))] +mod no_std { + // We pre-emptively export everything from libcore/liballoc, (even modules + // we aren't using currently) to make adding new code easier. Therefore, + // some of these imports will be unused. + #![allow(unused_imports)] + + pub(crate) use core::{ + any, array, ascii, cell, char, clone, cmp, convert, default, f32, f64, ffi, future, hash, + hint, i128, i16, i8, isize, iter, marker, mem, num, ops, option, pin, ptr, result, task, + time, u128, u16, u32, u8, usize, + }; + + pub(crate) use alloc::{boxed, collections, rc, string, vec}; + + pub(crate) mod borrow { + pub(crate) use alloc::borrow::*; + pub(crate) use core::borrow::*; + } + + pub(crate) mod fmt { + pub(crate) use alloc::fmt::*; + pub(crate) use core::fmt::*; + } + + pub(crate) mod slice { + pub(crate) use alloc::slice::*; + pub(crate) use core::slice::*; + } + + pub(crate) mod str { + pub(crate) use alloc::str::*; + pub(crate) use core::str::*; + } + + pub(crate) mod sync { + pub(crate) use alloc::sync::*; + pub(crate) use core::sync::*; + } +} diff --git a/vendor/tracing/src/subscriber.rs b/vendor/tracing/src/subscriber.rs new file mode 100644 index 000000000..343dc5914 --- /dev/null +++ b/vendor/tracing/src/subscriber.rs @@ -0,0 +1,68 @@ +//! Collects and records trace data. +pub use tracing_core::subscriber::*; + +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub use tracing_core::dispatcher::DefaultGuard; + +/// Sets this subscriber as the default for the duration of a closure. +/// +/// The default subscriber is used when creating a new [`Span`] or +/// [`Event`], _if no span is currently executing_. If a span is currently +/// executing, new spans or events are dispatched to the subscriber that +/// tagged that span, instead. +/// +/// [`Span`]: super::span::Span +/// [`Subscriber`]: super::subscriber::Subscriber +/// [`Event`]: super::event::Event +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +pub fn with_default<T, S>(subscriber: S, f: impl FnOnce() -> T) -> T +where + S: Subscriber + Send + Sync + 'static, +{ + crate::dispatcher::with_default(&crate::Dispatch::new(subscriber), f) +} + +/// Sets this subscriber as the global default for the duration of the entire program. +/// Will be used as a fallback if no thread-local subscriber has been set in a thread (using `with_default`.) +/// +/// Can only be set once; subsequent attempts to set the global default will fail. +/// Returns whether the initialization was successful. +/// +/// Note: Libraries should *NOT* call `set_global_default()`! That will cause conflicts when +/// executables try to set them later. +/// +/// [span]: super::span +/// [`Subscriber`]: super::subscriber::Subscriber +/// [`Event`]: super::event::Event +pub fn set_global_default<S>(subscriber: S) -> Result<(), SetGlobalDefaultError> +where + S: Subscriber + Send + Sync + 'static, +{ + crate::dispatcher::set_global_default(crate::Dispatch::new(subscriber)) +} + +/// Sets the subscriber as the default for the duration of the lifetime of the +/// returned [`DefaultGuard`] +/// +/// The default subscriber is used when creating a new [`Span`] or +/// [`Event`], _if no span is currently executing_. If a span is currently +/// executing, new spans or events are dispatched to the subscriber that +/// tagged that span, instead. +/// +/// [`Span`]: super::span::Span +/// [`Subscriber`]: super::subscriber::Subscriber +/// [`Event`]: super::event::Event +/// [`DefaultGuard`]: super::dispatcher::DefaultGuard +#[cfg(feature = "std")] +#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +#[must_use = "Dropping the guard unregisters the subscriber."] +pub fn set_default<S>(subscriber: S) -> DefaultGuard +where + S: Subscriber + Send + Sync + 'static, +{ + crate::dispatcher::set_default(&crate::Dispatch::new(subscriber)) +} + +pub use tracing_core::dispatcher::SetGlobalDefaultError; diff --git a/vendor/tracing/tests/enabled.rs b/vendor/tracing/tests/enabled.rs new file mode 100644 index 000000000..ea1c69804 --- /dev/null +++ b/vendor/tracing/tests/enabled.rs @@ -0,0 +1,54 @@ +#![cfg(feature = "std")] +use tracing::Level; +use tracing_mock::*; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn level_and_target() { + let subscriber = subscriber::mock() + .with_filter(|meta| { + if meta.target() == "debug_module" { + meta.level() <= &Level::DEBUG + } else { + meta.level() <= &Level::INFO + } + }) + .done() + .run(); + + let _guard = tracing::subscriber::set_default(subscriber); + + assert!(tracing::enabled!(target: "debug_module", Level::DEBUG)); + assert!(tracing::enabled!(Level::ERROR)); + assert!(!tracing::enabled!(Level::DEBUG)); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span_and_event() { + let subscriber = subscriber::mock() + .with_filter(|meta| { + if meta.target() == "debug_module" { + meta.level() <= &Level::DEBUG + } else if meta.is_span() { + meta.level() <= &Level::TRACE + } else if meta.is_event() { + meta.level() <= &Level::DEBUG + } else { + meta.level() <= &Level::INFO + } + }) + .done() + .run(); + + let _guard = tracing::subscriber::set_default(subscriber); + + // Ensure that the `_event` and `_span` alternatives work corretly + assert!(!tracing::event_enabled!(Level::TRACE)); + assert!(tracing::event_enabled!(Level::DEBUG)); + assert!(tracing::span_enabled!(Level::TRACE)); + + // target variants + assert!(tracing::span_enabled!(target: "debug_module", Level::DEBUG)); + assert!(tracing::event_enabled!(target: "debug_module", Level::DEBUG)); +} diff --git a/vendor/tracing/tests/event.rs b/vendor/tracing/tests/event.rs new file mode 100644 index 000000000..ffb63c816 --- /dev/null +++ b/vendor/tracing/tests/event.rs @@ -0,0 +1,476 @@ +// These tests require the thread-local scoped dispatcher, which only works when +// we have a standard library. The behaviour being tested should be the same +// with the standard lib disabled. +// +// The alternative would be for each of these tests to be defined in a separate +// file, which is :( +#![cfg(feature = "std")] + +use tracing::{ + debug, error, + field::{debug, display}, + info, + subscriber::with_default, + trace, warn, Level, +}; +use tracing_mock::*; + +macro_rules! event_without_message { + ($name:ident: $e:expr) => { + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[test] + fn $name() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("answer") + .with_value(&42) + .and( + field::mock("to_question") + .with_value(&"life, the universe, and everything"), + ) + .only(), + ), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + info!( + answer = $e, + to_question = "life, the universe, and everything" + ); + }); + + handle.assert_finished(); + } + }; +} + +event_without_message! {event_without_message: 42} +event_without_message! {wrapping_event_without_message: std::num::Wrapping(42)} +event_without_message! {nonzeroi32_event_without_message: std::num::NonZeroI32::new(42).unwrap()} +// needs API breakage +//event_without_message!{nonzerou128_event_without_message: std::num::NonZeroU128::new(42).unwrap()} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event_with_message() { + let (subscriber, handle) = subscriber::mock() + .event(event::msg(format_args!( + "hello from my event! yak shaved = {:?}", + true + ))) + .done() + .run_with_handle(); + + with_default(subscriber, || { + debug!("hello from my event! yak shaved = {:?}", true); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn message_without_delims() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("answer") + .with_value(&42) + .and(field::mock("question").with_value(&"life, the universe, and everything")) + .and(field::msg(format_args!( + "hello from my event! tricky? {:?}!", + true + ))) + .only(), + ), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let question = "life, the universe, and everything"; + debug!(answer = 42, question, "hello from {where}! tricky? {:?}!", true, where = "my event"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn string_message_without_delims() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("answer") + .with_value(&42) + .and(field::mock("question").with_value(&"life, the universe, and everything")) + .and(field::msg(format_args!("hello from my event"))) + .only(), + ), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let question = "life, the universe, and everything"; + debug!(answer = 42, question, "hello from my event"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn one_with_everything() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock() + .with_fields( + field::mock("message") + .with_value(&tracing::field::debug(format_args!( + "{:#x} make me one with{what:.>20}", + 4_277_009_102u64, + what = "everything" + ))) + .and(field::mock("foo").with_value(&666)) + .and(field::mock("bar").with_value(&false)) + .and(field::mock("like_a_butterfly").with_value(&42.0)) + .only(), + ) + .at_level(Level::ERROR) + .with_target("whatever"), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + tracing::event!( + target: "whatever", + Level::ERROR, + { foo = 666, bar = false, like_a_butterfly = 42.0 }, + "{:#x} make me one with{what:.>20}", 4_277_009_102u64, what = "everything" + ); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn moved_field() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("foo") + .with_value(&display("hello from my event")) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + let from = "my event"; + tracing::event!(Level::INFO, foo = display(format!("hello from {}", from))) + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn dotted_field_name() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("foo.bar") + .with_value(&true) + .and(field::mock("foo.baz").with_value(&false)) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::event!(Level::INFO, foo.bar = true, foo.baz = false); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn borrowed_field() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("foo") + .with_value(&display("hello from my event")) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + let from = "my event"; + let mut message = format!("hello from {}", from); + tracing::event!(Level::INFO, foo = display(&message)); + message.push_str(", which happened!"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +// If emitting log instrumentation, this gets moved anyway, breaking the test. +#[cfg(not(feature = "log"))] +fn move_field_out_of_struct() { + use tracing::field::debug; + + #[derive(Debug)] + struct Position { + x: f32, + y: f32, + } + + let pos = Position { + x: 3.234, + y: -1.223, + }; + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("x") + .with_value(&debug(3.234)) + .and(field::mock("y").with_value(&debug(-1.223))) + .only(), + ), + ) + .event(event::mock().with_fields(field::mock("position").with_value(&debug(&pos)))) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let pos = Position { + x: 3.234, + y: -1.223, + }; + debug!(x = debug(pos.x), y = debug(pos.y)); + debug!(target: "app_events", { position = debug(pos) }, "New position"); + }); + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn display_shorthand() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("my_field") + .with_value(&display("hello world")) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::event!(Level::TRACE, my_field = %"hello world"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug_shorthand() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("my_field") + .with_value(&debug("hello world")) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::event!(Level::TRACE, my_field = ?"hello world"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn both_shorthands() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("display_field") + .with_value(&display("hello world")) + .and(field::mock("debug_field").with_value(&debug("hello world"))) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::event!(Level::TRACE, display_field = %"hello world", debug_field = ?"hello world"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn explicit_child() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .event(event::mock().with_explicit_parent(Some("foo"))) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo"); + tracing::event!(parent: foo.id(), Level::TRACE, "bar"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn explicit_child_at_levels() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .event(event::mock().with_explicit_parent(Some("foo"))) + .event(event::mock().with_explicit_parent(Some("foo"))) + .event(event::mock().with_explicit_parent(Some("foo"))) + .event(event::mock().with_explicit_parent(Some("foo"))) + .event(event::mock().with_explicit_parent(Some("foo"))) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo"); + trace!(parent: foo.id(), "a"); + debug!(parent: foo.id(), "b"); + info!(parent: foo.id(), "c"); + warn!(parent: foo.id(), "d"); + error!(parent: foo.id(), "e"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn option_values() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("some_str") + .with_value(&"yes") + .and(field::mock("some_bool").with_value(&true)) + .and(field::mock("some_u64").with_value(&42_u64)) + .only(), + ), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let some_str = Some("yes"); + let none_str: Option<&'static str> = None; + let some_bool = Some(true); + let none_bool: Option<bool> = None; + let some_u64 = Some(42_u64); + let none_u64: Option<u64> = None; + trace!( + some_str = some_str, + none_str = none_str, + some_bool = some_bool, + none_bool = none_bool, + some_u64 = some_u64, + none_u64 = none_u64 + ); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn option_ref_values() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("some_str") + .with_value(&"yes") + .and(field::mock("some_bool").with_value(&true)) + .and(field::mock("some_u64").with_value(&42_u64)) + .only(), + ), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let some_str = &Some("yes"); + let none_str: &Option<&'static str> = &None; + let some_bool = &Some(true); + let none_bool: &Option<bool> = &None; + let some_u64 = &Some(42_u64); + let none_u64: &Option<u64> = &None; + trace!( + some_str = some_str, + none_str = none_str, + some_bool = some_bool, + none_bool = none_bool, + some_u64 = some_u64, + none_u64 = none_u64 + ); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn option_ref_mut_values() { + let (subscriber, handle) = subscriber::mock() + .event( + event::mock().with_fields( + field::mock("some_str") + .with_value(&"yes") + .and(field::mock("some_bool").with_value(&true)) + .and(field::mock("some_u64").with_value(&42_u64)) + .only(), + ), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let some_str = &mut Some("yes"); + let none_str: &mut Option<&'static str> = &mut None; + let some_bool = &mut Some(true); + let none_bool: &mut Option<bool> = &mut None; + let some_u64 = &mut Some(42_u64); + let none_u64: &mut Option<u64> = &mut None; + trace!( + some_str = some_str, + none_str = none_str, + some_bool = some_bool, + none_bool = none_bool, + some_u64 = some_u64, + none_u64 = none_u64 + ); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing/tests/filter_caching_is_lexically_scoped.rs b/vendor/tracing/tests/filter_caching_is_lexically_scoped.rs new file mode 100644 index 000000000..e291103d7 --- /dev/null +++ b/vendor/tracing/tests/filter_caching_is_lexically_scoped.rs @@ -0,0 +1,65 @@ +// Tests that depend on a count of the number of times their filter is evaluated +// can't exist in the same file with other tests that add subscribers to the +// registry. The registry was changed so that each time a new dispatcher is +// added all filters are re-evaluated. The tests being run only in separate +// threads with shared global state lets them interfere with each other + +#[cfg(not(feature = "std"))] +extern crate std; + +use tracing::{span, Level}; +use tracing_mock::*; + +use std::sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, +}; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn filter_caching_is_lexically_scoped() { + pub fn my_great_function() -> bool { + span!(Level::TRACE, "emily").in_scope(|| true) + } + + pub fn my_other_function() -> bool { + span!(Level::TRACE, "frank").in_scope(|| true) + } + + let count = Arc::new(AtomicUsize::new(0)); + let count2 = count.clone(); + + let subscriber = subscriber::mock() + .with_filter(move |meta| match meta.name() { + "emily" | "frank" => { + count2.fetch_add(1, Ordering::Relaxed); + true + } + _ => false, + }) + .run(); + + // Since this test is in its own file anyway, we can do this. Thus, this + // test will work even with no-std. + tracing::subscriber::set_global_default(subscriber).unwrap(); + + // Call the function once. The filter should be re-evaluated. + assert!(my_great_function()); + assert_eq!(count.load(Ordering::Relaxed), 1); + + // Call the function again. The cached result should be used. + assert!(my_great_function()); + assert_eq!(count.load(Ordering::Relaxed), 1); + + assert!(my_other_function()); + assert_eq!(count.load(Ordering::Relaxed), 2); + + assert!(my_great_function()); + assert_eq!(count.load(Ordering::Relaxed), 2); + + assert!(my_other_function()); + assert_eq!(count.load(Ordering::Relaxed), 2); + + assert!(my_great_function()); + assert_eq!(count.load(Ordering::Relaxed), 2); +} diff --git a/vendor/tracing/tests/filters_are_not_reevaluated_for_the_same_span.rs b/vendor/tracing/tests/filters_are_not_reevaluated_for_the_same_span.rs new file mode 100644 index 000000000..e9b2529b8 --- /dev/null +++ b/vendor/tracing/tests/filters_are_not_reevaluated_for_the_same_span.rs @@ -0,0 +1,70 @@ +// Tests that depend on a count of the number of times their filter is evaluated +// cant exist in the same file with other tests that add subscribers to the +// registry. The registry was changed so that each time a new dispatcher is +// added all filters are re-evaluated. The tests being run only in separate +// threads with shared global state lets them interfere with each other +#[cfg(not(feature = "std"))] +extern crate std; + +use tracing::{span, Level}; +use tracing_mock::*; + +use std::sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, +}; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn filters_are_not_reevaluated_for_the_same_span() { + // Asserts that the `span!` macro caches the result of calling + // `Subscriber::enabled` for each span. + let alice_count = Arc::new(AtomicUsize::new(0)); + let bob_count = Arc::new(AtomicUsize::new(0)); + let alice_count2 = alice_count.clone(); + let bob_count2 = bob_count.clone(); + + let (subscriber, handle) = subscriber::mock() + .with_filter(move |meta| match meta.name() { + "alice" => { + alice_count2.fetch_add(1, Ordering::Relaxed); + false + } + "bob" => { + bob_count2.fetch_add(1, Ordering::Relaxed); + true + } + _ => false, + }) + .run_with_handle(); + + // Since this test is in its own file anyway, we can do this. Thus, this + // test will work even with no-std. + tracing::subscriber::set_global_default(subscriber).unwrap(); + + // Enter "alice" and then "bob". The dispatcher expects to see "bob" but + // not "alice." + let alice = span!(Level::TRACE, "alice"); + let bob = alice.in_scope(|| { + let bob = span!(Level::TRACE, "bob"); + bob.in_scope(|| ()); + bob + }); + + // The filter should have seen each span a single time. + assert_eq!(alice_count.load(Ordering::Relaxed), 1); + assert_eq!(bob_count.load(Ordering::Relaxed), 1); + + alice.in_scope(|| bob.in_scope(|| {})); + + // The subscriber should see "bob" again, but the filter should not have + // been called. + assert_eq!(alice_count.load(Ordering::Relaxed), 1); + assert_eq!(bob_count.load(Ordering::Relaxed), 1); + + bob.in_scope(|| {}); + assert_eq!(alice_count.load(Ordering::Relaxed), 1); + assert_eq!(bob_count.load(Ordering::Relaxed), 1); + + handle.assert_finished(); +} diff --git a/vendor/tracing/tests/filters_are_reevaluated_for_different_call_sites.rs b/vendor/tracing/tests/filters_are_reevaluated_for_different_call_sites.rs new file mode 100644 index 000000000..265d4a886 --- /dev/null +++ b/vendor/tracing/tests/filters_are_reevaluated_for_different_call_sites.rs @@ -0,0 +1,80 @@ +// Tests that depend on a count of the number of times their filter is evaluated +// cant exist in the same file with other tests that add subscribers to the +// registry. The registry was changed so that each time a new dispatcher is +// added all filters are re-evaluated. The tests being run only in separate +// threads with shared global state lets them interfere with each other +#[cfg(not(feature = "std"))] +extern crate std; + +use tracing::{span, Level}; +use tracing_mock::*; + +use std::sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, +}; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn filters_are_reevaluated_for_different_call_sites() { + // Asserts that the `span!` macro caches the result of calling + // `Subscriber::enabled` for each span. + let charlie_count = Arc::new(AtomicUsize::new(0)); + let dave_count = Arc::new(AtomicUsize::new(0)); + let charlie_count2 = charlie_count.clone(); + let dave_count2 = dave_count.clone(); + + let subscriber = subscriber::mock() + .with_filter(move |meta| { + println!("Filter: {:?}", meta.name()); + match meta.name() { + "charlie" => { + charlie_count2.fetch_add(1, Ordering::Relaxed); + false + } + "dave" => { + dave_count2.fetch_add(1, Ordering::Relaxed); + true + } + _ => false, + } + }) + .run(); + + // Since this test is in its own file anyway, we can do this. Thus, this + // test will work even with no-std. + tracing::subscriber::set_global_default(subscriber).unwrap(); + + // Enter "charlie" and then "dave". The dispatcher expects to see "dave" but + // not "charlie." + let charlie = span!(Level::TRACE, "charlie"); + let dave = charlie.in_scope(|| { + let dave = span!(Level::TRACE, "dave"); + dave.in_scope(|| {}); + dave + }); + + // The filter should have seen each span a single time. + assert_eq!(charlie_count.load(Ordering::Relaxed), 1); + assert_eq!(dave_count.load(Ordering::Relaxed), 1); + + charlie.in_scope(|| dave.in_scope(|| {})); + + // The subscriber should see "dave" again, but the filter should not have + // been called. + assert_eq!(charlie_count.load(Ordering::Relaxed), 1); + assert_eq!(dave_count.load(Ordering::Relaxed), 1); + + // A different span with the same name has a different call site, so it + // should cause the filter to be reapplied. + let charlie2 = span!(Level::TRACE, "charlie"); + charlie.in_scope(|| {}); + assert_eq!(charlie_count.load(Ordering::Relaxed), 2); + assert_eq!(dave_count.load(Ordering::Relaxed), 1); + + // But, the filter should not be re-evaluated for the new "charlie" span + // when it is re-entered. + charlie2.in_scope(|| span!(Level::TRACE, "dave").in_scope(|| {})); + assert_eq!(charlie_count.load(Ordering::Relaxed), 2); + assert_eq!(dave_count.load(Ordering::Relaxed), 2); +} diff --git a/vendor/tracing/tests/filters_dont_leak.rs b/vendor/tracing/tests/filters_dont_leak.rs new file mode 100644 index 000000000..2ef1c9c70 --- /dev/null +++ b/vendor/tracing/tests/filters_dont_leak.rs @@ -0,0 +1,81 @@ +#![cfg(feature = "std")] + +use tracing_mock::*; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn spans_dont_leak() { + fn do_span() { + let span = tracing::debug_span!("alice"); + let _e = span.enter(); + } + + let (subscriber, handle) = subscriber::mock() + .named("spans/subscriber1") + .with_filter(|_| false) + .done() + .run_with_handle(); + + let _guard = tracing::subscriber::set_default(subscriber); + + do_span(); + + let alice = span::mock().named("alice"); + let (subscriber2, handle2) = subscriber::mock() + .named("spans/subscriber2") + .with_filter(|_| true) + .new_span(alice.clone()) + .enter(alice.clone()) + .exit(alice.clone()) + .drop_span(alice) + .done() + .run_with_handle(); + + tracing::subscriber::with_default(subscriber2, || { + println!("--- subscriber 2 is default ---"); + do_span() + }); + + println!("--- subscriber 1 is default ---"); + do_span(); + + handle.assert_finished(); + handle2.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn events_dont_leak() { + fn do_event() { + tracing::debug!("alice"); + } + + let (subscriber, handle) = subscriber::mock() + .named("events/subscriber1") + .with_filter(|_| false) + .done() + .run_with_handle(); + + let _guard = tracing::subscriber::set_default(subscriber); + + do_event(); + + let (subscriber2, handle2) = subscriber::mock() + .named("events/subscriber2") + .with_filter(|_| true) + .event(event::mock()) + .done() + .run_with_handle(); + + tracing::subscriber::with_default(subscriber2, || { + println!("--- subscriber 2 is default ---"); + do_event() + }); + + println!("--- subscriber 1 is default ---"); + + do_event(); + + handle.assert_finished(); + handle2.assert_finished(); +} diff --git a/vendor/tracing/tests/future_send.rs b/vendor/tracing/tests/future_send.rs new file mode 100644 index 000000000..5e5f9f18b --- /dev/null +++ b/vendor/tracing/tests/future_send.rs @@ -0,0 +1,22 @@ +// These tests reproduce the following issues: +// - https://github.com/tokio-rs/tracing/issues/1487 +// - https://github.com/tokio-rs/tracing/issues/1793 + +use core::future::{self, Future}; +#[test] +fn async_fn_is_send() { + async fn some_async_fn() { + tracing::info!("{}", future::ready("test").await); + } + + assert_send(some_async_fn()) +} + +#[test] +fn async_block_is_send() { + assert_send(async { + tracing::info!("{}", future::ready("test").await); + }) +} + +fn assert_send<F: Future + Send>(_f: F) {} diff --git a/vendor/tracing/tests/macro_imports.rs b/vendor/tracing/tests/macro_imports.rs new file mode 100644 index 000000000..2d0a9d652 --- /dev/null +++ b/vendor/tracing/tests/macro_imports.rs @@ -0,0 +1,23 @@ +use tracing::Level; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn prefixed_span_macros() { + tracing::span!(Level::DEBUG, "foo"); + tracing::trace_span!("foo"); + tracing::debug_span!("foo"); + tracing::info_span!("foo"); + tracing::warn_span!("foo"); + tracing::error_span!("foo"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn prefixed_event_macros() { + tracing::event!(Level::DEBUG, "foo"); + tracing::trace!("foo"); + tracing::debug!("foo"); + tracing::info!("foo"); + tracing::warn!("foo"); + tracing::error!("foo"); +} diff --git a/vendor/tracing/tests/macros.rs b/vendor/tracing/tests/macros.rs new file mode 100644 index 000000000..a9679a3e9 --- /dev/null +++ b/vendor/tracing/tests/macros.rs @@ -0,0 +1,963 @@ +#![deny(warnings)] +use tracing::{ + callsite, debug, debug_span, enabled, error, error_span, event, event_enabled, info, info_span, + span, span_enabled, trace, trace_span, warn, warn_span, Level, +}; + +// Tests that macros work across various invocation syntax. +// +// These are quite repetitive, and _could_ be generated by a macro. However, +// they're compile-time tests, so I want to get line numbers etc out of +// failures, and producing them with a macro would muddy the waters a bit. + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span() { + span!(target: "foo_events", Level::DEBUG, "foo", bar.baz = ?2, quux = %3, quuux = 4); + span!(target: "foo_events", Level::DEBUG, "foo", bar.baz = 2, quux = 3); + span!(target: "foo_events", Level::DEBUG, "foo", bar.baz = 2, quux = 4,); + span!(target: "foo_events", Level::DEBUG, "foo"); + span!(target: "foo_events", Level::DEBUG, "bar",); + span!(Level::DEBUG, "foo", bar.baz = 2, quux = 3); + span!(Level::DEBUG, "foo", bar.baz = 2, quux = 4,); + span!(Level::DEBUG, "foo", bar.baz = 2, quux = 3); + span!(Level::DEBUG, "foo", bar.baz = 2, quux = 4,); + span!(Level::DEBUG, "foo", bar.baz = ?2); + span!(Level::DEBUG, "foo", bar.baz = %2); + span!(Level::DEBUG, "foo"); + span!(Level::DEBUG, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn trace_span() { + trace_span!(target: "foo_events", "foo", bar.baz = ?2, quux = %3, quuux = 4); + trace_span!(target: "foo_events", "foo", bar.baz = 2, quux = 3); + trace_span!(target: "foo_events", "foo", bar.baz = 2, quux = 4,); + trace_span!(target: "foo_events", "foo"); + trace_span!(target: "foo_events", "bar",); + trace_span!("foo", bar.baz = 2, quux = 3); + trace_span!("foo", bar.baz = 2, quux = 4,); + trace_span!("foo", bar.baz = ?2); + trace_span!("foo", bar.baz = %2); + trace_span!("bar"); + trace_span!("bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug_span() { + debug_span!(target: "foo_events", "foo", bar.baz = ?2, quux = %3, quuux = 4); + debug_span!(target: "foo_events", "foo", bar.baz = 2, quux = 3); + debug_span!(target: "foo_events", "foo", bar.baz = 2, quux = 4,); + debug_span!(target: "foo_events", "foo"); + debug_span!(target: "foo_events", "bar",); + debug_span!("foo", bar.baz = 2, quux = 3); + debug_span!("foo", bar.baz = 2, quux = 4,); + debug_span!("foo", bar.baz = ?2); + debug_span!("foo", bar.baz = %2); + debug_span!("bar"); + debug_span!("bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn info_span() { + info_span!(target: "foo_events", "foo", bar.baz = ?2, quux = %3, quuux = 4); + info_span!(target: "foo_events", "foo", bar.baz = 2, quux = 3); + info_span!(target: "foo_events", "foo", bar.baz = 2, quux = 4,); + info_span!(target: "foo_events", "foo"); + info_span!(target: "foo_events", "bar",); + info_span!("foo", bar.baz = 2, quux = 3); + info_span!("foo", bar.baz = 2, quux = 4,); + info_span!("foo", bar.baz = ?2); + info_span!("foo", bar.baz = %2); + info_span!("bar"); + info_span!("bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn warn_span() { + warn_span!(target: "foo_events", "foo", bar.baz = ?2, quux = %3, quuux = 4); + warn_span!(target: "foo_events", "foo", bar.baz = 2, quux = 3); + warn_span!(target: "foo_events", "foo", bar.baz = 2, quux = 4,); + warn_span!(target: "foo_events", "foo"); + warn_span!(target: "foo_events", "bar",); + warn_span!("foo", bar.baz = 2, quux = 3); + warn_span!("foo", bar.baz = 2, quux = 4,); + warn_span!("foo", bar.baz = ?2); + warn_span!("foo", bar.baz = %2); + warn_span!("bar"); + warn_span!("bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn error_span() { + error_span!(target: "foo_events", "foo", bar.baz = ?2, quux = %3, quuux = 4); + error_span!(target: "foo_events", "foo", bar.baz = 2, quux = 3); + error_span!(target: "foo_events", "foo", bar.baz = 2, quux = 4,); + error_span!(target: "foo_events", "foo"); + error_span!(target: "foo_events", "bar",); + error_span!("foo", bar.baz = 2, quux = 3); + error_span!("foo", bar.baz = 2, quux = 4,); + error_span!("foo", bar.baz = ?2); + error_span!("foo", bar.baz = %2); + error_span!("bar"); + error_span!("bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span_root() { + span!(target: "foo_events", parent: None, Level::TRACE, "foo", bar.baz = 2, quux = 3); + span!(target: "foo_events", parent: None, Level::TRACE, "foo", bar.baz = 2, quux = 3); + span!(target: "foo_events", parent: None, Level::TRACE, "foo", bar.baz = 2, quux = 4,); + span!(target: "foo_events", parent: None, Level::TRACE, "foo"); + span!(target: "foo_events", parent: None, Level::TRACE, "bar",); + span!(parent: None, Level::DEBUG, "foo", bar.baz = 2, quux = 3); + span!(parent: None, Level::DEBUG, "foo", bar.baz = 2, quux = 4,); + span!(parent: None, Level::DEBUG, "foo"); + span!(parent: None, Level::DEBUG, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn trace_span_root() { + trace_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 3); + trace_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 4,); + trace_span!(target: "foo_events", parent: None, "foo"); + trace_span!(target: "foo_events", parent: None, "bar",); + trace_span!(parent: None, "foo", bar.baz = 2, quux = 3); + trace_span!(parent: None, "foo", bar.baz = 2, quux = 4,); + trace_span!(parent: None, "foo"); + trace_span!(parent: None, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug_span_root() { + debug_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 3); + debug_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 4,); + debug_span!(target: "foo_events", parent: None, "foo"); + debug_span!(target: "foo_events", parent: None, "bar",); + debug_span!(parent: None, "foo", bar.baz = 2, quux = 3); + debug_span!(parent: None, "foo", bar.baz = 2, quux = 4,); + debug_span!(parent: None, "foo"); + debug_span!(parent: None, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn info_span_root() { + info_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 3); + info_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 4,); + info_span!(target: "foo_events", parent: None, "foo"); + info_span!(target: "foo_events", parent: None, "bar",); + info_span!(parent: None, "foo", bar.baz = 2, quux = 3); + info_span!(parent: None, "foo", bar.baz = 2, quux = 4,); + info_span!(parent: None, "foo"); + info_span!(parent: None, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn warn_span_root() { + warn_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 3); + warn_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 4,); + warn_span!(target: "foo_events", parent: None, "foo"); + warn_span!(target: "foo_events", parent: None, "bar",); + warn_span!(parent: None, "foo", bar.baz = 2, quux = 3); + warn_span!(parent: None, "foo", bar.baz = 2, quux = 4,); + warn_span!(parent: None, "foo"); + warn_span!(parent: None, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn error_span_root() { + error_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 3); + error_span!(target: "foo_events", parent: None, "foo", bar.baz = 2, quux = 4,); + error_span!(target: "foo_events", parent: None, "foo"); + error_span!(target: "foo_events", parent: None, "bar",); + error_span!(parent: None, "foo", bar.baz = 2, quux = 3); + error_span!(parent: None, "foo", bar.baz = 2, quux = 4,); + error_span!(parent: None, "foo"); + error_span!(parent: None, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + span!(target: "foo_events", parent: &p, Level::TRACE, "foo", bar.baz = 2, quux = 3); + span!(target: "foo_events", parent: &p, Level::TRACE, "foo", bar.baz = 2, quux = 4,); + span!(target: "foo_events", parent: &p, Level::TRACE, "foo"); + span!(target: "foo_events", parent: &p, Level::TRACE, "bar",); + span!(parent: &p, Level::DEBUG, "foo", bar.baz = 2, quux = 3); + span!(parent: &p, Level::DEBUG, "foo", bar.baz = 2, quux = 4,); + span!(parent: &p, Level::DEBUG, "foo"); + span!(parent: &p, Level::DEBUG, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn trace_span_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + trace_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 3); + trace_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 4,); + trace_span!(target: "foo_events", parent: &p, "foo"); + trace_span!(target: "foo_events", parent: &p, "bar",); + + trace_span!(parent: &p, "foo", bar.baz = 2, quux = 3); + trace_span!(parent: &p, "foo", bar.baz = 2, quux = 4,); + + trace_span!(parent: &p, "foo"); + trace_span!(parent: &p, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug_span_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + debug_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 3); + debug_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 4,); + debug_span!(target: "foo_events", parent: &p, "foo"); + debug_span!(target: "foo_events", parent: &p, "bar",); + + debug_span!(parent: &p, "foo", bar.baz = 2, quux = 3); + debug_span!(parent: &p, "foo", bar.baz = 2, quux = 4,); + + debug_span!(parent: &p, "foo"); + debug_span!(parent: &p, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn info_span_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + info_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 3); + info_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 4,); + info_span!(target: "foo_events", parent: &p, "foo"); + info_span!(target: "foo_events", parent: &p, "bar",); + + info_span!(parent: &p, "foo", bar.baz = 2, quux = 3); + info_span!(parent: &p, "foo", bar.baz = 2, quux = 4,); + + info_span!(parent: &p, "foo"); + info_span!(parent: &p, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn warn_span_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + warn_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 3); + warn_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 4,); + warn_span!(target: "foo_events", parent: &p, "foo"); + warn_span!(target: "foo_events", parent: &p, "bar",); + + warn_span!(parent: &p, "foo", bar.baz = 2, quux = 3); + warn_span!(parent: &p, "foo", bar.baz = 2, quux = 4,); + + warn_span!(parent: &p, "foo"); + warn_span!(parent: &p, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn error_span_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + error_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 3); + error_span!(target: "foo_events", parent: &p, "foo", bar.baz = 2, quux = 4,); + error_span!(target: "foo_events", parent: &p, "foo"); + error_span!(target: "foo_events", parent: &p, "bar",); + + error_span!(parent: &p, "foo", bar.baz = 2, quux = 3); + error_span!(parent: &p, "foo", bar.baz = 2, quux = 4,); + + error_span!(parent: &p, "foo"); + error_span!(parent: &p, "bar",); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span_with_non_rust_symbol() { + span!(Level::TRACE, "non-rust", "guid:x-request-id" = ?"abcdef", "more {}", 42); + span!(Level::TRACE, "non-rust", "guid:x-request-id" = %"abcdef", "more {}", 51); + span!( + Level::TRACE, + "non-rust", + "guid:x-request-id" = "abcdef", + "more {}", + 60 + ); + span!(Level::TRACE, "non-rust", "guid:x-request-id" = ?"abcdef"); + span!(Level::TRACE, "non-rust", "guid:x-request-id" = %"abcdef"); + span!(Level::TRACE, "non-rust", "guid:x-request-id" = "abcdef"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event() { + event!(Level::DEBUG, foo = ?3, bar.baz = %2, quux = false); + event!(Level::DEBUG, foo = 3, bar.baz = 2, quux = false); + event!(Level::DEBUG, foo = 3, bar.baz = 3,); + event!(Level::DEBUG, "foo"); + event!(Level::DEBUG, "foo: {}", 3); + event!(Level::INFO, foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42); + event!( + Level::INFO, + foo = 3, + bar.baz = 2, + quux = false, + "hello world {:?}", + 42 + ); + event!(Level::INFO, foo = 3, bar.baz = 3, "hello world {:?}", 42,); + event!(Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux"); + event!(Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + event!(Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + event!(Level::DEBUG, { foo = ?2, bar.baz = %78 }, "quux"); + event!(target: "foo_events", Level::DEBUG, foo = 3, bar.baz = 2, quux = false); + event!(target: "foo_events", Level::DEBUG, foo = 3, bar.baz = 3,); + event!(target: "foo_events", Level::DEBUG, "foo"); + event!(target: "foo_events", Level::DEBUG, "foo: {}", 3); + event!(target: "foo_events", Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux"); + event!(target: "foo_events", Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + event!(target: "foo_events", Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + event!(target: "foo_events", Level::DEBUG, { foo = 2, bar.baz = 78, }, "quux"); + let foo = 1; + event!(Level::DEBUG, ?foo); + event!(Level::DEBUG, %foo); + event!(Level::DEBUG, foo); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn enabled() { + enabled!(Level::DEBUG, foo, bar.baz, quux,); + enabled!(Level::DEBUG, message); + enabled!(Level::INFO, foo, bar.baz, quux, message,); + enabled!(Level::INFO, foo, bar., message,); + enabled!(Level::DEBUG, foo); + + enabled!(Level::DEBUG); + enabled!(target: "rando", Level::DEBUG); + enabled!(target: "rando", Level::DEBUG, field); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span_enabled() { + span_enabled!(Level::DEBUG, foo, bar.baz, quux,); + span_enabled!(Level::DEBUG, message); + span_enabled!(Level::INFO, foo, bar.baz, quux, message,); + span_enabled!(Level::INFO, foo, bar., message,); + span_enabled!(Level::DEBUG, foo); + + span_enabled!(Level::DEBUG); + span_enabled!(target: "rando", Level::DEBUG); + span_enabled!(target: "rando", Level::DEBUG, field); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event_enabled() { + event_enabled!(Level::DEBUG, foo, bar.baz, quux,); + event_enabled!(Level::DEBUG, message); + event_enabled!(Level::INFO, foo, bar.baz, quux, message,); + event_enabled!(Level::INFO, foo, bar., message,); + event_enabled!(Level::DEBUG, foo); + + event_enabled!(Level::DEBUG); + event_enabled!(target: "rando", Level::DEBUG); + event_enabled!(target: "rando", Level::DEBUG, field); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn locals_with_message() { + let data = (42, "forty-two"); + let private_data = "private"; + let error = "a bad error"; + event!(Level::ERROR, %error, "Received error"); + event!( + target: "app_events", + Level::WARN, + private_data, + ?data, + "App warning: {}", + error + ); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn locals_no_message() { + let data = (42, "forty-two"); + let private_data = "private"; + let error = "a bad error"; + event!( + target: "app_events", + Level::WARN, + private_data, + ?data, + ); + event!( + target: "app_events", + Level::WARN, + private_data, + ?data, + error, + ); + event!( + target: "app_events", + Level::WARN, + private_data, + ?data, + error + ); + event!(Level::WARN, private_data, ?data, error,); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn trace() { + trace!(foo = ?3, bar.baz = %2, quux = false); + trace!(foo = 3, bar.baz = 2, quux = false); + trace!(foo = 3, bar.baz = 3,); + trace!("foo"); + trace!("foo: {}", 3); + trace!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42); + trace!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42); + trace!(foo = 3, bar.baz = 3, "hello world {:?}", 42,); + trace!({ foo = 3, bar.baz = 80 }, "quux"); + trace!({ foo = 2, bar.baz = 79 }, "quux {:?}", true); + trace!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + trace!({ foo = 2, bar.baz = 78 }, "quux"); + trace!({ foo = ?2, bar.baz = %78 }, "quux"); + trace!(target: "foo_events", foo = 3, bar.baz = 2, quux = false); + trace!(target: "foo_events", foo = 3, bar.baz = 3,); + trace!(target: "foo_events", "foo"); + trace!(target: "foo_events", "foo: {}", 3); + trace!(target: "foo_events", { foo = 3, bar.baz = 80 }, "quux"); + trace!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}", true); + trace!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + trace!(target: "foo_events", { foo = 2, bar.baz = 78, }, "quux"); + let foo = 1; + trace!(?foo); + trace!(%foo); + trace!(foo); + trace!(target: "foo_events", ?foo); + trace!(target: "foo_events", %foo); + trace!(target: "foo_events", foo); + trace!(target: "foo_events", ?foo, true, "message"); + trace!(target: "foo_events", %foo, true, "message"); + trace!(target: "foo_events", foo, true, "message"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug() { + debug!(foo = ?3, bar.baz = %2, quux = false); + debug!(foo = 3, bar.baz = 2, quux = false); + debug!(foo = 3, bar.baz = 3,); + debug!("foo"); + debug!("foo: {}", 3); + debug!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42); + debug!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42); + debug!(foo = 3, bar.baz = 3, "hello world {:?}", 42,); + debug!({ foo = 3, bar.baz = 80 }, "quux"); + debug!({ foo = 2, bar.baz = 79 }, "quux {:?}", true); + debug!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + debug!({ foo = 2, bar.baz = 78 }, "quux"); + debug!({ foo = ?2, bar.baz = %78 }, "quux"); + debug!(target: "foo_events", foo = 3, bar.baz = 2, quux = false); + debug!(target: "foo_events", foo = 3, bar.baz = 3,); + debug!(target: "foo_events", "foo"); + debug!(target: "foo_events", "foo: {}", 3); + debug!(target: "foo_events", { foo = 3, bar.baz = 80 }, "quux"); + debug!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}", true); + debug!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + debug!(target: "foo_events", { foo = 2, bar.baz = 78, }, "quux"); + let foo = 1; + debug!(?foo); + debug!(%foo); + debug!(foo); + debug!(target: "foo_events", ?foo); + debug!(target: "foo_events", %foo); + debug!(target: "foo_events", foo); + debug!(target: "foo_events", ?foo, true, "message"); + debug!(target: "foo_events", %foo, true, "message"); + debug!(target: "foo_events", foo, true, "message"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn info() { + info!(foo = ?3, bar.baz = %2, quux = false); + info!(foo = 3, bar.baz = 2, quux = false); + info!(foo = 3, bar.baz = 3,); + info!("foo"); + info!("foo: {}", 3); + info!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42); + info!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42); + info!(foo = 3, bar.baz = 3, "hello world {:?}", 42,); + info!({ foo = 3, bar.baz = 80 }, "quux"); + info!({ foo = 2, bar.baz = 79 }, "quux {:?}", true); + info!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + info!({ foo = 2, bar.baz = 78 }, "quux"); + info!({ foo = ?2, bar.baz = %78 }, "quux"); + info!(target: "foo_events", foo = 3, bar.baz = 2, quux = false); + info!(target: "foo_events", foo = 3, bar.baz = 3,); + info!(target: "foo_events", "foo"); + info!(target: "foo_events", "foo: {}", 3); + info!(target: "foo_events", { foo = 3, bar.baz = 80 }, "quux"); + info!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}", true); + info!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + info!(target: "foo_events", { foo = 2, bar.baz = 78, }, "quux"); + let foo = 1; + info!(?foo); + info!(%foo); + info!(foo); + info!(target: "foo_events", ?foo); + info!(target: "foo_events", %foo); + info!(target: "foo_events", foo); + info!(target: "foo_events", ?foo, true, "message"); + info!(target: "foo_events", %foo, true, "message"); + info!(target: "foo_events", foo, true, "message"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn warn() { + warn!(foo = ?3, bar.baz = %2, quux = false); + warn!(foo = 3, bar.baz = 2, quux = false); + warn!(foo = 3, bar.baz = 3,); + warn!("foo"); + warn!("foo: {}", 3); + warn!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42); + warn!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42); + warn!(foo = 3, bar.baz = 3, "hello world {:?}", 42,); + warn!({ foo = 3, bar.baz = 80 }, "quux"); + warn!({ foo = 2, bar.baz = 79 }, "quux {:?}", true); + warn!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + warn!({ foo = 2, bar.baz = 78 }, "quux"); + warn!({ foo = ?2, bar.baz = %78 }, "quux"); + warn!(target: "foo_events", foo = 3, bar.baz = 2, quux = false); + warn!(target: "foo_events", foo = 3, bar.baz = 3,); + warn!(target: "foo_events", "foo"); + warn!(target: "foo_events", "foo: {}", 3); + warn!(target: "foo_events", { foo = 3, bar.baz = 80 }, "quux"); + warn!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}", true); + warn!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + warn!(target: "foo_events", { foo = 2, bar.baz = 78, }, "quux"); + let foo = 1; + warn!(?foo); + warn!(%foo); + warn!(foo); + warn!(target: "foo_events", ?foo); + warn!(target: "foo_events", %foo); + warn!(target: "foo_events", foo); + warn!(target: "foo_events", ?foo, true, "message"); + warn!(target: "foo_events", %foo, true, "message"); + warn!(target: "foo_events", foo, true, "message"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn error() { + error!(foo = ?3, bar.baz = %2, quux = false); + error!(foo = 3, bar.baz = 2, quux = false); + error!(foo = 3, bar.baz = 3,); + error!("foo"); + error!("foo: {}", 3); + error!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42); + error!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42); + error!(foo = 3, bar.baz = 3, "hello world {:?}", 42,); + error!({ foo = 3, bar.baz = 80 }, "quux"); + error!({ foo = 2, bar.baz = 79 }, "quux {:?}", true); + error!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + error!({ foo = 2, bar.baz = 78, }, "quux"); + error!({ foo = ?2, bar.baz = %78 }, "quux"); + error!(target: "foo_events", foo = 3, bar.baz = 2, quux = false); + error!(target: "foo_events", foo = 3, bar.baz = 3,); + error!(target: "foo_events", "foo"); + error!(target: "foo_events", "foo: {}", 3); + error!(target: "foo_events", { foo = 3, bar.baz = 80 }, "quux"); + error!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}", true); + error!(target: "foo_events", { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + error!(target: "foo_events", { foo = 2, bar.baz = 78, }, "quux"); + let foo = 1; + error!(?foo); + error!(%foo); + error!(foo); + error!(target: "foo_events", ?foo); + error!(target: "foo_events", %foo); + error!(target: "foo_events", foo); + error!(target: "foo_events", ?foo, true, "message"); + error!(target: "foo_events", %foo, true, "message"); + error!(target: "foo_events", foo, true, "message"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event_root() { + event!(parent: None, Level::DEBUG, foo = ?3, bar.baz = %2, quux = false); + event!( + parent: None, + Level::DEBUG, + foo = 3, + bar.baz = 2, + quux = false + ); + event!(parent: None, Level::DEBUG, foo = 3, bar.baz = 3,); + event!(parent: None, Level::DEBUG, "foo"); + event!(parent: None, Level::DEBUG, "foo: {}", 3); + event!(parent: None, Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux"); + event!(parent: None, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + event!(parent: None, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + event!(parent: None, Level::DEBUG, { foo = ?2, bar.baz = %78 }, "quux"); + event!(target: "foo_events", parent: None, Level::DEBUG, foo = 3, bar.baz = 2, quux = false); + event!(target: "foo_events", parent: None, Level::DEBUG, foo = 3, bar.baz = 3,); + event!(target: "foo_events", parent: None, Level::DEBUG, "foo"); + event!(target: "foo_events", parent: None, Level::DEBUG, "foo: {}", 3); + event!(target: "foo_events", parent: None, Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux"); + event!(target: "foo_events", parent: None, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + event!(target: "foo_events", parent: None, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + event!(target: "foo_events", parent: None, Level::DEBUG, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn trace_root() { + trace!(parent: None, foo = ?3, bar.baz = %2, quux = false); + trace!(parent: None, foo = 3, bar.baz = 2, quux = false); + trace!(parent: None, foo = 3, bar.baz = 3,); + trace!(parent: None, "foo"); + trace!(parent: None, "foo: {}", 3); + trace!(parent: None, { foo = 3, bar.baz = 80 }, "quux"); + trace!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + trace!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + trace!(parent: None, { foo = 2, bar.baz = 78 }, "quux"); + trace!(parent: None, { foo = ?2, bar.baz = %78 }, "quux"); + trace!(target: "foo_events", parent: None, foo = 3, bar.baz = 2, quux = false); + trace!(target: "foo_events", parent: None, foo = 3, bar.baz = 3,); + trace!(target: "foo_events", parent: None, "foo"); + trace!(target: "foo_events", parent: None, "foo: {}", 3); + trace!(target: "foo_events", parent: None, { foo = 3, bar.baz = 80 }, "quux"); + trace!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + trace!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + trace!(target: "foo_events", parent: None, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug_root() { + debug!(parent: None, foo = ?3, bar.baz = %2, quux = false); + debug!(parent: None, foo = 3, bar.baz = 2, quux = false); + debug!(parent: None, foo = 3, bar.baz = 3,); + debug!(parent: None, "foo"); + debug!(parent: None, "foo: {}", 3); + debug!(parent: None, { foo = 3, bar.baz = 80 }, "quux"); + debug!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + debug!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + debug!(parent: None, { foo = 2, bar.baz = 78 }, "quux"); + debug!(parent: None, { foo = ?2, bar.baz = %78 }, "quux"); + debug!(target: "foo_events", parent: None, foo = 3, bar.baz = 2, quux = false); + debug!(target: "foo_events", parent: None, foo = 3, bar.baz = 3,); + debug!(target: "foo_events", parent: None, "foo"); + debug!(target: "foo_events", parent: None, "foo: {}", 3); + debug!(target: "foo_events", parent: None, { foo = 3, bar.baz = 80 }, "quux"); + debug!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + debug!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + debug!(target: "foo_events", parent: None, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn info_root() { + info!(parent: None, foo = ?3, bar.baz = %2, quux = false); + info!(parent: None, foo = 3, bar.baz = 2, quux = false); + info!(parent: None, foo = 3, bar.baz = 3,); + info!(parent: None, "foo"); + info!(parent: None, "foo: {}", 3); + info!(parent: None, { foo = 3, bar.baz = 80 }, "quux"); + info!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + info!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + info!(parent: None, { foo = 2, bar.baz = 78 }, "quux"); + info!(parent: None, { foo = ?2, bar.baz = %78 }, "quux"); + info!(target: "foo_events", parent: None, foo = 3, bar.baz = 2, quux = false); + info!(target: "foo_events", parent: None, foo = 3, bar.baz = 3,); + info!(target: "foo_events", parent: None, "foo"); + info!(target: "foo_events", parent: None, "foo: {}", 3); + info!(target: "foo_events", parent: None, { foo = 3, bar.baz = 80 }, "quux"); + info!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + info!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + info!(target: "foo_events", parent: None, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn warn_root() { + warn!(parent: None, foo = ?3, bar.baz = %2, quux = false); + warn!(parent: None, foo = 3, bar.baz = 2, quux = false); + warn!(parent: None, foo = 3, bar.baz = 3,); + warn!(parent: None, "foo"); + warn!(parent: None, "foo: {}", 3); + warn!(parent: None, { foo = 3, bar.baz = 80 }, "quux"); + warn!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + warn!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + warn!(parent: None, { foo = 2, bar.baz = 78 }, "quux"); + warn!(parent: None, { foo = ?2, bar.baz = %78 }, "quux"); + warn!(target: "foo_events", parent: None, foo = 3, bar.baz = 2, quux = false); + warn!(target: "foo_events", parent: None, foo = 3, bar.baz = 3,); + warn!(target: "foo_events", parent: None, "foo"); + warn!(target: "foo_events", parent: None, "foo: {}", 3); + warn!(target: "foo_events", parent: None, { foo = 3, bar.baz = 80 }, "quux"); + warn!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + warn!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + warn!(target: "foo_events", parent: None, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn error_root() { + error!(parent: None, foo = ?3, bar.baz = %2, quux = false); + error!(parent: None, foo = 3, bar.baz = 2, quux = false); + error!(parent: None, foo = 3, bar.baz = 3,); + error!(parent: None, "foo"); + error!(parent: None, "foo: {}", 3); + error!(parent: None, { foo = 3, bar.baz = 80 }, "quux"); + error!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + error!(parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + error!(parent: None, { foo = 2, bar.baz = 78 }, "quux"); + error!(parent: None, { foo = ?2, bar.baz = %78 }, "quux"); + error!(target: "foo_events", parent: None, foo = 3, bar.baz = 2, quux = false); + error!(target: "foo_events", parent: None, foo = 3, bar.baz = 3,); + error!(target: "foo_events", parent: None, "foo"); + error!(target: "foo_events", parent: None, "foo: {}", 3); + error!(target: "foo_events", parent: None, { foo = 3, bar.baz = 80 }, "quux"); + error!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + error!(target: "foo_events", parent: None, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + error!(target: "foo_events", parent: None, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + event!(parent: &p, Level::DEBUG, foo = ?3, bar.baz = %2, quux = false); + event!(parent: &p, Level::DEBUG, foo = 3, bar.baz = 2, quux = false); + event!(parent: &p, Level::DEBUG, foo = 3, bar.baz = 3,); + event!(parent: &p, Level::DEBUG, "foo"); + event!(parent: &p, Level::DEBUG, "foo: {}", 3); + event!(parent: &p, Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux"); + event!(parent: &p, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + event!(parent: &p, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + event!(parent: &p, Level::DEBUG, { foo = ?2, bar.baz = %78 }, "quux"); + event!(target: "foo_events", parent: &p, Level::DEBUG, foo = 3, bar.baz = 2, quux = false); + event!(target: "foo_events", parent: &p, Level::DEBUG, foo = 3, bar.baz = 3,); + event!(target: "foo_events", parent: &p, Level::DEBUG, "foo"); + event!(target: "foo_events", parent: &p, Level::DEBUG, "foo: {}", 3); + event!(target: "foo_events", parent: &p, Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux"); + event!(target: "foo_events", parent: &p, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + event!(target: "foo_events", parent: &p, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + event!(target: "foo_events", parent: &p, Level::DEBUG, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn trace_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + trace!(parent: &p, foo = ?3, bar.baz = %2, quux = false); + trace!(parent: &p, foo = 3, bar.baz = 2, quux = false); + trace!(parent: &p, foo = 3, bar.baz = 3,); + trace!(parent: &p, "foo"); + trace!(parent: &p, "foo: {}", 3); + trace!(parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + trace!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + trace!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + trace!(parent: &p, { foo = 2, bar.baz = 78 }, "quux"); + trace!(parent: &p, { foo = ?2, bar.baz = %78 }, "quux"); + trace!(target: "foo_events", parent: &p, foo = 3, bar.baz = 2, quux = false); + trace!(target: "foo_events", parent: &p, foo = 3, bar.baz = 3,); + trace!(target: "foo_events", parent: &p, "foo"); + trace!(target: "foo_events", parent: &p, "foo: {}", 3); + trace!(target: "foo_events", parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + trace!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + trace!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + trace!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + debug!(parent: &p, foo = ?3, bar.baz = %2, quux = false); + debug!(parent: &p, foo = 3, bar.baz = 2, quux = false); + debug!(parent: &p, foo = 3, bar.baz = 3,); + debug!(parent: &p, "foo"); + debug!(parent: &p, "foo: {}", 3); + debug!(parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + debug!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + debug!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + debug!(parent: &p, { foo = 2, bar.baz = 78 }, "quux"); + debug!(parent: &p, { foo = ?2, bar.baz = %78 }, "quux"); + debug!(target: "foo_events", parent: &p, foo = 3, bar.baz = 2, quux = false); + debug!(target: "foo_events", parent: &p, foo = 3, bar.baz = 3,); + debug!(target: "foo_events", parent: &p, "foo"); + debug!(target: "foo_events", parent: &p, "foo: {}", 3); + debug!(target: "foo_events", parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + debug!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + debug!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + debug!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn info_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + info!(parent: &p, foo = ?3, bar.baz = %2, quux = false); + info!(parent: &p, foo = 3, bar.baz = 2, quux = false); + info!(parent: &p, foo = 3, bar.baz = 3,); + info!(parent: &p, "foo"); + info!(parent: &p, "foo: {}", 3); + info!(parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + info!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + info!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + info!(parent: &p, { foo = 2, bar.baz = 78 }, "quux"); + info!(parent: &p, { foo = ?2, bar.baz = %78 }, "quux"); + info!(target: "foo_events", parent: &p, foo = 3, bar.baz = 2, quux = false); + info!(target: "foo_events", parent: &p, foo = 3, bar.baz = 3,); + info!(target: "foo_events", parent: &p, "foo"); + info!(target: "foo_events", parent: &p, "foo: {}", 3); + info!(target: "foo_events", parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + info!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + info!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + info!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn warn_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + warn!(parent: &p, foo = ?3, bar.baz = %2, quux = false); + warn!(parent: &p, foo = 3, bar.baz = 2, quux = false); + warn!(parent: &p, foo = 3, bar.baz = 3,); + warn!(parent: &p, "foo"); + warn!(parent: &p, "foo: {}", 3); + warn!(parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + warn!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + warn!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + warn!(parent: &p, { foo = 2, bar.baz = 78 }, "quux"); + warn!(parent: &p, { foo = ?2, bar.baz = %78 }, "quux"); + warn!(target: "foo_events", parent: &p, foo = 3, bar.baz = 2, quux = false); + warn!(target: "foo_events", parent: &p, foo = 3, bar.baz = 3,); + warn!(target: "foo_events", parent: &p, "foo"); + warn!(target: "foo_events", parent: &p, "foo: {}", 3); + warn!(target: "foo_events", parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + warn!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + warn!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + warn!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn error_with_parent() { + let p = span!(Level::TRACE, "im_a_parent!"); + error!(parent: &p, foo = ?3, bar.baz = %2, quux = false); + error!(parent: &p, foo = 3, bar.baz = 2, quux = false); + error!(parent: &p, foo = 3, bar.baz = 3,); + error!(parent: &p, "foo"); + error!(parent: &p, "foo: {}", 3); + error!(parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + error!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + error!(parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + error!(parent: &p, { foo = 2, bar.baz = 78 }, "quux"); + error!(parent: &p, { foo = ?2, bar.baz = %78 }, "quux"); + error!(target: "foo_events", parent: &p, foo = 3, bar.baz = 2, quux = false); + error!(target: "foo_events", parent: &p, foo = 3, bar.baz = 3,); + error!(target: "foo_events", parent: &p, "foo"); + error!(target: "foo_events", parent: &p, "foo: {}", 3); + error!(target: "foo_events", parent: &p, { foo = 3, bar.baz = 80 }, "quux"); + error!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}", true); + error!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false); + error!(target: "foo_events", parent: &p, { foo = 2, bar.baz = 78, }, "quux"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn field_shorthand_only() { + #[derive(Debug)] + struct Position { + x: f32, + y: f32, + } + let pos = Position { + x: 3.234, + y: -1.223, + }; + + trace!(?pos.x, ?pos.y); + debug!(?pos.x, ?pos.y); + info!(?pos.x, ?pos.y); + warn!(?pos.x, ?pos.y); + error!(?pos.x, ?pos.y); + event!(Level::TRACE, ?pos.x, ?pos.y); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn borrow_val_events() { + // Reproduces https://github.com/tokio-rs/tracing/issues/954 + let mut foo = (String::new(), String::new()); + let zero = &mut foo.0; + trace!(one = ?foo.1); + debug!(one = ?foo.1); + info!(one = ?foo.1); + warn!(one = ?foo.1); + error!(one = ?foo.1); + zero.push_str("hello world"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn borrow_val_spans() { + // Reproduces https://github.com/tokio-rs/tracing/issues/954 + let mut foo = (String::new(), String::new()); + let zero = &mut foo.0; + let _span = trace_span!("span", one = ?foo.1); + let _span = debug_span!("span", one = ?foo.1); + let _span = info_span!("span", one = ?foo.1); + let _span = warn_span!("span", one = ?foo.1); + let _span = error_span!("span", one = ?foo.1); + zero.push_str("hello world"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn callsite_macro_api() { + // This test should catch any inadvertent breaking changes + // caused by changes to the macro. + let _callsite = callsite! { + name: "test callsite", + kind: tracing::metadata::Kind::EVENT, + target: "test target", + level: tracing::Level::TRACE, + fields: foo, bar, + }; + let _callsite = callsite! { + name: "test callsite", + kind: tracing::metadata::Kind::SPAN, + level: tracing::Level::TRACE, + fields: foo, + }; + let _callsite = callsite! { + name: "test callsite", + kind: tracing::metadata::Kind::SPAN, + fields: foo, + }; +} diff --git a/vendor/tracing/tests/macros_incompatible_concat.rs b/vendor/tracing/tests/macros_incompatible_concat.rs new file mode 100644 index 000000000..bda6b964f --- /dev/null +++ b/vendor/tracing/tests/macros_incompatible_concat.rs @@ -0,0 +1,24 @@ +use tracing::{enabled, event, span, Level}; + +#[macro_export] +macro_rules! concat { + () => {}; +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span() { + span!(Level::DEBUG, "foo"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event() { + event!(Level::DEBUG, "foo"); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn enabled() { + enabled!(Level::DEBUG); +} diff --git a/vendor/tracing/tests/macros_redefined_core.rs b/vendor/tracing/tests/macros_redefined_core.rs new file mode 100644 index 000000000..d830dcdb0 --- /dev/null +++ b/vendor/tracing/tests/macros_redefined_core.rs @@ -0,0 +1,18 @@ +extern crate self as core; + +use tracing::{enabled, event, span, Level}; + +#[test] +fn span() { + span!(Level::DEBUG, "foo"); +} + +#[test] +fn event() { + event!(Level::DEBUG, "foo"); +} + +#[test] +fn enabled() { + enabled!(Level::DEBUG); +} diff --git a/vendor/tracing/tests/max_level_hint.rs b/vendor/tracing/tests/max_level_hint.rs new file mode 100644 index 000000000..63d3af635 --- /dev/null +++ b/vendor/tracing/tests/max_level_hint.rs @@ -0,0 +1,37 @@ +use tracing::Level; +use tracing_mock::*; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn max_level_hints() { + // This test asserts that when a subscriber provides us with the global + // maximum level that it will enable (by implementing the + // `Subscriber::max_level_hint` method), we will never call + // `Subscriber::enabled` for events above that maximum level. + // + // In this case, we test that by making the `enabled` method assert that no + // `Metadata` for spans or events at the `TRACE` or `DEBUG` levels. + let (subscriber, handle) = subscriber::mock() + .with_max_level_hint(Level::INFO) + .with_filter(|meta| { + assert!( + dbg!(meta).level() <= &Level::INFO, + "a TRACE or DEBUG event was dynamically filtered: " + ); + true + }) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + tracing::subscriber::set_global_default(subscriber).unwrap(); + + tracing::info!("doing a thing that you might care about"); + tracing::debug!("charging turboencabulator with interocitor"); + tracing::warn!("extremely serious warning, pay attention"); + tracing::trace!("interocitor charge level is 10%"); + tracing::error!("everything is on fire"); + handle.assert_finished(); +} diff --git a/vendor/tracing/tests/multiple_max_level_hints.rs b/vendor/tracing/tests/multiple_max_level_hints.rs new file mode 100644 index 000000000..dd50a193b --- /dev/null +++ b/vendor/tracing/tests/multiple_max_level_hints.rs @@ -0,0 +1,69 @@ +#![cfg(feature = "std")] + +use tracing::Level; +use tracing_mock::*; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn multiple_max_level_hints() { + // This test ensures that when multiple subscribers are active, their max + // level hints are handled correctly. The global max level should be the + // maximum of the level filters returned by the two `Subscriber`'s + // `max_level_hint` method. + // + // In this test, we create a subscriber whose max level is `INFO`, and + // another whose max level is `DEBUG`. We then add an assertion to both of + // those subscribers' `enabled` method that no metadata for `TRACE` spans or + // events are filtered, since they are disabled by the global max filter. + + fn do_events() { + tracing::info!("doing a thing that you might care about"); + tracing::debug!("charging turboencabulator with interocitor"); + tracing::warn!("extremely serious warning, pay attention"); + tracing::trace!("interocitor charge level is 10%"); + tracing::error!("everything is on fire"); + } + + let (subscriber1, handle1) = subscriber::mock() + .named("subscriber1") + .with_max_level_hint(Level::INFO) + .with_filter(|meta| { + let level = dbg!(meta.level()); + assert!( + level <= &Level::DEBUG, + "a TRACE event was dynamically filtered by subscriber1" + ); + level <= &Level::INFO + }) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + let (subscriber2, handle2) = subscriber::mock() + .named("subscriber2") + .with_max_level_hint(Level::DEBUG) + .with_filter(|meta| { + let level = dbg!(meta.level()); + assert!( + level <= &Level::DEBUG, + "a TRACE event was dynamically filtered by subscriber2" + ); + level <= &Level::DEBUG + }) + .event(event::mock().at_level(Level::INFO)) + .event(event::mock().at_level(Level::DEBUG)) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .done() + .run_with_handle(); + + let dispatch1 = tracing::Dispatch::new(subscriber1); + + tracing::dispatcher::with_default(&dispatch1, do_events); + handle1.assert_finished(); + + let dispatch2 = tracing::Dispatch::new(subscriber2); + tracing::dispatcher::with_default(&dispatch2, do_events); + handle2.assert_finished(); +} diff --git a/vendor/tracing/tests/no_subscriber.rs b/vendor/tracing/tests/no_subscriber.rs new file mode 100644 index 000000000..5f927c1de --- /dev/null +++ b/vendor/tracing/tests/no_subscriber.rs @@ -0,0 +1,15 @@ +#![cfg(feature = "std")] + +use tracing::subscriber::{self, NoSubscriber}; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn no_subscriber_disables_global() { + // Reproduces https://github.com/tokio-rs/tracing/issues/1999 + let (subscriber, handle) = tracing_mock::subscriber::mock().done().run_with_handle(); + subscriber::set_global_default(subscriber).expect("setting global default must succeed"); + subscriber::with_default(NoSubscriber::default(), || { + tracing::info!("this should not be recorded"); + }); + handle.assert_finished(); +} diff --git a/vendor/tracing/tests/register_callsite_deadlock.rs b/vendor/tracing/tests/register_callsite_deadlock.rs new file mode 100644 index 000000000..e4c116c75 --- /dev/null +++ b/vendor/tracing/tests/register_callsite_deadlock.rs @@ -0,0 +1,47 @@ +use std::{sync::mpsc, thread, time::Duration}; +use tracing::{ + metadata::Metadata, + span, + subscriber::{self, Interest, Subscriber}, + Event, +}; + +#[test] +fn register_callsite_doesnt_deadlock() { + pub struct EvilSubscriber; + + impl Subscriber for EvilSubscriber { + fn register_callsite(&self, meta: &'static Metadata<'static>) -> Interest { + tracing::info!(?meta, "registered a callsite"); + Interest::always() + } + + fn enabled(&self, _: &Metadata<'_>) -> bool { + true + } + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(1) + } + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + fn event(&self, _: &Event<'_>) {} + fn enter(&self, _: &span::Id) {} + fn exit(&self, _: &span::Id) {} + } + + subscriber::set_global_default(EvilSubscriber).unwrap(); + + // spawn a thread, and assert it doesn't hang... + let (tx, didnt_hang) = mpsc::channel(); + let th = thread::spawn(move || { + tracing::info!("hello world!"); + tx.send(()).unwrap(); + }); + + didnt_hang + // Note: 60 seconds is *way* more than enough, but let's be generous in + // case of e.g. slow CI machines. + .recv_timeout(Duration::from_secs(60)) + .expect("the thread must not have hung!"); + th.join().expect("thread should join successfully"); +} diff --git a/vendor/tracing/tests/scoped_clobbers_default.rs b/vendor/tracing/tests/scoped_clobbers_default.rs new file mode 100644 index 000000000..362d34a82 --- /dev/null +++ b/vendor/tracing/tests/scoped_clobbers_default.rs @@ -0,0 +1,35 @@ +#![cfg(feature = "std")] +use tracing_mock::*; + +#[test] +fn scoped_clobbers_global() { + // Reproduces https://github.com/tokio-rs/tracing/issues/2050 + + let (scoped, scoped_handle) = subscriber::mock() + .event(event::msg("before global")) + .event(event::msg("before drop")) + .done() + .run_with_handle(); + + let (global, global_handle) = subscriber::mock() + .event(event::msg("after drop")) + .done() + .run_with_handle(); + + // Set a scoped default subscriber, returning a guard. + let guard = tracing::subscriber::set_default(scoped); + tracing::info!("before global"); + + // Now, set the global default. + tracing::subscriber::set_global_default(global) + .expect("global default should not already be set"); + // This event should still be collected by the scoped default. + tracing::info!("before drop"); + + // Drop the guard. Now, the global default subscriber should be used. + drop(guard); + tracing::info!("after drop"); + + scoped_handle.assert_finished(); + global_handle.assert_finished(); +} diff --git a/vendor/tracing/tests/span.rs b/vendor/tracing/tests/span.rs new file mode 100644 index 000000000..4ed650023 --- /dev/null +++ b/vendor/tracing/tests/span.rs @@ -0,0 +1,825 @@ +// These tests require the thread-local scoped dispatcher, which only works when +// we have a standard library. The behaviour being tested should be the same +// with the standard lib disabled. +#![cfg(feature = "std")] + +use std::thread; + +use tracing::{ + field::{debug, display}, + subscriber::with_default, + Level, Span, +}; +use tracing_mock::*; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn handles_to_the_same_span_are_equal() { + // Create a mock subscriber that will return `true` on calls to + // `Subscriber::enabled`, so that the spans will be constructed. We + // won't enter any spans in this test, so the subscriber won't actually + // expect to see any spans. + with_default(subscriber::mock().run(), || { + let foo1 = tracing::span!(Level::TRACE, "foo"); + let foo2 = foo1.clone(); + // Two handles that point to the same span are equal. + assert_eq!(foo1, foo2); + }); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn handles_to_different_spans_are_not_equal() { + with_default(subscriber::mock().run(), || { + // Even though these spans have the same name and fields, they will have + // differing metadata, since they were created on different lines. + let foo1 = tracing::span!(Level::TRACE, "foo", bar = 1u64, baz = false); + let foo2 = tracing::span!(Level::TRACE, "foo", bar = 1u64, baz = false); + + assert_ne!(foo1, foo2); + }); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn handles_to_different_spans_with_the_same_metadata_are_not_equal() { + // Every time time this function is called, it will return a _new + // instance_ of a span with the same metadata, name, and fields. + fn make_span() -> Span { + tracing::span!(Level::TRACE, "foo", bar = 1u64, baz = false) + } + + with_default(subscriber::mock().run(), || { + let foo1 = make_span(); + let foo2 = make_span(); + + assert_ne!(foo1, foo2); + // assert_ne!(foo1.data(), foo2.data()); + }); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn spans_always_go_to_the_subscriber_that_tagged_them() { + let subscriber1 = subscriber::mock() + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run(); + let subscriber2 = subscriber::mock().run(); + + let foo = with_default(subscriber1, || { + let foo = tracing::span!(Level::TRACE, "foo"); + foo.in_scope(|| {}); + foo + }); + // Even though we enter subscriber 2's context, the subscriber that + // tagged the span should see the enter/exit. + with_default(subscriber2, move || foo.in_scope(|| {})); +} + +// This gets exempt from testing in wasm because of: `thread::spawn` which is +// not yet possible to do in WASM. There is work going on see: +// <https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html> +// +// But for now since it's not possible we don't need to test for it :) +#[test] +fn spans_always_go_to_the_subscriber_that_tagged_them_even_across_threads() { + let subscriber1 = subscriber::mock() + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run(); + let foo = with_default(subscriber1, || { + let foo = tracing::span!(Level::TRACE, "foo"); + foo.in_scope(|| {}); + foo + }); + + // Even though we enter subscriber 2's context, the subscriber that + // tagged the span should see the enter/exit. + thread::spawn(move || { + with_default(subscriber::mock().run(), || { + foo.in_scope(|| {}); + }) + }) + .join() + .unwrap(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn dropping_a_span_calls_drop_span() { + let (subscriber, handle) = subscriber::mock() + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo"); + span.in_scope(|| {}); + drop(span); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span_closes_after_event() { + let (subscriber, handle) = subscriber::mock() + .enter(span::mock().named("foo")) + .event(event::mock()) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::span!(Level::TRACE, "foo").in_scope(|| { + tracing::event!(Level::DEBUG, {}, "my tracing::event!"); + }); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn new_span_after_event() { + let (subscriber, handle) = subscriber::mock() + .enter(span::mock().named("foo")) + .event(event::mock()) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .enter(span::mock().named("bar")) + .exit(span::mock().named("bar")) + .drop_span(span::mock().named("bar")) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::span!(Level::TRACE, "foo").in_scope(|| { + tracing::event!(Level::DEBUG, {}, "my tracing::event!"); + }); + tracing::span!(Level::TRACE, "bar").in_scope(|| {}); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event_outside_of_span() { + let (subscriber, handle) = subscriber::mock() + .event(event::mock()) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::debug!("my tracing::event!"); + tracing::span!(Level::TRACE, "foo").in_scope(|| {}); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn cloning_a_span_calls_clone_span() { + let (subscriber, handle) = subscriber::mock() + .clone_span(span::mock().named("foo")) + .run_with_handle(); + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo"); + // Allow the "redundant" `.clone` since it is used to call into the `.clone_span` hook. + #[allow(clippy::redundant_clone)] + let _span2 = span.clone(); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn drop_span_when_exiting_dispatchers_context() { + let (subscriber, handle) = subscriber::mock() + .clone_span(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .run_with_handle(); + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo"); + let _span2 = span.clone(); + drop(span); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn clone_and_drop_span_always_go_to_the_subscriber_that_tagged_the_span() { + let (subscriber1, handle1) = subscriber::mock() + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .clone_span(span::mock().named("foo")) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .run_with_handle(); + let subscriber2 = subscriber::mock().done().run(); + + let foo = with_default(subscriber1, || { + let foo = tracing::span!(Level::TRACE, "foo"); + foo.in_scope(|| {}); + foo + }); + // Even though we enter subscriber 2's context, the subscriber that + // tagged the span should see the enter/exit. + with_default(subscriber2, move || { + let foo2 = foo.clone(); + foo.in_scope(|| {}); + drop(foo); + drop(foo2); + }); + + handle1.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn span_closes_when_exited() { + let (subscriber, handle) = subscriber::mock() + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo"); + + foo.in_scope(|| {}); + + drop(foo); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn enter() { + let (subscriber, handle) = subscriber::mock() + .enter(span::mock().named("foo")) + .event(event::mock()) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo"); + let _enter = foo.enter(); + tracing::debug!("dropping guard..."); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn entered() { + let (subscriber, handle) = subscriber::mock() + .enter(span::mock().named("foo")) + .event(event::mock()) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + let _span = tracing::span!(Level::TRACE, "foo").entered(); + tracing::debug!("dropping guard..."); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn entered_api() { + let (subscriber, handle) = subscriber::mock() + .enter(span::mock().named("foo")) + .event(event::mock()) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo").entered(); + let _derefs_to_span = span.id(); + tracing::debug!("exiting span..."); + let _: Span = span.exit(); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn moved_field() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("bar") + .with_value(&display("hello from my span")) + .only(), + ), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + with_default(subscriber, || { + let from = "my span"; + let span = tracing::span!( + Level::TRACE, + "foo", + bar = display(format!("hello from {}", from)) + ); + span.in_scope(|| {}); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn dotted_field_name() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock() + .named("foo") + .with_field(field::mock("fields.bar").with_value(&true).only()), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::span!(Level::TRACE, "foo", fields.bar = true); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn borrowed_field() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("bar") + .with_value(&display("hello from my span")) + .only(), + ), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let from = "my span"; + let mut message = format!("hello from {}", from); + let span = tracing::span!(Level::TRACE, "foo", bar = display(&message)); + span.in_scope(|| { + message.insert_str(10, " inside"); + }); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +// If emitting log instrumentation, this gets moved anyway, breaking the test. +#[cfg(not(feature = "log"))] +fn move_field_out_of_struct() { + use tracing::field::debug; + + #[derive(Debug)] + struct Position { + x: f32, + y: f32, + } + + let pos = Position { + x: 3.234, + y: -1.223, + }; + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("x") + .with_value(&debug(3.234)) + .and(field::mock("y").with_value(&debug(-1.223))) + .only(), + ), + ) + .new_span( + span::mock() + .named("bar") + .with_field(field::mock("position").with_value(&debug(&pos)).only()), + ) + .run_with_handle(); + + with_default(subscriber, || { + let pos = Position { + x: 3.234, + y: -1.223, + }; + let foo = tracing::span!(Level::TRACE, "foo", x = debug(pos.x), y = debug(pos.y)); + let bar = tracing::span!(Level::TRACE, "bar", position = debug(pos)); + foo.in_scope(|| {}); + bar.in_scope(|| {}); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn float_values() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("x") + .with_value(&3.234) + .and(field::mock("y").with_value(&-1.223)) + .only(), + ), + ) + .run_with_handle(); + + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo", x = 3.234, y = -1.223); + foo.in_scope(|| {}); + }); + + handle.assert_finished(); +} + +// TODO(#1138): determine a new syntax for uninitialized span fields, and +// re-enable these. +/* +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn add_field_after_new_span() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock() + .named("foo") + .with_field(field::mock("bar").with_value(&5) + .and(field::mock("baz").with_value).only()), + ) + .record( + span::mock().named("foo"), + field::mock("baz").with_value(&true).only(), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo", bar = 5, baz = false); + span.record("baz", &true); + span.in_scope(|| {}) + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn add_fields_only_after_new_span() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .record( + span::mock().named("foo"), + field::mock("bar").with_value(&5).only(), + ) + .record( + span::mock().named("foo"), + field::mock("baz").with_value(&true).only(), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo", bar = _, baz = _); + span.record("bar", &5); + span.record("baz", &true); + span.in_scope(|| {}) + }); + + handle.assert_finished(); +} +*/ + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn record_new_value_for_field() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("bar") + .with_value(&5) + .and(field::mock("baz").with_value(&false)) + .only(), + ), + ) + .record( + span::mock().named("foo"), + field::mock("baz").with_value(&true).only(), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo", bar = 5, baz = false); + span.record("baz", &true); + span.in_scope(|| {}) + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn record_new_values_for_fields() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("bar") + .with_value(&4) + .and(field::mock("baz").with_value(&false)) + .only(), + ), + ) + .record( + span::mock().named("foo"), + field::mock("bar").with_value(&5).only(), + ) + .record( + span::mock().named("foo"), + field::mock("baz").with_value(&true).only(), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let span = tracing::span!(Level::TRACE, "foo", bar = 4, baz = false); + span.record("bar", &5); + span.record("baz", &true); + span.in_scope(|| {}) + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn new_span_with_target_and_log_level() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock() + .named("foo") + .with_target("app_span") + .at_level(Level::DEBUG), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + tracing::span!(target: "app_span", Level::DEBUG, "foo"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn explicit_root_span_is_root() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo").with_explicit_parent(None)) + .done() + .run_with_handle(); + + with_default(subscriber, || { + tracing::span!(parent: None, Level::TRACE, "foo"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn explicit_root_span_is_root_regardless_of_ctx() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .enter(span::mock().named("foo")) + .new_span(span::mock().named("bar").with_explicit_parent(None)) + .exit(span::mock().named("foo")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + tracing::span!(Level::TRACE, "foo").in_scope(|| { + tracing::span!(parent: None, Level::TRACE, "bar"); + }) + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn explicit_child() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .new_span(span::mock().named("bar").with_explicit_parent(Some("foo"))) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo"); + tracing::span!(parent: foo.id(), Level::TRACE, "bar"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn explicit_child_at_levels() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .new_span(span::mock().named("a").with_explicit_parent(Some("foo"))) + .new_span(span::mock().named("b").with_explicit_parent(Some("foo"))) + .new_span(span::mock().named("c").with_explicit_parent(Some("foo"))) + .new_span(span::mock().named("d").with_explicit_parent(Some("foo"))) + .new_span(span::mock().named("e").with_explicit_parent(Some("foo"))) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo"); + tracing::trace_span!(parent: foo.id(), "a"); + tracing::debug_span!(parent: foo.id(), "b"); + tracing::info_span!(parent: foo.id(), "c"); + tracing::warn_span!(parent: foo.id(), "d"); + tracing::error_span!(parent: foo.id(), "e"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn explicit_child_regardless_of_ctx() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .new_span(span::mock().named("bar")) + .enter(span::mock().named("bar")) + .new_span(span::mock().named("baz").with_explicit_parent(Some("foo"))) + .exit(span::mock().named("bar")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + let foo = tracing::span!(Level::TRACE, "foo"); + tracing::span!(Level::TRACE, "bar") + .in_scope(|| tracing::span!(parent: foo.id(), Level::TRACE, "baz")) + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn contextual_root() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo").with_contextual_parent(None)) + .done() + .run_with_handle(); + + with_default(subscriber, || { + tracing::span!(Level::TRACE, "foo"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn contextual_child() { + let (subscriber, handle) = subscriber::mock() + .new_span(span::mock().named("foo")) + .enter(span::mock().named("foo")) + .new_span( + span::mock() + .named("bar") + .with_contextual_parent(Some("foo")), + ) + .exit(span::mock().named("foo")) + .done() + .run_with_handle(); + + with_default(subscriber, || { + tracing::span!(Level::TRACE, "foo").in_scope(|| { + tracing::span!(Level::TRACE, "bar"); + }) + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn display_shorthand() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("my_span").with_field( + field::mock("my_field") + .with_value(&display("hello world")) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::span!(Level::TRACE, "my_span", my_field = %"hello world"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn debug_shorthand() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("my_span").with_field( + field::mock("my_field") + .with_value(&debug("hello world")) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::span!(Level::TRACE, "my_span", my_field = ?"hello world"); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn both_shorthands() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("my_span").with_field( + field::mock("display_field") + .with_value(&display("hello world")) + .and(field::mock("debug_field").with_value(&debug("hello world"))) + .only(), + ), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + tracing::span!(Level::TRACE, "my_span", display_field = %"hello world", debug_field = ?"hello world"); + }); + + handle.assert_finished(); +} diff --git a/vendor/tracing/tests/subscriber.rs b/vendor/tracing/tests/subscriber.rs new file mode 100644 index 000000000..15557c107 --- /dev/null +++ b/vendor/tracing/tests/subscriber.rs @@ -0,0 +1,130 @@ +// These tests require the thread-local scoped dispatcher, which only works when +// we have a standard library. The behaviour being tested should be the same +// with the standard lib disabled. +// +// The alternative would be for each of these tests to be defined in a separate +// file, which is :( +#![cfg(feature = "std")] +use tracing::{ + field::display, + span::{Attributes, Id, Record}, + subscriber::{with_default, Interest, Subscriber}, + Event, Level, Metadata, +}; + +use tracing_mock::*; + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn event_macros_dont_infinite_loop() { + // This test ensures that an event macro within a subscriber + // won't cause an infinite loop of events. + struct TestSubscriber; + impl Subscriber for TestSubscriber { + fn register_callsite(&self, _: &Metadata<'_>) -> Interest { + // Always return sometimes so that `enabled` will be called + // (which can loop). + Interest::sometimes() + } + + fn enabled(&self, meta: &Metadata<'_>) -> bool { + assert!(meta.fields().iter().any(|f| f.name() == "foo")); + tracing::event!(Level::TRACE, bar = false); + true + } + + fn new_span(&self, _: &Attributes<'_>) -> Id { + Id::from_u64(0xAAAA) + } + + fn record(&self, _: &Id, _: &Record<'_>) {} + + fn record_follows_from(&self, _: &Id, _: &Id) {} + + fn event(&self, event: &Event<'_>) { + assert!(event.metadata().fields().iter().any(|f| f.name() == "foo")); + tracing::event!(Level::TRACE, baz = false); + } + + fn enter(&self, _: &Id) {} + + fn exit(&self, _: &Id) {} + } + + with_default(TestSubscriber, || { + tracing::event!(Level::TRACE, foo = false); + }) +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn boxed_subscriber() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("bar") + .with_value(&display("hello from my span")) + .only(), + ), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .done() + .run_with_handle(); + let subscriber: Box<dyn Subscriber + Send + Sync + 'static> = Box::new(subscriber); + + with_default(subscriber, || { + let from = "my span"; + let span = tracing::span!( + Level::TRACE, + "foo", + bar = format_args!("hello from {}", from) + ); + span.in_scope(|| {}); + }); + + handle.assert_finished(); +} + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn arced_subscriber() { + use std::sync::Arc; + + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock().named("foo").with_field( + field::mock("bar") + .with_value(&display("hello from my span")) + .only(), + ), + ) + .enter(span::mock().named("foo")) + .exit(span::mock().named("foo")) + .drop_span(span::mock().named("foo")) + .event( + event::mock() + .with_fields(field::mock("message").with_value(&display("hello from my event"))), + ) + .done() + .run_with_handle(); + let subscriber: Arc<dyn Subscriber + Send + Sync + 'static> = Arc::new(subscriber); + + // Test using a clone of the `Arc`ed subscriber + with_default(subscriber.clone(), || { + let from = "my span"; + let span = tracing::span!( + Level::TRACE, + "foo", + bar = format_args!("hello from {}", from) + ); + span.in_scope(|| {}); + }); + + with_default(subscriber, || { + tracing::info!("hello from my event"); + }); + + handle.assert_finished(); +} |