1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
use crate::Record;
use crate::RecordKind;
use itertools::Itertools;
use std::marker::Send;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Trait
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// This trait allows to filter log records ([`Record`]) using [`check`] method which returns [`bool`] value.
/// It should be implemented for structures which are going to be used as filtering part inside [`LoggedStream`].
///
/// [`check`]: RecordFilter::check
/// [`LoggedStream`]: crate::LoggedStream
pub trait RecordFilter: Send + 'static {
/// This method returns [`bool`] value depending on if received log record ([`Record`]) should be processed
/// by logging part inside [`LoggedStream`].
///
/// [`LoggedStream`]: crate::LoggedStream
fn check(&self, record: &Record) -> bool;
}
impl RecordFilter for Box<dyn RecordFilter> {
fn check(&self, record: &Record) -> bool {
(**self).check(record)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DefaultFilter
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// This is default implementation of [`RecordFilter`] trait which [`check`] method always return `true`.
/// It should be constructed using [`Default::default`] method.
///
/// [`check`]: RecordFilter::check
#[derive(Debug, Clone, Copy, Default)]
pub struct DefaultFilter;
impl RecordFilter for DefaultFilter {
fn check(&self, _record: &Record) -> bool {
true
}
}
impl RecordFilter for Box<DefaultFilter> {
fn check(&self, record: &Record) -> bool {
(**self).check(record)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// RecordKindFilter
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// This implementation of [`RecordFilter`] trait accepts allowed log record kinds ([`RecordKind`]) array during
/// construction and its [`check`] method returns `true` if received log record kind presented inside this array.
///
/// [`check`]: RecordFilter::check
#[derive(Debug)]
pub struct RecordKindFilter {
allowed_kinds: Vec<RecordKind>,
}
impl RecordKindFilter {
/// Construct a new instance of [`RecordKindFilter`] using provided array of allowed log record kinds ([`RecordKind`]).
pub fn new(kinds: &'static [RecordKind]) -> Self {
Self {
allowed_kinds: kinds.iter().copied().unique().collect(),
}
}
}
impl RecordFilter for RecordKindFilter {
fn check(&self, record: &Record) -> bool {
self.allowed_kinds.contains(&record.kind)
}
}
impl RecordFilter for Box<RecordKindFilter> {
fn check(&self, record: &Record) -> bool {
(**self).check(record)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use crate::filter::DefaultFilter;
use crate::filter::RecordFilter;
use crate::filter::RecordKindFilter;
use crate::record::Record;
use crate::record::RecordKind;
use std::convert::From;
use std::marker::Send;
use std::marker::Unpin;
fn assert_unpin<T: Unpin>() {}
#[test]
fn test_unpin() {
assert_unpin::<DefaultFilter>();
assert_unpin::<RecordKindFilter>();
}
#[test]
fn test_default_filter() {
let filter = DefaultFilter::default();
assert!(filter.check(&Record::new(
RecordKind::Read,
String::from("01:02:03:04:05:06")
)));
assert!(filter.check(&Record::new(
RecordKind::Write,
String::from("01:02:03:04:05:06")
)));
assert!(filter.check(&Record::new(RecordKind::Drop, String::from("deallocated"))));
assert!(filter.check(&Record::new(
RecordKind::Shutdown,
String::from("write shutdown request")
)));
}
#[test]
fn test_record_kind_filter() {
let filter = RecordKindFilter::new(&[RecordKind::Read]);
assert!(filter.check(&Record::new(
RecordKind::Read,
String::from("01:02:03:04:05:06")
)));
assert!(!filter.check(&Record::new(
RecordKind::Write,
String::from("01:02:03:04:05:06")
)));
assert!(!filter.check(&Record::new(RecordKind::Drop, String::from("deallocated"))));
assert!(!filter.check(&Record::new(
RecordKind::Shutdown,
String::from("write shutdown request")
)));
}
#[test]
fn test_trait_object_safety() {
// Assert traint object construct.
let default: Box<dyn RecordFilter> = Box::<DefaultFilter>::default();
let record_kind: Box<dyn RecordFilter> = Box::new(RecordKindFilter::new(&[]));
let record = Record::new(RecordKind::Open, String::from("test log record"));
// Assert that trait object methods are dispatchable.
_ = default.check(&record);
_ = record_kind.check(&record);
}
fn assert_record_filter<T: RecordFilter>() {}
#[test]
fn test_box() {
assert_record_filter::<Box<dyn RecordFilter>>();
assert_record_filter::<Box<RecordKindFilter>>();
assert_record_filter::<Box<DefaultFilter>>();
}
fn assert_send<T: Send>() {}
#[test]
fn test_send() {
assert_send::<RecordKindFilter>();
assert_send::<DefaultFilter>();
assert_send::<Box<dyn RecordFilter>>();
assert_send::<Box<RecordKindFilter>>();
assert_send::<Box<DefaultFilter>>();
}
}