From 70754218dd2a23c4fddf34b9f010dc7546a6fe6e Mon Sep 17 00:00:00 2001 From: ddidderr Date: Wed, 3 Aug 2022 13:10:14 +0200 Subject: [PATCH] [chg] rewrite current line with timestamp prefix after newline timestamp marks the time when the line was "sent" or "committed" --- .cargo/config.toml | 3 +++ Cargo.lock | 18 +++++++------- Cargo.toml | 8 +++---- src/main.rs | 58 +++++++++++++++++++++++++++++----------------- 4 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..c7e76ff --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ +[build] +rustflags = ["-C", "target-cpu=native", "-C", "strip=symbols"] +target = "x86_64-unknown-linux-musl" diff --git a/Cargo.lock b/Cargo.lock index 956c773..405c6ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,10 +1,12 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "chrono" @@ -21,9 +23,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.80" +version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] name = "logtimes" @@ -34,9 +36,9 @@ dependencies = [ [[package]] name = "num-integer" -version = "0.1.44" +version = "0.1.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" dependencies = [ "autocfg", "num-traits", @@ -44,9 +46,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" dependencies = [ "autocfg", ] diff --git a/Cargo.toml b/Cargo.toml index d659bc0..78039fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,13 +2,13 @@ name = "logtimes" version = "0.1.0" authors = ["Paul Schulze "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +edition = "2021" [dependencies] chrono = "0.4" [profile.release] -debug = false lto = true +codegen-units = 1 +debug = false +panic = "unwind" diff --git a/src/main.rs b/src/main.rs index 518d419..6d9348d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,49 +1,65 @@ -use std::io::{Write, Read, ErrorKind, stdin, stdout}; use chrono::prelude::*; - +use std::io::{stdin, stdout, ErrorKind, Read, StdoutLock, Write}; #[inline(always)] -fn print_time() { +fn print_time(output: &mut StdoutLock) -> Result<(), std::io::Error> { let date_now = Local::now().format("%H:%M:%S%.9f"); let color_green = "\x1b\x5b\x30\x3b\x33\x32\x6d"; let color_off = "\x1b\x5b\x30\x6d"; - print!("{}[{}]{} ", color_green, &date_now, color_off); + write!(output, "{}[{}]{} ", color_green, &date_now, color_off) } +#[inline(always)] +fn print_delete_line(output: &mut StdoutLock) -> Result<(), std::io::Error> { + // tput dl1 und tput hpa 0 + let bytes = "\x1b\x5b\x4d\x1b\x5b\x31\x47"; + write!(output, "{}", bytes) +} -fn main() { +fn run() -> Result<(), std::io::Error> { let out = stdout(); let mut output = out.lock(); let inp = stdin(); let mut input = inp.lock(); + let mut buf = [0; 1]; + + let mut linebuf = Vec::with_capacity(64 * 1024); - print_time(); loop { - let mut buf = [0; 1]; - - let _ = input.read_exact(&mut buf).map_err(|e| { - - if e.kind() != ErrorKind::UnexpectedEof { - println!("{}", e.to_string()); - std::process::exit(1); + if let Err(e) = input.read_exact(&mut buf) { + if e.kind() == ErrorKind::UnexpectedEof { + return Err(e); + } else { + output.flush()?; + return Ok(()); } + } - let _ = output.flush(); - std::process::exit(0); - }); - + linebuf.push(buf[0]); if buf[0] == 0xa { - print!("\n"); - print_time(); + print_delete_line(&mut output)?; + print_time(&mut output)?; + output.write_all(&linebuf)?; + + output.flush()?; + + linebuf.clear(); continue; } - let _ = output.write_all(&mut buf); - let _ = output.flush(); + output.write_all(&buf)?; + output.flush()?; + } +} + +fn main() { + if let Err(e) = run() { + println!("{:?}", e); + std::process::exit(1); } }