Skip to content
Open
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
25 changes: 7 additions & 18 deletions src/cmds/system/log_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Deduplicates repeated log lines and shows counts instead.

use crate::core::guard::never_worse;
use crate::core::runner::emit_guarded;
use crate::core::tracking;
use crate::core::truncate::{CAP_WARNINGS, reduced};
use crate::core::utils::truncate;
use anyhow::Result;
use regex::Regex;
use std::collections::HashMap;
use std::fs;
use std::io::{self, BufRead};
use std::io;
use std::path::Path;
use std::sync::LazyLock;

Expand All @@ -32,29 +32,18 @@ pub fn run_file(file: &Path, verbose: u8) -> Result<()> {
}

let content = fs::read_to_string(file)?;
let result = analyze_logs(&content);
let shown = never_worse(&content, &result);
println!("{}", shown);
timer.track(&super::read::cat_label(file), "rtk log", &content, shown);
let shown = emit_guarded(&analyze_logs(&content), None, &content);
timer.track(&super::read::cat_label(file), "rtk log", &content, &shown);
Ok(())
}

/// Filter logs from stdin
pub fn run_stdin(_verbose: u8) -> Result<()> {
let timer = tracking::TimedExecution::start();

let mut content = String::new();
let stdin = io::stdin();
for line in stdin.lock().lines() {
content.push_str(&line?);
content.push('\n');
}

let result = analyze_logs(&content);
let shown = never_worse(&content, &result);
println!("{}", shown);

timer.track("log (stdin)", "rtk log (stdin)", &content, shown);
let content = io::read_to_string(io::stdin().lock())?;
let shown = emit_guarded(&analyze_logs(&content), None, &content);
timer.track("log (stdin)", "rtk log (stdin)", &content, &shown);

Ok(())
}
Expand Down
31 changes: 31 additions & 0 deletions tests/guard_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ fn rtk_stdin(args: &[&str], input: &str) -> String {
.write_all(input.as_bytes())
.expect("write stdin");
let out = child.wait_with_output().expect("wait rtk");
assert!(
out.status.success(),
"rtk {args:?} exited with {}",
out.status
);
String::from_utf8_lossy(&out.stdout).into_owned()
}

Expand Down Expand Up @@ -78,6 +83,32 @@ fn rtk_in_dir(dir: &std::path::Path, args: &[&str]) -> (String, Option<i32>) {
(stdout, code)
}

#[test]
fn log_file_and_stdin_emit_the_same_line_endings() {
let dir = tempfile::tempdir().expect("tempdir");
let repeated = "INFO ready\n".repeat(80);
let summary = concat!(
"Log Summary\n",
" [error] 0 errors (0 unique)\n",
" [warn] 0 warnings (0 unique)\n",
" [info] 80 info messages\n",
);
for (input, expected) in [
("one\r\ntwo\r\n", "one\r\ntwo\r\n"),
("", ""),
("ok\n", "ok\n"),
("ok", "ok\n"),
("ok\n\n", "ok\n\n"),
(repeated.as_str(), summary),
] {
std::fs::write(dir.path().join("sample.log"), input).expect("write log");
let (file_output, code) = rtk_in_dir(dir.path(), &["log", "sample.log"]);
assert_eq!(code, Some(0));
assert_eq!(rtk_stdin(&["log"], input), expected, "stdin: {input:?}");
assert_eq!(file_output, expected, "file: {input:?}");
}
}

fn rg_available() -> bool {
Command::new("rg")
.arg("--version")
Expand Down
Loading