Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,4 @@
target/

# ignore result of running `nix build`
result
result
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,14 @@ single_char_add_str = "allow"
# time to time.)
type_complexity = "allow"

[[bin]]
name = "test-split-patches-in-dir"
path = "test/test-split-patches-in-dir.rs"

[[bin]]
name = "log-timestamp"
path = "test/log-timestamp.rs"

[dependencies]
patchparser = { path = "patchparser" }

Expand Down
18 changes: 14 additions & 4 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,9 +22,12 @@ clippy:
( cd patchparser && cargo clippy --color=always --all-targets --all-features $(CLIPPY_ARGS) )
cargo clippy --color=always --all-targets --all-features $(CLIPPY_ARGS)

# This is for use in CI
# This is for use in CI.
# Setting `RUSTFLAGS` to the same as in the `test_deny_warnings`
# target purely so that it can share the compiled binaries (i.e. save
# on compilation time).
clippy_deny:
CLIPPY_ARGS="-- -D warnings" make clippy
RUSTFLAGS="--deny warnings" CLIPPY_ARGS="-- -D warnings" make clippy

# This is for manual use
clippy_fix:
Expand All@@ -44,8 +47,10 @@ test_integration_opt:

test: cargo_test test_integration

# Setting `CARGO_TARGET_DIR` to allow this target to compile fully in
# parallel to the others
leak_test:
RUSTFLAGS="-Z sanitizer=leak" OUR_CARGO_FLAGS=+nightly make test
CARGO_TARGET_DIR=target/nightly RUSTFLAGS="-Z sanitizer=leak" OUR_CARGO_FLAGS=+nightly make test

# This is for use in CI
test_deny_warnings:
Expand All@@ -60,5 +65,10 @@ miri_run:
miri: miri_test miri_run

# Run in Github CI
ci:
ci: target/debug/log-timestamp
test/ci-make test_deny_warnings clippy_deny leak_test check_formatting

target/debug/log-timestamp: test/log-timestamp.rs
mkdir -p target/debug/
rustc $< -o $@

2 changes: 1 addition & 1 deletion test/ci-make
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@ tmpfiles=()
for target in "${targets[@]}"; do
tmp=$(mktemp)
tmpfiles+=("$tmp")
run "$target" > "$tmp" 2>&1 &
( run "$target" 2>&1 | target/debug/log-timestamp > "$tmp" ) &
done

for target in "${targets[@]}"; do
Expand Down
59 changes: 59 additions & 0 deletions test/log-timestamp.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
//! Copies lines from stdin to stdout and prepends them with a unix
//! time stamp in microsecond precision
//!
//! Uses no dependencies so that it can be compiled as quickly as
//! possible directly by `rustc`.

use std::{
io::{stdin, stdout, BufRead, BufReader, BufWriter, Write},
process::exit,
time::{SystemTime, UNIX_EPOCH},
};

fn on_stdin<T>(res: Result<T, std::io::Error>) -> Result<T, String> {
res.map_err(|e| format!("stdin: {e:#}"))
}

fn on_stdout<T>(res: Result<T, std::io::Error>) -> Result<T, String> {
res.map_err(|e| format!("stdout: {e:#}"))
}

fn write_now(mut output: impl Write) -> Result<(), String> {
let t = SystemTime::now();
let unixtime = t
.duration_since(UNIX_EPOCH)
.map_err(|e| format!("getting time: {e}"))?;

let us = unixtime.as_micros();
let s = us / 1_000_000;
let rest = us % 1_000_000;

on_stdout(write!(&mut output, "{s}.{rest:06}"))
}

fn log_timestamp() -> Result<(), String> {
let mut input = BufReader::new(stdin().lock());
let mut output = BufWriter::new(stdout().lock());
let mut buf = Vec::new();
loop {
buf.clear();
let n = on_stdin(input.read_until(b'\n', &mut buf))?;
if n == 0 {
break Ok(());
}
write_now(&mut output)?;
on_stdout((|| {
output.write_all(b"\t")?;
output.write_all(&buf)?;
output.flush()?;
Ok(())
})())?;
}
}

fn main() {
if let Err(e) = log_timestamp() {
eprintln!("Error: {e}");
exit(1);
}
}
File renamed without changes.
Loading