Skip to content

Commit

Permalink
feat: use env_logger
Browse files Browse the repository at this point in the history
Instead of the custom logger impl, which limits the features we can
easily provide for users.

This introduces the `BCACHEFS_LOG` environment variable for setting the
log verbosity. Setting `BCACHEFS_LOG=trace`, e.g. in a test environment,
will yield all log messages.

Also I think it's reasonable to print INFO level logs by default.

Signed-off-by: Thomas Mühlbacher <[email protected]>
  • Loading branch information
tmuehlbacher committed Jun 28, 2024
1 parent 411df4e commit c0d84a1
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 60 deletions.
62 changes: 49 additions & 13 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ path = "src/bcachefs.rs"

[dependencies]
log = { version = "0.4", features = ["std"] }
colored = "2"
clap = { version = "4.0.32", features = ["derive", "wrap_help"] }
clap_complete = "4.3.2"
anyhow = "1.0"
Expand All @@ -26,3 +25,8 @@ byteorder = "1.3"
strum = { version = "0.26", features = ["derive"] }
strum_macros = "0.26"
zeroize = { version = "1", features = ["std", "zeroize_derive"] }

[dependencies.env_logger]
version = "0.10"
default-features = false
features = ["auto-color"]
5 changes: 1 addition & 4 deletions src/bcachefs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod commands;
mod key;
mod logging;
mod wrappers;

use std::ffi::{c_char, CString};

use bch_bindgen::c;
use commands::logger::SimpleLogger;

#[derive(Debug)]
pub struct ErrnoError(pub errno::Errno);
Expand Down Expand Up @@ -94,9 +94,6 @@ fn main() {

unsafe { c::raid_init() };

log::set_boxed_logger(Box::new(SimpleLogger)).unwrap();
log::set_max_level(log::LevelFilter::Warn);

let cmd = match symlink_cmd {
Some(s) => s,
None => args[1].as_str(),
Expand Down
14 changes: 10 additions & 4 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use clap::Parser;
use log::error;
use std::io::{stdout, IsTerminal};

use crate::logging;

fn list_keys(fs: &Fs, opt: &Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
let mut iter = BtreeIter::new(
Expand Down Expand Up @@ -145,13 +147,14 @@ pub struct Cli {
#[arg(short, long)]
fsck: bool,

// FIXME: would be nicer to have `--color[=WHEN]` like diff or ls?
/// Force color on/off. Default: autodetect tty
#[arg(short, long, action = clap::ArgAction::Set, default_value_t=stdout().is_terminal())]
colorize: bool,

/// Verbose mode
#[arg(short, long)]
verbose: bool,
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,

#[arg(required(true))]
devices: Vec<std::path::PathBuf>,
Expand Down Expand Up @@ -180,7 +183,7 @@ fn cmd_list_inner(opt: &Cli) -> anyhow::Result<()> {
opt_set!(fs_opts, norecovery, 0);
}

if opt.verbose {
if opt.verbose > 0 {
opt_set!(fs_opts, verbose, 1);
}

Expand All @@ -196,7 +199,10 @@ fn cmd_list_inner(opt: &Cli) -> anyhow::Result<()> {

pub fn list(argv: Vec<String>) -> i32 {
let opt = Cli::parse_from(argv);
colored::control::set_override(opt.colorize);

// TODO: centralize this on the top level CLI
logging::setup(false, opt.verbose, opt.colorize);

if let Err(e) = cmd_list_inner(&opt) {
error!("Fatal error: {}", e);
1
Expand Down
28 changes: 0 additions & 28 deletions src/commands/logger.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use clap::Subcommand;

pub mod completions;
pub mod list;
pub mod logger;
pub mod mount;
pub mod subvolume;

Expand Down
17 changes: 8 additions & 9 deletions src/commands/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ use std::{
use anyhow::{ensure, Result};
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set, path_to_cstr};
use clap::Parser;
use log::{debug, error, info, LevelFilter};
use log::{debug, error, info};
use uuid::Uuid;

use crate::key::{KeyHandle, Passphrase, UnlockPolicy};
use crate::{
key::{KeyHandle, Passphrase, UnlockPolicy},
logging,
};

fn mount_inner(
src: String,
Expand Down Expand Up @@ -250,6 +253,7 @@ pub struct Cli {
#[arg(short, default_value = "")]
options: String,

// FIXME: would be nicer to have `--color[=WHEN]` like diff or ls?
/// Force color on/off. Autodetect tty is used to define default:
#[arg(short, long, action = clap::ArgAction::Set, default_value_t=stdout().is_terminal())]
colorize: bool,
Expand Down Expand Up @@ -383,14 +387,9 @@ pub fn mount(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {

let opt = Cli::parse_from(argv);

// @TODO : more granular log levels via mount option
log::set_max_level(match opt.verbose {
0 => LevelFilter::Warn,
1 => LevelFilter::Trace,
2_u8..=u8::MAX => todo!(),
});
// TODO: centralize this on the top level CLI
logging::setup(false, opt.verbose, opt.colorize);

colored::control::set_override(opt.colorize);
if let Err(e) = cmd_mount_inner(opt) {
error!("Fatal error: {}", e);
1
Expand Down
26 changes: 26 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use env_logger::WriteStyle;
use log::LevelFilter;

pub fn setup(quiet: bool, verbose: u8, color: bool) {
let level_filter = if quiet {
LevelFilter::Off
} else {
match verbose {
0 => LevelFilter::Info,
1 => LevelFilter::Debug,
_ => LevelFilter::Trace,
}
};

let style = if color {
WriteStyle::Always
} else {
WriteStyle::Never
};

env_logger::Builder::new()
.filter_level(level_filter)
.write_style(style)
.parse_env("BCACHEFS_LOG")
.init();
}

0 comments on commit c0d84a1

Please sign in to comment.