Skip to content

Commit

Permalink
Allow logging in stratis-predict-usage utility
Browse files Browse the repository at this point in the history
The default level is "off" since this option will be mostly for debugging.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Aug 13, 2024
1 parent 080c197 commit f6bd343
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/bin/utils/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -119,6 +128,12 @@ impl<'a> UtilCommand<'a> for StratisPredictUsage {
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
LevelFilter::from_str(
matches
.get_one::<String>("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"),
Expand All @@ -129,6 +144,12 @@ impl<'a> UtilCommand<'a> for StratisPredictUsage {
.collect::<Result<Vec<_>, _>>()
})
.expect("required argument")?,
LevelFilter::from_str(
matches
.get_one::<String>("log-level")
.expect("default value set"),
)
.expect("only valid entries allowed"),
),
_ => unreachable!("Impossible subcommand name"),
}
Expand Down
8 changes: 8 additions & 0 deletions src/bin/utils/predict_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -142,7 +144,10 @@ fn get_filesystem_prediction(
pub fn predict_filesystem_usage(
overprovisioned: bool,
filesystem_sizes: Vec<Bytes>,
log_level: LevelFilter,
) -> Result<(), Box<dyn Error>> {
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());
Expand All @@ -164,7 +169,10 @@ pub fn predict_pool_usage(
overprovisioned: bool,
device_sizes: Vec<Bytes>,
filesystem_sizes: Option<Vec<Bytes>>,
log_level: LevelFilter,
) -> Result<(), Box<dyn Error>> {
Builder::new().filter(None, log_level).init();

let fs_used = filesystem_sizes
.map(|sizes| get_filesystem_prediction(overprovisioned, sizes))
.transpose()?;
Expand Down

0 comments on commit f6bd343

Please sign in to comment.