Skip to content

Commit

Permalink
puts logging behind log feature
Browse files Browse the repository at this point in the history
- use log and env_logger crates
- removes --debug CLI flag
  • Loading branch information
wookietreiber committed Aug 20, 2024
1 parent d51f63a commit e4ba391
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 88 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ categories = ["command-line-utilities", "filesystem"]
edition = "2021"
rust-version = "1.74"

[features]
log = ["dep:log", "dep:env_logger"]

[dependencies]
anyhow = "1"
bstr = "1"
bytesize = "1"
env_logger = { version = "0.11", optional = true }
libc = "0.2"
log = { version = "0.4", optional = true }
pwd-grp = "0.1"
tempfile = "3"

Expand Down
7 changes: 0 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ pub fn build() -> Command {
directories are read from standard input.",
);

let debug = Arg::new("debug")
.long("debug")
.action(ArgAction::SetTrue)
.hide_short_help(true)
.long_help("Print debug messages while running.");

let max_depth = Arg::new("max-depth")
.short('d')
.long("max-depth")
Expand Down Expand Up @@ -108,7 +102,6 @@ pub fn build() -> Command {
.arg(max_depth)
.arg(count_links)
.arg(kb_allocated)
.arg(debug)
.arg(help)
.arg(version)
.after_help(
Expand Down
4 changes: 0 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use libc::{gid_t, uid_t};

#[derive(Debug)]
pub struct Config {
pub debug: bool,
pub filter: Filter,
pub count_links: bool,
pub max_depth: Option<usize>,
Expand All @@ -46,8 +45,6 @@ impl TryFrom<&ArgMatches> for Config {
type Error = anyhow::Error;

fn try_from(args: &ArgMatches) -> Result<Self> {
let debug = args.get_one::<bool>("debug").copied().unwrap_or_default();

let filter = Filter::try_from(args)?;

let count_links = args.get_flag("count-links");
Expand All @@ -74,7 +71,6 @@ impl TryFrom<&ArgMatches> for Config {
let count_mode = CountMode::from(args);

Ok(Self {
debug,
filter,
count_links,
max_depth,
Expand Down
40 changes: 0 additions & 40 deletions src/log.rs

This file was deleted.

34 changes: 22 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

mod cli;
mod config;
mod log;
mod output;
mod policy;
mod usage;
Expand All @@ -43,11 +42,15 @@ use anyhow::Result;
use crate::config::Config;

fn main() -> Result<()> {
let cli = crate::cli::build();
let args = cli.get_matches();
#[cfg(feature = "log")]
env_logger::init();

let cli = cli::build();
let args = cli.get_matches();
let config = Config::try_from(&args)?;
log::debug(format!("{config:#?}"), config.debug);

#[cfg(feature = "log")]
log::debug!("{config:#?}");

// ALLOW if let is easier to comprehend
#[allow(clippy::option_if_let_else)]
Expand All @@ -59,12 +62,12 @@ fn main() -> Result<()> {
let interactive = std::io::stdin().is_terminal();

if interactive {
log::warning("input is read from terminal");
log::warning("only experts do this on purpose");
log::warning("you may have forgotten to either");
log::warning("- specify directories on the command line or");
log::warning("- pipe data into this tool");
log::warning("press CTRL-D or CTRL-C to exit");
eprintln!("input is read from terminal");
eprintln!("only experts do this on purpose");
eprintln!("you may have forgotten to either");
eprintln!("- specify directories on the command line or");
eprintln!("- pipe data into this tool");
eprintln!("press CTRL-D or CTRL-C to exit");
}

let lines = io::stdin().lines();
Expand All @@ -78,9 +81,16 @@ fn main() -> Result<()> {
}

fn run(dir: &Path, config: &Config) {
log::debug(format!("running {} ...", dir.display()), config.debug);
#[cfg(feature = "log")]
log::debug!("running with directory {} ...", dir.display());

if let Err(error) = usage::run(dir, config) {
let dir = dir.display();
log::error(format!("skipping directory {dir}: {error:#}"));

#[cfg(not(feature = "log"))]
eprintln!("{}: skipping {dir}: {error:?}", clap::crate_name!());

#[cfg(feature = "log")]
log::warn!("skipping directory {dir}: {error:#}");
}
}
34 changes: 9 additions & 25 deletions src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use clap::crate_name;
use tempfile::{tempdir, tempdir_in};

use crate::config::Config;
use crate::log;
use crate::output::output;
use crate::policy::{self, Entry};

Expand Down Expand Up @@ -77,7 +76,8 @@ pub fn run(dir: &Path, config: &Config) -> Result<()> {
command.arg("-g").arg(global_work_dir);
};

log::debug(format!("command: {command:?}"), config.debug);
#[cfg(feature = "log")]
log::debug!("command: {command:?}");

let mut child = command
.stdout(Stdio::null())
Expand Down Expand Up @@ -115,8 +115,7 @@ fn sum(dir: &Path, report: &Path, config: &Config) -> Result<()> {
})?;

if let Some(depth) = config.max_depth {
let sizes =
sum_depth(dir, depth, report, config.count_links, config.debug)?;
let sizes = sum_depth(dir, depth, report, config.count_links)?;

for (dir, Acc { inodes, bytes }) in sizes {
output(&dir, inodes, bytes, config);
Expand All @@ -134,7 +133,6 @@ fn sum_depth(
depth: usize,
report: impl Read,
count_links: bool,
debug: bool,
) -> Result<BTreeMap<PathBuf, Acc>> {
let report = BufReader::new(report);

Expand All @@ -154,14 +152,10 @@ fn sum_depth(
let path_depth = path.iter().count();
let path_suffix_depth = path_depth - prefix_depth;

log::debug(format!("path: {path:?}"), debug);

for depth in 0..=depth.min(path_suffix_depth) {
let prefix: PathBuf =
path.iter().take(prefix_depth + depth).collect();

log::debug(format!("prefix: {prefix:?}"), debug);

if count_links || nlink == "1" {
sums.entry(prefix)
.and_modify(|v| v.acc += bytes)
Expand Down Expand Up @@ -321,14 +315,9 @@ mod test {
once.insert("/data/test/a".into(), Acc::from((2, 5120)));
once.insert("/data/test/b".into(), Acc::from((3, 6144)));

let result = sum_depth(
Path::new("/data/test"),
1,
source.as_bytes(),
false,
false,
)
.unwrap();
let result =
sum_depth(Path::new("/data/test"), 1, source.as_bytes(), false)
.unwrap();

assert_eq!(once, result);

Expand All @@ -337,14 +326,9 @@ mod test {
many.insert("/data/test/a".into(), Acc::from((3, 6144)));
many.insert("/data/test/b".into(), Acc::from((3, 6144)));

let result = sum_depth(
Path::new("/data/test"),
1,
source.as_bytes(),
true,
false,
)
.unwrap();
let result =
sum_depth(Path::new("/data/test"), 1, source.as_bytes(), true)
.unwrap();

assert_eq!(many, result);
}
Expand Down

0 comments on commit e4ba391

Please sign in to comment.