added -v/--verbose

This commit is contained in:
Lynne Megido 2021-04-26 22:15:14 +10:00
parent 7a315e9cf6
commit 2ceafb7acf
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
4 changed files with 47 additions and 15 deletions

View file

@ -4,6 +4,7 @@ Dates are given in YYYY-MM-DD format.
## v0.2
### v0.2.13 (2021-???)
#### Features
- Added `-v`/`--verbose` flag for setting verbosity without using `RUST_LOG`
- Added system extension set (`.dll`, `.so`, `.exe`...)
- Output is now sorted: Files that couldn't be read, then files with no known mimetype, then files with no known
extensions, then files with the wrong extension

View file

@ -62,16 +62,6 @@ cargo install fif --no-default-features
## Usage
See `fif --help` for more.
### Logging
By default, fif will log any warnings and/or errors encountered during execution. The verbosity of the logging can be
modified by the `RUST_LOG` to one of: `trace`, `debug`, `info`, `warn`, `error`.
For example:
```bash
RUST_LOG=debug fif ~/Downloads
```
### The basics
The simplest way to use fif looks like this:
@ -108,3 +98,32 @@ You can also manually specify an output format to use:
```bash
fif -O powershell ~/Documents > output.ps1
```
### Logging
By default, fif will log any warnings and/or errors encountered during execution. This can be changed with the `-v`
flag:
```bash
# also log info
fif -v ~/Downloads
# ...and debug
fif -vv ~/Downloads
# ...and trace
fif -vvv ~/Downloads
```
The verbosity of the logging can be
modified by the `RUST_LOG` to one of: `trace`, `debug`, `info`, `warn`, `error`.
For example:
```bash
RUST_LOG=debug fif ~/Downloads
```
The five logging levels are used as follows:
| Level | Description | Example |
|-|-|-|
| error | Errors that cause fif to stop running | fif was unable to open the provided directory |
| warn | Warnings that don't cause fif to stop running | fif was unable to determine the mime type of a given file |
| info | Information pertaining to fif's status | The provided directory was scanned without issue, and no files are in need of renaming |
| debug | Debug information - usually not important to end users | The list of extensions fif will consider |
| trace | Trace info - usually not important to end users | "Found 15 items to check", "Scan successful", etc. |

View file

@ -67,7 +67,7 @@ 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", "INFO"));
let mut builder = env_logger::Builder::from_env(Env::new().filter_or("RUST_LOG", args.default_verbosity()));
builder
// .format(|buf, r| writeln!(buf, "{} - {}", r.level(), r.args()))
@ -115,13 +115,13 @@ fn main() {
match result {
Ok(r) => {
debug!(
"{:?} is {}, should have file extension {}",
"{:?} is of type {}, should have extension \"{}\"",
r.file,
r.mime,
r.recommended_extension().unwrap_or_else(|| "???".into())
)
}
Err(f) => warn!("Error 0uo - {}", f),
Err(f) => warn!("{}", f),
}
}
@ -223,14 +223,12 @@ fn scan_file(entry: &DirEntry) -> Result<Findings, ScanError> {
if result.is_err() {
// an error occurred while trying to read the file
// error!("{}: {}", entry.path().to_string_lossy(), error);
return Err(ScanError::File(entry.path()));
}
let result = result.unwrap();
if result.is_none() {
// the file was read successfully, but we were unable to determine its mimetype
// warn!("Couldn't determine mimetype for {}", entry.path().to_string_lossy());
return Err(ScanError::Mime(entry.path()));
}

View file

@ -72,6 +72,11 @@ pub struct Parameters {
#[clap(short, long)]
pub follow_symlinks: bool,
/// Output verbosity. Defaults to only logging warnings and errors.
/// Can be overridden by RUST_LOG.
#[clap(short, long, parse(from_occurrences))]
pub verbose: u8,
/// The directory to process.
// TODO: right now this can only take a single directory - should this be improved?
#[clap(name = "DIR", default_value = ".", parse(from_os_str))]
@ -110,4 +115,13 @@ impl Parameters {
follow_symlinks: self.follow_symlinks,
}
}
pub fn default_verbosity(&self) -> &'static str {
match self.verbose {
0 => "warn",
1 => "info",
2 => "debug",
3 | _ => "trace",
}
}
}