allow reading from stdin

This commit is contained in:
Robin Appelman 2025-09-26 15:59:56 +02:00
commit 3917b52905
2 changed files with 23 additions and 9 deletions

View file

@ -36,6 +36,10 @@ pub struct LogFile {
}
impl LogFile {
pub fn from_string(content: String) -> Self {
Self { content }
}
pub fn open<R: Read + Seek>(path: &str, file: R) -> Result<LogFile, ReadError> {
if path.ends_with(".zip") {
let mut zip = ZipArchive::new(file)?;

View file

@ -1,5 +1,5 @@
use crate::app::App;
use crate::error::LogError;
use crate::error::{LogError, ReadError};
use crate::logfile::{LogFile, LogLineNumber};
use crate::logs::ParsedLogs;
use crate::matcher::{MatchResult, Matcher};
@ -15,7 +15,7 @@ use rayon::prelude::*;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, Stderr, Write};
use std::io::{read_to_string, stdin, BufReader, Stderr, Write};
use std::iter::once;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::RwLock;
@ -41,6 +41,7 @@ static GLOBAL: Jemalloc = Jemalloc;
#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
struct Args {
/// File to read from, or "-" for stdin
file: String,
/// Collect data and exit, intended for profiling
#[arg(long)]
@ -51,7 +52,7 @@ struct Args {
}
fn main() -> MainResult {
let args = Args::parse();
let args: Args = Args::parse();
if let Some(date_format) = args.date_format.as_deref() {
let date_format = if date_format.contains('%') {
@ -67,12 +68,21 @@ fn main() -> MainResult {
.expect("Set only once");
}
let file = File::open(&args.file)?;
let file = BufReader::new(file);
let log_file = LogFile::open(&args.file, file).map_err(|err| LogError::Read {
err,
let log_file = if args.file == "-" {
let content = read_to_string(stdin()).map_err(|err| LogError::Read {
err: ReadError::Io(err),
path: args.file,
})?;
LogFile::from_string(content)
} else {
let file = File::open(&args.file)?;
let file = BufReader::new(file);
LogFile::open(&args.file, file).map_err(|err| LogError::Read {
err,
path: args.file,
})?
};
let lines: Vec<_> = log_file
.iter()
.enumerate()