Skip to content

Commit 097b448

Browse files
committed
More layer modification
1 parent a3e7944 commit 097b448

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

tracing-subscriber/src/filter/subscriber_filters/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,43 @@ impl<S, F, C> Filtered<S, F, C> {
499499
pub fn filter_mut(&mut self) -> &mut F {
500500
&mut self.filter
501501
}
502+
503+
/// Borrows the underlying [`Subscribe`] that is being filtered.
504+
pub fn wrapped(&self) -> &S {
505+
&self.subscriber
506+
}
507+
508+
/// Mutably borrows the underlying [`Subscribe`] that is being filtered.
509+
///
510+
/// When this subscriber can be mutably borrowed, this may be used to mutate the underlying
511+
/// subsrcribe. Generally, this will primarily be used with the
512+
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method.
513+
///
514+
/// # Examples
515+
///
516+
/// ```
517+
/// # use tracing::info;
518+
/// # use tracing_subscriber::{filter,fmt,reload,Registry,prelude::*};
519+
/// # fn non_blocking<T: std::io::Write>(writer: T) -> (fn() -> std::io::Stdout) {
520+
/// # std::io::stdout
521+
/// # }
522+
/// # fn main() {
523+
/// let filtered_subscriber = fmt::subscriber().with_writer(non_blocking(std::io::stderr())).with_filter(filter::LevelFilter::INFO);
524+
/// let (filtered_subscriber, reload_handle) = reload::Subscriber::new(filtered_subscriber);
525+
/// #
526+
/// # // specifying the Registry type is required
527+
/// # let _: &reload::Handle<filter::Filtered<fmt::Subscriber<Registry, _, _, fn() -> std::io::Stdout>,
528+
/// # filter::LevelFilter, Registry>>
529+
/// # = &reload_handle;
530+
/// #
531+
/// info!("This will be logged to stderr");
532+
/// reload_handle.modify(|subscriber| *subscriber.wrapped_mut().writer_mut() = non_blocking(std::io::stdout()));
533+
/// info!("This will be logged to stdout");
534+
/// # }
535+
/// ```
536+
pub fn wrapped_mut(&mut self) -> &mut S {
537+
&mut self.subscriber
538+
}
502539
}
503540

504541
impl<C, S, F> Subscribe<C> for Filtered<S, F, C>

tracing-subscriber/src/fmt/fmt_subscriber.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,52 @@ impl<C, N, E, W> Subscriber<C, N, E, W> {
157157
}
158158
}
159159

160+
/// Borrows the underlying [`MakeWriter`] for this subscriber.
161+
pub fn writer(&self) -> &W {
162+
&self.make_writer
163+
}
164+
165+
/// Mutably borrows the underlying [`MakeWriter`] for this subscriber.
166+
///
167+
/// When this subscriber can be mutably borrowed, this may be used to mutate the writer.
168+
/// Generally, this will primarily be used with the
169+
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method.
170+
///
171+
/// # Examples
172+
///
173+
/// ```
174+
/// # use tracing::info;
175+
/// # use tracing_subscriber::{fmt,reload,Registry,prelude::*};
176+
/// # fn non_blocking<T: std::io::Write>(writer: T) -> (fn() -> std::io::Stdout) {
177+
/// # std::io::stdout
178+
/// # }
179+
/// # fn main() {
180+
/// let subscriber = fmt::subscriber().with_writer(non_blocking(std::io::stderr()));
181+
/// let (subscriber, reload_handle) = reload::Subscriber::new(subscriber);
182+
/// #
183+
/// # // specifying the Registry type is required
184+
/// # let _: &reload::Handle<fmt::Subscriber<Registry, _, _, _>> = &reload_handle;
185+
/// #
186+
/// info!("This will be logged to stderr");
187+
/// reload_handle.modify(|subscriber| *subscriber.writer_mut() = non_blocking(std::io::stdout()));
188+
/// info!("This will be logged to stdout");
189+
/// # }
190+
/// ```
191+
pub fn writer_mut(&mut self) -> &mut W {
192+
&mut self.make_writer
193+
}
194+
195+
/// Changes whether this should use ansi colors.
196+
///
197+
/// Generally, this will primarily be used with the
198+
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method when changing
199+
/// the writer.
200+
#[cfg(feature = "ansi")]
201+
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
202+
pub fn set_ansi(&mut self, ansi: bool) {
203+
self.is_ansi = ansi;
204+
}
205+
160206
/// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in
161207
/// unit tests.
162208
///

0 commit comments

Comments
 (0)