fuchsia_triage/plugins/helpers.rs
1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::metrics::fetch::FileDataFetcher;
6use regex::{Match, Regex};
7
8/// Analyzes the klog and syslog streams looking for a match to `re` and
9/// collects the capture groups matches when `re` matches. Note that optional
10/// capture groups that didn't participate in the match are omitted and callers
11/// should consider this when making assumptions about the length of the Vec
12/// passed to `match_fn`. WrappedMatch structs are in the Vec passed to the
13/// the match function because WrappedMatch provides a convenient implementation
14/// to turn the match into an &str.
15pub fn analyze_logs<'t, F>(inputs: &FileDataFetcher<'t>, re: Regex, mut match_fn: F)
16where
17 F: FnMut(Vec<WrappedMatch<'t>>),
18{
19 for line in inputs.klog.lines.iter().chain(inputs.syslog.lines.iter()) {
20 if let Some(captures) = re.captures(line) {
21 let mut parts: Vec<WrappedMatch<'t>> = vec![];
22 for capture in captures.iter().flatten() {
23 parts.push(WrappedMatch(capture));
24 }
25 match_fn(parts);
26 }
27 }
28}
29
30pub struct WrappedMatch<'t>(pub Match<'t>);
31
32impl<'t> From<WrappedMatch<'t>> for &'t str {
33 fn from(m: WrappedMatch<'t>) -> Self {
34 m.0.as_str()
35 }
36}