From 606d882ec19f39e63d37031c3c1176944d4fbe9a Mon Sep 17 00:00:00 2001 From: ddidderr Date: Thu, 30 Jul 2020 10:15:18 +0200 Subject: [PATCH] [chg] logtimes initial version --- .gitignore | 1 + Cargo.lock | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 14 +++++++++ src/main.rs | 49 +++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..956c773 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,91 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "libc" +version = "0.2.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" + +[[package]] +name = "logtimes" +version = "0.1.0" +dependencies = [ + "chrono", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d659bc0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +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 + +[dependencies] +chrono = "0.4" + +[profile.release] +debug = false +lto = true diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..518d419 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,49 @@ +use std::io::{Write, Read, ErrorKind, stdin, stdout}; +use chrono::prelude::*; + + +#[inline(always)] +fn print_time() { + 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); +} + + +fn main() { + let out = stdout(); + let mut output = out.lock(); + + let inp = stdin(); + let mut input = inp.lock(); + + + 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); + } + + let _ = output.flush(); + std::process::exit(0); + }); + + + if buf[0] == 0xa { + print!("\n"); + print_time(); + continue; + } + + let _ = output.write_all(&mut buf); + let _ = output.flush(); + } +}