Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
68c6732c4b
|
|||
| c9f4ac9fe5 | |||
|
e5962ca543
|
|||
|
1151dc606f
|
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -49,6 +49,15 @@ dependencies = [
|
|||||||
"vec_map",
|
"vec_map",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "daemonize"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ab8bfdaacb3c887a54d41bdf48d3af8873b3f5566469f8ba21b92057509f116e"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dirs-next"
|
name = "dirs-next"
|
||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
@@ -142,9 +151,10 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rotate-puts"
|
name = "rotate-puts"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"daemonize",
|
||||||
"rust_util",
|
"rust_util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
11
Cargo.toml
11
Cargo.toml
@@ -1,12 +1,17 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rotate-puts"
|
name = "rotate-puts"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
authors = ["Hatter Jiang"]
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
repository = "https://git.hatter.ink/hatter/rotate-puts"
|
||||||
|
description = "Rotate outputs to files"
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
keywords = ["log", "rotate"]
|
||||||
|
categories = ["log"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "2.33"
|
clap = "2.33"
|
||||||
|
daemonize = "0.5.0"
|
||||||
rust_util = "0.6.41"
|
rust_util = "0.6.41"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|||||||
@@ -3,13 +3,22 @@
|
|||||||
Rotate outputs from std in or assigned file
|
Rotate outputs from std in or assigned file
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
command | rotate-puts
|
command | rotate-puts
|
||||||
```
|
```
|
||||||
|
|
||||||
Use `mkfifo`:
|
Use `mkfifo`:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
mkfido test-fifo
|
mkfido test-fifo
|
||||||
rotate-puts --file test-fifo
|
rotate-puts --file test-fifo
|
||||||
command > test-fifo
|
command > test-fifo
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Run as daemon:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
rotate-put --file test-fifo --daemon --iden test [--continue-read]
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
105
src/main.rs
105
src/main.rs
@@ -4,7 +4,8 @@ use std::process::exit;
|
|||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
use clap::{App, AppSettings, Arg};
|
use clap::{App, AppSettings, Arg};
|
||||||
use rust_util::{iff, information, util_size};
|
use daemonize::Daemonize;
|
||||||
|
use rust_util::{failure_and_exit, iff, information, util_size};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let app = App::new(env!("CARGO_PKG_NAME"))
|
let app = App::new(env!("CARGO_PKG_NAME"))
|
||||||
@@ -12,15 +13,21 @@ fn main() {
|
|||||||
.about(env!("CARGO_PKG_DESCRIPTION"))
|
.about(env!("CARGO_PKG_DESCRIPTION"))
|
||||||
.long_about("Rotate standard in to log files")
|
.long_about("Rotate standard in to log files")
|
||||||
.arg(Arg::with_name("prefix")
|
.arg(Arg::with_name("prefix")
|
||||||
.long("prefix").takes_value(true).default_value("temp").help("Log file prefix"))
|
.long("prefix").short("P").takes_value(true).default_value("temp").help("Log file prefix"))
|
||||||
.arg(Arg::with_name("suffix")
|
.arg(Arg::with_name("suffix")
|
||||||
.long("suffix").takes_value(true).default_value("log").help("Log file suffix"))
|
.long("suffix").short("S").takes_value(true).default_value("log").help("Log file suffix"))
|
||||||
.arg(Arg::with_name("file-size")
|
.arg(Arg::with_name("file-size")
|
||||||
.long("file-size").takes_value(true).default_value("10m").help("Single log file size"))
|
.long("file-size").short("s").takes_value(true).default_value("10m").help("Single log file size"))
|
||||||
.arg(Arg::with_name("file-count")
|
.arg(Arg::with_name("file-count")
|
||||||
.long("file-count").takes_value(true).default_value("10").help("Keep file count (from 0 to 1000)"))
|
.long("file-count").short("c").takes_value(true).default_value("10").help("Keep file count (from 0 to 1000)"))
|
||||||
.arg(Arg::with_name("file")
|
.arg(Arg::with_name("file")
|
||||||
.long("file").takes_value(true).required(false).help("Read from file, default stdin"))
|
.long("file").short("F").takes_value(true).required(false).help("Read from file, default stdin"))
|
||||||
|
.arg(Arg::with_name("continue-read")
|
||||||
|
.long("continue-read").short("r").help("Continue read"))
|
||||||
|
.arg(Arg::with_name("ident")
|
||||||
|
.long("ident").short("i").takes_value(true).help("Identity when run in daemon mode"))
|
||||||
|
.arg(Arg::with_name("daemon")
|
||||||
|
.long("daemon").short("d").help("Run in daemon mode"))
|
||||||
.setting(AppSettings::ColoredHelp);
|
.setting(AppSettings::ColoredHelp);
|
||||||
|
|
||||||
let arg_matchers = app.get_matches();
|
let arg_matchers = app.get_matches();
|
||||||
@@ -30,11 +37,38 @@ fn main() {
|
|||||||
let file_size = util_size::parse_size(file_size).unwrap_or_else(|_| 10 * 1024 * 1028) as usize;
|
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 = arg_matchers.value_of("file-count").unwrap();
|
||||||
let file_count = file_count.parse().unwrap_or_else(|_| 10);
|
let file_count = file_count.parse().unwrap_or_else(|_| 10);
|
||||||
let file_count = if file_count < 0 {
|
let file_count: i32 = if file_count < 0 { 0 } else if file_count > 1000 { 1000 } else { file_count };
|
||||||
0
|
let continue_read = arg_matchers.is_present("continue-read");
|
||||||
} else if file_count > 1000 {
|
|
||||||
1000
|
let daemon_mode = arg_matchers.is_present("daemon");
|
||||||
} else { file_count as i32 };
|
if daemon_mode {
|
||||||
|
let ident = match arg_matchers.value_of("ident") {
|
||||||
|
Some(ident) => ident,
|
||||||
|
None => failure_and_exit!("--ident is required when running in daemon mode"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let stdout = File::create(format!("/tmp/rotate-puts-daemon-{}-out.log", ident))
|
||||||
|
.expect("Create daemon out file failed");
|
||||||
|
let stderr = File::create(format!("/tmp/rotate-puts-daemon-{}-err.log", ident))
|
||||||
|
.expect("Create daemon err file failed");
|
||||||
|
let current_dir = std::env::current_dir().expect("Get current dir failed");
|
||||||
|
let daemonize = Daemonize::new()
|
||||||
|
.pid_file(&format!("/tmp/rotate-puts-daemon-{}.pid", ident))
|
||||||
|
// .chown_pid_file(true) // is optional, see `Daemonize` documentation
|
||||||
|
.working_directory(current_dir) // for default behaviour.
|
||||||
|
// .user("nobody")
|
||||||
|
// .group("daemon") // Group name
|
||||||
|
// .group(2) // or group id.
|
||||||
|
.umask(0o777) // Set umask, `0o027` by default.
|
||||||
|
.stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
|
||||||
|
.stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
|
||||||
|
.privileged_action(|| "Executed before drop privileges");
|
||||||
|
|
||||||
|
match daemonize.start() {
|
||||||
|
Ok(child) => information!("Success, daemonized: {}", child),
|
||||||
|
Err(e) => failure_and_exit!("Error, {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
information!("Prefix: {}, suffix: {}, file size: {}, file count: {}",
|
information!("Prefix: {}, suffix: {}, file size: {}, file count: {}",
|
||||||
prefix, suffix, util_size::get_display_size(file_size as i64), file_count
|
prefix, suffix, util_size::get_display_size(file_size as i64), file_count
|
||||||
@@ -104,30 +138,41 @@ fn main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let file_opt = arg_matchers.value_of("file");
|
let mut continue_read_count = 0;
|
||||||
let mut read_in: Box<dyn Read> = if let Some(file) = file_opt {
|
information!("Continue read: {}", continue_read);
|
||||||
let file_read = File::open(file).expect("Open file failed!");
|
'read_open_loop: while continue_read || (continue_read_count == 0) {
|
||||||
Box::new(file_read)
|
if continue_read_count > 0 { information!("Continue read at #{}", continue_read_count); }
|
||||||
} else {
|
continue_read_count += 1;
|
||||||
Box::new(std::io::stdin())
|
let file_opt = arg_matchers.value_of("file");
|
||||||
};
|
let mut read_in: Box<dyn Read> = if let Some(file) = file_opt {
|
||||||
|
let file_read = File::open(file).expect("Open file failed!");
|
||||||
|
Box::new(file_read)
|
||||||
|
} else {
|
||||||
|
Box::new(std::io::stdin())
|
||||||
|
};
|
||||||
|
|
||||||
let mut buff = [0_u8; 128];
|
let mut buff = [0_u8; 128];
|
||||||
loop {
|
loop {
|
||||||
match read_in.read(&mut buff) {
|
match read_in.read(&mut buff) {
|
||||||
Ok(len) if len == 0 => {
|
Ok(len) if len == 0 => {
|
||||||
sender.send(Vec::new()).ok();
|
if continue_read {
|
||||||
}
|
continue 'read_open_loop; // continue and reopen file or stdin
|
||||||
Ok(len) => {
|
} else {
|
||||||
let mut vec = Vec::with_capacity(len);
|
sender.send(Vec::new()).ok();
|
||||||
vec.extend_from_slice(&buff[0..len]);
|
}
|
||||||
if let Err(e) = sender.send(vec) {
|
|
||||||
eprintln!("[ERROR] Send error: {}", e);
|
|
||||||
}
|
}
|
||||||
|
Ok(len) => {
|
||||||
|
let mut vec = Vec::with_capacity(len);
|
||||||
|
vec.extend_from_slice(&buff[0..len]);
|
||||||
|
if let Err(e) = sender.send(vec) {
|
||||||
|
eprintln!("[ERROR] Send error: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => eprintln!("[ERROR] Send error: {}", e),
|
||||||
}
|
}
|
||||||
Err(e) => eprintln!("[ERROR] Send error: {}", e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
information!("End rotate-puts")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_new_file_name(prefix: &str, suffix: &str, file_count: i32, index: &mut i32) -> String {
|
fn make_new_file_name(prefix: &str, suffix: &str, file_count: i32, index: &mut i32) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user