Refactor logging
To supporte more than a single log level `log` and `env_logger` crates are used. For the application to support the different type of argument, the verbose argument definition was adapted.
This commit is contained in:
parent
6a41335603
commit
12b117db7e
8 changed files with 107 additions and 36 deletions
24
src/main.rs
24
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(())
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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::<Duration>()?)?;
|
||||
|
||||
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(())
|
||||
|
|
|
@ -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::<Duration>()?,
|
||||
)?;
|
||||
|
||||
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(())
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue