diff --git a/examples/examples/tower-load.rs b/examples/examples/tower-load.rs index 443f74ddf2..f201283275 100644 --- a/examples/examples/tower-load.rs +++ b/examples/examples/tower-load.rs @@ -185,11 +185,11 @@ impl Service for MakeSvc { } } -struct AdminSvc { - handle: Handle, +struct AdminSvc { + handle: Handle, } -impl Clone for AdminSvc { +impl Clone for AdminSvc { fn clone(&self) -> Self { Self { handle: self.handle.clone(), @@ -197,11 +197,8 @@ impl Clone for AdminSvc { } } -impl<'a, S> Service<&'a AddrStream> for AdminSvc -where - S: tracing::Subscriber, -{ - type Response = AdminSvc; +impl<'a> Service<&'a AddrStream> for AdminSvc { + type Response = AdminSvc; type Error = hyper::Error; type Future = Ready>; @@ -214,10 +211,7 @@ where } } -impl Service> for AdminSvc -where - S: tracing::Subscriber + 'static, -{ +impl Service> for AdminSvc { type Response = Response; type Error = Err; type Future = Pin, Err>> + std::marker::Send>>; @@ -252,10 +246,7 @@ where } } -impl AdminSvc -where - S: tracing::Subscriber + 'static, -{ +impl AdminSvc { fn set_from(&self, bytes: Bytes) -> Result<(), String> { use std::str; let body = str::from_utf8(&bytes.as_ref()).map_err(|e| format!("{}", e))?; diff --git a/tracing-subscriber/src/fmt/mod.rs b/tracing-subscriber/src/fmt/mod.rs index c3f256d69c..541f4c372a 100644 --- a/tracing-subscriber/src/fmt/mod.rs +++ b/tracing-subscriber/src/fmt/mod.rs @@ -137,6 +137,7 @@ use crate::{ filter::LevelFilter, layer, registry::{LookupSpan, Registry}, + reload, }; #[doc(inline)] @@ -646,35 +647,13 @@ impl SubscriberBuilder SubscriberBuilder -where - Formatter: tracing_core::Subscriber + 'static, -{ - /// Configures the subscriber being built to allow filter reloading at - /// runtime. - pub fn with_filter_reloading( - self, - ) -> SubscriberBuilder>, W> - { - let (filter, _) = crate::reload::Layer::new(self.filter); - SubscriberBuilder { - filter, - inner: self.inner, - } - } -} - -#[cfg(feature = "env-filter")] -#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] -impl SubscriberBuilder>, W> +impl SubscriberBuilder, W> where Formatter: tracing_core::Subscriber + 'static, { /// Returns a `Handle` that may be used to reload the constructed subscriber's /// filter. - pub fn reload_handle(&self) -> crate::reload::Handle> { + pub fn reload_handle(&self) -> reload::Handle { self.filter.handle() } } @@ -814,6 +793,51 @@ impl SubscriberBuilder { } } + /// Configures the subscriber being built to allow filter reloading at + /// runtime. + /// + /// The returned builder will have a [`reload_handle`] method, which returns + /// a [`reload::Handle`] that may be used to set a new filter value. + /// + /// For example: + /// + /// ``` + /// use tracing::Level; + /// use tracing_subscriber::prelude::*; + /// + /// let builder = tracing_subscriber::fmt() + /// // Set a max level filter on the subscriber + /// .with_max_level(Level::INFO) + /// .with_filter_reloading(); + /// + /// // Get a handle for modifying the subscriber's max level filter. + /// let handle = builder.reload_handle(); + /// + /// // Finish building the subscriber, and set it as the default. + /// builder.finish().init(); + /// + /// // Currently, the max level is INFO, so this event will be disabled. + /// tracing::debug!("this is not recorded!"); + /// + /// // Use the handle to set a new max level filter. + /// // (this returns an error if the subscriber has been dropped, which shouldn't + /// // happen in this example.) + /// handle.reload(Level::DEBUG).expect("the subscriber should still exist"); + /// + /// // Now, the max level is INFO, so this event will be recorded. + /// tracing::debug!("this is recorded!"); + /// ``` + /// + /// [`reload_handle`]: SubscriberBuilder::reload_handle + /// [`reload::Handle`]: crate::reload::Handle + pub fn with_filter_reloading(self) -> SubscriberBuilder, W> { + let (filter, _) = reload::Layer::new(self.filter); + SubscriberBuilder { + filter, + inner: self.inner, + } + } + /// Sets the function that the subscriber being built should use to format /// events that occur. pub fn event_format(self, fmt_event: E2) -> SubscriberBuilder diff --git a/tracing-subscriber/src/reload.rs b/tracing-subscriber/src/reload.rs index 14a069dae0..c302144dae 100644 --- a/tracing-subscriber/src/reload.rs +++ b/tracing-subscriber/src/reload.rs @@ -16,7 +16,6 @@ use crate::sync::RwLock; use std::{ error, fmt, - marker::PhantomData, sync::{Arc, Weak}, }; use tracing_core::{ @@ -27,20 +26,18 @@ use tracing_core::{ /// Wraps a `Layer`, allowing it to be reloaded dynamically at runtime. #[derive(Debug)] -pub struct Layer { +pub struct Layer { // TODO(eliza): this once used a `crossbeam_util::ShardedRwLock`. We may // eventually wish to replace it with a sharded lock implementation on top // of our internal `RwLock` wrapper type. If possible, we should profile // this first to determine if it's necessary. inner: Arc>, - _s: PhantomData, } /// Allows reloading the state of an associated `Layer`. #[derive(Debug)] -pub struct Handle { +pub struct Handle { inner: Weak>, - _s: PhantomData, } /// Indicates that an error occurred when reloading a layer. @@ -57,7 +54,7 @@ enum ErrorKind { // ===== impl Layer ===== -impl crate::Layer for Layer +impl crate::Layer for Layer where L: crate::Layer + 'static, S: Subscriber, @@ -113,38 +110,28 @@ where } } -impl Layer -where - L: crate::Layer + 'static, - S: Subscriber, -{ +impl Layer { /// Wraps the given `Layer`, returning a `Layer` and a `Handle` that allows /// the inner type to be modified at runtime. - pub fn new(inner: L) -> (Self, Handle) { + pub fn new(inner: L) -> (Self, Handle) { let this = Self { inner: Arc::new(RwLock::new(inner)), - _s: PhantomData, }; let handle = this.handle(); (this, handle) } /// Returns a `Handle` that can be used to reload the wrapped `Layer`. - pub fn handle(&self) -> Handle { + pub fn handle(&self) -> Handle { Handle { inner: Arc::downgrade(&self.inner), - _s: PhantomData, } } } // ===== impl Handle ===== -impl Handle -where - L: crate::Layer + 'static, - S: Subscriber, -{ +impl Handle { /// Replace the current layer with the provided `new_layer`. pub fn reload(&self, new_layer: impl Into) -> Result<(), Error> { self.modify(|layer| { @@ -189,11 +176,10 @@ where } } -impl Clone for Handle { +impl Clone for Handle { fn clone(&self) -> Self { Handle { inner: self.inner.clone(), - _s: PhantomData, } } }