1use log::Log;
23use crate::Filter;
45/// Decorate a [`log::Log`] with record [`Filter`]ing.
6///
7/// Records that match the filter will be forwarded to the wrapped log.
8/// Other records will be ignored.
9#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for FilteredLog<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "FilteredLog",
"log", &self.log, "filter", &&self.filter)
}
}Debug)]
10pub struct FilteredLog<T> {
11 log: T,
12 filter: Filter,
13}
1415impl<T: Log> FilteredLog<T> {
16/// Create a new filtered log.
17pub fn new(log: T, filter: Filter) -> Self {
18Self { log, filter }
19 }
20}
2122impl<T: Log> Logfor FilteredLog<T> {
23/// Determines if a log message with the specified metadata would be logged.
24 ///
25 /// For the wrapped log, this returns `true` only if both the filter and the wrapped log return `true`.
26fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
27self.filter.enabled(metadata) && self.log.enabled(metadata)
28 }
2930/// Logs the record.
31 ///
32 /// Forwards the record to the wrapped log, but only if the record matches the filter.
33fn log(&self, record: &log::Record<'_>) {
34if self.filter.matches(record) {
35self.log.log(record);
36 }
37 }
3839/// Flushes any buffered records.
40 ///
41 /// Forwards directly to the wrapped log.
42fn flush(&self) {
43self.log.flush();
44 }
45}