Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 9, 2024
1 parent 735203e commit 60d8d14
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ log = "0.4"
notify-debouncer-mini = "0.5.0"
once_cell = "1"
psutil = "3"
regex = "1.11.1"
rev_lines = "0.3.0"
rmp-serde = "1.3.0"
serde = { version = "1", features = ["derive"] }
Expand Down
59 changes: 40 additions & 19 deletions src/cli/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{env, Result};
use itertools::Itertools;
use std::collections::{BTreeMap, HashSet};
use xx::regex;

/// Displays logs for daemon(s)
#[derive(Debug, clap::Args)]
Expand Down Expand Up @@ -38,29 +39,49 @@ impl Logs {
.filter(|(_, f)| f.exists())
.collect::<BTreeMap<_, _>>();

let log_lines = log_files
.iter()
.flat_map(|(name, path)| {
let rev = match xx::file::open(path) {
Ok(f) => rev_lines::RevLines::new(f),
Err(e) => {
error!("{}: {}", path.display(), e);
return vec![];
let log_lines = log_files.iter().flat_map(|(name, path)| {
let rev = match xx::file::open(path) {
Ok(f) => rev_lines::RevLines::new(f),
Err(e) => {
error!("{}: {}", path.display(), e);
return vec![];
}
};
let lines = rev.into_iter().filter_map(Result::ok);
let lines = if self.n == 0 {
lines.collect_vec()
} else {
lines.take(self.n).collect_vec()
};
lines.into_iter().fold(vec![], |mut acc, line| {
match regex!(r"^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (.*)$")
.captures(&line)
{
Some(caps) => {
let date = caps.get(1).unwrap().as_str().to_string();
let msg = caps.get(2).unwrap().as_str().to_string();
acc.push((date, name, msg));
acc
}
None => {
if let Some(l) = acc.last_mut() {
l.2.push_str(&line)
}
acc
}
};
let lines = rev.into_iter()
.filter_map(Result::ok)
.map(|l| (name, l));
if self.n == 0 {
lines.collect()
} else {
lines.take(self.n).collect()
}
})
.rev()
.collect_vec();
});
let log_lines = if self.n == 0 {
log_lines.collect_vec()
} else {
log_lines.take(self.n).collect_vec()
};
let log_lines = log_lines.into_iter().rev().collect_vec();

dbg!(&log_lines);
for (date, name, msg) in log_lines {
println!("{} {} {}", date, name, msg);
}
Ok(())
}
}

0 comments on commit 60d8d14

Please sign in to comment.