mirror of
https://codeberg.org/icewind/logsmash.git
synced 2026-06-03 10:04:12 +02:00
allow reading from stdin
This commit is contained in:
parent
4600c8fb3c
commit
3917b52905
2 changed files with 23 additions and 9 deletions
|
|
@ -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)?;
|
||||
|
|
|
|||
28
src/main.rs
28
src/main.rs
|
|
@ -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,
|
||||
path: args.file,
|
||||
})?;
|
||||
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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue