diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d0e221..9b2752f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ Dates are given in YYYY-MM-DD format. #### Features - Added `--canonical-paths` flag for outputting canonical paths in output - for example, `mv /home/lynne/file.jpg /home/lynne/file.mp3` instead of the default `mv file.jpg file.mp3` +#### Other +- The `FIF_LOG` environment variable can now be used to set log level, in addition to `RUST_LOG` +- Log output now uses abbreviated level names: For example, `[D] Message` instead of `[DEBUG] Message` ### v0.3.2 (2021-06-14) #### Bugfixes diff --git a/Cargo.lock b/Cargo.lock index b18b744..f124e25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,9 +240,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] diff --git a/README.md b/README.md index 02f0ed1..af2d17b 100644 --- a/README.md +++ b/README.md @@ -149,16 +149,17 @@ fif -qq ~/Downloads fif -qqq ~/Downloads ``` -The verbosity of the logging can also be modified by setting the environment variable `RUST_LOG` to `off`, `trace`, -`debug`, `info`, `warn`, or `error`. Values set by `RUST_LOG` override the `-v` and `-q` flags. +The verbosity of the logging can also be modified by setting the environment variable `FIF_LOG` or `RUST_LOG` to `off`, +`trace`, `debug`, `info`, `warn`, or `error`. Values set by `FIF_LOG` override `RUST_LOG`, and both override the `-v` +and `-q` flags. For example: ```bash # show all levels except trace -RUST_LOG=debug fif ~/Downloads +FIF_LOG=debug fif ~/Downloads # only show errors -RUST_LOG=error fif ~/Downloads +FIF_LOG=error fif ~/Downloads ``` The five logging levels are used as follows: diff --git a/src/formats.rs b/src/formats.rs index 7f56b22..d67fc9c 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -196,7 +196,17 @@ impl Format for Shell { } fn no_known_extension(&self, f: &mut W, path: &Path) -> io::Result<()> { - smart_write(f, writablesln!["cat <<- '???'", Newline, "No known extension for ", path, Newline, "???"]) + smart_write( + f, + writablesln![ + "cat <<- '???'", + Newline, + "No known extension for ", + path, + Newline, + "???" + ], + ) } fn unreadable(&self, f: &mut W, path: &Path) -> io::Result<()> { @@ -239,7 +249,12 @@ impl Format for PowerShell { // there doesn't seem to be a way to rename the file, prompting only if the target already exists. smart_write( f, - writablesln!["Rename-Item -Verbose -Path ", from, " -NewName ", (to.file_name().unwrap())], + writablesln![ + "Rename-Item -Verbose -Path ", + from, + " -NewName ", + (to.file_name().unwrap()) + ], ) } diff --git a/src/main.rs b/src/main.rs index cf36cd9..ac3edbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,7 @@ use std::process::exit; use cfg_if::cfg_if; use clap::Clap; -use env_logger::Env; -use log::{debug, error, info, trace, warn}; +use log::{debug, error, info, trace, warn, Level}; use once_cell::sync::OnceCell; use walkdir::{DirEntry, WalkDir}; @@ -63,13 +62,20 @@ cfg_if! { fn main() { let args: parameters::Parameters = parameters::Parameters::parse(); - let mut builder = env_logger::Builder::from_env(Env::new().filter_or("RUST_LOG", args.default_verbosity())); - + let mut builder = env_logger::Builder::new(); builder - // .format(|buf, r| writeln!(buf, "{} - {}", r.level(), r.args())) - .format_module_path(false) // don't include module in logs, as it's not necessary - .format_timestamp(None) // don't include timestamps (unnecessary, and the feature flag is disabled anyway) - // .target(env_logger::Target::Stdout) // log to stdout rather than stderr + .filter_level(args.default_verbosity()) // set default log level + .parse_default_env() // set log level from RUST_LOG + .parse_env("FIF_LOG") // set log level from FIF_LOG + .format(|buf, r| { + let mut style = buf.default_level_style(r.level()); + // use bold for warnings and errors + style.set_bold(r.level() <= Level::Warn); + // only use the first character of the log level name + let abbreviation = style.value(r.level().to_string().chars().next().unwrap()); + // e.g. [D] Debug message + writeln!(buf, "[{}] {}", abbreviation, r.args()) + }) .init(); trace!( @@ -78,7 +84,7 @@ fn main() { std::env::consts::ARCH, os_name() ); - trace!("Initialise mimetype database"); + trace!("Initialising mimetype database"); init_db(); debug!("Iterating directory: {:?}", args.dir); diff --git a/src/parameters.rs b/src/parameters.rs index d4cb06c..6bd9d0f 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -109,11 +109,12 @@ pub struct Parameters { pub follow_symlinks: bool, /// Output verbosity. Each additional `-v` increases verbosity. - /// Can be overridden by RUST_LOG. + /// Can be overridden by FIF_LOG or RUST_LOG. #[clap(short, long, parse(from_occurrences), group = "verbosity")] pub verbose: u8, /// Output quietness. Each additional `-q` decreases verbosity. + /// Can be overridden by FIF_LOG or RUST_LOG. #[clap(short, long, parse(from_occurrences), group = "verbosity")] pub quiet: u8, @@ -216,21 +217,23 @@ impl Parameters { } } - pub fn default_verbosity(&self) -> &'static str { + pub fn default_verbosity(&self) -> log::LevelFilter { #![allow(clippy::missing_const_for_fn)] // match was not permitted inside const functions until 1.46 + use log::LevelFilter; + match self.quiet { 0 => { match self.verbose { - 0 => "info", // no verbosity flags specified - 1 => "debug", // -v - _ => "trace", // -vv... + 0 => LevelFilter::Info, // no verbosity flags specified + 1 => LevelFilter::Debug, // -v + _ => LevelFilter::Trace, // -vv... } } - 1 => "warn", // -q - 2 => "error", // -qq - _ => "off", // -qqq... + 1 => LevelFilter::Warn, // -q + 2 => LevelFilter::Error, // -qq + _ => LevelFilter::Off, // -qqq... } } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 36287eb..a745d1e 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -444,21 +444,22 @@ fn writables_is_correct() { #[test] /// Test various combinations of verbosity flags. fn verbosity() { + use log::LevelFilter; assert!( Parameters::try_parse_from(&["fif", "-q", "-v"]).is_err(), "Failed to reject usage of both -q and -v!" ); let mut expected_results = HashMap::new(); - expected_results.insert("-qqqqqqqq", "off"); - expected_results.insert("-qqq", "off"); - expected_results.insert("-qq", "error"); - expected_results.insert("-q", "warn"); - expected_results.insert("-s", "info"); - expected_results.insert("-v", "debug"); - expected_results.insert("-vv", "trace"); - expected_results.insert("-vvv", "trace"); - expected_results.insert("-vvvvvvvv", "trace"); + expected_results.insert("-qqqqqqqq", LevelFilter::Off); + expected_results.insert("-qqq", LevelFilter::Off); + expected_results.insert("-qq", LevelFilter::Error); + expected_results.insert("-q", LevelFilter::Warn); + expected_results.insert("-s", LevelFilter::Info); + expected_results.insert("-v", LevelFilter::Debug); + expected_results.insert("-vv", LevelFilter::Trace); + expected_results.insert("-vvv", LevelFilter::Trace); + expected_results.insert("-vvvvvvvv", LevelFilter::Trace); for (flags, level) in expected_results { assert_eq!(Parameters::parse_from(&["fif", flags]).default_verbosity(), level); diff --git a/src/utils.rs b/src/utils.rs index ee95d8b..1feb668 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -31,8 +31,7 @@ pub fn clap_long_version() -> &'static str { CLAP_LONG_VERSION.get_or_init(|| format!("v{} ({} backend)", VERSION.unwrap_or("???"), BACKEND)) } -/// Returns the name of the target operating system, like "Windows" or "macOS". Won't account for things like Wine or -/// Linuxulator. +/// Returns the name of the target operating system with proper casing, like "Windows" or "macOS". #[allow(clippy::option_map_unit_fn)] pub fn os_name() -> String { use std::env::consts::OS;