Skip to content

Commit 71d55ce

Browse files
committed
More layer modification
1 parent a3e7944 commit 71d55ce

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ impl<S, F, C> Filtered<S, F, C> {
473473

474474
/// Mutably borrows the [`Filter`](crate::subscribe::Filter) used by this subscriber.
475475
///
476-
/// When this subscriber can be mutably borrowed, this may be used to mutate the filter.
477-
/// Generally, this will primarily be used with the
476+
/// This method is primarily expected to be used with the
478477
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method.
479478
///
480479
/// # Examples
@@ -499,6 +498,46 @@ impl<S, F, C> Filtered<S, F, C> {
499498
pub fn filter_mut(&mut self) -> &mut F {
500499
&mut self.filter
501500
}
501+
502+
/// Borrows the inner [subscriber] wrapped by this `Filtered` subscriber.
503+
///
504+
/// [subscriber]: Subscribe
505+
pub fn inner(&self) -> &S {
506+
&self.subscriber
507+
}
508+
509+
/// Mutably borrows the inner [subscriber] wrapped by this `Filtered` subscriber.
510+
///
511+
/// This method is primarily expected to 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.inner_mut().writer_mut() = non_blocking(std::io::stdout()));
533+
/// info!("This will be logged to stdout");
534+
/// # }
535+
/// ```
536+
///
537+
/// [subscriber]: Subscribe
538+
pub fn inner_mut(&mut self) -> &mut S {
539+
&mut self.subscriber
540+
}
502541
}
503542

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

tracing-subscriber/src/fmt/fmt_subscriber.rs

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

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

0 commit comments

Comments
 (0)