mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-02-22 00:00:03 +03:00
chore: logger for idiomatic style and expanded logging levels
Improve the Rust logger by adhering to idiomatic Rust conventions and incorporating additional logging levels: warn, debug, and trace. Signed-off-by: TruongSinh Tran-Nguyen <i@truongsinh.pro>
This commit is contained in:
parent
4f6b28f54f
commit
47ec3ed6ed
@ -9,7 +9,7 @@ crate-type = ["staticlib"]
|
||||
|
||||
[dependencies]
|
||||
atty = "0.2.14"
|
||||
log = "0.4"
|
||||
log = { version = "0.4", features = ["std"] }
|
||||
chrono = "0.4"
|
||||
colored = "2"
|
||||
clap = { version = "4.0.32", features = ["derive", "wrap_help"] }
|
||||
|
@ -3,7 +3,6 @@ pub mod btree;
|
||||
pub mod bkey;
|
||||
pub mod errcode;
|
||||
pub mod keyutils;
|
||||
pub mod log;
|
||||
pub mod rs;
|
||||
pub mod fs;
|
||||
pub mod opts;
|
||||
|
@ -1,57 +0,0 @@
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
|
||||
pub const MUTE: u8 = 0;
|
||||
pub const ERROR: u8 = 1;
|
||||
pub const INFO: u8 = 2;
|
||||
pub const DEBUG: u8 = 3;
|
||||
|
||||
// error level by default
|
||||
pub static VERBOSE: AtomicU8 = AtomicU8::new(ERROR);
|
||||
|
||||
#[inline]
|
||||
pub fn set_verbose_level(level: u8) {
|
||||
VERBOSE.store(level, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
pub fn max_level() -> u8 {
|
||||
VERBOSE.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
($($arg:tt)*) => {
|
||||
if 2 <= $crate::log::max_level() {
|
||||
println!("{} {} {}",
|
||||
" INFO".green(),
|
||||
format!("{}:", module_path!()).bright_black(),
|
||||
format_args!($($arg)*)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
($($arg:tt)*) => {
|
||||
if 3 <= $crate::log::max_level() {
|
||||
println!("{} {} {}",
|
||||
"DEBUG".bright_blue(),
|
||||
format!("{}:", module_path!()).bright_black(),
|
||||
format_args!($($arg)*)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
($($arg:tt)*) => {
|
||||
if 1 <= $crate::log::max_level() {
|
||||
println!("{} {} {}",
|
||||
"ERROR".bright_red(),
|
||||
format!("{}:", module_path!()).bright_black(),
|
||||
format_args!($($arg)*)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use atty::Stream;
|
||||
use bch_bindgen::error;
|
||||
use log::{error};
|
||||
use bch_bindgen::bcachefs;
|
||||
use bch_bindgen::opt_set;
|
||||
use bch_bindgen::fs::Fs;
|
||||
@ -9,7 +9,6 @@ use bch_bindgen::btree::BtreeIter;
|
||||
use bch_bindgen::btree::BtreeNodeIter;
|
||||
use bch_bindgen::btree::BtreeIterFlags;
|
||||
use clap::Parser;
|
||||
use colored::Colorize;
|
||||
use std::ffi::{CStr, OsStr, c_int, c_char};
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
use atty::Stream;
|
||||
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, debug, error, info};
|
||||
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle};
|
||||
use log::{info, warn, debug, error, trace, LevelFilter};
|
||||
use clap::Parser;
|
||||
use colored::Colorize;
|
||||
use uuid::Uuid;
|
||||
use std::convert::TryInto;
|
||||
use std::path::PathBuf;
|
||||
use crate::key;
|
||||
use crate::key::KeyLoc;
|
||||
use crate::logger::SimpleLogger;
|
||||
use std::ffi::{CStr, CString, OsStr, c_int, c_char, c_void};
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
|
||||
@ -202,9 +204,20 @@ pub extern "C" fn cmd_mount(argc: c_int, argv: *const *const c_char) {
|
||||
.collect();
|
||||
|
||||
let opt = Cli::parse_from(argv);
|
||||
bch_bindgen::log::set_verbose_level(opt.verbose + bch_bindgen::log::ERROR);
|
||||
|
||||
log::set_boxed_logger(Box::new(SimpleLogger)).unwrap();
|
||||
|
||||
// @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!(),
|
||||
});
|
||||
|
||||
colored::control::set_override(opt.colorize);
|
||||
if let Err(e) = cmd_mount_inner(opt) {
|
||||
error!("Fatal error: {}", e);
|
||||
} else {
|
||||
info!("Successfully mounted");
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
use bch_bindgen::info;
|
||||
use log::{info};
|
||||
use bch_bindgen::bcachefs::bch_sb_handle;
|
||||
use colored::Colorize;
|
||||
use crate::c_str;
|
||||
use anyhow::anyhow;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
pub mod key;
|
||||
pub mod logger;
|
||||
pub mod cmd_mount;
|
||||
pub mod cmd_list;
|
||||
|
||||
|
28
rust-src/src/logger.rs
Normal file
28
rust-src/src/logger.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use colored::Colorize;
|
||||
use log::{Level, Metadata, Record};
|
||||
|
||||
pub struct SimpleLogger;
|
||||
|
||||
impl log::Log for SimpleLogger {
|
||||
fn enabled(&self, _: &Metadata) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
let debug_prefix = match record.level() {
|
||||
Level::Error => "ERROR".bright_red(),
|
||||
Level::Warn => "WARN".bright_yellow(),
|
||||
Level::Info => "INFO".green(),
|
||||
Level::Debug => "DEBUG".bright_blue(),
|
||||
Level::Trace => "TRACE".into(),
|
||||
};
|
||||
println!(
|
||||
"{} - {}: {}",
|
||||
debug_prefix,
|
||||
record.module_path().unwrap_or_default().bright_black(),
|
||||
record.args()
|
||||
);
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user