diff options
Diffstat (limited to '')
145 files changed, 13942 insertions, 18202 deletions
diff --git a/vendor/tracing-attributes/.cargo-checksum.json b/vendor/tracing-attributes/.cargo-checksum.json index 995155f68..e29c0854b 100644 --- a/vendor/tracing-attributes/.cargo-checksum.json +++ b/vendor/tracing-attributes/.cargo-checksum.json @@ -1 +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 +{"files":{"CHANGELOG.md":"7c292985e41b0fd9c89d32bd15594b76bb3109ff96177af7461d1a0d3ea48574","Cargo.toml":"1c272401b940ef2fd88638a6228af02c14e3d47d4b592ee5c445f7470a0d1763","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"aae710a2c0c4ade929a84f8f3d9f98fcb9ad1c1e9e52ae0b0393db9f353f6248","src/attr.rs":"4d26dad70c765fff3d4677ebe8e71b63599d67ceba5c48dbcb204d247b5394ce","src/expand.rs":"f070e38eb3526c4c50e98cfafb2c26e50c7170f27d0bd716838a29777c1174cd","src/lib.rs":"14803c868525f6ae95b846ce2f369a47d50b2befba336335354867cf2a1b2455","tests/async_fn.rs":"6ae845ab6c508f9f2d1ea7ec3855971e8cff85afdae326593089d0a53c9c6dcf","tests/destructuring.rs":"26b9800678bad09e06512a113a54556e2fac3ecb15a18dcccefe105fb8911c26","tests/err.rs":"3beb5f065e57a578825c383a56b9d64fb89794a508018bf36e16f113557909b8","tests/fields.rs":"3882bd4e744d6b492f59beac7475e8bf4ff4ca8ad85c6951c305a22c78e75fae","tests/follows_from.rs":"5bc856923e87b34e0558959149118238fe668ac621f1748cc444c21c90a86647","tests/instrument.rs":"e3a90f2aef0d2f56c2c25018e2fbacf518e90ef6810930941f86740279252d03","tests/levels.rs":"80ffb684163a4d28c69c40e31a82609ac02daf922086bab8247bca125aec3c69","tests/names.rs":"5afd6c4d526588bcea3141c130a45a21872956495b6868a01b44ddff57749827","tests/parents.rs":"673d3f81eed6ba433f685ec53fd007c5dd957b97d32499d7ea1537e1f289cb2e","tests/ret.rs":"083b4ca456d766c469b2fff7608b59524b422941e5c5e84f07c1ce0d7b345c7a","tests/targets.rs":"95ce1ce1e2d29794062c5b3429d91c1bfaba5813251d5d8440c12cb2db6e11bf","tests/ui.rs":"c73c70fe7371998df077862092f40df7210b13496e119ab4e657a1848e42ab30","tests/ui/async_instrument.rs":"00fcde05841e8f9f6cc6f9434f8cc4baed5cf6e3ca73e8faddccbdace14e9485","tests/ui/async_instrument.stderr":"6f0457b5cd035a9bc82f5e98fb6d3a74177336ec6a19ba2e7cb9dbf3e67c3aff"},"package":"4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"}
\ No newline at end of file diff --git a/vendor/tracing-attributes/CHANGELOG.md b/vendor/tracing-attributes/CHANGELOG.md index 4fcc81f29..42cb09b6a 100644 --- a/vendor/tracing-attributes/CHANGELOG.md +++ b/vendor/tracing-attributes/CHANGELOG.md @@ -1,3 +1,24 @@ +# 0.1.23 (October 6, 2022) + +This release of `tracing-attributes` fixes a bug where compiler diagnostic spans +for type errors in `#[instrument]`ed `async fn`s have the location of the +`#[instrument]` attribute rather than the location of the actual error, and a +bug where inner attributes in `#[instrument]`ed functions would cause a compiler +error. +### Fixed + +- Fix incorrect handling of inner attributes in `#[instrument]`ed functions ([#2307]) +- Add fake return to improve spans generated for type errors in `async fn`s ([#2270]) +- Updated `syn` dependency to fix compilation with `-Z minimal-versions` + ([#2246]) + +Thanks to new contributors @compiler-errors and @e-nomem, as well as @CAD97, for +contributing to this release! + +[#2307]: https://github.com/tokio-rs/tracing/pull/2307 +[#2270]: https://github.com/tokio-rs/tracing/pull/2270 +[#2246]: https://github.com/tokio-rs/tracing/pull/2246 + # 0.1.22 (July 1, 2022) This release fixes an issue where using the `err` or `ret` arguments to diff --git a/vendor/tracing-attributes/Cargo.toml b/vendor/tracing-attributes/Cargo.toml index 3ecaba6fc..5639de0b3 100644 --- a/vendor/tracing-attributes/Cargo.toml +++ b/vendor/tracing-attributes/Cargo.toml @@ -13,7 +13,7 @@ edition = "2018" rust-version = "1.49.0" name = "tracing-attributes" -version = "0.1.22" +version = "0.1.23" authors = [ "Tokio Contributors <team@tokio.rs>", "Eliza Weisman <eliza@buoyant.io>", @@ -49,7 +49,7 @@ version = "1" version = "1" [dependencies.syn] -version = "1.0.43" +version = "1.0.98" features = [ "full", "parsing", @@ -63,21 +63,27 @@ features = [ default-features = false [dev-dependencies.async-trait] -version = "0.1.44" +version = "0.1.56" + +[dev-dependencies.rustversion] +version = "1.0.9" [dev-dependencies.tokio-test] version = "0.3.0" [dev-dependencies.tracing] -version = "0.1" +version = "0.1.35" [dev-dependencies.tracing-core] -version = "0.1" +version = "0.1.28" [dev-dependencies.tracing-subscriber] -version = "0.3" +version = "0.3.0" features = ["env-filter"] +[dev-dependencies.trybuild] +version = "1.0.64" + [features] async-await = [] diff --git a/vendor/tracing-attributes/README.md b/vendor/tracing-attributes/README.md index 636179468..356b511f2 100644 --- a/vendor/tracing-attributes/README.md +++ b/vendor/tracing-attributes/README.md @@ -18,7 +18,7 @@ Macro attributes for application-level tracing. [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-url]: https://docs.rs/tracing-attributes/0.1.23 [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 @@ -47,7 +47,7 @@ First, add this to your `Cargo.toml`: ```toml [dependencies] -tracing-attributes = "0.1.22" +tracing-attributes = "0.1.23" ``` diff --git a/vendor/tracing-attributes/src/expand.rs b/vendor/tracing-attributes/src/expand.rs index 81ee6ed90..7005b4423 100644 --- a/vendor/tracing-attributes/src/expand.rs +++ b/vendor/tracing-attributes/src/expand.rs @@ -2,15 +2,16 @@ use std::iter; use proc_macro2::TokenStream; use quote::{quote, quote_spanned, ToTokens}; +use syn::visit_mut::VisitMut; 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, + Path, ReturnType, Signature, Stmt, Token, Type, TypePath, }; use crate::{ attr::{Field, Fields, FormatMode, InstrumentArgs}, - MaybeItemFnRef, + MaybeItemFn, MaybeItemFnRef, }; /// Given an existing function, generate an instrumented version of that function @@ -18,20 +19,21 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>( input: MaybeItemFnRef<'a, B>, args: InstrumentArgs, instrumented_function_name: &str, - self_type: Option<&syn::TypePath>, + self_type: Option<&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, + outer_attrs, + inner_attrs, vis, sig, block, } = input; let Signature { - output: return_type, + output, inputs: params, unsafety, asyncness, @@ -49,8 +51,35 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>( let warnings = args.warnings(); + let (return_type, return_span) = if let ReturnType::Type(_, return_type) = &output { + (erase_impl_trait(return_type), return_type.span()) + } else { + // Point at function name if we don't have an explicit return type + (syn::parse_quote! { () }, ident.span()) + }; + // Install a fake return statement as the first thing in the function + // body, so that we eagerly infer that the return type is what we + // declared in the async fn signature. + // The `#[allow(..)]` is given because the return statement is + // unreachable, but does affect inference, so it needs to be written + // exactly that way for it to do its magic. + let fake_return_edge = quote_spanned! {return_span=> + #[allow(unreachable_code, clippy::diverging_sub_expression, clippy::let_unit_value)] + if false { + let __tracing_attr_fake_return: #return_type = + unreachable!("this is just for type inference, and is unreachable code"); + return __tracing_attr_fake_return; + } + }; + let block = quote! { + { + #fake_return_edge + #block + } + }; + let body = gen_block( - block, + &block, params, asyncness.is_some(), args, @@ -59,10 +88,11 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>( ); quote!( - #(#attrs) * - #vis #constness #unsafety #asyncness #abi fn #ident<#gen_params>(#params) #return_type + #(#outer_attrs) * + #vis #constness #unsafety #asyncness #abi fn #ident<#gen_params>(#params) #output #where_clause { + #(#inner_attrs) * #warnings #body } @@ -76,7 +106,7 @@ fn gen_block<B: ToTokens>( async_context: bool, mut args: InstrumentArgs, instrumented_function_name: &str, - self_type: Option<&syn::TypePath>, + self_type: Option<&TypePath>, ) -> proc_macro2::TokenStream { // generate the span's name let span_name = args @@ -393,11 +423,11 @@ impl RecordType { "Wrapping", ]; - /// Parse `RecordType` from [syn::Type] by looking up + /// Parse `RecordType` from [Type] by looking up /// the [RecordType::TYPES_FOR_VALUE] array. - fn parse_from_ty(ty: &syn::Type) -> Self { + fn parse_from_ty(ty: &Type) -> Self { match ty { - syn::Type::Path(syn::TypePath { path, .. }) + Type::Path(TypePath { path, .. }) if path .segments .iter() @@ -410,9 +440,7 @@ impl RecordType { { RecordType::Value } - syn::Type::Reference(syn::TypeReference { elem, .. }) => { - RecordType::parse_from_ty(&*elem) - } + Type::Reference(syn::TypeReference { elem, .. }) => RecordType::parse_from_ty(elem), _ => RecordType::Debug, } } @@ -471,7 +499,7 @@ pub(crate) struct AsyncInfo<'block> { // statement that must be patched source_stmt: &'block Stmt, kind: AsyncKind<'block>, - self_type: Option<syn::TypePath>, + self_type: Option<TypePath>, input: &'block ItemFn, } @@ -606,11 +634,11 @@ impl<'block> AsyncInfo<'block> { 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 { + if let Type::Reference(syn::TypeReference { elem, .. }) = ty { ty = *elem; } - if let syn::Type::Path(tp) = ty { + if let Type::Path(tp) = ty { self_type = Some(tp); break; } @@ -631,7 +659,7 @@ impl<'block> AsyncInfo<'block> { self, args: InstrumentArgs, instrumented_function_name: &str, - ) -> proc_macro::TokenStream { + ) -> Result<proc_macro::TokenStream, syn::Error> { // let's rewrite some statements! let mut out_stmts: Vec<TokenStream> = self .input @@ -652,12 +680,15 @@ impl<'block> AsyncInfo<'block> { // 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(), - ), + AsyncKind::Function(fun) => { + let fun = MaybeItemFn::from(fun.clone()); + gen_function( + fun.as_ref(), + args, + instrumented_function_name, + self.self_type.as_ref(), + ) + } // `async move { ... }`, optionally pinned AsyncKind::Async { async_expr, @@ -688,13 +719,13 @@ impl<'block> AsyncInfo<'block> { let vis = &self.input.vis; let sig = &self.input.sig; let attrs = &self.input.attrs; - quote!( + Ok(quote!( #(#attrs) * #vis #sig { #(#out_stmts) * } ) - .into() + .into()) } } @@ -722,7 +753,7 @@ struct IdentAndTypesRenamer<'a> { idents: Vec<(Ident, Ident)>, } -impl<'a> syn::visit_mut::VisitMut for IdentAndTypesRenamer<'a> { +impl<'a> 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)] @@ -734,11 +765,11 @@ impl<'a> syn::visit_mut::VisitMut for IdentAndTypesRenamer<'a> { } } - fn visit_type_mut(&mut self, ty: &mut syn::Type) { + fn visit_type_mut(&mut self, ty: &mut Type) { for (type_name, new_type) in &self.types { - if let syn::Type::Path(TypePath { path, .. }) = ty { + if let Type::Path(TypePath { path, .. }) = ty { if path_to_string(path) == *type_name { - *ty = syn::Type::Path(new_type.clone()); + *ty = Type::Path(new_type.clone()); } } } @@ -751,10 +782,33 @@ struct AsyncTraitBlockReplacer<'a> { patched_block: Block, } -impl<'a> syn::visit_mut::VisitMut for AsyncTraitBlockReplacer<'a> { +impl<'a> VisitMut for AsyncTraitBlockReplacer<'a> { fn visit_block_mut(&mut self, i: &mut Block) { if i == self.block { *i = self.patched_block.clone(); } } } + +// Replaces any `impl Trait` with `_` so it can be used as the type in +// a `let` statement's LHS. +struct ImplTraitEraser; + +impl VisitMut for ImplTraitEraser { + fn visit_type_mut(&mut self, t: &mut Type) { + if let Type::ImplTrait(..) = t { + *t = syn::TypeInfer { + underscore_token: Token![_](t.span()), + } + .into(); + } else { + syn::visit_mut::visit_type_mut(self, t); + } + } +} + +fn erase_impl_trait(ty: &Type) -> Type { + let mut ty = ty.clone(); + ImplTraitEraser.visit_type_mut(&mut ty); + ty +} diff --git a/vendor/tracing-attributes/src/lib.rs b/vendor/tracing-attributes/src/lib.rs index 49b3c794a..f5974e4e5 100644 --- a/vendor/tracing-attributes/src/lib.rs +++ b/vendor/tracing-attributes/src/lib.rs @@ -16,7 +16,7 @@ //! //! ```toml //! [dependencies] -//! tracing-attributes = "0.1.22" +//! tracing-attributes = "0.1.23" //! ``` //! //! The [`#[instrument]`][instrument] attribute can now be added to a function @@ -52,7 +52,7 @@ //! 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_root_url = "https://docs.rs/tracing-attributes/0.1.23")] #![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/" @@ -86,7 +86,7 @@ extern crate proc_macro; use proc_macro2::TokenStream; use quote::ToTokens; use syn::parse::{Parse, ParseStream}; -use syn::{Attribute, Block, ItemFn, Signature, Visibility}; +use syn::{Attribute, ItemFn, Signature, Visibility}; mod attr; mod expand; @@ -587,11 +587,13 @@ fn instrument_precise( // 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())); + return async_like.gen_async(args, instrumented_function_name.as_str()); } + let input = MaybeItemFn::from(input); + Ok(expand::gen_function( - (&input).into(), + input.as_ref(), args, instrumented_function_name.as_str(), None, @@ -603,7 +605,8 @@ fn instrument_precise( /// which's block is just a `TokenStream` (it may contain invalid code). #[derive(Debug, Clone)] struct MaybeItemFn { - attrs: Vec<Attribute>, + outer_attrs: Vec<Attribute>, + inner_attrs: Vec<Attribute>, vis: Visibility, sig: Signature, block: TokenStream, @@ -612,7 +615,8 @@ struct MaybeItemFn { impl MaybeItemFn { fn as_ref(&self) -> MaybeItemFnRef<'_, TokenStream> { MaybeItemFnRef { - attrs: &self.attrs, + outer_attrs: &self.outer_attrs, + inner_attrs: &self.inner_attrs, vis: &self.vis, sig: &self.sig, block: &self.block, @@ -624,12 +628,14 @@ impl 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 outer_attrs = input.call(Attribute::parse_outer)?; let vis: Visibility = input.parse()?; let sig: Signature = input.parse()?; + let inner_attrs = input.call(Attribute::parse_inner)?; let block: TokenStream = input.parse()?; Ok(Self { - attrs, + outer_attrs, + inner_attrs, vis, sig, block, @@ -637,23 +643,35 @@ impl Parse for MaybeItemFn { } } +impl From<ItemFn> for MaybeItemFn { + fn from( + ItemFn { + attrs, + vis, + sig, + block, + }: ItemFn, + ) -> Self { + let (outer_attrs, inner_attrs) = attrs + .into_iter() + .partition(|attr| attr.style == syn::AttrStyle::Outer); + Self { + outer_attrs, + inner_attrs, + vis, + sig, + block: block.to_token_stream(), + } + } +} + /// 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>, + outer_attrs: &'a Vec<Attribute>, + inner_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 index 7e27fb5ce..c89963672 100644 --- a/vendor/tracing-attributes/tests/async_fn.rs +++ b/vendor/tracing-attributes/tests/async_fn.rs @@ -14,6 +14,7 @@ async fn test_async_fn(polls: usize) -> Result<(), ()> { // Reproduces a compile error when returning an `impl Trait` from an // instrumented async fn (see https://github.com/tokio-rs/tracing/issues/1615) +#[allow(dead_code)] // this is just here to test whether it compiles. #[instrument] async fn test_ret_impl_trait(n: i32) -> Result<impl Iterator<Item = i32>, ()> { let n = n; @@ -22,6 +23,7 @@ async fn test_ret_impl_trait(n: i32) -> Result<impl Iterator<Item = i32>, ()> { // Reproduces a compile error when returning an `impl Trait` from an // instrumented async fn (see https://github.com/tokio-rs/tracing/issues/1615) +#[allow(dead_code)] // this is just here to test whether it compiles. #[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)) @@ -30,6 +32,15 @@ async fn test_ret_impl_trait_err(n: i32) -> Result<impl Iterator<Item = i32>, &' #[instrument] async fn test_async_fn_empty() {} +// Reproduces a compile error when an instrumented function body contains inner +// attributes (https://github.com/tokio-rs/tracing/issues/2294). +#[deny(unused_variables)] +#[instrument] +async fn repro_async_2294() { + #![allow(unused_variables)] + let i = 42; +} + // Reproduces https://github.com/tokio-rs/tracing/issues/1613 #[instrument] // LOAD-BEARING `#[rustfmt::skip]`! This is necessary to reproduce the bug; @@ -53,6 +64,7 @@ async fn repro_1613_2() { } // Reproduces https://github.com/tokio-rs/tracing/issues/1831 +#[allow(dead_code)] // this is just here to test whether it compiles. #[instrument] #[deny(unused_braces)] fn repro_1831() -> Pin<Box<dyn Future<Output = ()>>> { @@ -61,6 +73,7 @@ fn repro_1831() -> Pin<Box<dyn Future<Output = ()>>> { // This replicates the pattern used to implement async trait methods on nightly using the // `type_alias_impl_trait` feature +#[allow(dead_code)] // this is just here to test whether it compiles. #[instrument(ret, err)] #[deny(unused_braces)] #[allow(clippy::manual_async_fn)] diff --git a/vendor/tracing-attributes/tests/instrument.rs b/vendor/tracing-attributes/tests/instrument.rs index 2b2fee71e..768692748 100644 --- a/vendor/tracing-attributes/tests/instrument.rs +++ b/vendor/tracing-attributes/tests/instrument.rs @@ -3,6 +3,15 @@ use tracing::Level; use tracing_attributes::instrument; use tracing_mock::*; +// Reproduces a compile error when an instrumented function body contains inner +// attributes (https://github.com/tokio-rs/tracing/issues/2294). +#[deny(unused_variables)] +#[instrument] +fn repro_2294() { + #![allow(unused_variables)] + let i = 42; +} + #[test] fn override_everything() { #[instrument(target = "my_target", level = "debug")] diff --git a/vendor/tracing-attributes/tests/ui.rs b/vendor/tracing-attributes/tests/ui.rs new file mode 100644 index 000000000..f11cc019e --- /dev/null +++ b/vendor/tracing-attributes/tests/ui.rs @@ -0,0 +1,7 @@ +// Only test on nightly, since UI tests are bound to change over time +#[rustversion::stable] +#[test] +fn async_instrument() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/async_instrument.rs"); +} diff --git a/vendor/tracing-attributes/tests/ui/async_instrument.rs b/vendor/tracing-attributes/tests/ui/async_instrument.rs new file mode 100644 index 000000000..5b245746a --- /dev/null +++ b/vendor/tracing-attributes/tests/ui/async_instrument.rs @@ -0,0 +1,46 @@ +#![allow(unreachable_code)] + +#[tracing::instrument] +async fn unit() { + "" +} + +#[tracing::instrument] +async fn simple_mismatch() -> String { + "" +} + +// FIXME: this span is still pretty poor +#[tracing::instrument] +async fn opaque_unsatisfied() -> impl std::fmt::Display { + ("",) +} + +struct Wrapper<T>(T); + +#[tracing::instrument] +async fn mismatch_with_opaque() -> Wrapper<impl std::fmt::Display> { + "" +} + +#[tracing::instrument] +async fn early_return_unit() { + if true { + return ""; + } +} + +#[tracing::instrument] +async fn early_return() -> String { + if true { + return ""; + } + String::new() +} + +#[tracing::instrument] +async fn extra_semicolon() -> i32 { + 1; +} + +fn main() {} diff --git a/vendor/tracing-attributes/tests/ui/async_instrument.stderr b/vendor/tracing-attributes/tests/ui/async_instrument.stderr new file mode 100644 index 000000000..db6f6b434 --- /dev/null +++ b/vendor/tracing-attributes/tests/ui/async_instrument.stderr @@ -0,0 +1,98 @@ +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:5:5 + | +5 | "" + | ^^ expected `()`, found `&str` + | +note: return type inferred to be `()` here + --> tests/ui/async_instrument.rs:4:10 + | +4 | async fn unit() { + | ^^^^ + +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:10:5 + | +10 | "" + | ^^- help: try using a conversion method: `.to_string()` + | | + | expected struct `String`, found `&str` + | +note: return type inferred to be `String` here + --> tests/ui/async_instrument.rs:9:31 + | +9 | async fn simple_mismatch() -> String { + | ^^^^^^ + +error[E0277]: `(&str,)` doesn't implement `std::fmt::Display` + --> tests/ui/async_instrument.rs:14:1 + | +14 | #[tracing::instrument] + | ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter + | + = help: the trait `std::fmt::Display` is not implemented for `(&str,)` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead + = note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: `(&str,)` doesn't implement `std::fmt::Display` + --> tests/ui/async_instrument.rs:15:34 + | +15 | async fn opaque_unsatisfied() -> impl std::fmt::Display { + | ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter + | + = help: the trait `std::fmt::Display` is not implemented for `(&str,)` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead + +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:23:5 + | +23 | "" + | ^^ expected struct `Wrapper`, found `&str` + | + = note: expected struct `Wrapper<_>` + found reference `&'static str` +note: return type inferred to be `Wrapper<_>` here + --> tests/ui/async_instrument.rs:22:36 + | +22 | async fn mismatch_with_opaque() -> Wrapper<impl std::fmt::Display> { + | ^^^^^^^ +help: try wrapping the expression in `Wrapper` + | +23 | Wrapper("") + | ++++++++ + + +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:29:16 + | +29 | return ""; + | ^^ expected `()`, found `&str` + | +note: return type inferred to be `()` here + --> tests/ui/async_instrument.rs:27:10 + | +27 | async fn early_return_unit() { + | ^^^^^^^^^^^^^^^^^ + +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:36:16 + | +36 | return ""; + | ^^- help: try using a conversion method: `.to_string()` + | | + | expected struct `String`, found `&str` + | +note: return type inferred to be `String` here + --> tests/ui/async_instrument.rs:34:28 + | +34 | async fn early_return() -> String { + | ^^^^^^ + +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:42:35 + | +42 | async fn extra_semicolon() -> i32 { + | ___________________________________^ +43 | | 1; + | | - help: remove this semicolon +44 | | } + | |_^ expected `i32`, found `()` diff --git a/vendor/tracing-core/.cargo-checksum.json b/vendor/tracing-core/.cargo-checksum.json index 157a61ad4..6a9cec13f 100644 --- a/vendor/tracing-core/.cargo-checksum.json +++ b/vendor/tracing-core/.cargo-checksum.json @@ -1 +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 +{"files":{"CHANGELOG.md":"93400b6ed3661b8f238549f19e55b0482da163a4504080785fefdcdc8b719198","Cargo.toml":"798de58e851a5cc16f76850f08ab381628c3858a438c31a3fa3e35c06e60992c","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"5f5e27d08d3c5ae5f8cc304fcd71ecc61f0c61b27e4bd983c89558805353479d","src/callsite.rs":"479f3b3809afff20ac2a4652f11ec1adc8dd59bd90c608e4e248de4a43c5a43c","src/dispatcher.rs":"16c0ceb3e9de5b7c1365d1c8cc010d12048569d3e9b1146b99b3ea3f0f582ba8","src/event.rs":"f2673bf5d266972e567e521c9cd92fb33f28b0c7e010937e3bc2bf9eb483087f","src/field.rs":"4ec913012ffcf05d3feba2a5ea2ba99c35e2072dabfa2db75614c0e122961b7e","src/lazy.rs":"318e3558e4446abf26294287167c0788e343044a28072f9217bd985929809087","src/lib.rs":"088b29ecce4bdb5b68df9cbe3984f4b22d7124988866d5aace0e7029ea033d58","src/metadata.rs":"1a79326aee210b9e20eb08d5baa22133390f2b1e868cf4dc72a3e9bc9a37d17f","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":"baef1454ccf5f552b0a7ba6840eeeca19f7edf21e0303607b86e9e5ffc989a38","tests/common/mod.rs":"0bbb217baa17df0f96cc1ff57dfa74ccc5a959e7f66b15bb7d25d5f43358a278","tests/dispatch.rs":"d3f000fab43734a854c82a7783142910c5e79f806cbd3f8ec5eded598c59ddb1","tests/global_dispatch.rs":"cdc05d77e448ee8b50bfb930abafa3f19b4c6f922b7bebc7797fa1dbdaa1d398","tests/macros.rs":"b1603d888b349c8d103794deceec3b1ae4538b8d3eba805f3f561899e8ad0dd2"},"package":"24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"}
\ No newline at end of file diff --git a/vendor/tracing-core/CHANGELOG.md b/vendor/tracing-core/CHANGELOG.md index c6afcb077..cf0d8e3b9 100644 --- a/vendor/tracing-core/CHANGELOG.md +++ b/vendor/tracing-core/CHANGELOG.md @@ -1,3 +1,44 @@ +# 0.1.30 (October 6, 2022) + +This release of `tracing-core` adds a new `on_register_dispatch` method to the +`Subscriber` trait to allow the `Subscriber` to perform initialization after +being registered as a `Dispatch`, and a `WeakDispatch` type to allow a +`Subscriber` to store its own `Dispatch` without creating reference count +cycles. + +### Added + +- `Subscriber::on_register_dispatch` method ([#2269]) +- `WeakDispatch` type and `Dispatch::downgrade()` function ([#2293]) + +Thanks to @jswrenn for contributing to this release! + +[#2269]: https://github.com/tokio-rs/tracing/pull/2269 +[#2293]: https://github.com/tokio-rs/tracing/pull/2293 + +# 0.1.29 (July 29, 2022) + +This release of `tracing-core` adds `PartialEq` and `Eq` implementations for +metadata types, and improves error messages when setting the global default +subscriber fails. + +### Added + +- `PartialEq` and `Eq` implementations for `Metadata` ([#2229]) +- `PartialEq` and `Eq` implementations for `FieldSet` ([#2229]) + +### Fixed + +- Fixed unhelpful `fmt::Debug` output for `dispatcher::SetGlobalDefaultError` + ([#2250]) +- Fixed compilation with `-Z minimal-versions` ([#2246]) + +Thanks to @jswrenn and @CAD97 for contributing to this release! + +[#2229]: https://github.com/tokio-rs/tracing/pull/2229 +[#2246]: https://github.com/tokio-rs/tracing/pull/2246 +[#2250]: https://github.com/tokio-rs/tracing/pull/2250 + # 0.1.28 (June 23, 2022) This release of `tracing-core` adds new `Value` implementations, including one diff --git a/vendor/tracing-core/Cargo.toml b/vendor/tracing-core/Cargo.toml index 40d797d54..806006141 100644 --- a/vendor/tracing-core/Cargo.toml +++ b/vendor/tracing-core/Cargo.toml @@ -13,7 +13,7 @@ edition = "2018" rust-version = "1.49.0" name = "tracing-core" -version = "0.1.28" +version = "0.1.30" authors = ["Tokio Contributors <team@tokio.rs>"] description = """ Core primitives for application-level tracing. @@ -47,7 +47,7 @@ rustc-args = [ ] [dependencies.once_cell] -version = "1.12" +version = "1.13.0" optional = true [features] @@ -60,7 +60,7 @@ std = ["once_cell"] [target."cfg(tracing_unstable)".dependencies.valuable] version = "0.1.0" optional = true -default_features = false +default-features = false [badges.maintenance] status = "actively-developed" diff --git a/vendor/tracing-core/README.md b/vendor/tracing-core/README.md index f3afec07e..f06c76059 100644 --- a/vendor/tracing-core/README.md +++ b/vendor/tracing-core/README.md @@ -16,9 +16,9 @@ Core primitives for application-level tracing. [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 +[crates-url]: https://crates.io/crates/tracing-core/0.1.30 [docs-badge]: https://docs.rs/tracing-core/badge.svg -[docs-url]: https://docs.rs/tracing-core/0.1.28 +[docs-url]: https://docs.rs/tracing-core/0.1.30 [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 @@ -79,22 +79,22 @@ The following crate feature flags are available: ```toml [dependencies] - tracing-core = { version = "0.1.28", default-features = false } + tracing-core = { version = "0.1.30", 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 +[`span::Id`]: https://docs.rs/tracing-core/0.1.30/tracing_core/span/struct.Id.html +[`Event`]: https://docs.rs/tracing-core/0.1.30/tracing_core/event/struct.Event.html +[`Subscriber`]: https://docs.rs/tracing-core/0.1.30/tracing_core/subscriber/trait.Subscriber.html +[`Metadata`]: https://docs.rs/tracing-core/0.1.30/tracing_core/metadata/struct.Metadata.html +[`Callsite`]: https://docs.rs/tracing-core/0.1.30/tracing_core/callsite/trait.Callsite.html +[`Field`]: https://docs.rs/tracing-core/0.1.30/tracing_core/field/struct.Field.html +[`FieldSet`]: https://docs.rs/tracing-core/0.1.30/tracing_core/field/struct.FieldSet.html +[`Value`]: https://docs.rs/tracing-core/0.1.30/tracing_core/field/trait.Value.html +[`ValueSet`]: https://docs.rs/tracing-core/0.1.30/tracing_core/field/struct.ValueSet.html +[`Dispatch`]: https://docs.rs/tracing-core/0.1.30/tracing_core/dispatcher/struct.Dispatch.html ## Supported Rust Versions diff --git a/vendor/tracing-core/src/callsite.rs b/vendor/tracing-core/src/callsite.rs index d0fe1a3d6..f88713236 100644 --- a/vendor/tracing-core/src/callsite.rs +++ b/vendor/tracing-core/src/callsite.rs @@ -137,6 +137,15 @@ pub trait Callsite: Sync { /// Returns the [metadata] associated with the callsite. /// + /// <div class="example-wrap" style="display:inline-block"> + /// <pre class="ignore" style="white-space:normal;font:inherit;"> + /// + /// **Note:** Implementations of this method should not produce [`Metadata`] + /// that share the same callsite [`Identifier`] but otherwise differ in any + /// way (e.g., have different `name`s). + /// + /// </pre></div> + /// /// [metadata]: super::metadata::Metadata fn metadata(&self) -> &Metadata<'_>; @@ -478,6 +487,7 @@ impl Callsites { pub(crate) fn register_dispatch(dispatch: &Dispatch) { let dispatchers = DISPATCHERS.register_dispatch(dispatch); + dispatch.subscriber().on_register_dispatch(dispatch); CALLSITES.rebuild_interest(dispatchers); } diff --git a/vendor/tracing-core/src/dispatcher.rs b/vendor/tracing-core/src/dispatcher.rs index 041722c36..36b3cfd85 100644 --- a/vendor/tracing-core/src/dispatcher.rs +++ b/vendor/tracing-core/src/dispatcher.rs @@ -134,7 +134,7 @@ use crate::stdlib::{ fmt, sync::{ atomic::{AtomicBool, AtomicUsize, Ordering}, - Arc, + Arc, Weak, }, }; @@ -142,16 +142,50 @@ use crate::stdlib::{ use crate::stdlib::{ cell::{Cell, RefCell, RefMut}, error, - sync::Weak, }; +#[cfg(feature = "alloc")] +use alloc::sync::{Arc, Weak}; + +#[cfg(feature = "alloc")] +use core::ops::Deref; + /// `Dispatch` trace data to a [`Subscriber`]. -/// #[derive(Clone)] pub struct Dispatch { subscriber: Arc<dyn Subscriber + Send + Sync>, } +/// `WeakDispatch` is a version of [`Dispatch`] that holds a non-owning reference +/// to a [`Subscriber`]. +/// +/// The Subscriber` may be accessed by calling [`WeakDispatch::upgrade`], +/// which returns an `Option<Dispatch>`. If all [`Dispatch`] clones that point +/// at the `Subscriber` have been dropped, [`WeakDispatch::upgrade`] will return +/// `None`. Otherwise, it will return `Some(Dispatch)`. +/// +/// A `WeakDispatch` may be created from a [`Dispatch`] by calling the +/// [`Dispatch::downgrade`] method. The primary use for creating a +/// [`WeakDispatch`] is to allow a Subscriber` to hold a cyclical reference to +/// itself without creating a memory leak. See [here] for details. +/// +/// This type is analogous to the [`std::sync::Weak`] type, but for a +/// [`Dispatch`] rather than an [`Arc`]. +/// +/// [`Arc`]: std::sync::Arc +/// [here]: Subscriber#avoiding-memory-leaks +#[derive(Clone)] +pub struct WeakDispatch { + subscriber: Weak<dyn Subscriber + Send + Sync>, +} + +#[cfg(feature = "alloc")] +#[derive(Clone)] +enum Kind<T> { + Global(&'static (dyn Collect + Send + Sync)), + Scoped(T), +} + #[cfg(feature = "std")] thread_local! { static CURRENT_STATE: State = State { @@ -293,14 +327,21 @@ pub fn has_been_set() -> bool { } /// Returned if setting the global dispatcher fails. -#[derive(Debug)] pub struct SetGlobalDefaultError { _no_construct: (), } +impl fmt::Debug for SetGlobalDefaultError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("SetGlobalDefaultError") + .field(&Self::MESSAGE) + .finish() + } +} + 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") + f.pad(Self::MESSAGE) } } @@ -308,6 +349,10 @@ impl fmt::Display for SetGlobalDefaultError { #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl error::Error for SetGlobalDefaultError {} +impl SetGlobalDefaultError { + const MESSAGE: &'static str = "a global default trace dispatcher has already been set"; +} + /// 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 @@ -419,8 +464,34 @@ impl Dispatch { 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. + /// Creates a [`WeakDispatch`] from this `Dispatch`. + /// + /// A [`WeakDispatch`] is similar to a [`Dispatch`], but it does not prevent + /// the underlying [`Subscriber`] from being dropped. Instead, it only permits + /// access while other references to the `Subscriber` exist. This is equivalent + /// to the standard library's [`Arc::downgrade`] method, but for `Dispatch` + /// rather than `Arc`. + /// + /// The primary use for creating a [`WeakDispatch`] is to allow a `Subscriber` + /// to hold a cyclical reference to itself without creating a memory leak. + /// See [here] for details. + /// + /// [`Arc::downgrade`]: std::sync::Arc::downgrade + /// [here]: Subscriber#avoiding-memory-leaks + pub fn downgrade(&self) -> WeakDispatch { + WeakDispatch { + subscriber: Arc::downgrade(&self.subscriber), + } + } + + #[inline(always)] + #[cfg(not(feature = "alloc"))] + pub(crate) fn subscriber(&self) -> &(dyn Subscriber + Send + Sync) { + &self.subscriber + } + + /// Registers a new callsite with this collector, returning whether or not + /// the collector is interested in being notified about the callsite. /// /// This calls the [`register_callsite`] function on the [`Subscriber`] /// that this `Dispatch` forwards to. @@ -620,14 +691,14 @@ impl Dispatch { /// `T`. #[inline] pub fn is<T: Any>(&self) -> bool { - <dyn Subscriber>::is::<T>(&*self.subscriber) + <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) + <dyn Subscriber>::downcast_ref(&self.subscriber) } } @@ -656,6 +727,45 @@ where } } +// === impl WeakDispatch === + +impl WeakDispatch { + /// Attempts to upgrade this `WeakDispatch` to a [`Dispatch`]. + /// + /// Returns `None` if the referenced `Dispatch` has already been dropped. + /// + /// ## Examples + /// + /// ``` + /// # use tracing_core::subscriber::NoSubscriber; + /// # use tracing_core::dispatcher::Dispatch; + /// let strong = Dispatch::new(NoSubscriber::default()); + /// let weak = strong.downgrade(); + /// + /// // The strong here keeps it alive, so we can still access the object. + /// assert!(weak.upgrade().is_some()); + /// + /// drop(strong); // But not any more. + /// assert!(weak.upgrade().is_none()); + /// ``` + pub fn upgrade(&self) -> Option<Dispatch> { + self.subscriber + .upgrade() + .map(|subscriber| Dispatch { subscriber }) + } +} + +impl fmt::Debug for WeakDispatch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut tuple = f.debug_tuple("WeakDispatch"); + match self.subscriber.upgrade() { + Some(subscriber) => tuple.field(&format_args!("Some({:p})", subscriber)), + None => tuple.field(&format_args!("None")), + }; + tuple.finish() + } +} + #[cfg(feature = "std")] impl Registrar { pub(crate) fn upgrade(&self) -> Option<Dispatch> { diff --git a/vendor/tracing-core/src/field.rs b/vendor/tracing-core/src/field.rs index c9869ce2c..e103c75a9 100644 --- a/vendor/tracing-core/src/field.rs +++ b/vendor/tracing-core/src/field.rs @@ -145,6 +145,16 @@ pub struct Field { pub struct Empty; /// Describes the fields present on a span. +/// +/// ## Equality +/// +/// In well-behaved applications, two `FieldSet`s [initialized] with equal +/// [callsite identifiers] will have identical fields. Consequently, in release +/// builds, [`FieldSet::eq`] *only* checks that its arguments have equal +/// callsites. However, the equality of field names is checked in debug builds. +/// +/// [initialized]: Self::new +/// [callsite identifiers]: callsite::Identifier pub struct FieldSet { /// The names of each field on the described span. names: &'static [&'static str], @@ -911,6 +921,40 @@ impl fmt::Display for FieldSet { } } +impl Eq for FieldSet {} + +impl PartialEq for FieldSet { + fn eq(&self, other: &Self) -> bool { + if core::ptr::eq(&self, &other) { + true + } else if cfg!(not(debug_assertions)) { + // In a well-behaving application, two `FieldSet`s can be assumed to + // be totally equal so long as they share the same callsite. + self.callsite == other.callsite + } else { + // However, when debug-assertions are enabled, do NOT assume that + // the application is well-behaving; check every the field names of + // each `FieldSet` for equality. + + // `FieldSet` is destructured here to ensure a compile-error if the + // fields of `FieldSet` change. + let Self { + names: lhs_names, + callsite: lhs_callsite, + } = self; + + let Self { + names: rhs_names, + callsite: rhs_callsite, + } = &other; + + // Check callsite equality first, as it is probably cheaper to do + // than str equality. + lhs_callsite == rhs_callsite && lhs_names == rhs_names + } + } +} + // ===== impl Iter ===== impl Iterator for Iter { diff --git a/vendor/tracing-core/src/metadata.rs b/vendor/tracing-core/src/metadata.rs index 47b9388a4..a154419a7 100644 --- a/vendor/tracing-core/src/metadata.rs +++ b/vendor/tracing-core/src/metadata.rs @@ -35,28 +35,25 @@ use crate::stdlib::{ /// _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> +/// ## Equality +/// +/// In well-behaved applications, two `Metadata` with equal +/// [callsite identifiers] will be equal in all other ways (i.e., have the same +/// `name`, `target`, etc.). Consequently, in release builds, [`Metadata::eq`] +/// *only* checks that its arguments have equal callsites. However, the equality +/// of `Metadata`'s other fields is checked in debug builds. /// /// [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() +/// [name]: Self::name +/// [target]: Self::target +/// [fields]: Self::fields +/// [verbosity level]: Self::level +/// [file name]: Self::file +/// [line number]: Self::line +/// [module path]: Self::module_path /// [`Subscriber`]: super::subscriber::Subscriber -/// [`id`]: Metadata::id -/// [callsite identifier]: super::callsite::Identifier +/// [callsite identifiers]: Self::callsite pub struct Metadata<'a> { /// The name of the span described by this metadata. name: &'static str, @@ -443,6 +440,62 @@ impl fmt::Debug for Kind { } } +impl<'a> Eq for Metadata<'a> {} + +impl<'a> PartialEq for Metadata<'a> { + #[inline] + fn eq(&self, other: &Self) -> bool { + if core::ptr::eq(&self, &other) { + true + } else if cfg!(not(debug_assertions)) { + // In a well-behaving application, two `Metadata` can be assumed to + // be totally equal so long as they share the same callsite. + self.callsite() == other.callsite() + } else { + // However, when debug-assertions are enabled, do not assume that + // the application is well-behaving; check every field of `Metadata` + // for equality. + + // `Metadata` is destructured here to ensure a compile-error if the + // fields of `Metadata` change. + let Metadata { + name: lhs_name, + target: lhs_target, + level: lhs_level, + module_path: lhs_module_path, + file: lhs_file, + line: lhs_line, + fields: lhs_fields, + kind: lhs_kind, + } = self; + + let Metadata { + name: rhs_name, + target: rhs_target, + level: rhs_level, + module_path: rhs_module_path, + file: rhs_file, + line: rhs_line, + fields: rhs_fields, + kind: rhs_kind, + } = &other; + + // The initial comparison of callsites is purely an optimization; + // it can be removed without affecting the overall semantics of the + // expression. + self.callsite() == other.callsite() + && lhs_name == rhs_name + && lhs_target == rhs_target + && lhs_level == rhs_level + && lhs_module_path == rhs_module_path + && lhs_file == rhs_file + && lhs_line == rhs_line + && lhs_fields == rhs_fields + && lhs_kind == rhs_kind + } + } +} + // ===== impl Level ===== impl Level { diff --git a/vendor/tracing-core/src/subscriber.rs b/vendor/tracing-core/src/subscriber.rs index a6f0834e2..e8f444119 100644 --- a/vendor/tracing-core/src/subscriber.rs +++ b/vendor/tracing-core/src/subscriber.rs @@ -1,5 +1,5 @@ -//! Subscribers collect and record trace data. -use crate::{span, Event, LevelFilter, Metadata}; +//! Collectors collect and record trace data. +use crate::{span, Dispatch, Event, LevelFilter, Metadata}; use crate::stdlib::{ any::{Any, TypeId}, @@ -81,7 +81,28 @@ use crate::stdlib::{ /// [`event`]: Subscriber::event /// [`event_enabled`]: Subscriber::event_enabled pub trait Subscriber: 'static { - // === Span registry methods ============================================== + /// Invoked when this subscriber becomes a [`Dispatch`]. + /// + /// ## Avoiding Memory Leaks + /// + /// `Subscriber`s should not store their own [`Dispatch`]. Because the + /// `Dispatch` owns the `Subscriber`, storing the `Dispatch` within the + /// `Subscriber` will create a reference count cycle, preventing the `Dispatch` + /// from ever being dropped. + /// + /// Instead, when it is necessary to store a cyclical reference to the + /// `Dispatch` within a `Subscriber`, use [`Dispatch::downgrade`] to convert a + /// `Dispatch` into a [`WeakDispatch`]. This type is analogous to + /// [`std::sync::Weak`], and does not create a reference count cycle. A + /// [`WeakDispatch`] can be stored within a `Subscriber` without causing a + /// memory leak, and can be [upgraded] into a `Dispatch` temporarily when + /// the `Dispatch` must be accessed by the `Subscriber`. + /// + /// [`WeakDispatch`]: crate::dispatcher::WeakDispatch + /// [upgraded]: crate::dispatcher::WeakDispatch::upgrade + fn on_register_dispatch(&self, subscriber: &Dispatch) { + let _ = subscriber; + } /// Registers a new [callsite] with this subscriber, returning whether or not /// the subscriber is interested in being notified about the callsite. diff --git a/vendor/tracing-subscriber-0.3.3/.cargo-checksum.json b/vendor/tracing-subscriber-0.3.3/.cargo-checksum.json new file mode 100644 index 000000000..3fd14355e --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"fd7a732ca8d5997cf09a6ffbde0dda0667873031884aa3549d6044971dc00a66","Cargo.toml":"4908f04be83f94bc75fbcf4244fa57b8a52c984928a3320550b692c1038d5448","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"803714740b5ad75ac31a0f9dafd10e0d4d62f7c27c2c0e182d2076a313b0649b","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":"e6b2dcbf9cb1e9b5e862b462f91190adaf8e14f9c2c5d2048ad651f49cfa2007","src/field/display.rs":"9c06a52919dbe9bfd4cf7eec39293240c9facebe052a2fecc2f21184beb5195f","src/field/mod.rs":"35e3dd4ad7b49c99a24e80c6a40a00b3189a114488793657d6d733a90d2e10f6","src/filter/directive.rs":"7ecf87b17afbddadbc385764c2d9c1fda4b020a08d75f741f8e34c7dc475bd74","src/filter/env/directive.rs":"628f9f566ccee924d43d79b287c569abfc32c2fb74e078958aed6cb8285cfb4f","src/filter/env/field.rs":"9f2ceaedf2e2ecefaff863347ef8dfa85cd5f64a0fd09a0f77f64f412c9bb548","src/filter/env/mod.rs":"7f864a5ef0c008c7fe9a21d0a94bb87dd72746e628ff3e68c18b0fd173763918","src/filter/filter_fn.rs":"0debbc4a4b4d2a57b2a06905017ac908bf34b0a64aaf961535fbf6f4d5a700a9","src/filter/layer_filters.rs":"16ff19fed003b913de4f85a03b31864d71ee73c7ce86b07c80da07fe633f682e","src/filter/level.rs":"cc449757aac47caaf19dd5ba4d74c8efbcd7531fcd6c13da0c5f6fdda12cc9ca","src/filter/mod.rs":"8ebfd0dc92415ff27ec552f20919e598842a87186f13f120449053a96e1e3307","src/filter/targets.rs":"5cec882366d7f12de0a88f7daaac8499785ce9e3832619f251876a02ae19a6bf","src/fmt/fmt_layer.rs":"486264d810ab6b28428bd48e00774fc822762fc9f991332a2d94a42b3168f2e6","src/fmt/format/json.rs":"1a38c049e1bf99efaf7db1f1fd26d3a5bb1e768fc1524c95816708e5d39fca35","src/fmt/format/mod.rs":"ff92f7910bed4f3c0c2ce798333d273d6b5b5fc09cc78031f52d13e043986227","src/fmt/format/pretty.rs":"eecf278b15cc60b35c3f6aa5d05452401c7a4a29195357e92318d684fcfe3072","src/fmt/mod.rs":"270ffa0a9e6543a602247fccef276a3012daa558181f24cd9032292edbc8dc2c","src/fmt/time/datetime.rs":"778d4604d800e46b940087394e7b72750738b554e02aea523fa9820ab0768c08","src/fmt/time/mod.rs":"304c9383e5cfc0c42d79f47a10323ed5a98585e018b127924b0925ec067f0739","src/fmt/time/time_crate.rs":"bacec2c8bb31175b85f2fb3ae40155a08b2aab7a04adaf4c88679147dc651c58","src/fmt/writer.rs":"1d7a4e2dddddff1bfd1344f2cecdb6b2b69b015c9869d95987d26e177dffa793","src/layer/context.rs":"2478693e2faffdf2e519b6d37e1c3aa3dd75088185accc2b68ffa8612bf73195","src/layer/layered.rs":"ba918a9b944f2c083cbb75d6d7f99f90083aa0a29cf3f4f1dd78aa034e09ade6","src/layer/mod.rs":"e1804cfe91051020cac63fb1067d196552ebb844b6c6d1d2279b97dbec1c64df","src/layer/tests.rs":"3e974d627c4bc4269cfa10c82c890e596c9d46af8e6bc03c6c117bde1237e948","src/lib.rs":"cb362279b3c6d23645cda6d768707576f460e275d332d5350a036437cd5404e0","src/macros.rs":"e184bffc6b5999c48e365ad08343dca764a5fb711b789beb26bd1d5f1d767726","src/prelude.rs":"088635def33be9a4c4b6ed934dc22540c555e27d62f7625a43aa9c0e525ca467","src/registry/extensions.rs":"7333aefd69c767212a7924c57283442430edccb17092c91e02a7d13b2d312b11","src/registry/mod.rs":"4f0108e75e0f6e239b8eb69fcad052f25e3b887e412e951e0cbec02cf13f05d5","src/registry/sharded.rs":"972bdd94f43a33ef1f2ebf96ea69ebe4c1d4b0215e69315a3b525783c2025696","src/registry/stack.rs":"9ef333d6a8a28a064e80ff1e376dbb07bc597009010ec332b2dc3ab435d737c2","src/reload.rs":"41fa9a1a28fef626e302a80a68d665492e73ef6d1a2a3c2a7aac5d6c9a0bb496","src/sync.rs":"7f78f3de5b618a999be0e61f936a233975e7769f1ebb55a0e48c3d199e9c45e3","src/util.rs":"55b4e9d63112f9d5a12a287273a9b1212741058384332d3edc024168cacfd627","tests/cached_layer_filters_dont_break_other_layers.rs":"b2084542a014abeff821b30b2b8c21e32bfdcffae53ce5335fb588f557fa4244","tests/duplicate_spans.rs":"48f596bbfabcc6618244afddcf3c3f2e915b9d79284f17bdd0e0616ad29929be","tests/field_filter.rs":"c44d88ab711164a2b1b3a09377284b469f79ddf4651416515a035782c7c64b79","tests/filter.rs":"a43d23e867af779031b6245047092aca57ee26980a8f3faa19036542bcd37f06","tests/filter_log.rs":"e0cd9d394dbfeeb80570a7686bc7f588c5489657980436810711ed8852f86169","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/downcast_raw.rs":"9b90ead571543cbe14e89b4fe637360d9baf3069f6f656ed3bdf65e7318648f1","tests/layer_filters/filter_scopes.rs":"02611bc58d0d8a67a127eca8cab1b2d9a9901bd2c8a8daad41adf6089b28aee0","tests/layer_filters/main.rs":"0316d611c740e234b78ed9a9dae392fe80472c1e8b004a007ad2dd87d068c67b","tests/layer_filters/targets.rs":"138e3f9ddd68571d94c5aff9d54ee2fbc5f44724c6ee42477a411740ccb79ee6","tests/layer_filters/trees.rs":"4df7b5cf12da44a9255c56e5b80e2b0cf84820230ba916f324c67bc3ee4e4605","tests/multiple_layer_filter_interests_cached.rs":"1ea195f03e58d715228ec1b604f85bda2fc82812d05b2f6370d5edd34a035f32","tests/registry_max_level_hint.rs":"ba386d32b8d13832d7009163241c3d0723488c0393d85647eb9368776251e4fc","tests/registry_with_subscriber.rs":"13b92ed68d9013aefefbc4c73e695c690630e4460634206d214db4c19abb7c0f","tests/reload.rs":"4566386b1b26e6609f5a4bf0e6bef1c2245a591d12417cee189b26dfa14f7f95","tests/same_len_filters.rs":"50c8f5fa1494773410a9f52a56b303534a01a023b186cf2f3131e5e7706eb156","tests/support.rs":"75559505af8018012739d24b3c8743dd079b4d3a8ae28f08b4586a961720aa7b","tests/unhinted_layer_filters_dont_break_other_layers.rs":"519cfef4977e511af938546d4208c645a28248c8ed8666daf180f0ad32f0a261","tests/utils.rs":"2b04ce2d8b56a9062a025900104853e081eae8e3f113f990a915d5f9dea6577b"},"package":"245da694cc7fc4729f3f418b304cb57789f1bed2a78c575407ab8a23f53cb4d3"}
\ No newline at end of file diff --git a/vendor/tracing-subscriber-0.3.3/CHANGELOG.md b/vendor/tracing-subscriber-0.3.3/CHANGELOG.md new file mode 100644 index 000000000..c380ff3b1 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/CHANGELOG.md @@ -0,0 +1,902 @@ +# 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-0.3.3/Cargo.toml b/vendor/tracing-subscriber-0.3.3/Cargo.toml new file mode 100644 index 000000000..b5e7ba7db --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/Cargo.toml @@ -0,0 +1,150 @@ +# 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.42.0" +name = "tracing-subscriber" +version = "0.3.3" +authors = ["Eliza Weisman <eliza@buoyant.io>", "David Barsky <me@davidbarsky.com>", "Tokio Contributors <team@tokio.rs>"] +description = "Utilities for implementing and composing `tracing` subscribers.\n" +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.lazy_static] +version = "1" +optional = true + +[dependencies.matchers] +version = "0.1.0" +optional = true + +[dependencies.parking_lot] +version = ">= 0.7, <= 0.11" +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" +optional = true + +[dependencies.thread_local] +version = "1.0.1" +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.20" + +[dependencies.tracing-log] +version = "0.1.2" +features = ["log-tracer", "std"] +optional = true +default-features = false + +[dependencies.tracing-serde] +version = "0.1.2" +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 = "0.2" +features = ["rt-core", "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", "lazy_static", "tracing", "std"] +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"] +[badges.maintenance] +status = "experimental" diff --git a/vendor/tracing-subscriber/LICENSE b/vendor/tracing-subscriber-0.3.3/LICENSE index cdb28b4b5..cdb28b4b5 100644 --- a/vendor/tracing-subscriber/LICENSE +++ b/vendor/tracing-subscriber-0.3.3/LICENSE diff --git a/vendor/tracing-subscriber-0.3.3/README.md b/vendor/tracing-subscriber-0.3.3/README.md new file mode 100644 index 000000000..75c62e8ca --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/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.1 +[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.42+][msrv]* + +[msrv]: #supported-rust-versions + +## 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. + +## 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-0.3.3/benches/enter.rs index 49c6e730a..49c6e730a 100644 --- a/vendor/tracing-subscriber/benches/enter.rs +++ b/vendor/tracing-subscriber-0.3.3/benches/enter.rs diff --git a/vendor/tracing-subscriber/benches/filter.rs b/vendor/tracing-subscriber-0.3.3/benches/filter.rs index 91ab9c91d..91ab9c91d 100644 --- a/vendor/tracing-subscriber/benches/filter.rs +++ b/vendor/tracing-subscriber-0.3.3/benches/filter.rs diff --git a/vendor/tracing-subscriber/benches/filter_log.rs b/vendor/tracing-subscriber-0.3.3/benches/filter_log.rs index 4dcf3b4ec..4dcf3b4ec 100644 --- a/vendor/tracing-subscriber/benches/filter_log.rs +++ b/vendor/tracing-subscriber-0.3.3/benches/filter_log.rs diff --git a/vendor/tracing-subscriber/benches/fmt.rs b/vendor/tracing-subscriber-0.3.3/benches/fmt.rs index a039e66d4..a039e66d4 100644 --- a/vendor/tracing-subscriber/benches/fmt.rs +++ b/vendor/tracing-subscriber-0.3.3/benches/fmt.rs diff --git a/vendor/tracing-subscriber/benches/support/mod.rs b/vendor/tracing-subscriber-0.3.3/benches/support/mod.rs index 25e9e7e22..25e9e7e22 100644 --- a/vendor/tracing-subscriber/benches/support/mod.rs +++ b/vendor/tracing-subscriber-0.3.3/benches/support/mod.rs diff --git a/vendor/tracing-subscriber/src/field/debug.rs b/vendor/tracing-subscriber-0.3.3/src/field/debug.rs index cc67d29fe..cc67d29fe 100644 --- a/vendor/tracing-subscriber/src/field/debug.rs +++ b/vendor/tracing-subscriber-0.3.3/src/field/debug.rs diff --git a/vendor/tracing-subscriber/src/field/delimited.rs b/vendor/tracing-subscriber-0.3.3/src/field/delimited.rs index 98634cea9..8c78c4b20 100644 --- a/vendor/tracing-subscriber/src/field/delimited.rs +++ b/vendor/tracing-subscriber-0.3.3/src/field/delimited.rs @@ -41,7 +41,7 @@ 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 + /// [`MakeVisitor`]: ../trait.MakeVisitor.html pub fn new(delimiter: D, inner: V) -> Self { Self { delimiter, inner } } @@ -53,7 +53,7 @@ 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 + /// [`Visit`]: https://docs.rs/tracing-core/0.1.6/tracing_core/field/trait.Visit.html pub fn new(delimiter: D, inner: V) -> Self { Self { delimiter, diff --git a/vendor/tracing-subscriber/src/field/display.rs b/vendor/tracing-subscriber-0.3.3/src/field/display.rs index 78a039ce1..e0bbc55ed 100644 --- a/vendor/tracing-subscriber/src/field/display.rs +++ b/vendor/tracing-subscriber-0.3.3/src/field/display.rs @@ -18,7 +18,7 @@ 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 + /// [`MakeVisitor`]: ../trait.MakeVisitor.html pub fn new(inner: V) -> Self { Messages(inner) } diff --git a/vendor/tracing-subscriber-0.3.3/src/field/mod.rs b/vendor/tracing-subscriber-0.3.3/src/field/mod.rs new file mode 100644 index 000000000..f7d03f2cc --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/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]: https://docs.rs/tracing-core/latest/tracing_core/field/trait.Visit.html +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]: https://docs.rs/tracing-core/latest/tracing_core/field/trait.Visit.html +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]: https://docs.rs/tracing-core/latest/tracing_core/field/trait.Visit.html +/// [attr]: https://docs.rs/tracing-core/latest/tracing_core/span/struct.Attributes.html +/// [rec]: https://docs.rs/tracing-core/latest/tracing_core/span/struct.Record.html +/// [event]: https://docs.rs/tracing-core/latest/tracing_core/event/struct.Event.html +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!(&mut 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-0.3.3/src/filter/directive.rs b/vendor/tracing-subscriber-0.3.3/src/filter/directive.rs new file mode 100644 index 000000000..dd6b063c4 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/filter/directive.rs @@ -0,0 +1,424 @@ +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::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, + } + } +} + +// === 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, + } + } +} + +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-0.3.3/src/filter/env/directive.rs b/vendor/tracing-subscriber-0.3.3/src/filter/env/directive.rs new file mode 100644 index 000000000..66ca23dc4 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/filter/env/directive.rs @@ -0,0 +1,846 @@ +pub(crate) use crate::filter::directive::{FilterVec, ParseError, StaticDirective}; +use crate::filter::{ + directive::{DirectiveSet, Match}, + env::{field, FieldMap}, + level::LevelFilter, +}; +use lazy_static::lazy_static; +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(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) + } +} + +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 fields = meta.fields(); + for field in &self.fields { + if fields.field(&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> { + lazy_static! { + static ref DIRECTIVE_RE: Regex = 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 ref SPAN_PART_RE: Regex = + Regex::new(r#"(?P<name>[^\]\{]+)?(?:\{(?P<fields>[^\}]*)\})?"#).unwrap(); + static ref FIELD_FILTER_RE: Regex = + // TODO(eliza): this doesn't _currently_ handle value matchers that include comma + // characters. We should fix that. + 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| c.as_str().parse()) + .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(Directive { + level, + target, + in_span, + fields: fields?, + }) + } +} + +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-0.3.3/src/filter/env/field.rs b/vendor/tracing-subscriber-0.3.3/src/filter/env/field.rs new file mode 100644 index 000000000..970850f92 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/filter/env/field.rs @@ -0,0 +1,416 @@ +use matchers::Pattern; +use std::{ + cmp::Ordering, + error::Error, + fmt, + str::FromStr, + sync::{ + atomic::{AtomicBool, Ordering::*}, + Arc, + }, +}; + +use super::{FieldMap, LevelFilter}; +use tracing_core::field::{Field, Visit}; + +#[derive(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 { + Bool(bool), + F64(f64), + U64(u64), + I64(i64), + NaN, + 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, + } + } +} + +impl PartialOrd for ValueMatch { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +#[derive(Debug, Clone)] +pub(crate) struct MatchPattern { + pub(crate) matcher: Pattern, + 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 FromStr for Match { + type Err = Box<dyn Error + Send + Sync>; + fn from_str(s: &str) -> Result<Self, Self::Err> { + 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(ValueMatch::from_str).transpose()?; + Ok(Match { name, value }) + } +} + +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() + } +} + +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 FromStr for ValueMatch { + type Err = matchers::Error; + fn from_str(s: &str) -> Result<Self, Self::Err> { + 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))) + }) + } +} + +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::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) + } +} + +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 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); + } + _ => {} + } + } + + 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); + } + _ => {} + } + } +} diff --git a/vendor/tracing-subscriber-0.3.3/src/filter/env/mod.rs b/vendor/tracing-subscriber-0.3.3/src/filter/env/mod.rs new file mode 100644 index 000000000..81fe0e62d --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/filter/env/mod.rs @@ -0,0 +1,738 @@ +//! 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::{directive::Directive, field::BadName as BadFieldName}; +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 tracing_core::{ + callsite, + field::Field, + span, + subscriber::{Interest, Subscriber}, + Metadata, +}; + +/// A [`Layer`] which filters spans and events based on a set of filter +/// directives. +/// +/// # 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 acts as a regex on +/// the `std::fmt::Debug` output from the value. +/// - `level` sets a maximum verbosity level accepted by this directive. +/// +/// ## 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", ...);` +/// +/// ## Examples +/// +/// - `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. +/// +/// 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`]. +/// +/// [`Span`]: tracing_core::span +/// [fields]: tracing_core::Field +/// [`Event`]: tracing_core::Event +/// [`level`]: tracing_core::Level +/// [`Metadata`]: tracing_core::Metadata +/// [`Targets`]: crate::filter::Targets +#[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>>, +} + +thread_local! { + static SCOPE: RefCell<Vec<LevelFilter>> = RefCell::new(Vec::new()); +} + +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`]: #method.from_default_env + /// [`EnvFilter::try_from_default_env`]: #method.try_from_default_env + pub const DEFAULT_ENV: &'static str = "RUST_LOG"; + + /// Returns a new `EnvFilter` from the value of the `RUST_LOG` environment + /// variable, ignoring any invalid filter directives. + pub fn from_default_env() -> Self { + Self::from_env(Self::DEFAULT_ENV) + } + + /// Returns a new `EnvFilter` from the value of the given environment + /// variable, ignoring any invalid filter directives. + pub fn from_env<A: AsRef<str>>(env: A) -> Self { + env::var(env.as_ref()).map(Self::new).unwrap_or_default() + } + + /// Returns a new `EnvFilter` from the directives in the given string, + /// ignoring any that are invalid. + pub fn new<S: AsRef<str>>(dirs: S) -> Self { + let directives = dirs.as_ref().split(',').filter_map(|s| match s.parse() { + 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 try_new<S: AsRef<str>>(dirs: S) -> Result<Self, directive::ParseError> { + let directives = dirs + .as_ref() + .split(',') + .map(|s| s.parse()) + .collect::<Result<Vec<_>, _>>()?; + Ok(Self::from_directives(directives)) + } + + /// Returns a new `EnvFilter` from the value of the `RUST_LOG` environment + /// variable, or an error if the environment variable contains any invalid + /// filter directives. + pub fn try_from_default_env() -> Result<Self, FromEnvError> { + Self::try_from_env(Self::DEFAULT_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. + pub fn try_from_env<A: AsRef<str>>(env: A) -> Result<Self, FromEnvError> { + env::var(env.as_ref())?.parse().map_err(Into::into) + } + + /// 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`]: ../filter/struct.LevelFilter.html + /// [`Level`]: https://docs.rs/tracing-core/latest/tracing_core/struct.Level.html + /// + /// # 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(()) + /// # } + /// ``` + pub fn add_directive(mut self, directive: Directive) -> Self { + if let Some(stat) = directive.to_static() { + self.statics.add(stat) + } else { + self.has_dynamics = true; + self.dynamics.add(directive); + } + self + } + + fn from_directives(directives: impl IntoIterator<Item = Directive>) -> Self { + use tracing::level_filters::STATIC_MAX_LEVEL; + use tracing::Level; + + let directives: Vec<_> = directives.into_iter().collect(); + + let disabled: Vec<_> = directives + .iter() + .filter(|directive| directive.level > STATIC_MAX_LEVEL) + .collect(); + + 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 subscriber, 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!("note: {}", 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, mut statics) = Directive::make_tables(directives); + let has_dynamics = !dynamics.is_empty(); + + if statics.is_empty() && !has_dynamics { + statics.add(directive::StaticDirective::default()); + } + + Self { + statics, + dynamics, + has_dynamics, + by_id: RwLock::new(HashMap::new()), + by_cs: RwLock::new(HashMap::new()), + } + } + + 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() + } + } +} + +impl<S: Subscriber> Layer<S> for EnvFilter { + 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() + } + } + + 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(), + ) + } + + fn enabled(&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 local 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 = SCOPE.with(|scope| { + for filter in scope.borrow().iter() { + 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 + } + + fn on_new_span(&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); + } + } + + fn on_record(&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 on_enter(&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) { + SCOPE.with(|scope| scope.borrow_mut().push(span.level())); + } + } + + fn on_exit(&self, id: &span::Id, _: Context<'_, S>) { + if self.cares_about_span(id) { + SCOPE.with(|scope| scope.borrow_mut().pop()); + } + } + + fn on_close(&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); + } +} + +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 { + Self::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", + ); + } +} diff --git a/vendor/tracing-subscriber/src/filter/filter_fn.rs b/vendor/tracing-subscriber-0.3.3/src/filter/filter_fn.rs index 332bf860a..332bf860a 100644 --- a/vendor/tracing-subscriber/src/filter/filter_fn.rs +++ b/vendor/tracing-subscriber-0.3.3/src/filter/filter_fn.rs diff --git a/vendor/tracing-subscriber-0.3.3/src/filter/layer_filters.rs b/vendor/tracing-subscriber-0.3.3/src/filter/layer_filters.rs new file mode 100644 index 000000000..e77fd3751 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/filter/layer_filters.rs @@ -0,0 +1,830 @@ +//! ## 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, + sync::Arc, + thread_local, +}; +use tracing_core::{ + span, + subscriber::{Interest, Subscriber}, + Event, Metadata, +}; + +/// 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(); +} + +// === 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) + } +} + +impl<S> layer::Filter<S> for Arc<dyn layer::Filter<S> + Send + Sync + 'static> { + #[inline] + fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + (**self).enabled(meta, cx) + } + + #[inline] + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + (**self).callsite_enabled(meta) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + (**self).max_level_hint() + } +} + +impl<S> layer::Filter<S> for Box<dyn layer::Filter<S> + Send + Sync + 'static> { + #[inline] + fn enabled(&self, meta: &Metadata<'_>, cx: &Context<'_, S>) -> bool { + (**self).enabled(meta, cx) + } + + #[inline] + fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { + (**self).callsite_enabled(meta) + } + + #[inline] + fn max_level_hint(&self) -> Option<LevelFilter> { + (**self).max_level_hint() + } +} + +// === 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)) + } +} + +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(|| { + self.layer.on_new_span(attrs, id, cx.with_filter(self.id())); + }) + } + + #[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.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.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.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.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 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-0.3.3/src/filter/level.rs index 0fa601260..0fa601260 100644 --- a/vendor/tracing-subscriber/src/filter/level.rs +++ b/vendor/tracing-subscriber-0.3.3/src/filter/level.rs diff --git a/vendor/tracing-subscriber/src/filter/mod.rs b/vendor/tracing-subscriber-0.3.3/src/filter/mod.rs index 000a27195..000a27195 100644 --- a/vendor/tracing-subscriber/src/filter/mod.rs +++ b/vendor/tracing-subscriber-0.3.3/src/filter/mod.rs diff --git a/vendor/tracing-subscriber/src/filter/targets.rs b/vendor/tracing-subscriber-0.3.3/src/filter/targets.rs index 2a30d2db6..e0c7fcf82 100644 --- a/vendor/tracing-subscriber/src/filter/targets.rs +++ b/vendor/tracing-subscriber-0.3.3/src/filter/targets.rs @@ -20,7 +20,7 @@ use core::{ slice, str::FromStr, }; -use tracing_core::{Interest, Level, Metadata, Subscriber}; +use tracing_core::{Interest, Metadata, Subscriber}; /// A filter that enables or disables spans and events based on their [target] /// and [level]. @@ -111,7 +111,7 @@ use tracing_core::{Interest, Level, Metadata, Subscriber}; /// by the user at runtime. /// /// The `Targets` filter can be used as a [per-layer filter][plf] *and* as a -/// [global filter][global]: +/// [global filter]: /// /// ```rust /// use tracing_subscriber::{ @@ -313,35 +313,6 @@ impl Targets { 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 diff --git a/vendor/tracing-subscriber/src/fmt/fmt_layer.rs b/vendor/tracing-subscriber-0.3.3/src/fmt/fmt_layer.rs index 21992e780..0e0d5e0eb 100644 --- a/vendor/tracing-subscriber/src/fmt/fmt_layer.rs +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/fmt_layer.rs @@ -56,7 +56,7 @@ use tracing_core::{ /// # tracing::subscriber::set_global_default(subscriber).unwrap(); /// ``` /// -/// [`Layer`]: super::layer::Layer +/// [`Layer`]: ../layer/trait.Layer.html #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] #[derive(Debug)] pub struct Layer< @@ -70,11 +70,11 @@ pub struct Layer< fmt_event: E, fmt_span: format::FmtSpanConfig, is_ansi: bool, - _inner: PhantomData<fn(S)>, + _inner: PhantomData<S>, } impl<S> Layer<S> { - /// Returns a new [`Layer`][self::Layer] with the default configuration. + /// Returns a new [`Layer`](struct.Layer.html) with the default configuration. pub fn new() -> Self { Self::default() } @@ -87,8 +87,8 @@ where 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. + /// Sets the [event formatter][`FormatEvent`] that the layer 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 @@ -108,7 +108,7 @@ where /// ``` /// [`FormatEvent`]: format::FormatEvent /// [`Event`]: tracing::Event - /// [`Writer`]: format::Writer + /// [`Writer`]: crate::format::Writer pub fn event_format<E2>(self, e: E2) -> Layer<S, N, E2, W> where E2: FormatEvent<S, N> + 'static, @@ -122,40 +122,11 @@ where _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. + /// Sets the [`MakeWriter`] that the [`Layer`] being built will use to write events. /// /// # Examples /// @@ -171,6 +142,9 @@ impl<S, N, E, W> Layer<S, N, E, W> { /// # use tracing_subscriber::Layer as _; /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); /// ``` + /// + /// [`MakeWriter`]: ../fmt/trait.MakeWriter.html + /// [`Layer`]: ../layer/trait.Layer.html pub fn with_writer<W2>(self, make_writer: W2) -> Layer<S, N, E, W2> where W2: for<'writer> MakeWriter<'writer> + 'static, @@ -185,57 +159,7 @@ impl<S, N, E, W> Layer<S, N, E, W> { } } - /// 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 + /// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in /// unit tests. /// /// See [`TestWriter`] for additional details. @@ -256,7 +180,7 @@ impl<S, N, E, W> Layer<S, N, E, W> { /// ``` /// [capturing]: /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output - /// [`TestWriter`]: super::writer::TestWriter + /// [`TestWriter`]: writer/struct.TestWriter.html pub fn with_test_writer(self) -> Layer<S, N, E, TestWriter> { Layer { fmt_fields: self.fmt_fields, @@ -277,39 +201,6 @@ impl<S, N, E, W> Layer<S, N, E, W> { ..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> @@ -393,7 +284,7 @@ where /// `Layer`s added to this subscriber. /// /// [lifecycle]: https://docs.rs/tracing/latest/tracing/span/index.html#the-span-lifecycle - /// [time]: Layer::without_time() + /// [time]: #method.without_time pub fn with_span_events(self, kind: FmtSpan) -> Self { Layer { fmt_span: self.fmt_span.with_kind(kind), @@ -408,30 +299,6 @@ where ..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> { @@ -442,9 +309,9 @@ where } /// Sets whether or not the [thread ID] of the current thread is displayed - /// when formatting events. + /// when formatting events /// - /// [thread ID]: std::thread::ThreadId + /// [thread ID]: https://doc.rust-lang.org/stable/std/thread/struct.ThreadId.html 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), @@ -453,9 +320,9 @@ where } /// Sets whether or not the [name] of the current thread is displayed - /// when formatting events. + /// when formatting events /// - /// [name]: std::thread#naming-threads + /// [name]: https://doc.rust-lang.org/stable/std/thread/index.html#naming-threads pub fn with_thread_names( self, display_thread_names: bool, @@ -466,7 +333,7 @@ where } } - /// Sets the layer being built to use a [less verbose formatter][super::format::Compact]. + /// Sets the layer being built to use a [less verbose formatter](../fmt/format/struct.Compact.html). pub fn compact(self) -> Layer<S, N, format::Format<format::Compact, T>, W> where N: for<'writer> FormatFields<'writer> + 'static, @@ -495,7 +362,7 @@ where } } - /// Sets the layer being built to use a [JSON formatter][super::format::Json]. + /// Sets the layer being built to use a [JSON formatter](../fmt/format/struct.Json.html). /// /// The full format includes fields from all entered spans. /// @@ -510,7 +377,7 @@ where /// - [`Layer::flatten_event`] can be used to enable flattening event fields into the root /// object. /// - /// [`Layer::flatten_event`]: Layer::flatten_event() + /// [`Layer::flatten_event`]: #method.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> { @@ -531,7 +398,7 @@ where 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] + /// See [`format::Json`](../fmt/format/struct.Json.html) pub fn flatten_event( self, flatten_event: bool, @@ -546,7 +413,7 @@ impl<S, T, W> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { /// Sets whether or not the formatter will include the current span in /// formatted events. /// - /// See [`format::Json`][super::format::Json] + /// See [`format::Json`](../fmt/format/struct.Json.html) pub fn with_current_span( self, display_current_span: bool, @@ -561,7 +428,7 @@ impl<S, T, W> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { /// 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] + /// See [`format::Json`](../fmt/format/struct.Json.html) pub fn with_span_list( self, display_span_list: bool, @@ -590,36 +457,6 @@ impl<S, N, E, W> Layer<S, N, E, W> { _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> { @@ -660,7 +497,7 @@ where /// formatters are in use, each can store its own formatted representation /// without conflicting. /// -/// [extensions]: crate::registry::Extensions +/// [extensions]: ../registry/struct.Extensions.html #[derive(Default)] pub struct FormattedFields<E: ?Sized> { _format_fields: PhantomData<fn(E)>, @@ -984,7 +821,7 @@ where /// 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 + /// [stored data]: ../registry/struct.SpanRef.html #[inline] pub fn span(&self, id: &Id) -> Option<SpanRef<'_, S>> where @@ -1007,7 +844,7 @@ where /// /// If this returns `None`, then we are not currently within a span. /// - /// [stored data]: crate::registry::SpanRef + /// [stored data]: ../registry/struct.SpanRef.html #[inline] pub fn lookup_current(&self) -> Option<SpanRef<'_, S>> where diff --git a/vendor/tracing-subscriber-0.3.3/src/fmt/format/json.rs b/vendor/tracing-subscriber-0.3.3/src/fmt/format/json.rs new file mode 100644 index 000000000..cc86f03c7 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/format/json.rs @@ -0,0 +1,750 @@ +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 verbose JSON log format should be used. +/// +/// The full format includes fields from all entered spans. +/// +/// # Example Output +/// +/// ```json +/// { +/// "timestamp":"Feb 20 11:28:15.096", +/// "level":"INFO", +/// "fields":{"message":"some message","key":"value"} +/// "target":"mycrate", +/// "span":{name":"leaf"}, +/// "spans":[{"name":"root"},{"name":"leaf"}], +/// } +/// ``` +/// +/// # Options +/// +/// - [`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. +/// +/// [`Json::flatten_event`]: #method.flatten_event +/// [`Json::with_current_span`]: #method.with_current_span +/// [`Json::with_span_list`]: #method.with_span_list +#[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.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. +/// +/// [`FormatFields`]: trait.FormatFields.html +#[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. + /// + /// [`FormatFields`]: trait.FormatFields.html + 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]: ../../field/trait.Visit.html +/// [`JsonFields`]: struct.JsonFields.html +/// [`MakeVisitor`]: ../../field/trait.MakeVisitor.html +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> { + /// 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; + + 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_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() + ); + } +} diff --git a/vendor/tracing-subscriber-0.3.3/src/fmt/format/mod.rs b/vendor/tracing-subscriber-0.3.3/src/fmt/format/mod.rs new file mode 100644 index 000000000..9001e102e --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/format/mod.rs @@ -0,0 +1,1798 @@ +//! Formatters for logging `tracing` events. +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 +/// ``` +/// +/// [`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]: ../field/trait.RecordFields.html +/// [`FmtSubscriber`]: ../fmt/struct.Subscriber.html +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. +/// +/// [`FormatFields`]: trait.FormatFields.html +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. +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. +/// +/// [`FormatFields`]: trait.FormatFields.html +#[derive(Debug, Clone)] +pub struct FieldFn<F>(F); +/// The [visitor] produced by [`FieldFn`]'s [`MakeVisitor`] implementation. +/// +/// [visitor]: ../../field/trait.Visit.html +/// [`FieldFn`]: struct.FieldFn.html +/// [`MakeVisitor`]: ../../field/trait.MakeVisitor.html +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 only includes the fields from the most recently entered span. +#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)] +pub struct Compact; + +/// Marker for `Format` that indicates that the verbose log format should be used. +/// +/// The full format includes fields from all entered spans. +#[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 includes only the fields from the most-recently-entered +/// span. +#[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, +} + +// === 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, + } + } +} + +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, + } + } + + /// 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, + } + } + + /// 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. + /// + /// [`Format::flatten_event`]: #method.flatten_event + #[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, + } + } + + /// 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, + } + } + + /// 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, + } + } + + /// 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]: https://doc.rust-lang.org/stable/std/thread/struct.ThreadId.html + 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]: https://doc.rust-lang.org/stable/std/thread/index.html#naming-threads + pub fn with_thread_names(self, display_thread_name: bool) -> Format<F, T> { + Format { + display_thread_name, + ..self + } + } + + #[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())?; + self.timer.format_time(writer)?; + write!(writer, "{} ", style.suffix())?; + return Ok(()); + } + } + + // Otherwise, just format the timestamp without ANSI formatting. + self.timer.format_time(writer)?; + 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`](../format/struct.Json.html). + #[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`](../fmt/format/struct.Json.html) + #[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`](../fmt/format/struct.Json.html) + #[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(":") + )?; + } + + 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)?; + + if self.display_target { + write!( + writer, + "{}{} ", + writer.bold().paint(meta.target()), + writer.dimmed().paint(":") + )?; + } + + ctx.format_fields(writer.by_ref(), event)?; + + let dimmed = writer.dimmed(); + for span in ctx + .event_scope() + .into_iter() + .map(crate::registry::Scope::from_root) + .flatten() + { + 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. +/// +/// [`FormatFields`]: trait.FormatFields.html +#[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. + /// + /// [`FormatFields`]: trait.FormatFields.html + 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 + } +} + +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`](../struct.SubscriberBuilder.html#method.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 std::fmt; + + 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); + run_test(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); + } + + 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 collector = builder + .with_writer(make_writer.clone()) + .with_level(false) + .with_ansi(false) + .with_timer(MockTime) + .finish(); + + with_default(collector, || { + 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::*; + #[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(), + ) + } + } + + #[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)); + } +} diff --git a/vendor/tracing-subscriber-0.3.3/src/fmt/format/pretty.rs b/vendor/tracing-subscriber-0.3.3/src/fmt/format/pretty.rs new file mode 100644 index 000000000..3e47e2d93 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/format/pretty.rs @@ -0,0 +1,415 @@ +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. +#[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`. + pub fn with_source_location(self, display_location: bool) -> Self { + Self { + display_location, + ..self + } + } +} + +impl<T> Format<Pretty, T> { + /// Sets whether or not the source code location from which an event + /// originated is displayed. + /// + /// This defaults to `true`. + pub fn with_source_location(mut self, display_location: bool) -> Self { + self.format = self.format.with_source_location(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 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 (true, Some(file), Some(line)) = + (self.format.display_location, meta.file(), meta.line()) + { + write!( + writer, + " {} {}:{}{}", + dimmed.paint("at"), + file, + line, + dimmed.paint(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 { + write!(writer, " ({:?})", thread.id())?; + } + } else if !self.display_thread_id { + write!(writer, " {:?}", thread.id())?; + } + } else 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, false); + 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-0.3.3/src/fmt/mod.rs b/vendor/tracing-subscriber-0.3.3/src/fmt/mod.rs new file mode 100644 index 000000000..d5deb8f0c --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/mod.rs @@ -0,0 +1,1275 @@ +//! 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.2" +//! ``` +//! +//! *Compiler support: requires rustc 1.39+* +//! +//! 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. +//! +//! For example: +//! <pre><font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 1.59s +//! <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt` +//! <font color="#AAAAAA">Oct 24 12:55:47.814 </font><font color="#4E9A06"> INFO</font> fmt: preparing to shave yaks number_of_yaks=3 +//! <font color="#AAAAAA">Oct 24 12:55:47.814 </font><font color="#4E9A06"> INFO</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: fmt::yak_shave: shaving yaks +//! <font color="#AAAAAA">Oct 24 12:55:47.814 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>:<b>shave{</b>yak=1<b>}</b>: fmt::yak_shave: hello! I'm gonna shave a yak excitement="yay!" +//! <font color="#AAAAAA">Oct 24 12:55:47.814 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>:<b>shave{</b>yak=1<b>}</b>: fmt::yak_shave: yak shaved successfully +//! <font color="#AAAAAA">Oct 24 12:55:47.814 </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: yak_events: yak=1 shaved=true +//! <font color="#AAAAAA">Oct 24 12:55:47.814 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: fmt::yak_shave: yaks_shaved=1 +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>:<b>shave{</b>yak=2<b>}</b>: fmt::yak_shave: hello! I'm gonna shave a yak excitement="yay!" +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>:<b>shave{</b>yak=2<b>}</b>: fmt::yak_shave: yak shaved successfully +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: yak_events: yak=2 shaved=true +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: fmt::yak_shave: yaks_shaved=2 +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>:<b>shave{</b>yak=3<b>}</b>: fmt::yak_shave: hello! I'm gonna shave a yak excitement="yay!" +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#C4A000"> WARN</font> <b>shaving_yaks{</b>yaks=3<b>}</b>:<b>shave{</b>yak=3<b>}</b>: fmt::yak_shave: could not locate yak +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: yak_events: yak=3 shaved=false +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#CC0000">ERROR</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: fmt::yak_shave: failed to shave yak yak=3 error=missing yak +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b>yaks=3<b>}</b>: fmt::yak_shave: yaks_shaved=2 +//! <font color="#AAAAAA">Oct 24 12:55:47.815 </font><font color="#4E9A06"> INFO</font> fmt: yak shaving completed all_yaks_shaved=false +//! </pre> +//! +//! * [`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. +//! +//! For example: +//! <pre><font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 1.61s +//! <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-pretty` +//! Oct 24 12:57:29.386 <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 +//! +//! Oct 24 12:57:29.386 <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:38<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 +//! +//! Oct 24 12:57:29.387 <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:14<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 +//! +//! Oct 24 12:57:29.387 <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:22<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 +//! +//! Oct 24 12:57:29.387 <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:43<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 +//! +//! Oct 24 12:57:29.387 <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:52<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 +//! +//! Oct 24 12:57:29.387 <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:14<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 +//! +//! Oct 24 12:57:29.387 <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:22<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 +//! +//! Oct 24 12:57:29.387 <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:43<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 +//! +//! Oct 24 12:57:29.387 <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:52<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 +//! +//! Oct 24 12:57:29.387 <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:14<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 +//! +//! Oct 24 12:57:29.387 <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: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 +//! +//! Oct 24 12:57:29.387 <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:43<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 +//! +//! Oct 24 12:57:29.387 <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="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:48<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 +//! +//! Oct 24 12:57:29.387 <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:52<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 +//! +//! Oct 24 12:57:29.387 <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> +//! +//! * [`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, as seen below, is *not* +//! optimized for human readability. +//! +//! For example: +//! <pre><font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 1.58s +//! <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-json` +//! {"timestamp":"Oct 24 13:00:00.873","level":"INFO","fields":{"message":"preparing to shave yaks","number_of_yaks":3},"target":"fmt_json"} +//! {"timestamp":"Oct 24 13:00:00.874","level":"INFO","fields":{"message":"shaving yaks"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +//! {"timestamp":"Oct 24 13:00:00.874","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":"Oct 24 13:00:00.874","level":"TRACE","fields":{"message":"yak shaved successfully"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":"1","name":"shave"}]} +//! {"timestamp":"Oct 24 13:00:00.874","level":"DEBUG","fields":{"yak":1,"shaved":true},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} +//! {"timestamp":"Oct 24 13:00:00.874","level":"TRACE","fields":{"yaks_shaved":1},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +//! {"timestamp":"Oct 24 13:00:00.874","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":"Oct 24 13:00:00.874","level":"TRACE","fields":{"message":"yak shaved successfully"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":"2","name":"shave"}]} +//! {"timestamp":"Oct 24 13:00:00.874","level":"DEBUG","fields":{"yak":2,"shaved":true},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} +//! {"timestamp":"Oct 24 13:00:00.874","level":"TRACE","fields":{"yaks_shaved":2},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +//! {"timestamp":"Oct 24 13:00:00.874","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":"Oct 24 13:00:00.875","level":"WARN","fields":{"message":"could not locate yak"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":"3","name":"shave"}]} +//! {"timestamp":"Oct 24 13:00:00.875","level":"DEBUG","fields":{"yak":3,"shaved":false},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} +//! {"timestamp":"Oct 24 13:00:00.875","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":"Oct 24 13:00:00.875","level":"TRACE","fields":{"yaks_shaved":2},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} +//! {"timestamp":"Oct 24 13:00:00.875","level":"INFO","fields":{"message":"yak shaving completed","all_yaks_shaved":false},"target":"fmt_json"} +//! </pre> +//! +//! ### 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` collector 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`](../fmt/struct.Layer.html): +//! +//! ```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`]: ../filter/struct.EnvFilter.html +//! [`env_logger`]: https://docs.rs/env_logger/ +//! [`filter`]: ../filter/index.html +//! [`SubscriberBuilder`]: ./struct.SubscriberBuilder.html +//! [`FmtSubscriber`]: ./struct.Subscriber.html +//! [`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::{ + 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 +#[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]: https://docs.rs/tracing-core/0.1.5/tracing_core/struct.Level.html + /// [`SubscriberBuilder::with_max_level`]: struct.SubscriberBuilder.html#method.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(&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]: #method.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 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]: https://doc.rust-lang.org/stable/std/thread/index.html#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]: https://doc.rust-lang.org/stable/std/thread/struct.ThreadId.html + 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`](../fmt/format/struct.Json.html) + #[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`](../fmt/format/struct.Json.html) + 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`](../fmt/format/struct.Json.html) + 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`](../fmt/format/struct.Json.html) + 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 Visitor 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`]: ../filter/struct.EnvFilter.html + /// [`with_max_level`]: #method.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_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]: https://docs.rs/tracing-core/0.1.5/tracing_core/struct.Level.html + /// [`EnvFilter`]: ../filter/struct.EnvFilter.html + /// [`with_filter`]: #method.with_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 function that the subscriber being built should use to format + /// events that occur. + 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(); + /// ``` + /// + /// [`MakeWriter`]: trait.MakeWriter.html + 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/struct.TestWriter.html + pub fn with_test_writer(self) -> SubscriberBuilder<N, E, F, TestWriter> { + SubscriberBuilder { + filter: self.filter, + inner: self.inner.with_writer(TestWriter::default()), + } + } +} + +/// 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]: +/// ../filter/struct.EnvFilter.html#associatedconstant.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()); + + builder.try_init() +} + +/// 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]: +/// ../filter/struct.EnvFilter.html#associatedconstant.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-0.3.3/src/fmt/time/datetime.rs index 531331687..531331687 100644 --- a/vendor/tracing-subscriber/src/fmt/time/datetime.rs +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/time/datetime.rs diff --git a/vendor/tracing-subscriber-0.3.3/src/fmt/time/mod.rs b/vendor/tracing-subscriber-0.3.3/src/fmt/time/mod.rs new file mode 100644 index 000000000..621df16e4 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/time/mod.rs @@ -0,0 +1,134 @@ +//! 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(feature = "local-time")))] +pub use time_crate::LocalTime; + +/// 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`]: ./index.html +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-0.3.3/src/fmt/time/time_crate.rs b/vendor/tracing-subscriber-0.3.3/src/fmt/time/time_crate.rs new file mode 100644 index 000000000..64d274365 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/time/time_crate.rs @@ -0,0 +1,276 @@ +use crate::fmt::{format::Writer, time::FormatTime, writer::WriteAdaptor}; +use std::fmt; +use time::{format_description::well_known, formatting::Formattable, OffsetDateTime}; + +/// Formats the current [local time] using a [formatter] from the [`time` crate]. +/// +/// To format the current [UTC time] instead, use the [`UtcTime`] type. +/// +/// [local time]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.now_local +/// [UTC time]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.now_utc +/// [formatter]: https://docs.rs/time/0.3/time/formatting/trait.Formattable.html +/// [`time` crate]: https://docs.rs/time/0.3/time/ +#[derive(Clone, Debug)] +#[cfg_attr(docsrs, doc(cfg(all(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]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.now_local +/// [UTC time]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.now_utc +/// [formatter]: https://docs.rs/time/0.3/time/formatting/trait.Formattable.html +/// [`time` crate]: https://docs.rs/time/0.3/time/ +#[cfg_attr(docsrs, doc(cfg(feature = "time")))] +#[derive(Clone, Debug)] +pub struct UtcTime<F> { + 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]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.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. + /// + /// 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]: https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.now_local + /// [`time` crate]: https://docs.rs/time/0.3/time/ + /// [`Formattable`]: https://docs.rs/time/0.3/time/formatting/trait.Formattable.html + /// [well-known formats]: https://docs.rs/time/0.3/time/format_description/well_known/index.html + /// [`format_description!`]: https://docs.rs/time/0.3/time/macros/macro.format_description.html + /// [`time::format_description::parse`]: https://docs.rs/time/0.3/time/format_description/fn.parse.html + /// [`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]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.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]: https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.now_utc + /// [`time` crate]: https://docs.rs/time/0.3/time/ + /// [`Formattable`]: https://docs.rs/time/0.3/time/formatting/trait.Formattable.html + /// [well-known formats]: https://docs.rs/time/0.3/time/format_description/well_known/index.html + /// [`format_description!`]: https://docs.rs/time/0.3/time/macros/macro.format_description.html + /// [`time::format_description::parse`]: https://docs.rs/time/0.3/time/format_description/fn.parse.html + /// [`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()) + } +} + +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-0.3.3/src/fmt/writer.rs index 4aacd6d54..0974891f7 100644 --- a/vendor/tracing-subscriber/src/fmt/writer.rs +++ b/vendor/tracing-subscriber-0.3.3/src/fmt/writer.rs @@ -1,6 +1,6 @@ //! Abstractions for creating [`io::Write`] instances. //! -//! [`io::Write`]: std::io::Write +//! [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html use std::{ fmt, io::{self, Write}, @@ -96,8 +96,8 @@ use tracing_core::Metadata; pub trait MakeWriter<'a> { /// The concrete [`io::Write`] implementation returned by [`make_writer`]. /// - /// [`io::Write`]: std::io::Write - /// [`make_writer`]: MakeWriter::make_writer + /// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html + /// [`make_writer`]: #tymethod.make_writer type Writer: io::Write; /// Returns an instance of [`Writer`]. @@ -109,7 +109,7 @@ pub trait MakeWriter<'a> { /// creating a [`io::Write`] instance is expensive, be sure to cache it when implementing /// [`MakeWriter`] to improve performance. /// - /// [`Writer`]: MakeWriter::Writer + /// [`Writer`]: #associatedtype.Writer /// [`fmt::Layer`]: crate::fmt::Layer /// [`fmt::Subscriber`]: crate::fmt::Subscriber /// [`io::Write`]: std::io::Write @@ -501,13 +501,13 @@ pub trait MakeWriterExt<'a>: MakeWriter<'a> { /// 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 +/// [`fmt::Subscriber`]: ../struct.Subscriber.html +/// [`fmt::Layer`]: ../struct.Layer.html /// [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! +/// [`io::stdout`]: https://doc.rust-lang.org/std/io/fn.stdout.html +/// [`io::stderr`]: https://doc.rust-lang.org/std/io/fn.stderr.html +/// [`print!`]: https://doc.rust-lang.org/std/macro.print.html #[derive(Default, Debug)] pub struct TestWriter { _p: (), @@ -646,9 +646,10 @@ pub struct Tee<A, B> { /// 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 +/// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html +/// [`MutexGuard`]: https://doc.rust-lang.org/std/sync/struct.MutexGuard.html +/// [`Mutex`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html +/// [`MakeWriter`]: trait.MakeWriter.html #[derive(Debug)] pub struct MutexGuardWriter<'a, W>(MutexGuard<'a, W>); @@ -733,6 +734,7 @@ impl<'a> MakeWriter<'a> for TestWriter { impl BoxMakeWriter { /// Constructs a `BoxMakeWriter` wrapping a type implementing [`MakeWriter`]. /// + /// [`MakeWriter`]: trait.MakeWriter.html pub fn new<M>(make_writer: M) -> Self where M: for<'a> MakeWriter<'a> + Send + Sync + 'static, @@ -1023,8 +1025,6 @@ impl<A, B> Tee<A, B> { /// outputs. /// /// See the documentation for [`MakeWriterExt::and`] for details. - /// - /// [writers]: std::io::Write pub fn new(a: A, b: B) -> Self { Self { a, b } } diff --git a/vendor/tracing-subscriber-0.3.3/src/layer/context.rs b/vendor/tracing-subscriber-0.3.3/src/layer/context.rs new file mode 100644 index 000000000..e11959526 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/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`]: ../layer/trait.Layer.html +/// [`Subscriber`]: https://docs.rs/tracing-core/latest/tracing_core/trait.Subscriber.html +/// [stored data]: ../registry/struct.SpanRef.html +/// [`LookupSpan`]: "../registry/trait.LookupSpan.html +#[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]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html#method.register_callsite + /// [`enabled`]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html#method.enabled + /// [`Context::enabled`]: #method.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]: ../registry/struct.SpanRef.html + #[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]: ../registry/struct.SpanRef.html + #[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]: ../registry/struct.SpanRef.html + 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]: ../registry/struct.SpanRef.html + 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-0.3.3/src/layer/layered.rs index 805ec13dc..c690764ad 100644 --- a/vendor/tracing-subscriber/src/layer/layered.rs +++ b/vendor/tracing-subscriber-0.3.3/src/layer/layered.rs @@ -12,17 +12,13 @@ use crate::{ }; #[cfg(all(feature = "registry", feature = "std"))] use crate::{filter::FilterId, registry::Registry}; -use core::{ - any::{Any, TypeId}, - cmp, fmt, - marker::PhantomData, -}; +use core::{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 +/// [`Subscriber`]: https://docs.rs/tracing-core/latest/tracing_core/trait.Subscriber.html #[derive(Clone)] pub struct Layered<L, I, S = I> { /// The layer. @@ -67,30 +63,6 @@ pub struct Layered<L, I, S = I> { // === 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>, @@ -139,16 +111,6 @@ where 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()); @@ -190,7 +152,7 @@ where #[cfg(all(feature = "registry", feature = "std"))] { if let Some(g) = guard.as_mut() { - g.set_closing() + g.is_closing() }; } @@ -289,17 +251,6 @@ where } #[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); @@ -435,7 +386,7 @@ where // (rather than calling into the inner type), clear the current // per-layer filter interest state. #[cfg(feature = "registry")] - filter::FilterState::take_interest(); + drop(filter::FilterState::take_interest()); return outer; } diff --git a/vendor/tracing-subscriber-0.3.3/src/layer/mod.rs b/vendor/tracing-subscriber-0.3.3/src/layer/mod.rs new file mode 100644 index 000000000..f3f994490 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/layer/mod.rs @@ -0,0 +1,1285 @@ +//! 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. +//! +//! [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. +//! +//! ### 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(()) } +//! ``` +//! +//! ## Runtime Configuration With Layers +//! +//! 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`]s 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, sync::Arc}; +//! 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)); +//! subscriber.with(layer) +//! } else { +//! subscriber +//! }; +//! +//! 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, sync::Arc}; +//! 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(Arc::new(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(()) } +//! ``` +//! +//! [`Subscriber`]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html +//! [span IDs]: https://docs.rs/tracing-core/latest/tracing_core/span/struct.Id.html +//! [the current span]: Context::current_span +//! [`register_callsite`]: Layer::register_callsite +//! [`enabled`]: Layer::enabled +//! [`on_enter`]: Layer::on_enter +//! [`Layer::register_callsite`]: Layer::register_callsite +//! [`Layer::enabled`]: Layer::enabled +//! [`Interest::never()`]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/struct.Interest.html#method.never +//! [option-impl]: crate::layer::Layer#impl-Layer<S>-for-Option<L> +//! [`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`]: https://docs.rs/tracing-core/latest/tracing_core/struct.Interest.html + /// [`Subscriber::register_callsite`]: https://docs.rs/tracing-core/latest/tracing_core/trait.Subscriber.html#method.register_callsite + /// [`Interest::never()`]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/struct.Interest.html#method.never + /// [`Interest::always()`]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/struct.Interest.html#method.always + /// [`self.enabled`]: #method.enabled + /// [`Layer::enabled`]: #method.enabled + /// [`on_event`]: #method.on_event + /// [`on_enter`]: #method.on_enter + /// [`on_exit`]: #method.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`]: https://docs.rs/tracing-core/latest/tracing_core/struct.Interest.html + /// [`Context`]: ../struct.Context.html + /// [`Subscriber::enabled`]: https://docs.rs/tracing-core/latest/tracing_core/trait.Subscriber.html#method.enabled + /// [`Layer::register_callsite`]: #method.register_callsite + /// [`on_event`]: #method.on_event + /// [`on_enter`]: #method.on_enter + /// [`on_exit`]: #method.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>) {} + + /// 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`]: https://docs.rs/tracing-core/latest/tracing_core/trait.Subscriber.html + 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]: #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) + } + + #[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 + } + } +} + +/// 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 => None, + } + } + + #[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 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")] + + 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 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 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-0.3.3/src/layer/tests.rs index d7ad61769..d7ad61769 100644 --- a/vendor/tracing-subscriber/src/layer/tests.rs +++ b/vendor/tracing-subscriber-0.3.3/src/layer/tests.rs diff --git a/vendor/tracing-subscriber-0.3.3/src/lib.rs b/vendor/tracing-subscriber-0.3.3/src/lib.rs new file mode 100644 index 000000000..563a86dee --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/lib.rs @@ -0,0 +1,218 @@ +//! 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.42+][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. +//! +//! ### 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 [`Subscriber`] 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"] } +//! ``` +//! +//! ## 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. +//! +//! [`tracing`]: https://docs.rs/tracing/latest/tracing/ +//! [`Subscriber`]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html +//! [`EnvFilter`]: filter/struct.EnvFilter.html +//! [`fmt`]: fmt/index.html +//! [`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 +//! [`liballoc`]: https://doc.rust-lang.org/alloc/index.html +#![doc(html_root_url = "https://docs.rs/tracing-subscriber/0.3.1")] +#![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-0.3.3/src/macros.rs index 81351132f..81351132f 100644 --- a/vendor/tracing-subscriber/src/macros.rs +++ b/vendor/tracing-subscriber-0.3.3/src/macros.rs diff --git a/vendor/tracing-subscriber/src/prelude.rs b/vendor/tracing-subscriber-0.3.3/src/prelude.rs index c2230907b..c2230907b 100644 --- a/vendor/tracing-subscriber/src/prelude.rs +++ b/vendor/tracing-subscriber-0.3.3/src/prelude.rs diff --git a/vendor/tracing-subscriber/src/registry/extensions.rs b/vendor/tracing-subscriber-0.3.3/src/registry/extensions.rs index ff76fb599..899e1549f 100644 --- a/vendor/tracing-subscriber/src/registry/extensions.rs +++ b/vendor/tracing-subscriber-0.3.3/src/registry/extensions.rs @@ -78,7 +78,7 @@ impl<'a> ExtensionsMut<'a> { /// 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 + /// types like [`String`](https://doc.rust-lang.org/std/string/struct.String.html), to avoid accidental /// cross-`Layer` clobbering. /// /// ## Panics diff --git a/vendor/tracing-subscriber-0.3.3/src/registry/mod.rs b/vendor/tracing-subscriber-0.3.3/src/registry/mod.rs new file mode 100644 index 000000000..f3b77b6a9 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/registry/mod.rs @@ -0,0 +1,604 @@ +//! 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`]: +//! https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html +//! [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`]: ../layer/trait.Layer.html +/// [`Context`]: ../layer/struct.Context.html +/// [`span()`]: ../layer/struct.Context.html#method.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> + /// + /// [`SpanData`]: trait.SpanData.html + 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`. + /// + /// [`SpanRef`]: struct.SpanRef.html + /// [`SpanData`]: trait.SpanData.html + /// [`span_data`]: #method.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]: trait.SpanData.html +/// [registry]: trait.LookupSpan.html +#[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]: https://docs.rs/tracing-core/latest/tracing_core/field/index.html + 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-0.3.3/src/registry/sharded.rs index b81d5fef8..a6311cb71 100644 --- a/vendor/tracing-subscriber/src/registry/sharded.rs +++ b/vendor/tracing-subscriber-0.3.3/src/registry/sharded.rs @@ -75,16 +75,16 @@ use tracing_core::{ /// 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 +/// [span IDs]: https://docs.rs/tracing-core/latest/tracing_core/span/struct.Id.html +/// [slab]: https://docs.rs/crate/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() +/// [considered closed]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html#method.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 +/// [fields]: https://docs.rs/tracing-core/latest/tracing-core/field/index.html /// [stored span data]: crate::registry::SpanData::extensions_mut #[cfg(feature = "registry")] #[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))] @@ -102,8 +102,9 @@ pub struct Registry { /// [`Layer`s], such as formatted fields, metrics, or distributed traces should /// be stored in the [extensions] typemap. /// -/// [`Layer`s]: crate::layer::Layer -/// [extensions]: Extensions +/// [`Registry`]: struct.Registry.html +/// [`Layer`s]: ../layer/trait.Layer.html +/// [extensions]: struct.Extensions.html #[cfg(feature = "registry")] #[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))] #[derive(Debug)] @@ -114,11 +115,10 @@ pub struct Data<'a> { /// 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. +/// 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, @@ -173,6 +173,7 @@ fn id_to_idx(id: &Id) -> usize { /// greater than 0, `CloseGuard` decrements the counter by one and /// _does not_ remove the span from the [`Registry`]. /// +/// [`Registry`]: ./struct.Registry.html pub(crate) struct CloseGuard<'a> { id: Id, registry: &'a Registry, @@ -188,6 +189,7 @@ impl Registry { /// processed an `on_close` notification via the `CLOSE_COUNT` thread-local. /// For additional details, see [`CloseGuard`]. /// + /// [`CloseGuard`]: ./struct.CloseGuard.html pub(crate) fn start_close(&self, id: Id) -> CloseGuard<'_> { CLOSE_COUNT.with(|count| { let c = count.get(); @@ -214,6 +216,7 @@ thread_local! { /// track how many layers have processed the close. /// For additional details, see [`CloseGuard`]. /// + /// [`CloseGuard`]: ./struct.CloseGuard.html static CLOSE_COUNT: Cell<usize> = Cell::new(0); } @@ -377,7 +380,7 @@ impl<'a> LookupSpan<'a> for Registry { // === impl CloseGuard === impl<'a> CloseGuard<'a> { - pub(crate) fn set_closing(&mut self) { + pub(crate) fn is_closing(&mut self) { self.is_closing = true; } } diff --git a/vendor/tracing-subscriber/src/registry/stack.rs b/vendor/tracing-subscriber-0.3.3/src/registry/stack.rs index 4a3f7e59d..4a3f7e59d 100644 --- a/vendor/tracing-subscriber/src/registry/stack.rs +++ b/vendor/tracing-subscriber-0.3.3/src/registry/stack.rs diff --git a/vendor/tracing-subscriber-0.3.3/src/reload.rs b/vendor/tracing-subscriber-0.3.3/src/reload.rs new file mode 100644 index 000000000..b8ec67dfa --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/src/reload.rs @@ -0,0 +1,237 @@ +//! Wrapper for a `Layer` to allow it to be dynamically reloaded. +//! +//! This module provides a [`Layer` type] which wraps another type implementing +//! the [`Layer` trait], allowing the wrapped type to be replaced with another +//! instance of that type at runtime. +//! +//! This can be used in cases where a subset of `Subscriber` 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. +//! +//! [`Layer` type]: struct.Layer.html +//! [`Layer` trait]: ../layer/trait.Layer.html +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`, 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`. +#[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 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<L, S> Layer<L, S> +where + L: crate::Layer<S> + 'static, + S: Subscriber, +{ + /// Wraps the given `Layer`, returning a `Layer` and a `Handle` that allows + /// the inner type to be modified at runtime. + 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`. + pub fn handle(&self) -> Handle<L, S> { + Handle { + inner: Arc::downgrade(&self.inner), + _s: PhantomData, + } + } +} + +// ===== impl Handle ===== + +impl<L, S> Handle<L, S> +where + L: crate::Layer<S> + 'static, + S: Subscriber, +{ + /// Replace the current layer with the provided `new_layer`. + pub fn reload(&self, new_layer: impl Into<L>) -> Result<(), Error> { + self.modify(|layer| { + *layer = new_layer.into(); + }) + } + + /// Invokes a closure with a mutable reference to the current layer, + /// 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'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, + /// 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-0.3.3/src/sync.rs index ec42b834a..ec42b834a 100644 --- a/vendor/tracing-subscriber/src/sync.rs +++ b/vendor/tracing-subscriber-0.3.3/src/sync.rs diff --git a/vendor/tracing-subscriber/src/util.rs b/vendor/tracing-subscriber-0.3.3/src/util.rs index 1c98aa4d2..1c98aa4d2 100644 --- a/vendor/tracing-subscriber/src/util.rs +++ b/vendor/tracing-subscriber-0.3.3/src/util.rs diff --git a/vendor/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs b/vendor/tracing-subscriber-0.3.3/tests/cached_layer_filters_dont_break_other_layers.rs index 00e98a994..00e98a994 100644 --- a/vendor/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/cached_layer_filters_dont_break_other_layers.rs diff --git a/vendor/tracing-subscriber/tests/duplicate_spans.rs b/vendor/tracing-subscriber-0.3.3/tests/duplicate_spans.rs index 5d4dc6a85..c4a736f74 100644 --- a/vendor/tracing-subscriber/tests/duplicate_spans.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/duplicate_spans.rs @@ -1,4 +1,5 @@ #![cfg(all(feature = "env-filter", feature = "fmt"))] +mod support; use tracing::{self, subscriber::with_default, Span}; use tracing_subscriber::{filter::EnvFilter, FmtSubscriber}; diff --git a/vendor/tracing-subscriber/tests/field_filter.rs b/vendor/tracing-subscriber-0.3.3/tests/field_filter.rs index f14a0626d..12b4053b6 100644 --- a/vendor/tracing-subscriber/tests/field_filter.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/field_filter.rs @@ -1,7 +1,7 @@ #![cfg(feature = "env-filter")] - +mod support; +use self::support::*; use tracing::{self, subscriber::with_default, Level}; -use tracing_mock::*; use tracing_subscriber::{filter::EnvFilter, prelude::*}; #[test] diff --git a/vendor/tracing-subscriber-0.3.3/tests/filter.rs b/vendor/tracing-subscriber-0.3.3/tests/filter.rs new file mode 100644 index 000000000..8386d34d2 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/tests/filter.rs @@ -0,0 +1,187 @@ +#![cfg(feature = "env-filter")] + +mod support; +use self::support::*; +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 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::mock().named("cool_span")) + .event(event::mock().at_level(Level::DEBUG)) + .enter(span::mock().named("uncool_span")) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::DEBUG)) + .exit(span::mock().named("uncool_span")) + .exit(span::mock().named("cool_span")) + .enter(span::mock().named("uncool_span")) + .event(event::mock().at_level(Level::WARN)) + .event(event::mock().at_level(Level::ERROR)) + .exit(span::mock().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(); +} diff --git a/vendor/tracing-subscriber/tests/filter_log.rs b/vendor/tracing-subscriber-0.3.3/tests/filter_log.rs index 8d57ed600..28e742501 100644 --- a/vendor/tracing-subscriber/tests/filter_log.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/filter_log.rs @@ -1,7 +1,7 @@ #![cfg(all(feature = "env-filter", feature = "tracing-log"))] - +mod support; +use self::support::*; use tracing::{self, Level}; -use tracing_mock::*; use tracing_subscriber::{filter::EnvFilter, prelude::*}; mod my_module { diff --git a/vendor/tracing-subscriber/tests/fmt_max_level_hint.rs b/vendor/tracing-subscriber-0.3.3/tests/fmt_max_level_hint.rs index 57a0f6e3f..57a0f6e3f 100644 --- a/vendor/tracing-subscriber/tests/fmt_max_level_hint.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/fmt_max_level_hint.rs diff --git a/vendor/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs b/vendor/tracing-subscriber-0.3.3/tests/hinted_layer_filters_dont_break_other_layers.rs index 897dae282..897dae282 100644 --- a/vendor/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/hinted_layer_filters_dont_break_other_layers.rs diff --git a/vendor/tracing-subscriber/tests/layer_filter_interests_are_cached.rs b/vendor/tracing-subscriber-0.3.3/tests/layer_filter_interests_are_cached.rs index d89d3bf17..d89d3bf17 100644 --- a/vendor/tracing-subscriber/tests/layer_filter_interests_are_cached.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/layer_filter_interests_are_cached.rs diff --git a/vendor/tracing-subscriber/tests/layer_filters/boxed.rs b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/boxed.rs index 0fe37188e..0fe37188e 100644 --- a/vendor/tracing-subscriber/tests/layer_filters/boxed.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/boxed.rs diff --git a/vendor/tracing-subscriber/tests/layer_filters/downcast_raw.rs b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/downcast_raw.rs index b5f7e35ce..b5f7e35ce 100644 --- a/vendor/tracing-subscriber/tests/layer_filters/downcast_raw.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/downcast_raw.rs diff --git a/vendor/tracing-subscriber/tests/layer_filters/filter_scopes.rs b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/filter_scopes.rs index 7fd7d843b..7fd7d843b 100644 --- a/vendor/tracing-subscriber/tests/layer_filters/filter_scopes.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/filter_scopes.rs diff --git a/vendor/tracing-subscriber-0.3.3/tests/layer_filters/main.rs b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/main.rs new file mode 100644 index 000000000..2359584d7 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/main.rs @@ -0,0 +1,188 @@ +#![cfg(feature = "registry")] +#[path = "../support.rs"] +mod support; +use self::support::*; +mod boxed; +mod downcast_raw; +mod filter_scopes; +mod targets; +mod trees; + +use tracing::{level_filters::LevelFilter, Level}; +use tracing_subscriber::{filter, prelude::*}; + +#[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-0.3.3/tests/layer_filters/targets.rs index c8133044b..c8133044b 100644 --- a/vendor/tracing-subscriber/tests/layer_filters/targets.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/targets.rs diff --git a/vendor/tracing-subscriber/tests/layer_filters/trees.rs b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/trees.rs index 18cdd8ccc..18cdd8ccc 100644 --- a/vendor/tracing-subscriber/tests/layer_filters/trees.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/layer_filters/trees.rs diff --git a/vendor/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs b/vendor/tracing-subscriber-0.3.3/tests/multiple_layer_filter_interests_cached.rs index 5c25e7f03..5c25e7f03 100644 --- a/vendor/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/multiple_layer_filter_interests_cached.rs diff --git a/vendor/tracing-subscriber/tests/registry_max_level_hint.rs b/vendor/tracing-subscriber-0.3.3/tests/registry_max_level_hint.rs index f94c8a1fb..f94c8a1fb 100644 --- a/vendor/tracing-subscriber/tests/registry_max_level_hint.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/registry_max_level_hint.rs diff --git a/vendor/tracing-subscriber/tests/registry_with_subscriber.rs b/vendor/tracing-subscriber-0.3.3/tests/registry_with_subscriber.rs index 50d2f551d..3f8d99b1d 100644 --- a/vendor/tracing-subscriber/tests/registry_with_subscriber.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/registry_with_subscriber.rs @@ -4,7 +4,7 @@ use tracing_subscriber::prelude::*; #[tokio::test] async fn future_with_subscriber() { - tracing_subscriber::registry().init(); + let _default = tracing_subscriber::registry().init(); let span = tracing::info_span!("foo"); let _e = span.enter(); let span = tracing::info_span!("bar"); diff --git a/vendor/tracing-subscriber-0.3.3/tests/reload.rs b/vendor/tracing-subscriber-0.3.3/tests/reload.rs new file mode 100644 index 000000000..5fe422e08 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/tests/reload.rs @@ -0,0 +1,81 @@ +#![cfg(feature = "reload")] +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); + }) +} diff --git a/vendor/tracing-subscriber/tests/same_len_filters.rs b/vendor/tracing-subscriber-0.3.3/tests/same_len_filters.rs index 879e578d7..b525ea6fd 100644 --- a/vendor/tracing-subscriber/tests/same_len_filters.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/same_len_filters.rs @@ -1,9 +1,9 @@ // These tests include field filters with no targets, so they have to go in a // separate file. #![cfg(feature = "env-filter")] - +mod support; +use self::support::*; use tracing::{self, subscriber::with_default, Level}; -use tracing_mock::*; use tracing_subscriber::{filter::EnvFilter, prelude::*}; #[test] diff --git a/vendor/tracing-subscriber/tests/support.rs b/vendor/tracing-subscriber-0.3.3/tests/support.rs index 50e0e6669..848ebdc63 100644 --- a/vendor/tracing-subscriber/tests/support.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/support.rs @@ -1,15 +1,20 @@ #![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::{ +pub use self::support::{event, field, span, subscriber}; +// This has to have the same name as the module in `tracing`. +// path attribute requires referenced module to have same name so allow module inception here +#[allow(clippy::module_inception)] +#[path = "../../tracing/tests/support/mod.rs"] +mod support; + +use self::{ event::MockEvent, span::{MockSpan, NewSpan}, subscriber::{Expect, MockHandle}, }; +use tracing_core::{ + span::{Attributes, Id, Record}, + Event, Subscriber, +}; use tracing_subscriber::{ layer::{Context, Layer}, registry::{LookupSpan, SpanRef}, diff --git a/vendor/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs b/vendor/tracing-subscriber-0.3.3/tests/unhinted_layer_filters_dont_break_other_layers.rs index 9fa5c6bd4..9fa5c6bd4 100644 --- a/vendor/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs +++ b/vendor/tracing-subscriber-0.3.3/tests/unhinted_layer_filters_dont_break_other_layers.rs diff --git a/vendor/tracing-subscriber-0.3.3/tests/utils.rs b/vendor/tracing-subscriber-0.3.3/tests/utils.rs new file mode 100644 index 000000000..ff025a2a2 --- /dev/null +++ b/vendor/tracing-subscriber-0.3.3/tests/utils.rs @@ -0,0 +1,39 @@ +#![cfg(feature = "std")] +mod support; +use self::support::*; +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/.cargo-checksum.json b/vendor/tracing-subscriber/.cargo-checksum.json deleted file mode 100644 index 509cf43fc..000000000 --- a/vendor/tracing-subscriber/.cargo-checksum.json +++ /dev/null @@ -1 +0,0 @@ -{"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 deleted file mode 100644 index 35747566e..000000000 --- a/vendor/tracing-subscriber/CHANGELOG.md +++ /dev/null @@ -1,1212 +0,0 @@ -# 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 deleted file mode 100644 index 1d15642a2..000000000 --- a/vendor/tracing-subscriber/Cargo.toml +++ /dev/null @@ -1,232 +0,0 @@ -# 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/README.md b/vendor/tracing-subscriber/README.md deleted file mode 100644 index 95242b37e..000000000 --- a/vendor/tracing-subscriber/README.md +++ /dev/null @@ -1,61 +0,0 @@ -![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/src/field/mod.rs b/vendor/tracing-subscriber/src/field/mod.rs deleted file mode 100644 index 5dfddb362..000000000 --- a/vendor/tracing-subscriber/src/field/mod.rs +++ /dev/null @@ -1,366 +0,0 @@ -//! 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 deleted file mode 100644 index 2ae3f0f24..000000000 --- a/vendor/tracing-subscriber/src/filter/directive.rs +++ /dev/null @@ -1,456 +0,0 @@ -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 deleted file mode 100644 index 36b520543..000000000 --- a/vendor/tracing-subscriber/src/filter/env/builder.rs +++ /dev/null @@ -1,324 +0,0 @@ -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 deleted file mode 100644 index f062e6ef9..000000000 --- a/vendor/tracing-subscriber/src/filter/env/directive.rs +++ /dev/null @@ -1,860 +0,0 @@ -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 deleted file mode 100644 index 1394fd04a..000000000 --- a/vendor/tracing-subscriber/src/filter/env/field.rs +++ /dev/null @@ -1,626 +0,0 @@ -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 deleted file mode 100644 index 81a9ae2bd..000000000 --- a/vendor/tracing-subscriber/src/filter/env/mod.rs +++ /dev/null @@ -1,991 +0,0 @@ -//! 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/layer_filters/combinator.rs b/vendor/tracing-subscriber/src/filter/layer_filters/combinator.rs deleted file mode 100644 index e79de2087..000000000 --- a/vendor/tracing-subscriber/src/filter/layer_filters/combinator.rs +++ /dev/null @@ -1,469 +0,0 @@ -//! 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 deleted file mode 100644 index 8949cfb5a..000000000 --- a/vendor/tracing-subscriber/src/filter/layer_filters/mod.rs +++ /dev/null @@ -1,1135 +0,0 @@ -//! ## 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/fmt/format/json.rs b/vendor/tracing-subscriber/src/fmt/format/json.rs deleted file mode 100644 index c2f4d3755..000000000 --- a/vendor/tracing-subscriber/src/fmt/format/json.rs +++ /dev/null @@ -1,885 +0,0 @@ -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 deleted file mode 100644 index ec79ac140..000000000 --- a/vendor/tracing-subscriber/src/fmt/format/mod.rs +++ /dev/null @@ -1,2161 +0,0 @@ -//! 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 deleted file mode 100644 index a50d08ba7..000000000 --- a/vendor/tracing-subscriber/src/fmt/format/pretty.rs +++ /dev/null @@ -1,511 +0,0 @@ -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 deleted file mode 100644 index 3c6a6ac40..000000000 --- a/vendor/tracing-subscriber/src/fmt/mod.rs +++ /dev/null @@ -1,1324 +0,0 @@ -//! 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/mod.rs b/vendor/tracing-subscriber/src/fmt/time/mod.rs deleted file mode 100644 index e5b7c83b0..000000000 --- a/vendor/tracing-subscriber/src/fmt/time/mod.rs +++ /dev/null @@ -1,138 +0,0 @@ -//! 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 deleted file mode 100644 index 60d57fd0b..000000000 --- a/vendor/tracing-subscriber/src/fmt/time/time_crate.rs +++ /dev/null @@ -1,470 +0,0 @@ -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/layer/context.rs b/vendor/tracing-subscriber/src/layer/context.rs deleted file mode 100644 index 46254994f..000000000 --- a/vendor/tracing-subscriber/src/layer/context.rs +++ /dev/null @@ -1,434 +0,0 @@ -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/mod.rs b/vendor/tracing-subscriber/src/layer/mod.rs deleted file mode 100644 index 24b853323..000000000 --- a/vendor/tracing-subscriber/src/layer/mod.rs +++ /dev/null @@ -1,1798 +0,0 @@ -//! 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/lib.rs b/vendor/tracing-subscriber/src/lib.rs deleted file mode 100644 index 4bd61000e..000000000 --- a/vendor/tracing-subscriber/src/lib.rs +++ /dev/null @@ -1,252 +0,0 @@ -//! 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/registry/mod.rs b/vendor/tracing-subscriber/src/registry/mod.rs deleted file mode 100644 index 38af53e8a..000000000 --- a/vendor/tracing-subscriber/src/registry/mod.rs +++ /dev/null @@ -1,600 +0,0 @@ -//! 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/reload.rs b/vendor/tracing-subscriber/src/reload.rs deleted file mode 100644 index 3d1743440..000000000 --- a/vendor/tracing-subscriber/src/reload.rs +++ /dev/null @@ -1,350 +0,0 @@ -//! 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/tests/env_filter/main.rs b/vendor/tracing-subscriber/tests/env_filter/main.rs deleted file mode 100644 index 3c3d4868b..000000000 --- a/vendor/tracing-subscriber/tests/env_filter/main.rs +++ /dev/null @@ -1,547 +0,0 @@ -#![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 deleted file mode 100644 index 8bf5698a4..000000000 --- a/vendor/tracing-subscriber/tests/env_filter/per_layer.rs +++ /dev/null @@ -1,305 +0,0 @@ -//! 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 deleted file mode 100644 index 8f67cfcba..000000000 --- a/vendor/tracing-subscriber/tests/event_enabling.rs +++ /dev/null @@ -1,81 +0,0 @@ -#![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/layer_filters/combinators.rs b/vendor/tracing-subscriber/tests/layer_filters/combinators.rs deleted file mode 100644 index 6052a2d00..000000000 --- a/vendor/tracing-subscriber/tests/layer_filters/combinators.rs +++ /dev/null @@ -1,42 +0,0 @@ -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/main.rs b/vendor/tracing-subscriber/tests/layer_filters/main.rs deleted file mode 100644 index 0233f063b..000000000 --- a/vendor/tracing-subscriber/tests/layer_filters/main.rs +++ /dev/null @@ -1,189 +0,0 @@ -#![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/vec.rs b/vendor/tracing-subscriber/tests/layer_filters/vec.rs deleted file mode 100644 index 87244e4ab..000000000 --- a/vendor/tracing-subscriber/tests/layer_filters/vec.rs +++ /dev/null @@ -1,120 +0,0 @@ -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/option.rs b/vendor/tracing-subscriber/tests/option.rs deleted file mode 100644 index 738cc0a6c..000000000 --- a/vendor/tracing-subscriber/tests/option.rs +++ /dev/null @@ -1,41 +0,0 @@ -#![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/reload.rs b/vendor/tracing-subscriber/tests/reload.rs deleted file mode 100644 index b8b6c2b46..000000000 --- a/vendor/tracing-subscriber/tests/reload.rs +++ /dev/null @@ -1,141 +0,0 @@ -#![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/utils.rs b/vendor/tracing-subscriber/tests/utils.rs deleted file mode 100644 index e95868d5e..000000000 --- a/vendor/tracing-subscriber/tests/utils.rs +++ /dev/null @@ -1,39 +0,0 @@ -#![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 deleted file mode 100644 index 92abf0bff..000000000 --- a/vendor/tracing-subscriber/tests/vec.rs +++ /dev/null @@ -1,19 +0,0 @@ -#![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 deleted file mode 100644 index 10467cb7d..000000000 --- a/vendor/tracing-subscriber/tests/vec_subscriber_filter_interests_cached.rs +++ /dev/null @@ -1,117 +0,0 @@ -#![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/.cargo-checksum.json b/vendor/tracing/.cargo-checksum.json index 404d4f3c0..8bfa395bb 100644 --- a/vendor/tracing/.cargo-checksum.json +++ b/vendor/tracing/.cargo-checksum.json @@ -1 +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 +{"files":{"CHANGELOG.md":"dc8a552611986799a0eebb56fd119ea8ae32c9b8da4be9914fe127627ad4f38a","Cargo.toml":"f8d12a05bc7ed5cfef787db206c05cc5ebcd2c165e6e629b613d1a3ff4f138de","LICENSE":"898b1ae9821e98daf8964c8d6c7f61641f5f5aa78ad500020771c0939ee0dea1","README.md":"6ff17749109fbe0855220188015846f40d3997bc2631c3163496c183c0bd50ce","benches/baseline.rs":"43a3e31b6c33dba2e6328052301b707b212487b83f0dcffc843061a9c48a2319","benches/dispatch_get_clone.rs":"866239abeb74a82440741c948a4e7e0a44e92e8cc87319ec57e3b057c9e8f5dd","benches/dispatch_get_ref.rs":"dd2803259a6784c256304e676bbce05de233e4c8451ac85863787213343e9be7","benches/empty_span.rs":"9f51cf376414ea751b2f50c357f2435a545d606118286f5b8b89f185e28aad8c","benches/enter_span.rs":"4410ec73d277e7b54e9f306c00ff3b79a150d1832966b7fc29984c8e3ad8d57c","benches/event.rs":"98de3c82ed18abe0a3cbe6eda9a4f9deec2b69bca42c3aac11dea4b608b85a67","benches/shared.rs":"2623311af7d153685064e664a5903d03e7dc3179754c324f3a76f29f060515e6","benches/span_fields.rs":"9166cd43ef2783e5419dd61ea57a02e48e8cc38aa1b357e9b79fa581929b60d8","benches/span_no_fields.rs":"79cc4befacf27d7ce728246087c4f06a6066f913e831d9043caeb7941f0193f6","benches/span_repeated.rs":"e4b3c99a7a9fc15d9042b8db399a56cf647b4eebd26f29d95325bb057b68330b","src/dispatcher.rs":"a8732392ffe56b1178f8fd3d6e6e02d40b51475c38bb4600abd9cd170df1bf6c","src/field.rs":"55c7a2798b9ad0269e7c738c3f15a5d0281bf34ac3a6196a3f0b15801e5278bd","src/instrument.rs":"1fe4de5c13b5ba048e9872d78d1fa4e85655f9f2ed10f79b72b5da881c9b8b45","src/level_filters.rs":"baae8e797897bae9cdd9ec64b8e9a3d71156e9c03261be17b5b18acba034e154","src/lib.rs":"325d0d9487ecd646a7e5e22617f5f291c4112e0e7e359e0033de1677217c5b22","src/macros.rs":"1b38906bcb32cad50b60d350c88f6f4f1fa4d46d99bf50318c44d75219760c42","src/span.rs":"372695b3eda8354a892316826d2598f821fcb835fb18e1e0271767bce730b7ad","src/stdlib.rs":"248514a9bae6106e436358aee44c92abf8e7f79022895c4a25136ddef211d198","src/subscriber.rs":"8933d8766439f929c0a98a0863d20aff37b221314b3825edd9058be511149968","tests/enabled.rs":"1333339aace87ea9d701f2f76a1985820cc513a75013a7ed89669f7a2c635479","tests/event.rs":"7678d1cc8a29ae8b716fbddb7cc4836422732ba3dce109ff511c8bb6100da606","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":"8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"}
\ No newline at end of file diff --git a/vendor/tracing/CHANGELOG.md b/vendor/tracing/CHANGELOG.md index 1c527f2de..978e0ca55 100644 --- a/vendor/tracing/CHANGELOG.md +++ b/vendor/tracing/CHANGELOG.md @@ -1,3 +1,85 @@ +# 0.1.37 (October 6, 2022) + +This release of `tracing` incorporates changes from `tracing-core` +[v0.1.30][core-0.1.30] and `tracing-attributes` [v0.1.23][attrs-0.1.23], +including the new `Subscriber::on_register_dispatch` method for performing late +initialization after a `Subscriber` is registered as a `Dispatch`, and bugfixes +for the `#[instrument]` attribute. Additionally, it fixes instances of the +`bare_trait_objects` lint, which is now a warning on `tracing`'s MSRV and will +become an error in the next edition. + +### Fixed + +- **attributes**: Incorrect handling of inner attributes in `#[instrument]`ed + functions ([#2307]) +- **attributes**: Incorrect location of compiler diagnostic spans generated for + type errors in `#[instrument]`ed `async fn`s ([#2270]) +- **attributes**: Updated `syn` dependency to fix compilation with `-Z + minimal-versions` ([#2246]) +- `bare_trait_objects` warning in `valueset!` macro expansion ([#2308]) + +### Added + +- **core**: `Subscriber::on_register_dispatch` method ([#2269]) +- **core**: `WeakDispatch` type and `Dispatch::downgrade()` function ([#2293]) + +### Changed + +- `tracing-core`: updated to [0.1.30][core-0.1.30] +- `tracing-attributes`: updated to [0.1.23][attrs-0.1.23] + +### Documented + +- Added [`tracing-web`] and [`reqwest-tracing`] to related crates ([#2283], + [#2331]) + +Thanks to new contributors @compiler-errors, @e-nomem, @WorldSEnder, @Xiami2012, +and @tl-rodrigo-gryzinski, as well as @jswrenn and @CAD97, for contributing to +this release! + +[core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 +[attrs-0.1.23]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.23 +[`tracing-web`]: https://crates.io/crates/tracing-web/ +[`reqwest-tracing`]: https://crates.io/crates/reqwest-tracing/ +[#2246]: https://github.com/tokio-rs/tracing/pull/2246 +[#2269]: https://github.com/tokio-rs/tracing/pull/2269 +[#2283]: https://github.com/tokio-rs/tracing/pull/2283 +[#2270]: https://github.com/tokio-rs/tracing/pull/2270 +[#2293]: https://github.com/tokio-rs/tracing/pull/2293 +[#2307]: https://github.com/tokio-rs/tracing/pull/2307 +[#2308]: https://github.com/tokio-rs/tracing/pull/2308 +[#2331]: https://github.com/tokio-rs/tracing/pull/2331 + +# 0.1.36 (July 29, 2022) + +This release adds support for owned values and fat pointers as arguments to the +`Span::record` method, as well as updating the minimum `tracing-core` version +and several documentation improvements. + +### Fixed + +- Incorrect docs in `dispatcher::set_default` ([#2220]) +- Compilation with `-Z minimal-versions` ([#2246]) + +### Added + +- Support for owned values and fat pointers in `Span::record` ([#2212]) +- Documentation improvements ([#2208], [#2163]) + +### Changed + +- `tracing-core`: updated to [0.1.29][core-0.1.29] + +Thanks to @fredr, @cgbur, @jyn514, @matklad, and @CAD97 for contributing to this +release! + +[core-0.1.29]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.29 +[#2220]: https://github.com/tokio-rs/tracing/pull/2220 +[#2246]: https://github.com/tokio-rs/tracing/pull/2246 +[#2212]: https://github.com/tokio-rs/tracing/pull/2212 +[#2208]: https://github.com/tokio-rs/tracing/pull/2208 +[#2163]: https://github.com/tokio-rs/tracing/pull/2163 + # 0.1.35 (June 8, 2022) This release reduces the overhead of callsite registration by using new diff --git a/vendor/tracing/Cargo.toml b/vendor/tracing/Cargo.toml index dd6f1fb47..90f084d8e 100644 --- a/vendor/tracing/Cargo.toml +++ b/vendor/tracing/Cargo.toml @@ -13,7 +13,7 @@ edition = "2018" rust-version = "1.49.0" name = "tracing" -version = "0.1.35" +version = "0.1.37" authors = [ "Eliza Weisman <eliza@buoyant.io>", "Tokio Contributors <team@tokio.rs>", @@ -52,37 +52,65 @@ rustc-args = [ ] [[bench]] -name = "subscriber" +name = "baseline" harness = false [[bench]] -name = "no_subscriber" +name = "dispatch_get_clone" +harness = false + +[[bench]] +name = "dispatch_get_ref" +harness = false + +[[bench]] +name = "empty_span" +harness = false + +[[bench]] +name = "enter_span" +harness = false + +[[bench]] +name = "event" +harness = false + +[[bench]] +name = "span_fields" +harness = false + +[[bench]] +name = "span_no_fields" +harness = false + +[[bench]] +name = "span_repeated" harness = false [dependencies.cfg-if] version = "1.0.0" [dependencies.log] -version = "0.4" +version = "0.4.17" optional = true [dependencies.pin-project-lite] -version = "0.2" +version = "0.2.9" [dependencies.tracing-attributes] -version = "0.1.20" +version = "0.1.23" optional = true [dependencies.tracing-core] -version = "0.1.27" +version = "0.1.30" default-features = false [dev-dependencies.criterion] -version = "0.3" -default_features = false +version = "0.3.6" +default-features = false [dev-dependencies.log] -version = "0.4" +version = "0.4.17" [features] async-await = [] diff --git a/vendor/tracing/README.md b/vendor/tracing/README.md index 5da141a3a..694734577 100644 --- a/vendor/tracing/README.md +++ b/vendor/tracing/README.md @@ -16,9 +16,9 @@ Application-level tracing for Rust. [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 +[crates-url]: https://crates.io/crates/tracing [docs-badge]: https://docs.rs/tracing/badge.svg -[docs-url]: https://docs.rs/tracing/0.1.35 +[docs-url]: https://docs.rs/tracing [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 @@ -250,7 +250,7 @@ my_future 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) +[`#[instrument]`](https://docs.rs/tracing/0.1.37/tracing/attr.instrument.html) attribute: ```rust @@ -297,7 +297,7 @@ span.in_scope(|| { // 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 +The [`#[instrument]`](https://docs.rs/tracing/0.1.37/tracing/attr.instrument.html) attribute macro can reduce some of this boilerplate: ```rust @@ -395,6 +395,7 @@ maintained by the `tokio` project. These include: 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]. +- [`tracing-logfmt`] provides a layer that formats events and spans into the logfmt format. 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! @@ -424,6 +425,7 @@ please let us know! We'd love to add your project to the list! [Sentry]: https://sentry.io/welcome/ [`tracing-loki`]: https://crates.io/crates/tracing-loki [Grafana Loki]: https://grafana.com/oss/loki/ +[`tracing-logfmt`]: https://crates.io/crates/tracing-logfmt **Note:** that some of the ecosystem crates are currently unreleased and undergoing active development. They may be less stable than `tracing` and diff --git a/vendor/tracing/benches/baseline.rs b/vendor/tracing/benches/baseline.rs new file mode 100644 index 000000000..93c14f422 --- /dev/null +++ b/vendor/tracing/benches/baseline.rs @@ -0,0 +1,24 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +fn bench(c: &mut Criterion) { + use std::sync::atomic::{AtomicUsize, Ordering}; + + let mut group = c.benchmark_group("comparison"); + 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(); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/dispatch_get_clone.rs b/vendor/tracing/benches/dispatch_get_clone.rs new file mode 100644 index 000000000..15577c696 --- /dev/null +++ b/vendor/tracing/benches/dispatch_get_clone.rs @@ -0,0 +1,15 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +mod shared; + +fn bench(c: &mut Criterion) { + shared::for_all_dispatches(&mut c.benchmark_group("Dispatch::get_clone"), |b| { + b.iter(|| { + let current = tracing::dispatcher::get_default(|current| current.clone()); + black_box(current); + }) + }); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/dispatch_get_ref.rs b/vendor/tracing/benches/dispatch_get_ref.rs new file mode 100644 index 000000000..a59c34379 --- /dev/null +++ b/vendor/tracing/benches/dispatch_get_ref.rs @@ -0,0 +1,16 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +mod shared; + +fn bench(c: &mut Criterion) { + shared::for_all_dispatches(&mut c.benchmark_group("Dispatch::get_ref"), |b| { + b.iter(|| { + tracing::dispatcher::get_default(|current| { + black_box(¤t); + }) + }) + }); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/empty_span.rs b/vendor/tracing/benches/empty_span.rs new file mode 100644 index 000000000..fb38b08e1 --- /dev/null +++ b/vendor/tracing/benches/empty_span.rs @@ -0,0 +1,43 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +mod shared; + +fn bench(c: &mut Criterion) { + let mut group = c.benchmark_group("empty_span"); + shared::for_all_dispatches(&mut group, |b| { + b.iter(|| { + let span = tracing::span::Span::none(); + black_box(&span); + }) + }); + group.bench_function("baseline_struct", |b| { + b.iter(|| { + let span = FakeEmptySpan::new(); + black_box(&span); + }) + }); +} + +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); + } +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/enter_span.rs b/vendor/tracing/benches/enter_span.rs new file mode 100644 index 000000000..757350a53 --- /dev/null +++ b/vendor/tracing/benches/enter_span.rs @@ -0,0 +1,16 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use tracing::{span, Level}; + +mod shared; + +fn bench(c: &mut Criterion) { + shared::for_all_dispatches(&mut c.benchmark_group("enter_span"), |b| { + let span = span!(Level::TRACE, "span"); + b.iter(|| { + let _span = span.enter(); + }) + }); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/event.rs b/vendor/tracing/benches/event.rs new file mode 100644 index 000000000..164932548 --- /dev/null +++ b/vendor/tracing/benches/event.rs @@ -0,0 +1,12 @@ +use criterion::{criterion_group, criterion_main, Criterion}; + +mod shared; + +fn bench(c: &mut Criterion) { + shared::for_all_recording(&mut c.benchmark_group("event"), |b| { + b.iter(|| tracing::info!("hello world!")) + }); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/global_subscriber.rs b/vendor/tracing/benches/global_subscriber.rs deleted file mode 100644 index 83519610a..000000000 --- a/vendor/tracing/benches/global_subscriber.rs +++ /dev/null @@ -1,136 +0,0 @@ -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 deleted file mode 100644 index e0f82b56a..000000000 --- a/vendor/tracing/benches/no_subscriber.rs +++ /dev/null @@ -1,101 +0,0 @@ -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/shared.rs b/vendor/tracing/benches/shared.rs new file mode 100644 index 000000000..56508c4ab --- /dev/null +++ b/vendor/tracing/benches/shared.rs @@ -0,0 +1,160 @@ +#![allow(dead_code)] +use criterion::{black_box, measurement::WallTime, Bencher}; +use tracing::{field, span, Event, Id, Metadata}; + +use std::{ + fmt::{self, Write}, + sync::{Mutex, MutexGuard}, +}; + +pub fn for_all_recording( + group: &mut criterion::BenchmarkGroup<'_, WallTime>, + mut iter: impl FnMut(&mut Bencher<'_, WallTime>), +) { + // first, run benchmarks with no subscriber + group.bench_function("none", &mut iter); + + // then, run benchmarks with a scoped default subscriber + tracing::subscriber::with_default(EnabledSubscriber, || { + group.bench_function("scoped", &mut iter) + }); + + let subscriber = VisitingSubscriber(Mutex::new(String::from(""))); + tracing::subscriber::with_default(subscriber, || { + group.bench_function("scoped_recording", &mut iter); + }); + + // finally, set a global default subscriber, and run the benchmarks again. + tracing::subscriber::set_global_default(EnabledSubscriber) + .expect("global default should not have already been set!"); + let _ = log::set_logger(&NOP_LOGGER); + log::set_max_level(log::LevelFilter::Trace); + group.bench_function("global", &mut iter); +} + +pub fn for_all_dispatches( + group: &mut criterion::BenchmarkGroup<'_, WallTime>, + mut iter: impl FnMut(&mut Bencher<'_, WallTime>), +) { + // first, run benchmarks with no subscriber + group.bench_function("none", &mut iter); + + // then, run benchmarks with a scoped default subscriber + tracing::subscriber::with_default(EnabledSubscriber, || { + group.bench_function("scoped", &mut iter) + }); + + // finally, set a global default subscriber, and run the benchmarks again. + tracing::subscriber::set_global_default(EnabledSubscriber) + .expect("global default should not have already been set!"); + let _ = log::set_logger(&NOP_LOGGER); + log::set_max_level(log::LevelFilter::Trace); + group.bench_function("global", &mut iter); +} + +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(()) + } +} + +/// 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) { + 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; + } +} + +/// 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; + } +} diff --git a/vendor/tracing/benches/span_fields.rs b/vendor/tracing/benches/span_fields.rs new file mode 100644 index 000000000..5ad828982 --- /dev/null +++ b/vendor/tracing/benches/span_fields.rs @@ -0,0 +1,23 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use tracing::{span, Level}; + +mod shared; + +fn bench(c: &mut Criterion) { + shared::for_all_recording(&mut c.benchmark_group("span_fields"), |b| { + b.iter(|| { + let span = span!( + Level::TRACE, + "span", + foo = "foo", + bar = "bar", + baz = 3, + quuux = tracing::field::debug(0.99) + ); + criterion::black_box(span) + }) + }); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/span_no_fields.rs b/vendor/tracing/benches/span_no_fields.rs new file mode 100644 index 000000000..8a1ff6e04 --- /dev/null +++ b/vendor/tracing/benches/span_no_fields.rs @@ -0,0 +1,13 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use tracing::{span, Level}; + +mod shared; + +fn bench(c: &mut Criterion) { + shared::for_all_recording(&mut c.benchmark_group("span_no_fields"), |b| { + b.iter(|| span!(Level::TRACE, "span")) + }); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/span_repeated.rs b/vendor/tracing/benches/span_repeated.rs new file mode 100644 index 000000000..4c6ac409d --- /dev/null +++ b/vendor/tracing/benches/span_repeated.rs @@ -0,0 +1,20 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tracing::{span, Level}; + +mod shared; + +fn bench(c: &mut Criterion) { + shared::for_all_recording(&mut c.benchmark_group("span_repeated"), |b| { + let n = black_box(N_SPANS); + b.iter(|| (0..n).fold(mk_span(0), |_, i| mk_span(i as u64))) + }); +} + +#[inline] +fn mk_span(i: u64) -> tracing::Span { + span!(Level::TRACE, "span", i = i) +} + +const N_SPANS: usize = 100; +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/vendor/tracing/benches/subscriber.rs b/vendor/tracing/benches/subscriber.rs deleted file mode 100644 index c6418010f..000000000 --- a/vendor/tracing/benches/subscriber.rs +++ /dev/null @@ -1,189 +0,0 @@ -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 index 8817ac033..a84b99f4e 100644 --- a/vendor/tracing/src/dispatcher.rs +++ b/vendor/tracing/src/dispatcher.rs @@ -133,7 +133,7 @@ pub use tracing_core::dispatcher::with_default; #[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, + get_default, set_global_default, Dispatch, SetGlobalDefaultError, WeakDispatch, }; /// Private API for internal use by tracing's macros. diff --git a/vendor/tracing/src/lib.rs b/vendor/tracing/src/lib.rs index 4743eba20..342e04a82 100644 --- a/vendor/tracing/src/lib.rs +++ b/vendor/tracing/src/lib.rs @@ -727,6 +727,8 @@ //! 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`)]. +//! - [`tracing-web`] provides a layer implementation of level-aware logging of events +//! to web browsers' `console.*` and span events to the [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. @@ -743,6 +745,8 @@ //! - [`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]. +//! - [`tracing-logfmt`] provides a layer that formats events and spans into the logfmt format. +//! - [`reqwest-tracing`] provides a middleware to trace [`reqwest`] HTTP requests. //! //! 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! @@ -760,6 +764,7 @@ //! [`tracing-bunyan-formatter`]: https://crates.io/crates/tracing-bunyan-formatter //! [bunyan]: https://github.com/trentm/node-bunyan //! [`tracing-wasm`]: https://docs.rs/tracing-wasm +//! [`tracing-web`]: https://docs.rs/tracing-web //! [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 @@ -779,6 +784,9 @@ //! [`tracing-forest`]: https://crates.io/crates/tracing-forest //! [`tracing-loki`]: https://crates.io/crates/tracing-loki //! [Grafana Loki]: https://grafana.com/oss/loki/ +//! [`tracing-logfmt`]: https://crates.io/crates/tracing-logfmt +//! [`reqwest-tracing`]: https://crates.io/crates/reqwest-tracing +//! [`reqwest`]: https://crates.io/crates/reqwest //! //! <pre class="ignore" style="white-space:normal;font:inherit;"> //! <strong>Note</strong>: Some of these ecosystem crates are currently @@ -809,7 +817,7 @@ //! //! ```toml //! [dependencies] -//! tracing = { version = "0.1.35", default-features = false } +//! tracing = { version = "0.1.37", default-features = false } //! ``` //! //! <pre class="ignore" style="white-space:normal;font:inherit;"> @@ -892,7 +900,7 @@ //! [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_root_url = "https://docs.rs/tracing/0.1.37")] #![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/" diff --git a/vendor/tracing/src/macros.rs b/vendor/tracing/src/macros.rs index b134af6e8..f3968e5c1 100644 --- a/vendor/tracing/src/macros.rs +++ b/vendor/tracing/src/macros.rs @@ -2193,79 +2193,79 @@ macro_rules! valueset { // }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = ?$val:expr, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&debug(&$val) as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = %$val:expr, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&display(&$val) as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = $val:expr, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&$val as &Value)) }, + @ { $($out),*, (&$next, Some(&$val as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&$($k).+ as &Value)) }, + @ { $($out),*, (&$next, Some(&$($k).+ as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, ?$($k:ident).+, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&debug(&$($k).+) as &Value)) }, + @ { $($out),*, (&$next, Some(&debug(&$($k).+) as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, %$($k:ident).+, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&display(&$($k).+) as &Value)) }, + @ { $($out),*, (&$next, Some(&display(&$($k).+) as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = ?$val:expr) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&debug(&$val) as &dyn Value)) }, $next, ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = %$val:expr) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&display(&$val) as &dyn Value)) }, $next, ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = $val:expr) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&$val as &Value)) }, + @ { $($out),*, (&$next, Some(&$val as &dyn Value)) }, $next, ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&$($k).+ as &Value)) }, + @ { $($out),*, (&$next, Some(&$($k).+ as &dyn Value)) }, $next, ) }; (@ { $(,)* $($out:expr),* }, $next:expr, ?$($k:ident).+) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&debug(&$($k).+) as &Value)) }, + @ { $($out),*, (&$next, Some(&debug(&$($k).+) as &dyn Value)) }, $next, ) }; (@ { $(,)* $($out:expr),* }, $next:expr, %$($k:ident).+) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&display(&$($k).+) as &Value)) }, + @ { $($out),*, (&$next, Some(&display(&$($k).+) as &dyn Value)) }, $next, ) }; @@ -2273,47 +2273,47 @@ macro_rules! valueset { // Handle literal names (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = ?$val:expr, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&debug(&$val) as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = %$val:expr, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&display(&$val) as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = $val:expr, $($rest:tt)*) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&$val as &Value)) }, + @ { $($out),*, (&$next, Some(&$val as &dyn Value)) }, $next, $($rest)* ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = ?$val:expr) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&debug(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&debug(&$val) as &dyn Value)) }, $next, ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = %$val:expr) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&display(&$val) as &Value)) }, + @ { $($out),*, (&$next, Some(&display(&$val) as &dyn Value)) }, $next, ) }; (@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = $val:expr) => { $crate::valueset!( - @ { $($out),*, (&$next, Some(&$val as &Value)) }, + @ { $($out),*, (&$next, Some(&$val as &dyn 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, ) + $crate::valueset!(@ { (&$next, Some(&format_args!($($rest)+) as &dyn Value)), $($out),* }, $next, ) }; // === entry === diff --git a/vendor/tracing/src/span.rs b/vendor/tracing/src/span.rs index 21358eb27..58822f4d9 100644 --- a/vendor/tracing/src/span.rs +++ b/vendor/tracing/src/span.rs @@ -1136,7 +1136,7 @@ impl Span { /// /// // Now, record a value for parting as well. /// // (note that the field name is passed as a string slice) - /// span.record("parting", &"goodbye world!"); + /// span.record("parting", "goodbye world!"); /// ``` /// However, it may also be used to record a _new_ value for a field whose /// value was already recorded: @@ -1154,7 +1154,7 @@ impl Span { /// } /// Err(_) => { /// // Things are no longer okay! - /// span.record("is_okay", &false); + /// span.record("is_okay", false); /// } /// } /// ``` @@ -1181,17 +1181,17 @@ impl Span { /// // 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"); + /// 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"); + /// 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 + pub fn record<Q: ?Sized, V>(&self, field: &Q, value: V) -> &Self where Q: field::AsField, V: field::Value, @@ -1201,7 +1201,7 @@ impl Span { self.record_all( &meta .fields() - .value_set(&[(&field, Some(value as &dyn field::Value))]), + .value_set(&[(&field, Some(&value as &dyn field::Value))]), ); } } @@ -1614,4 +1614,10 @@ mod test { impl AssertSync for Span {} impl AssertSync for Entered<'_> {} impl AssertSync for EnteredSpan {} + + #[test] + fn test_record_backwards_compat() { + Span::current().record("some-key", &"some text"); + Span::current().record("some-key", &false); + } } diff --git a/vendor/tracing/src/subscriber.rs b/vendor/tracing/src/subscriber.rs index 343dc5914..f55698d13 100644 --- a/vendor/tracing/src/subscriber.rs +++ b/vendor/tracing/src/subscriber.rs @@ -5,12 +5,12 @@ pub use tracing_core::subscriber::*; #[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. +/// Sets this [`Subscriber`] as the default for the current thread 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. +/// [`Event`]. +/// /// /// [`Span`]: super::span::Span /// [`Subscriber`]: super::subscriber::Subscriber @@ -43,13 +43,10 @@ where 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`] +/// Sets the [`Subscriber`] as the default for the current thread 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. +/// The default subscriber is used when creating a new [`Span`] or [`Event`]. /// /// [`Span`]: super::span::Span /// [`Subscriber`]: super::subscriber::Subscriber diff --git a/vendor/tracing/tests/event.rs b/vendor/tracing/tests/event.rs index ffb63c816..61df19ad3 100644 --- a/vendor/tracing/tests/event.rs +++ b/vendor/tracing/tests/event.rs @@ -474,3 +474,27 @@ fn option_ref_mut_values() { handle.assert_finished(); } + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] +#[test] +fn string_field() { + let (subscriber, handle) = subscriber::mock() + .event(event::mock().with_fields(field::mock("my_string").with_value(&"hello").only())) + .event( + event::mock().with_fields(field::mock("my_string").with_value(&"hello world!").only()), + ) + .done() + .run_with_handle(); + with_default(subscriber, || { + let mut my_string = String::from("hello"); + + tracing::event!(Level::INFO, my_string); + + // the string is not moved by using it as a field! + my_string.push_str(" world!"); + + tracing::event!(Level::INFO, my_string); + }); + + handle.assert_finished(); +} |