aboutsummaryrefslogtreecommitdiff
path: root/src/filters
diff options
context:
space:
mode:
authorDaniel <kingdread@gmx.de>2020-05-04 12:24:24 +0200
committerDaniel <kingdread@gmx.de>2020-05-04 12:24:24 +0200
commit17e27776ea152509e1095bbf80f5af65bc7a7997 (patch)
tree054f71f588a617fd4f0af5ceaf2cc08d401c89b1 /src/filters
parent5aa264ca62efc17e3d55af616aa752c1d24573a2 (diff)
downloadraidgrep-17e27776ea152509e1095bbf80f5af65bc7a7997.tar.gz
raidgrep-17e27776ea152509e1095bbf80f5af65bc7a7997.tar.bz2
raidgrep-17e27776ea152509e1095bbf80f5af65bc7a7997.zip
Add a -log-before & -log-after predicate
With the file name heuristic for -before and -after in place, we might want a way for the user to disable it. For now, we simply do this by providing a new set of predicates without the filter. In the future, we might have a --disable-heuristics switch to disable the heuristics, in case we ever add more.
Diffstat (limited to 'src/filters')
-rw-r--r--src/filters/log.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/filters/log.rs b/src/filters/log.rs
index 1728d4b..2f4a6a0 100644
--- a/src/filters/log.rs
+++ b/src/filters/log.rs
@@ -92,10 +92,14 @@ pub fn weekday(weekdays: HashSet<Weekday>) -> Box<dyn LogFilter> {
}
#[derive(Debug, Clone)]
-struct TimeFilter(Option<DateTime<Utc>>, Option<DateTime<Utc>>);
+struct TimeFilter(Option<DateTime<Utc>>, Option<DateTime<Utc>>, bool);
impl Filter<EarlyLogResult, LogResult> for TimeFilter {
fn filter_early(&self, early_log: &EarlyLogResult) -> Inclusion {
+ // Ignore the filename heuristic if the user wishes so.
+ if !self.2 {
+ return Inclusion::Unknown
+ }
early_log
.log_file
.file_name()
@@ -147,7 +151,7 @@ fn datetime_from_filename(name: &OsStr) -> Option<DateTime<Utc>> {
/// If a bound is not given, -Infinity is assumed for the lower bound, and Infinity for the upper
/// bound.
pub fn time(lower: Option<DateTime<Utc>>, upper: Option<DateTime<Utc>>) -> Box<dyn LogFilter> {
- Box::new(TimeFilter(lower, upper))
+ Box::new(TimeFilter(lower, upper, true))
}
/// A `LogFilter` that only accepts logs after the given date.
@@ -164,6 +168,24 @@ pub fn before(when: DateTime<Utc>) -> Box<dyn LogFilter> {
time(None, Some(when))
}
+/// A `LogFilter` that only accepts logs in the given time frame.
+///
+/// Compared to [`time`][time], this filter ignores the file name. This can result in more accurate
+/// results if you renamed logs, but if also leads to a worse runtime.
+pub fn log_time(lower: Option<DateTime<Utc>>, upper: Option<DateTime<Utc>>) -> Box<dyn LogFilter> {
+ Box::new(TimeFilter(lower, upper, false))
+}
+
+/// Like [`after`][after], but ignores the file name for date calculations.
+pub fn log_after(when: DateTime<Utc>) -> Box<dyn LogFilter> {
+ log_time(Some(when), None)
+}
+
+/// Like [`before`][before], but ignores the file name for date calculations.
+pub fn log_before(when: DateTime<Utc>) -> Box<dyn LogFilter> {
+ log_time(None, Some(when))
+}
+
#[cfg(test)]
mod tests {
use super::*;