From 39d55a6ecac8a95ff4a75d8d75754b34ed1163bd Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 31 Jul 2022 15:25:23 +0800 Subject: [PATCH] feat: v0.1.4 --- Cargo.lock | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- src/main.rs | 37 ++++++++---- 3 files changed, 195 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2134180..b329ce5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "2.34.0" @@ -43,6 +49,38 @@ dependencies = [ "vec_map", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -52,25 +90,120 @@ dependencies = [ "libc", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +[[package]] +name = "proc-macro2" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c278e965f1d8cf32d6e0e96de3d3e79712178ae67986d9cf9151f51e95aac89b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "rotate-puts" -version = "0.1.3" +version = "0.1.4" dependencies = [ "clap", + "rust_util", ] +[[package]] +name = "rust_util" +version = "0.6.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df24005feacce81f4ae340464b39c380f7e01e7225bfdef62d40cb44cb1c11d7" +dependencies = [ + "lazy_static", + "libc", + "term", + "term_size", +] + +[[package]] +name = "rustversion" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24c8ad4f0c00e1eb5bc7614d236a7f1300e3dbd76b68cac8e06fb00b015ad8d8" + [[package]] name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + +[[package]] +name = "term_size" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -80,6 +213,32 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7" + [[package]] name = "unicode-width" version = "0.1.9" @@ -92,6 +251,12 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index a37418b..6248e3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,10 @@ [package] name = "rotate-puts" -version = "0.1.3" +version = "0.1.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] clap = "2.33" +rust_util = "0.6.41" diff --git a/src/main.rs b/src/main.rs index 3838ec1..28d9a29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use std::process::exit; use std::time::Duration; use clap::{App, AppSettings, Arg}; +use rust_util::{iff, information, util_size}; fn main() { let app = App::new(env!("CARGO_PKG_NAME")) @@ -14,18 +15,38 @@ fn main() { .long("prefix").takes_value(true).default_value("temp").help("Log file prefix")) .arg(Arg::with_name("suffix") .long("suffix").takes_value(true).default_value("log").help("Log file suffix")) + .arg(Arg::with_name("file-size") + .long("file-size").takes_value(true).default_value("10m").help("Single log file size")) + .arg(Arg::with_name("file-count") + .long("file-count").takes_value(true).default_value("10").help("Keep file count (from 0 to 1000)")) .setting(AppSettings::ColoredHelp); let arg_matchers = app.get_matches(); let prefix = arg_matchers.value_of("prefix").unwrap().to_string(); let suffix = arg_matchers.value_of("suffix").unwrap().to_string(); + let file_size = arg_matchers.value_of("file-size").unwrap(); + let file_size = util_size::parse_size(file_size).unwrap_or_else(|_| 10 * 1024 * 1028) as usize; + let file_count = arg_matchers.value_of("file-count").unwrap(); + let file_count = file_count.parse().unwrap_or_else(|_| 10); + let file_count = match file_count { + i if i < 0 => { + 0 + } + i if i > 1000 => { + 1000 + } + i => i as i32, + }; + information!("Prefix: {}, suffix: {}, file size: {}, file count: {}", + prefix, suffix, util_size::get_display_size(file_size as i64), file_count + ); let (sender, receiver) = std::sync::mpsc::channel::>(); std::thread::spawn(move || { let mut file_index = 0; let mut written_len = 0; - let file_name = make_new_file_name(&prefix, &suffix, &mut file_index); + let file_name = make_new_file_name(&prefix, &suffix, file_count, &mut file_index); let mut out_file = BufWriter::new(File::create(&file_name) .expect(&format!("Create file failed: {}", file_name))); @@ -58,9 +79,9 @@ fn main() { write_buffer = left_buffer; out_file.flush().ok(); - if written_len >= 10 * 1024 * 1024 { + if written_len >= file_size { written_len = 0; - let file_name = make_new_file_name(&prefix, &suffix, &mut file_index); + let file_name = make_new_file_name(&prefix, &suffix, file_count, &mut file_index); out_file = BufWriter::new(File::create(&file_name) .expect(&format!("Create file failed: {}", file_name))); } @@ -92,11 +113,11 @@ fn main() { } } -fn make_new_file_name(prefix: &str, suffix: &str, index: &mut i32) -> String { +fn make_new_file_name(prefix: &str, suffix: &str, file_count: i32, index: &mut i32) -> String { let i = *index; *index = i + 1; - let pending_rm = generate_file_name(prefix, suffix, i - 10); + let pending_rm = generate_file_name(prefix, suffix, i - file_count); if let Ok(_) = std::fs::metadata(&pending_rm) { println!("[INFO] Remove log file: {}", &pending_rm); std::fs::remove_file(&pending_rm).ok(); @@ -108,9 +129,5 @@ fn make_new_file_name(prefix: &str, suffix: &str, index: &mut i32) -> String { } fn generate_file_name(prefix: &str, suffix: &str, index: i32) -> String { - format!("{}_{:03}{}", prefix, index, if suffix.is_empty() { - "".to_string() - } else { - ".".to_string() + suffix - }) + format!("{}_{:03}{}{}", prefix, index, iff!(suffix.is_empty(), "", "."), suffix) }