From 71d55ce50d0e80937ef39e5c47b7175699a78688 Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Tue, 29 Mar 2022 14:49:31 -0700 Subject: [PATCH] More layer modification --- .../src/filter/subscriber_filters/mod.rs | 43 +++++++++++++++- tracing-subscriber/src/fmt/fmt_subscriber.rs | 49 +++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/tracing-subscriber/src/filter/subscriber_filters/mod.rs b/tracing-subscriber/src/filter/subscriber_filters/mod.rs index 266b6493a2..463122a52f 100644 --- a/tracing-subscriber/src/filter/subscriber_filters/mod.rs +++ b/tracing-subscriber/src/filter/subscriber_filters/mod.rs @@ -473,8 +473,7 @@ impl Filtered { /// Mutably borrows the [`Filter`](crate::subscribe::Filter) used by this subscriber. /// - /// When this subscriber can be mutably borrowed, this may be used to mutate the filter. - /// Generally, this will primarily be used with the + /// This method is primarily expected to be used with the /// [`reload::Handle::modify`](crate::reload::Handle::modify) method. /// /// # Examples @@ -499,6 +498,46 @@ impl Filtered { pub fn filter_mut(&mut self) -> &mut F { &mut self.filter } + + /// Borrows the inner [subscriber] wrapped by this `Filtered` subscriber. + /// + /// [subscriber]: Subscribe + pub fn inner(&self) -> &S { + &self.subscriber + } + + /// Mutably borrows the inner [subscriber] wrapped by this `Filtered` subscriber. + /// + /// 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(writer: T) -> (fn() -> std::io::Stdout) { + /// # std::io::stdout + /// # } + /// # fn main() { + /// let filtered_subscriber = fmt::subscriber().with_writer(non_blocking(std::io::stderr())).with_filter(filter::LevelFilter::INFO); + /// let (filtered_subscriber, reload_handle) = reload::Subscriber::new(filtered_subscriber); + /// # + /// # // specifying the Registry type is required + /// # let _: &reload::Handle std::io::Stdout>, + /// # filter::LevelFilter, Registry>> + /// # = &reload_handle; + /// # + /// info!("This will be logged to stderr"); + /// reload_handle.modify(|subscriber| *subscriber.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 S { + &mut self.subscriber + } } impl Subscribe for Filtered diff --git a/tracing-subscriber/src/fmt/fmt_subscriber.rs b/tracing-subscriber/src/fmt/fmt_subscriber.rs index 03b7cbfe65..ad60e198b4 100644 --- a/tracing-subscriber/src/fmt/fmt_subscriber.rs +++ b/tracing-subscriber/src/fmt/fmt_subscriber.rs @@ -157,6 +157,55 @@ impl Subscriber { } } + /// Borrows the [writer] for this subscriber. + /// + /// [writer]: MakeWriter + pub fn writer(&self) -> &W { + &self.make_writer + } + + /// Mutably borrows the [writer] for this subscriber. + /// + /// 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(writer: T) -> (fn() -> std::io::Stdout) { + /// # std::io::stdout + /// # } + /// # fn main() { + /// let subscriber = fmt::subscriber().with_writer(non_blocking(std::io::stderr())); + /// let (subscriber, reload_handle) = reload::Subscriber::new(subscriber); + /// # + /// # // specifying the Registry type is required + /// # let _: &reload::Handle> = &reload_handle; + /// # + /// info!("This will be logged to stderr"); + /// reload_handle.modify(|subscriber| *subscriber.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 + } + + /// Changes whether this should use ansi 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 subscriber to support [`libtest`'s output capturing][capturing] when used in /// unit tests. ///