Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 58 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub enum LogError {
/// Collection of log statements in a single source file
#[derive(Debug)]
pub struct StatementsInFile {
pub filename: String,
pub log_statements: Vec<SourceRef>,
/// A single matcher for all log statements.
/// XXX If there are too many in the file, the RegexSet constructor
Expand Down Expand Up @@ -165,17 +166,36 @@ impl LogMatcher {
/// Attempt to match the given log message.
pub fn match_log_statement<'a>(&self, log_ref: &LogRef<'a>) -> Option<LogMapping<'a>> {
for (_path, coll) in &self.roots {
let matches = coll
.statements
.par_iter()
.flat_map(|src_ref_coll| {
let file_matches = src_ref_coll.matcher.matches(log_ref.body());
match file_matches.iter().next() {
None => None,
Some(index) => src_ref_coll.log_statements.get(index),
}
})
.collect::<Vec<&SourceRef>>();
let matches = if let Some(LogDetails {
file: Some(filename),
body: Some(body),
..
}) = log_ref.details
{
// XXX this block and the else are basically the same, try to refactor
coll.statements
.iter()
.filter(|stmts| stmts.filename.contains(filename))
.flat_map(|stmts| {
let file_matches = stmts.matcher.matches(body);
match file_matches.iter().next() {
None => None,
Some(index) => stmts.log_statements.get(index),
}
})
.collect::<Vec<&SourceRef>>()
} else {
coll.statements
.par_iter()
.flat_map(|src_ref_coll| {
let file_matches = src_ref_coll.matcher.matches(log_ref.body());
match file_matches.iter().next() {
None => None,
Some(index) => src_ref_coll.log_statements.get(index),
}
})
.collect::<Vec<&SourceRef>>()
};
if let Some(src_ref) = matches.first() {
let variables = extract_variables(log_ref, src_ref);
return Some(LogMapping {
Expand Down Expand Up @@ -268,24 +288,14 @@ pub struct LogMapping<'a> {
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct LogRef<'a> {
pub line: &'a str,
details: Option<LogDetails<'a>>,
}

impl<'a> LogRef<'a> {
pub fn body(self) -> &'a str {
if let Some(LogDetails { body: Some(s), .. }) = self.details {
s
} else {
self.line
}
}
pub details: Option<LogDetails<'a>>,
}

#[derive(Copy, Clone, Debug, PartialEq)]
struct LogDetails<'a> {
file: Option<&'a str>,
lineno: Option<u32>,
body: Option<&'a str>,
pub struct LogDetails<'a> {
pub file: Option<&'a str>,
pub lineno: Option<u32>,
pub body: Option<&'a str>,
}

impl<'a> LogRef<'a> {
Expand All @@ -296,6 +306,18 @@ impl<'a> LogRef<'a> {
}
}

pub fn from_parsed(file: Option<&'a str>, lineno: Option<u32>, body: &'a str) -> Self {
let details = Some(LogDetails {
file,
lineno,
body: Some(body),
});
Self {
line: body,
details,
}
}

pub fn with_format(line: &'a str, log_format: LogFormat) -> Self {
let captures = log_format.captures(line);
let file = captures.name("file").map(|file_match| file_match.as_str());
Expand All @@ -308,6 +330,14 @@ impl<'a> LogRef<'a> {
details: Some(LogDetails { file, lineno, body }),
}
}

pub fn body(self) -> &'a str {
if let Some(LogDetails { body: Some(s), .. }) = self.details {
s
} else {
self.line
}
}
}

pub fn link_to_source<'a>(log_ref: &LogRef, src_refs: &'a [SourceRef]) -> Option<&'a SourceRef> {
Expand Down Expand Up @@ -364,7 +394,7 @@ pub fn extract_variables<'a>(
variables
}

pub fn filter_log<R>(buffer: &str, filter: R, log_format: Option<String>) -> Vec<LogRef>
pub fn filter_log<R>(buffer: &str, filter: R, log_format: Option<String>) -> Vec<LogRef<'_>>
where
R: RangeBounds<usize>,
{
Expand Down Expand Up @@ -432,6 +462,7 @@ pub fn extract_logging(sources: &[CodeSource], tracker: &ProgressTracker) -> Vec
None
} else {
Some(StatementsInFile {
filename: matched.first().unwrap().source_path.clone(),
log_statements: matched,
matcher: RegexSet::new(patterns).expect("To combine patterns"),
})
Expand Down
10 changes: 5 additions & 5 deletions src/source_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub enum FormatArgument {
#[derive(Clone, Debug, Serialize)]
pub struct SourceRef {
#[serde(rename(serialize = "sourcePath"))]
pub(crate) source_path: String,
pub source_path: String,
#[serde(rename(serialize = "lineNumber"))]
pub line_no: usize,
pub(crate) column: usize,
pub(crate) name: String,
pub(crate) text: String,
pub column: usize,
pub name: String,
pub text: String,
#[serde(skip_serializing)]
pub(crate) matcher: Regex,
pub(crate) pattern: String,
pub pattern: String,
pub(crate) args: Vec<FormatArgument>,
pub(crate) vars: Vec<String>,
}
Expand Down
Loading