add interface for current log filter; fix console suppression

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-11 01:02:05 +00:00
parent 160f48043e
commit bc58e5002d
2 changed files with 23 additions and 5 deletions

View File

@ -22,10 +22,14 @@ use crate::{error, Result};
///
/// [1]: <https://github.com/tokio-rs/tracing/pull/1035/commits/8a87ea52425098d3ef8f56d92358c2f6c144a28f>
pub trait ReloadHandle<L> {
fn current(&self) -> Option<L>;
fn reload(&self, new_value: L) -> Result<(), reload::Error>;
}
impl<L, S> ReloadHandle<L> for reload::Handle<L, S> {
impl<L: Clone, S> ReloadHandle<L> for reload::Handle<L, S> {
fn current(&self) -> Option<L> { Self::clone_current(self) }
fn reload(&self, new_value: L) -> Result<(), reload::Error> { Self::reload(self, new_value) }
}
@ -57,6 +61,15 @@ impl LogLevelReloadHandles {
Ok(())
}
#[must_use]
pub fn current(&self, name: &str) -> Option<EnvFilter> {
self.handles
.lock()
.expect("locked")
.get(name)
.map(|handle| handle.current())?
}
}
impl Default for LogLevelReloadHandles {

View File

@ -10,16 +10,21 @@ pub struct Suppress {
impl Suppress {
pub fn new(server: &Arc<Server>) -> Self {
let handle = "console";
let config = &server.config.log;
Self::from_filters(server, EnvFilter::try_new(config).unwrap_or_default(), &EnvFilter::default())
}
let suppress = EnvFilter::default();
let restore = server
.log
.reload
.current(handle)
.unwrap_or_else(|| EnvFilter::try_new(config).unwrap_or_default());
fn from_filters(server: &Arc<Server>, restore: EnvFilter, suppress: &EnvFilter) -> Self {
server
.log
.reload
.reload(suppress, Some(&["console"]))
.reload(&suppress, Some(&[handle]))
.expect("log filter reloaded");
Self {
server: server.clone(),
restore,