feat: v0.1.3
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,4 +1,6 @@
|
|||||||
.idea/
|
.idea/
|
||||||
|
temp_*.log
|
||||||
|
|
||||||
# ---> macOS
|
# ---> macOS
|
||||||
# General
|
# General
|
||||||
.DS_Store
|
.DS_Store
|
||||||
@@ -33,10 +35,6 @@ Temporary Items
|
|||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
debug/
|
debug/
|
||||||
target/
|
target/
|
||||||
|
|
||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
|
||||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
|
||||||
Cargo.lock
|
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|||||||
115
Cargo.lock
generated
Normal file
115
Cargo.lock
generated
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ansi_term"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atty"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "2.34.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
|
||||||
|
dependencies = [
|
||||||
|
"ansi_term",
|
||||||
|
"atty",
|
||||||
|
"bitflags",
|
||||||
|
"strsim",
|
||||||
|
"textwrap",
|
||||||
|
"unicode-width",
|
||||||
|
"vec_map",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.1.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.126"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rotate-puts"
|
||||||
|
version = "0.1.3"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "textwrap"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-width"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vec_map"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||||
|
|
||||||
|
[[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"
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rotate-puts"
|
name = "rotate-puts"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = "2.33"
|
||||||
|
|||||||
@@ -6,4 +6,3 @@ Usage:
|
|||||||
```shell
|
```shell
|
||||||
command | rotate-puts
|
command | rotate-puts
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
38
src/main.rs
38
src/main.rs
@@ -3,13 +3,29 @@ use std::io::{BufWriter, Read, Write};
|
|||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use clap::{App, AppSettings, Arg};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let app = App::new(env!("CARGO_PKG_NAME"))
|
||||||
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
|
.about(env!("CARGO_PKG_DESCRIPTION"))
|
||||||
|
.long_about("Rotate standard in to log files")
|
||||||
|
.arg(Arg::with_name("prefix")
|
||||||
|
.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"))
|
||||||
|
.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 (sender, receiver) = std::sync::mpsc::channel::<Vec<u8>>();
|
let (sender, receiver) = std::sync::mpsc::channel::<Vec<u8>>();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let mut file_index = 0;
|
let mut file_index = 0;
|
||||||
let mut written_len = 0;
|
let mut written_len = 0;
|
||||||
|
|
||||||
let file_name = make_new_file_name("temp", &mut file_index);
|
let file_name = make_new_file_name(&prefix, &suffix, &mut file_index);
|
||||||
let mut out_file = BufWriter::new(File::create(&file_name)
|
let mut out_file = BufWriter::new(File::create(&file_name)
|
||||||
.expect(&format!("Create file failed: {}", file_name)));
|
.expect(&format!("Create file failed: {}", file_name)));
|
||||||
|
|
||||||
@@ -44,8 +60,7 @@ fn main() {
|
|||||||
|
|
||||||
if written_len >= 10 * 1024 * 1024 {
|
if written_len >= 10 * 1024 * 1024 {
|
||||||
written_len = 0;
|
written_len = 0;
|
||||||
let file_name = make_new_file_name("temp", &mut file_index);
|
let file_name = make_new_file_name(&prefix, &suffix, &mut file_index);
|
||||||
println!("new file: {}", file_name);
|
|
||||||
out_file = BufWriter::new(File::create(&file_name)
|
out_file = BufWriter::new(File::create(&file_name)
|
||||||
.expect(&format!("Create file failed: {}", file_name)));
|
.expect(&format!("Create file failed: {}", file_name)));
|
||||||
}
|
}
|
||||||
@@ -77,14 +92,25 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_new_file_name(prefix: &str, index: &mut i32) -> String {
|
fn make_new_file_name(prefix: &str, suffix: &str, index: &mut i32) -> String {
|
||||||
let i = *index;
|
let i = *index;
|
||||||
*index = i + 1;
|
*index = i + 1;
|
||||||
|
|
||||||
let pending_rm = format!("{}_{:03}.log", prefix, i - 10);
|
let pending_rm = generate_file_name(prefix, suffix, i - 10);
|
||||||
if let Ok(_) = std::fs::metadata(&pending_rm) {
|
if let Ok(_) = std::fs::metadata(&pending_rm) {
|
||||||
|
println!("[INFO] Remove log file: {}", &pending_rm);
|
||||||
std::fs::remove_file(&pending_rm).ok();
|
std::fs::remove_file(&pending_rm).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
format!("{}_{:03}.log", prefix, i)
|
let file_name = generate_file_name(prefix, suffix, i);
|
||||||
|
println!("[INFO] New log file: {}", &file_name);
|
||||||
|
file_name
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user