From f6bd3430d01984dd518214b13e1b95d0357976f2 Mon Sep 17 00:00:00 2001 From: mulhern Date: Tue, 13 Aug 2024 13:41:29 -0400 Subject: [PATCH] Allow logging in stratis-predict-usage utility The default level is "off" since this option will be mostly for debugging. Signed-off-by: mulhern --- src/bin/utils/cmds.rs | 21 +++++++++++++++++++++ src/bin/utils/predict_usage.rs | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/src/bin/utils/cmds.rs b/src/bin/utils/cmds.rs index 3aed22a87d..e221165644 100644 --- a/src/bin/utils/cmds.rs +++ b/src/bin/utils/cmds.rs @@ -5,12 +5,14 @@ use std::{ error::Error, fmt::{self, Display}, + str::FromStr, }; use clap::{Arg, ArgAction, Command}; #[cfg(feature = "systemd_compat")] use clap::builder::Str; +use log::LevelFilter; use devicemapper::Bytes; @@ -41,6 +43,13 @@ impl StratisPredictUsage { fn cmd() -> Command { Command::new("stratis-predict-usage") .about("Predicts space usage for Stratis.") + .arg( + Arg::new("log-level") + .value_parser(["off", "error", "warn", "info", "debug", "trace"]) + .default_value("off") + .long("log-level") + .help("Sets level for generation of log messages"), + ) .subcommand_required(true) .subcommands(vec![ Command::new("pool") @@ -119,6 +128,12 @@ impl<'a> UtilCommand<'a> for StratisPredictUsage { .collect::, _>>() }) .transpose()?, + LevelFilter::from_str( + matches + .get_one::("log-level") + .expect("default value set"), + ) + .expect("only valid entries allowed"), ), Some(("filesystem", sub_m)) => predict_usage::predict_filesystem_usage( !sub_m.get_flag("no-overprovision"), @@ -129,6 +144,12 @@ impl<'a> UtilCommand<'a> for StratisPredictUsage { .collect::, _>>() }) .expect("required argument")?, + LevelFilter::from_str( + matches + .get_one::("log-level") + .expect("default value set"), + ) + .expect("only valid entries allowed"), ), _ => unreachable!("Impossible subcommand name"), } diff --git a/src/bin/utils/predict_usage.rs b/src/bin/utils/predict_usage.rs index 8ac63628b9..ef3eb70dcd 100644 --- a/src/bin/utils/predict_usage.rs +++ b/src/bin/utils/predict_usage.rs @@ -7,6 +7,8 @@ use std::{ fmt::{self, Display}, }; +use env_logger::Builder; +use log::LevelFilter; use serde_json::{json, Value}; use devicemapper::{Bytes, Sectors}; @@ -142,7 +144,10 @@ fn get_filesystem_prediction( pub fn predict_filesystem_usage( overprovisioned: bool, filesystem_sizes: Vec, + log_level: LevelFilter, ) -> Result<(), Box> { + Builder::new().filter(None, log_level).init(); + let fs_used = get_filesystem_prediction(overprovisioned, filesystem_sizes)?; let used_size_str = Value::String((*(fs_used.bytes())).to_string()); @@ -164,7 +169,10 @@ pub fn predict_pool_usage( overprovisioned: bool, device_sizes: Vec, filesystem_sizes: Option>, + log_level: LevelFilter, ) -> Result<(), Box> { + Builder::new().filter(None, log_level).init(); + let fs_used = filesystem_sizes .map(|sizes| get_filesystem_prediction(overprovisioned, sizes)) .transpose()?;