Skip to content

Commit

Permalink
More layer modification
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Mar 30, 2022
1 parent a3e7944 commit 097b448
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tracing-subscriber/src/filter/subscriber_filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,43 @@ impl<S, F, C> Filtered<S, F, C> {
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<T: std::io::Write>(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<filter::Filtered<fmt::Subscriber<Registry, _, _, fn() -> 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<C, S, F> Subscribe<C> for Filtered<S, F, C>
Expand Down
46 changes: 46 additions & 0 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,52 @@ impl<C, N, E, W> Subscriber<C, N, E, W> {
}
}

/// 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<T: std::io::Write>(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<fmt::Subscriber<Registry, _, _, _>> = &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.
///
Expand Down

0 comments on commit 097b448

Please sign in to comment.