Skip to content

Commit 5d53aaf

Browse files
committed
More layer modification
1 parent a3e7944 commit 5d53aaf

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

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

504545
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)