Skip to content

Commit b1977ea

Browse files
wagenethawkw
authored andcommitted
subscriber: Add Filtered and fmt::Layer accessors (#2034)
This adds more modification methods for use with reloading. Replaces #2032. ## Motivation I have a `Filtered` layer that I'd like to modify with a reload handle. If I use `reload` then the filter doesn't work. If I use `modify` with `filter_mut` I can't update the writer. ## Solution Adds some additional accessor methods to allow the writer to be modified in `modify`: * `Filtered::inner` * `Filtered::inner_mut` * `fmt::Layer::writer` * `fmt::Layer::writer_mut` * `fmt::Layer::set_ansi`
1 parent 5cd4b57 commit b1977ea

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,44 @@ impl<L, F, S> Filtered<L, F, S> {
504504
pub fn filter_mut(&mut self) -> &mut F {
505505
&mut self.filter
506506
}
507+
508+
/// Borrows the inner [`Layer`] wrapped by this `Filtered` layer.
509+
pub fn inner(&self) -> &L {
510+
&self.layer
511+
}
512+
513+
/// Mutably borrows the inner [`Layer`] wrapped by this `Filtered` layer.
514+
///
515+
/// This method is primarily expected to be used with the
516+
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method.
517+
///
518+
/// # Examples
519+
///
520+
/// ```
521+
/// # use tracing::info;
522+
/// # use tracing_subscriber::{filter,fmt,reload,Registry,prelude::*};
523+
/// # fn non_blocking<T: std::io::Write>(writer: T) -> (fn() -> std::io::Stdout) {
524+
/// # std::io::stdout
525+
/// # }
526+
/// # fn main() {
527+
/// let filtered_layer = fmt::layer().with_writer(non_blocking(std::io::stderr())).with_filter(filter::LevelFilter::INFO);
528+
/// let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer);
529+
/// #
530+
/// # // specifying the Registry type is required
531+
/// # let _: &reload::Handle<filter::Filtered<fmt::Layer<Registry, _, _, fn() -> std::io::Stdout>,
532+
/// # filter::LevelFilter, Registry>, _>
533+
/// # = &reload_handle;
534+
/// #
535+
/// info!("This will be logged to stderr");
536+
/// reload_handle.modify(|layer| *layer.inner_mut().writer_mut() = non_blocking(std::io::stdout()));
537+
/// info!("This will be logged to stdout");
538+
/// # }
539+
/// ```
540+
///
541+
/// [subscriber]: Subscribe
542+
pub fn inner_mut(&mut self) -> &mut L {
543+
&mut self.layer
544+
}
507545
}
508546

509547
impl<S, L, F> Layer<S> for Filtered<L, F, S>

tracing-subscriber/src/fmt/fmt_layer.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,57 @@ impl<S, N, E, W> Layer<S, N, E, W> {
185185
}
186186
}
187187

188-
/// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in
188+
/// Borrows the [writer] for this [`Layer`].
189+
///
190+
/// [writer]: MakeWriter
191+
pub fn writer(&self) -> &W {
192+
&self.make_writer
193+
}
194+
195+
/// Mutably borrows the [writer] for this [`Layer`].
196+
///
197+
/// This method is primarily expected to be used with the
198+
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method.
199+
///
200+
/// # Examples
201+
///
202+
/// ```
203+
/// # use tracing::info;
204+
/// # use tracing_subscriber::{fmt,reload,Registry,prelude::*};
205+
/// # fn non_blocking<T: std::io::Write>(writer: T) -> (fn() -> std::io::Stdout) {
206+
/// # std::io::stdout
207+
/// # }
208+
/// # fn main() {
209+
/// let layer = fmt::layer().with_writer(non_blocking(std::io::stderr()));
210+
/// let (layer, reload_handle) = reload::Layer::new(layer);
211+
/// #
212+
/// # // specifying the Registry type is required
213+
/// # let _: &reload::Handle<fmt::Layer<Registry, _, _, _>, Registry> = &reload_handle;
214+
/// #
215+
/// info!("This will be logged to stderr");
216+
/// reload_handle.modify(|layer| *layer.writer_mut() = non_blocking(std::io::stdout()));
217+
/// info!("This will be logged to stdout");
218+
/// # }
219+
/// ```
220+
///
221+
/// [writer]: MakeWriter
222+
pub fn writer_mut(&mut self) -> &mut W {
223+
&mut self.make_writer
224+
}
225+
226+
/// Sets whether this layer should use ANSI terminal formatting
227+
/// escape codes (such as colors).
228+
///
229+
/// This method is primarily expected to be used with the
230+
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method when changing
231+
/// the writer.
232+
#[cfg(feature = "ansi")]
233+
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
234+
pub fn set_ansi(&mut self, ansi: bool) {
235+
self.is_ansi = ansi;
236+
}
237+
238+
/// Configures the layer to support [`libtest`'s output capturing][capturing] when used in
189239
/// unit tests.
190240
///
191241
/// See [`TestWriter`] for additional details.

0 commit comments

Comments
 (0)