From eeea9f41b4635ccdfc9d03d74a4eb3e1977dc5c2 Mon Sep 17 00:00:00 2001 From: Andrew Gunnerson Date: Sun, 18 Aug 2024 23:58:11 -0400 Subject: [PATCH] cli/args: Use tracing::Level directly Signed-off-by: Andrew Gunnerson --- avbroot/src/cli/args.rs | 41 ++++------------------------------------- e2e/src/cli.rs | 7 ++++--- 2 files changed, 8 insertions(+), 40 deletions(-) diff --git a/avbroot/src/cli/args.rs b/avbroot/src/cli/args.rs index 3d780b3..c66f830 100644 --- a/avbroot/src/cli/args.rs +++ b/avbroot/src/cli/args.rs @@ -37,39 +37,6 @@ pub enum Command { MagiskInfo(boot::MagiskInfoCli), } -#[derive(Debug, Clone, Copy, ValueEnum)] -pub enum LogLevel { - Trace, - Debug, - Info, - Warn, - Error, -} - -impl LogLevel { - fn as_level(self) -> Level { - match self { - Self::Trace => Level::TRACE, - Self::Debug => Level::DEBUG, - Self::Info => Level::INFO, - Self::Warn => Level::WARN, - Self::Error => Level::ERROR, - } - } -} - -impl Default for LogLevel { - fn default() -> Self { - Self::Info - } -} - -impl fmt::Display for LogLevel { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(self.to_possible_value().ok_or(fmt::Error)?.get_name()) - } -} - #[derive(Debug, Clone, Copy, ValueEnum)] pub enum LogFormat { Short, @@ -96,8 +63,8 @@ pub struct Cli { pub command: Command, /// Lowest log message severity to output. - #[arg(long, global = true, value_name = "LEVEL", default_value_t)] - pub log_level: LogLevel, + #[arg(long, global = true, value_name = "LEVEL", default_value_t = Level::INFO)] + pub log_level: Level, /// Output format for log messages. #[arg(long, global = true, value_name = "FORMAT", default_value_t)] @@ -124,11 +91,11 @@ impl FormatTime for ShortUptime { } } -pub fn init_logging(log_level: LogLevel, log_format: LogFormat) { +pub fn init_logging(log_level: Level, log_format: LogFormat) { let builder = tracing_subscriber::fmt() .with_writer(io::stderr) .with_ansi(io::stderr().is_terminal()) - .with_max_level(log_level.as_level()); + .with_max_level(log_level); match log_format { LogFormat::Short => { diff --git a/e2e/src/cli.rs b/e2e/src/cli.rs index 949bc84..63d9946 100644 --- a/e2e/src/cli.rs +++ b/e2e/src/cli.rs @@ -5,8 +5,9 @@ use std::{ffi::OsString, path::PathBuf}; -use avbroot::cli::args::{LogFormat, LogLevel}; +use avbroot::cli::args::LogFormat; use clap::{Args, Parser, Subcommand, ValueEnum}; +use tracing::Level; #[derive(Debug, Args)] pub struct ProfileGroup { @@ -69,8 +70,8 @@ pub struct Cli { pub command: Command, /// Lowest log message severity to output. - #[arg(long, global = true, value_name = "LEVEL", default_value_t)] - pub log_level: LogLevel, + #[arg(long, global = true, value_name = "LEVEL", default_value_t = Level::INFO)] + pub log_level: Level, /// Output format for log messages. #[arg(long, global = true, value_name = "FORMAT", default_value_t = LogFormat::Medium)]