mirror of
https://github.com/openharmony/third_party_rust_tracing.git
synced 2026-07-01 21:24:06 -04:00
fmt: add reload::Handle::with_current (#91)
## Motivation In some cases, when filter reloading is in use, it can be valuable to inspect the current value of a filter without reloading it. For example, this could be used to format the current state, or call filter-specific methods on it. ## Solution This branch adds a new `Handle::with_current` method. This method invokes a closure with a borrowed reference to the filter's current state and returns the result. This does not require acquiring a write lock. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
@@ -105,7 +105,17 @@ where
|
||||
where
|
||||
F: Clone,
|
||||
{
|
||||
self.inner.upgrade().map(|inner| inner.read().clone())
|
||||
self.with_current(F::clone).ok()
|
||||
}
|
||||
|
||||
/// Invokes a closure with a borrowed reference to the current filter,
|
||||
/// returning the result (or an error if the filter no longer exists).
|
||||
pub fn with_current<T>(&self, f: impl FnOnce(&F) -> T) -> Result<T, Error> {
|
||||
let inner = self.inner.upgrade().ok_or(Error {
|
||||
kind: ErrorKind::SubscriberGone,
|
||||
})?;
|
||||
let inner = inner.read();
|
||||
Ok(f(&*inner))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user