Skip to content

Commit

Permalink
Add a header to log files
Browse files Browse the repository at this point in the history
  • Loading branch information
triarius committed Sep 21, 2024
1 parent 14c4cbf commit 2cc5492
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 8 deletions.
174 changes: 174 additions & 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 @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
chrono = "0.4.38"
clap = { version = "4.5.17", features = ["derive", "env"] }
crossbeam = "0.8.4"
eyre = "0.6.12"
Expand Down
34 changes: 26 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
mod flat_map_err;
mod log_file;

use crate::flat_map_err::FlatMapErr;
use chrono::Utc;
use core::panic;
use crossbeam::channel::{bounded, select, Receiver, TrySendError};
use eyre::{eyre, Result};
use std::{
fs::OpenOptions,
io::{stderr, stdin, stdout, Read, Write},
path::Path,
process::{Command, Stdio},
thread::{self, Scope},
};

fn default_header(name: &str) -> String {
format!("[{}]: Begin runner log of {name}", Utc::now())
}

fn io_streams<W: Write + Send + 'static>(
header: Option<&str>,
writer: W,
log_path: Option<&Path>,
) -> Result<(Stdio, Vec<Box<dyn Write + Send>>)> {
match log_path {
Some(path) => {
let file = OpenOptions::new().create(true).append(true).open(path)?;
let file = log_file::new(header, path)?;
Ok((Stdio::piped(), vec![Box::new(writer), Box::new(file)]))
}
None => Ok((Stdio::inherit(), vec![Box::new(writer)])),
Expand All @@ -36,13 +42,23 @@ fn io_streams<W: Write + Send + 'static>(
pub fn run(
cmd: &str,
args: &[&str],
no_header: bool,
stdin_log_path: Option<&Path>,
stdout_log_path: Option<&Path>,
stderr_log_path: Option<&Path>,
) -> Result<i32> {
let in_io = stdout_log_path.map_or(Stdio::inherit(), |_| Stdio::piped());
let (out_io, mut out_writers) = io_streams(stdout(), stdout_log_path)?;
let (err_io, mut err_writers) = io_streams(stderr(), stderr_log_path)?;

let (out_header, err_header) = if no_header {
(None, None)
} else {
(
Some(default_header("stdout")),
Some(default_header("stderr")),
)
};
let (out_io, mut out_writers) = io_streams(out_header.as_deref(), stdout(), stdout_log_path)?;
let (err_io, mut err_writers) = io_streams(err_header.as_deref(), stderr(), stderr_log_path)?;

let mut child = Command::new(cmd)
.args(args)
Expand All @@ -53,10 +69,12 @@ pub fn run(

match child.stdin.take() {
Some(child_in) => {
let in_file = OpenOptions::new()
.create(true)
.append(true)
.open(stdin_log_path.unwrap())?;
let header = if no_header {
None
} else {
Some(default_header("stdin"))
};
let in_file = log_file::new(header.as_deref(), stdin_log_path.unwrap())?;
let mut in_writers: Vec<Box<dyn Write + Send>> =
vec![Box::new(child_in), Box::new(in_file)];

Expand Down
13 changes: 13 additions & 0 deletions src/log_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::{
fs::{File, OpenOptions},
io::{Result, Write},
path::Path,
};

pub(crate) fn new(header: Option<&str>, path: &Path) -> Result<File> {
let mut file = OpenOptions::new().create(true).append(true).open(path)?;
if let Some(header) = header {
writeln!(file, "{header}")?;
}
Ok(file)
}
Loading

0 comments on commit 2cc5492

Please sign in to comment.