diff --git a/Cargo.lock b/Cargo.lock index 8034921..afa11c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,16 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + [[package]] name = "anyhow" version = "1.0.41" @@ -29,6 +40,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.19" @@ -74,6 +91,19 @@ dependencies = [ "syn", ] +[[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "hashbrown" version = "0.11.2" @@ -126,6 +156,21 @@ version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" + [[package]] name = "mqrs" version = "0.1.1" @@ -133,7 +178,9 @@ dependencies = [ "anyhow", "chrono", "clap", + "env_logger", "humantime", + "log", "posixmq", ] @@ -213,6 +260,23 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + [[package]] name = "strsim" version = "0.10.0" diff --git a/Cargo.toml b/Cargo.toml index 073ba33..a42c1c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ clap = "3.0.0-beta.2" posixmq = "1.0" chrono = "0.4" humantime = "2.1" +log = "0.4" +env_logger = "0.8" [package.metadata.deb] extended-description = "`mqrs` is a small cli application to handle POSIX message queues." diff --git a/mqrs.1 b/mqrs.1 index 795809a..50fa2d0 100644 --- a/mqrs.1 +++ b/mqrs.1 @@ -26,7 +26,8 @@ completed further. Prints help information .TP 8 .B \-v, \-\-verbose -Produce verbose output +Produce verbose output, multiple -v options increase the verbosity +(max. 3) .TP 8 .B \-V, \-\-version Prints version information diff --git a/src/main.rs b/src/main.rs index ba152c7..40296fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,9 @@ enum Command { global_setting = AppSettings::InferSubcommands, )] struct Opts { - /// Produce verbose output - #[clap(short, long, global = true)] - verbose: bool, + /// Produce verbose output, multiple -v options increase the verbosity (max. 3) + #[clap(short, long, global = true, parse(from_occurrences))] + verbose: u32, #[clap(subcommand)] command: Command, } @@ -31,12 +31,22 @@ struct Opts { fn main() -> Result<()> { let opts: Opts = Opts::parse(); + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or( + match opts.verbose { + 0 => "warn", + 1 => "info", + 2 => "debug", + _ => "trace", + }, + )) + .init(); + match opts.command { - Command::Create(c) => c.run(opts.verbose)?, + Command::Create(c) => c.run()?, Command::Info(i) => i.run()?, - Command::Unlink(u) => u.run(opts.verbose)?, - Command::Send(s) => s.run(opts.verbose)?, - Command::Recv(r) => r.run(opts.verbose)?, + Command::Unlink(u) => u.run()?, + Command::Send(s) => s.run()?, + Command::Recv(r) => r.run()?, } Ok(()) diff --git a/src/posix/create.rs b/src/posix/create.rs index eac24ac..d475d36 100644 --- a/src/posix/create.rs +++ b/src/posix/create.rs @@ -1,5 +1,6 @@ use anyhow::Result; use clap::Clap; +use log::{info, log_enabled, Level::Info}; use posixmq::PosixMq; use std::fs; @@ -35,7 +36,7 @@ fn msg_default() -> usize { } impl Create { - pub fn run(&self, verbose: bool) -> Result<()> { + pub fn run(&self) -> Result<()> { let mq = &mut posixmq::OpenOptions::readonly(); if let Some(m) = &self.permissions { @@ -47,11 +48,11 @@ impl Create { .create_new() .open(&self.queue)?; - if verbose { + if log_enabled!(Info) { let mq = PosixMq::open(&self.queue)?; let attributes = mq.attributes()?; - println!("Created message queue: {} with attributes msgsize: {}, capacity: {}, current_messages: {}", + info!("Created message queue: {} with attributes msgsize: {}, capacity: {}, current_messages: {}", &self.queue, &attributes.max_msg_len, &attributes.capacity, diff --git a/src/posix/recv.rs b/src/posix/recv.rs index afb6758..0b8563a 100644 --- a/src/posix/recv.rs +++ b/src/posix/recv.rs @@ -2,6 +2,7 @@ use anyhow::Result; use chrono::{DateTime, Local}; use clap::Clap; use humantime::Duration; +use log::info; use posixmq::PosixMq; use std::str; @@ -28,10 +29,8 @@ pub struct Recv { pub queue: String, } -fn print_message(verbose: bool, priority: u32, length: usize, timestamp: bool, msg: &str) { - if verbose { - println!("Priority: {}, length: {}", priority, length); - } +fn print_message(priority: u32, length: usize, timestamp: bool, msg: &str) { + info!("Priority: {}, length: {}", priority, length); if timestamp { println!("{}", Local::now()); @@ -41,30 +40,30 @@ fn print_message(verbose: bool, priority: u32, length: usize, timestamp: bool, m } impl Recv { - fn receive(&self, verbose: bool, mq: &PosixMq) -> Result<()> { + fn receive(&self, mq: &PosixMq) -> Result<()> { let mut buf = vec![0; mq.attributes()?.max_msg_len]; if let Some(timeout) = &self.timeout { let (prio, len) = mq.recv_timeout(&mut buf, *timeout.parse::()?)?; - print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?); + print_message(prio, len, self.timestamp, str::from_utf8(&buf)?); } else if let Some(deadline) = &self.deadline { let (prio, len) = mq.recv_deadline( &mut buf, DateTime::parse_from_str(deadline, "%Y-%m-%d %H:%M:%S")?.into(), )?; - print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?); + print_message(prio, len, self.timestamp, str::from_utf8(&buf)?); } else { let (prio, len) = mq.recv(&mut buf)?; - print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?); + print_message(prio, len, self.timestamp, str::from_utf8(&buf)?); } Ok(()) } - pub fn run(&self, verbose: bool) -> Result<()> { + pub fn run(&self) -> Result<()> { let mq = &mut posixmq::OpenOptions::readonly(); if self.non_blocking { @@ -75,10 +74,10 @@ impl Recv { if self.follow { loop { - self.receive(verbose, &mq)?; + self.receive(&mq)?; } } else { - self.receive(verbose, &mq)?; + self.receive(&mq)?; } Ok(()) diff --git a/src/posix/send.rs b/src/posix/send.rs index 835b677..917679b 100644 --- a/src/posix/send.rs +++ b/src/posix/send.rs @@ -2,6 +2,7 @@ use anyhow::Result; use chrono::DateTime; use clap::Clap; use humantime::Duration; +use log::info; /// Send a message to a message queue #[derive(Clap, Debug)] @@ -26,14 +27,8 @@ pub struct Send { pub msg: String, } -fn print_verbose(verbose: bool, msg: &str, queue: &str) { - if verbose { - println!("Sent message: \"{}\" to queue: {}", &msg, &queue); - } -} - impl Send { - pub fn run(&self, verbose: bool) -> Result<()> { + pub fn run(&self) -> Result<()> { let mq = &mut posixmq::OpenOptions::writeonly(); if self.non_blocking { @@ -47,7 +42,7 @@ impl Send { *timeout.parse::()?, )?; - print_verbose(verbose, &self.msg, &self.queue); + info!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue); } else if let Some(deadline) = &self.deadline { mq.open(&self.queue)?.send_deadline( self.priority, @@ -55,12 +50,12 @@ impl Send { DateTime::parse_from_str(deadline, "%Y-%m-%d %H:%M:%S")?.into(), )?; - print_verbose(verbose, &self.msg, &self.queue); + info!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue); } else { mq.open(&self.queue)? .send(self.priority, &self.msg.as_bytes())?; - print_verbose(verbose, &self.msg, &self.queue); + info!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue); } Ok(()) diff --git a/src/posix/unlink.rs b/src/posix/unlink.rs index ae837e1..4b89a27 100644 --- a/src/posix/unlink.rs +++ b/src/posix/unlink.rs @@ -1,5 +1,6 @@ use anyhow::Result; use clap::Clap; +use log::info; /// Delete a message queue #[derive(Clap, Debug)] @@ -10,12 +11,10 @@ pub struct Unlink { } impl Unlink { - pub fn run(&self, verbose: bool) -> Result<()> { + pub fn run(&self) -> Result<()> { posixmq::remove_queue(&self.queue)?; - if verbose { - println!("Removed message queue: {}", &self.queue); - } + info!("Removed message queue: {}", &self.queue); Ok(()) }