Skip to content

Commit

Permalink
Merge pull request #95 from pamburus/feature/record-filter
Browse files Browse the repository at this point in the history
new: record filtering refactored for greater flexibility
  • Loading branch information
pamburus authored Oct 11, 2023
2 parents daa0849 + caf81ef commit cf82428
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ categories = ["command-line-utilities"]
description = "Utility for viewing json-formatted log files."
keywords = ["cli", "human", "log"]
name = "hl"
version = "0.20.0-beta.14.8.1"
version = "0.20.0-beta.14.8.2"
edition = "2021"
build = "build.rs"

Expand Down
162 changes: 93 additions & 69 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,75 +40,8 @@ impl<'a> Record<'a> {
self.extra.iter().chain(self.extrax.iter())
}

pub fn matches(&self, filter: &Filter) -> bool {
if filter.is_empty() {
return true;
}

if filter.since.is_some() || filter.until.is_some() {
if let Some(ts) = self.ts.as_ref().and_then(|ts| ts.parse()) {
if let Some(since) = filter.since {
if ts < since {
return false;
}
}
if let Some(until) = filter.until {
if ts > until {
return false;
}
}
}
}

if let Some(bound) = &filter.level {
if let Some(level) = self.level.as_ref() {
if level > bound {
return false;
}
}
}

if !filter.fields.0.is_empty() {
for field in filter.fields.0.iter() {
match &field.key[..] {
"msg" | "message" => {
if !field.match_value(self.message.map(|x| x.get()), true) {
return false;
}
}
"logger" => {
if !field.match_value(self.logger, false) {
return false;
}
}
"caller" => {
if !field.match_value(self.caller, false) {
return false;
}
}
_ => {
let mut matched = false;
for (k, v) in self.fields() {
match field.match_key(*k) {
None => {}
Some(KeyMatch::Full) => {
let escaped = v.get().starts_with('"');
matched |= field.match_value(Some(v.get()), escaped);
}
Some(KeyMatch::Partial(subkey)) => {
matched |= field.match_value_partial(subkey, *v);
}
}
}
if !matched {
return false;
}
}
}
}
}

return true;
pub fn matches<F: RecordFilter>(&self, filter: &F) -> bool {
filter.apply(self)
}

fn with_capacity(capacity: usize) -> Self {
Expand All @@ -130,6 +63,12 @@ impl<'a> Record<'a> {

// ---

pub trait RecordFilter {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool;
}

// ---

#[derive(Default)]
pub struct ParserSettings {
fields: HashMap<String, (FieldSettings, usize)>,
Expand Down Expand Up @@ -565,6 +504,48 @@ impl FieldFilter {
}
}

impl RecordFilter for FieldFilter {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
match &self.key[..] {
"msg" | "message" => {
if !self.match_value(record.message.map(|x| x.get()), true) {
return false;
}
}
"logger" => {
if !self.match_value(record.logger, false) {
return false;
}
}
"caller" => {
if !self.match_value(record.caller, false) {
return false;
}
}
_ => {
let mut matched = false;
for (k, v) in record.fields() {
match self.match_key(*k) {
None => {}
Some(KeyMatch::Full) => {
let escaped = v.get().starts_with('"');
matched |= self.match_value(Some(v.get()), escaped);
}
Some(KeyMatch::Partial(subkey)) => {
matched |= self.match_value_partial(subkey, *v);
}
}
}
if !matched {
return false;
}
}
}

true
}
}

// ---

#[derive(Debug, Default)]
Expand All @@ -580,6 +561,12 @@ impl FieldFilterSet {
}
}

impl RecordFilter for FieldFilterSet {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.0.iter().all(|field| field.apply(record))
}
}

// ---

#[derive(Debug, Default)]
Expand All @@ -596,6 +583,43 @@ impl Filter {
}
}

impl RecordFilter for Filter {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
if self.is_empty() {
return true;
}

if self.since.is_some() || self.until.is_some() {
if let Some(ts) = record.ts.as_ref().and_then(|ts| ts.parse()) {
if let Some(since) = self.since {
if ts < since {
return false;
}
}
if let Some(until) = self.until {
if ts > until {
return false;
}
}
}
}

if let Some(bound) = &self.level {
if let Some(level) = record.level.as_ref() {
if level > bound {
return false;
}
}
}

if !self.fields.apply(record) {
return false;
}

true
}
}

// ---

pub struct Object<'a> {
Expand Down

0 comments on commit cf82428

Please sign in to comment.