From 17e27776ea152509e1095bbf80f5af65bc7a7997 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 May 2020 12:24:24 +0200 Subject: 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. --- raidgrep.1.asciidoc | 8 ++++++++ src/fexpr/grammar.lalrpop | 2 ++ src/filters/log.rs | 26 ++++++++++++++++++++++++-- src/main.rs | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/raidgrep.1.asciidoc b/raidgrep.1.asciidoc index 65b5d18..78dcf78 100644 --- a/raidgrep.1.asciidoc +++ b/raidgrep.1.asciidoc @@ -96,6 +96,14 @@ Those predicates can be used as-is in the filter: Include logs made after the given date. See *-before* for valid date formats. +*-log-before* 'DATE', *-log-after* 'DATE':: + Same as *-before* and *-after*, but does not use file name heuristics to + filter the date. + + + + This can fix wrong results in case you renamed files, but it comes at the + cost of worse runtime performance. You should always use *-before*/*-after* + when possible! + *-boss* 'BOSSES':: Include logs from the given bosses. See below for a list of valid boss names. Names can also be comma separated. diff --git a/src/fexpr/grammar.lalrpop b/src/fexpr/grammar.lalrpop index c0165ce..55bd669 100644 --- a/src/fexpr/grammar.lalrpop +++ b/src/fexpr/grammar.lalrpop @@ -50,6 +50,8 @@ LogPredicate: Box = { "-weekday" > => filters::log::weekday(<>), "-before" => filters::log::before(<>), "-after" => filters::log::after(<>), + "-log-before" => filters::log::log_before(<>), + "-log-after" => filters::log::log_after(<>), "-boss" > => filters::log::boss(<>), 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) -> Box { } #[derive(Debug, Clone)] -struct TimeFilter(Option>, Option>); +struct TimeFilter(Option>, Option>, bool); impl Filter 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> { /// If a bound is not given, -Infinity is assumed for the lower bound, and Infinity for the upper /// bound. pub fn time(lower: Option>, upper: Option>) -> Box { - 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) -> Box { 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>, upper: Option>) -> Box { + Box::new(TimeFilter(lower, upper, false)) +} + +/// Like [`after`][after], but ignores the file name for date calculations. +pub fn log_after(when: DateTime) -> Box { + log_time(Some(when), None) +} + +/// Like [`before`][before], but ignores the file name for date calculations. +pub fn log_before(when: DateTime) -> Box { + log_time(None, Some(when)) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index ad35d20..625e869 100644 --- a/src/main.rs +++ b/src/main.rs @@ -344,7 +344,7 @@ fn maybe_save_history(rl: &Editor<()>, path: Option<&Path>) { debug!("Saving history to {:?}", path); let parent = path .parent() - .ok_or(anyhow!("Path does not have a parent"))?; + .ok_or_else(|| anyhow!("Path does not have a parent"))?; fs::create_dir_all(parent).context("Could not create directory")?; rl.save_history(path)?; Ok(()) -- cgit v1.2.3