From 097b448d40f7b3051c8956552af52ef902faf4c9 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 | 37 +++++++++++++++ tracing-subscriber/src/fmt/fmt_subscriber.rs | 46 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/tracing-subscriber/src/filter/subscriber_filters/mod.rs b/tracing-subscriber/src/filter/subscriber_filters/mod.rs index 266b6493a2..d602c1c52f 100644 --- a/tracing-subscriber/src/filter/subscriber_filters/mod.rs +++ b/tracing-subscriber/src/filter/subscriber_filters/mod.rs @@ -499,6 +499,43 @@ impl Filtered { pub fn filter_mut(&mut self) -> &mut F { &mut self.filter } + + /// Borrows the underlying [`Subscribe`] that is being filtered. + pub fn wrapped(&self) -> &S { + &self.subscriber + } + + /// Mutably borrows the underlying [`Subscribe`] that is being filtered. + /// + /// When this subscriber can be mutably borrowed, this may be used to mutate the underlying + /// subsrcribe. 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 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.wrapped_mut().writer_mut() = non_blocking(std::io::stdout())); + /// info!("This will be logged to stdout"); + /// # } + /// ``` + pub fn wrapped_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..9aeb500dfa 100644 --- a/tracing-subscriber/src/fmt/fmt_subscriber.rs +++ b/tracing-subscriber/src/fmt/fmt_subscriber.rs @@ -157,6 +157,52 @@ impl Subscriber { } } + /// Borrows the underlying [`MakeWriter`] for this subscriber. + pub fn writer(&self) -> &W { + &self.make_writer + } + + /// Mutably borrows the underlying [`MakeWriter`] for this subscriber. + /// + /// When this subscriber can be mutably borrowed, this may be used to mutate the writer. + /// Generally, this will primarily 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"); + /// # } + /// ``` + pub fn writer_mut(&mut self) -> &mut W { + &mut self.make_writer + } + + /// Changes whether this should use ansi colors. + /// + /// Generally, this will primarily 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. ///