Skip to content
Snippets Groups Projects
Commit 18bef3fb authored by Federico Lolli's avatar Federico Lolli
Browse files

improved logging

parent 2cad24b9
Branches
Tags
No related merge requests found
......@@ -2,7 +2,7 @@ use std::time::Duration;
use clap::{Parser, Subcommand};
use crate::log::LogFormatter;
use crate::log::{LogFormatter, LogLevel};
#[derive(Debug, Clone, Parser)]
#[command(version, about, infer_subcommands = true)]
......@@ -13,8 +13,8 @@ pub struct Cli {
pub time: bool,
/// Enable verbose output (prints more information).
#[arg(short, long)]
pub verbose: bool,
#[arg(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
#[command(subcommand)]
pub command: Subcommands,
......@@ -73,9 +73,15 @@ impl DiscoverOptions {
impl From<&Cli> for LogFormatter {
fn from(value: &Cli) -> Self {
let level = match value.verbose {
0 => LogLevel::Info,
1 => LogLevel::Debug,
_ => LogLevel::Trace,
};
LogFormatter {
show_time: value.time,
show_debug: value.verbose,
level,
}
}
}
......@@ -10,17 +10,26 @@ pub use formatter::LogFormatter;
pub static LOG_HANDLER: LazyLock<LogHandler> = LazyLock::new(LogHandler::default);
#[derive(Debug, Clone, Copy)]
enum LogLevel {
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
pub enum LogLevel {
Trace,
Debug,
#[default]
Info,
Warning,
Error,
}
impl std::fmt::Display for LogLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl LogLevel {
fn symbol_prepend(&self) -> char {
match self {
LogLevel::Trace => '&',
LogLevel::Debug => '@',
LogLevel::Info => '*',
LogLevel::Warning => '#',
......@@ -30,6 +39,7 @@ impl LogLevel {
fn style(&self) -> Style {
match self {
LogLevel::Trace => Style::new().cyan(),
LogLevel::Debug => Style::new().green(),
LogLevel::Info => Style::new().blue(),
LogLevel::Warning => Style::new().yellow(),
......@@ -38,6 +48,16 @@ impl LogLevel {
}
}
#[macro_export]
macro_rules! trace {
($input:expr) => {{
$crate::log::LOG_HANDLER.print_trace($input);
}};
($input:expr, $($args:expr),+) => {{
$crate::log::LOG_HANDLER.print_trace(format!($input, $($args),+));
}};
}
#[macro_export]
macro_rules! debug {
($input:expr) => {{
......
......@@ -6,7 +6,7 @@ use super::LogLevel;
#[derive(Debug, Clone, Default)]
pub struct LogFormatter {
pub show_time: bool,
pub show_debug: bool,
pub level: LogLevel,
}
impl LogFormatter {
......
......@@ -25,19 +25,16 @@ macro_rules! define_print {
($name:ident, $level:ident) => {
#[inline]
pub fn $name(&self, input: impl AsRef<str>) {
if self.formatter.read().unwrap().level <= LogLevel::$level {
self.print_with(input.as_ref(), LogLevel::$level);
}
}
};
}
impl LogHandler {
#[inline]
pub fn print_debug(&self, input: impl AsRef<str>) {
if self.formatter.read().unwrap().show_debug {
self.print_with(input.as_ref(), LogLevel::Debug);
}
}
define_print!(print_trace, Trace);
define_print!(print_debug, Debug);
define_print!(print_info, Info);
define_print!(print_warn, Warning);
define_print!(print_error, Error);
......
......@@ -38,8 +38,9 @@ const DEFAULT_MAVLINK_PORT: u16 = 14551;
fn main() {
// parse args and set up logging
let args = Cli::parse();
LOG_HANDLER.set_formatter(LogFormatter::from(&args));
debug!("Debug mode: {}", args.verbose);
let fmt = LogFormatter::from(&args);
LOG_HANDLER.set_formatter(fmt.clone());
debug!("Log mode: {}", fmt.level.to_string().to_uppercase());
// run the command
run_command(&args).unwrap_or_else(|e| {
......@@ -84,7 +85,7 @@ fn wait_for_interface(wait_for: Duration) -> PResult<Ipv4Addr> {
Ok(ip)
}
None => {
debug!("No link-local address found, retrying in 1s...");
trace!("No link-local address found, retrying in 1s...");
Err(StepError::Timeout)
}
}
......@@ -217,7 +218,7 @@ fn handle_discover_replies(
MessageReadError::Io(e)
if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut =>
{
debug!("No message received, retrying...");
trace!("No message received, retrying...");
Err(StepError::Timeout)
}
_ => {
......@@ -280,7 +281,7 @@ fn handle_discover_requests(
MessageReadError::Io(e)
if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut =>
{
debug!("No message received, retrying...");
trace!("No message received, retrying...");
Err(StepError::Timeout)
}
_ => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment