summaryrefslogtreecommitdiff
path: root/rust-src/bch_bindgen/src
diff options
context:
space:
mode:
authorAlexander Fougner <fougner89@gmail.com>2023-01-16 17:08:52 +0100
committerAlexander Fougner <fougner89@gmail.com>2023-01-18 06:47:46 +0100
commit8accfdc3c596bfe5cbd10dac3f1650192981eb17 (patch)
treee93535eb6ded70806cc7518fb453448d569abaa1 /rust-src/bch_bindgen/src
parent20aecb42d8d1c869b17b38b979b9f2f7cbd2ca34 (diff)
rust: replace tracing with logger
tracing framework is a overengineered for simple mount helper. Add a few very barebone logging macros to allow configurable verbosity and colorized output with a small footprint. Signed-off-by: Alexander Fougner <fougner89@gmail.com>
Diffstat (limited to 'rust-src/bch_bindgen/src')
-rw-r--r--rust-src/bch_bindgen/src/lib.rs2
-rw-r--r--rust-src/bch_bindgen/src/log.rs57
-rw-r--r--rust-src/bch_bindgen/src/rs.rs18
3 files changed, 65 insertions, 12 deletions
diff --git a/rust-src/bch_bindgen/src/lib.rs b/rust-src/bch_bindgen/src/lib.rs
index dc8bcf4a..c54786aa 100644
--- a/rust-src/bch_bindgen/src/lib.rs
+++ b/rust-src/bch_bindgen/src/lib.rs
@@ -1,7 +1,7 @@
pub mod bcachefs;
pub mod keyutils;
+pub mod log;
pub mod rs;
-
pub mod c {
pub use crate::bcachefs::*;
}
diff --git a/rust-src/bch_bindgen/src/log.rs b/rust-src/bch_bindgen/src/log.rs
new file mode 100644
index 00000000..32927f16
--- /dev/null
+++ b/rust-src/bch_bindgen/src/log.rs
@@ -0,0 +1,57 @@
+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)*)
+ );
+ }
+ }
+}
diff --git a/rust-src/bch_bindgen/src/rs.rs b/rust-src/bch_bindgen/src/rs.rs
index 7a3a3eaf..4f5dbcc0 100644
--- a/rust-src/bch_bindgen/src/rs.rs
+++ b/rust-src/bch_bindgen/src/rs.rs
@@ -1,4 +1,6 @@
use crate::bcachefs;
+use crate::{error, info};
+use colored::Colorize;
pub const SUPERBLOCK_MAGIC: uuid::Uuid =
uuid::Uuid::from_u128(0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81);
@@ -13,27 +15,22 @@ pub enum ReadSuperErr {
type RResult<T> = std::io::Result<std::io::Result<T>>;
-#[tracing_attributes::instrument(skip(opts))]
pub fn read_super_opts(
path: &std::path::Path,
mut opts: bcachefs::bch_opts,
) -> RResult<bcachefs::bch_sb_handle> {
- // let devp = camino::Utf8Path::from_path(devp).unwrap();
-
use std::os::unix::ffi::OsStrExt;
let path = std::ffi::CString::new(path.as_os_str().as_bytes())?;
let mut sb = std::mem::MaybeUninit::zeroed();
- // use gag::{BufferRedirect};
- // // Stop libbcachefs from spamming the output
- // let gag = BufferRedirect::stderr().unwrap();
- // tracing::trace!("entering libbcachefs");
-
let ret =
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
- tracing::trace!(%ret);
+ println!("{}", ret);
+ info!("something");
+ error!("an error");
+ String::from("something").bright_black();
match -ret {
libc::EACCES => Err(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
@@ -45,7 +42,7 @@ pub fn read_super_opts(
"Not a BCacheFS SuperBlock",
))),
code => {
- tracing::debug!(msg = "BCacheFS return error code", ?code);
+ println!("BCacheFS return error code: {}", code);
Ok(Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to Read SuperBlock",
@@ -54,7 +51,6 @@ pub fn read_super_opts(
}
}
-#[tracing_attributes::instrument]
pub fn read_super(path: &std::path::Path) -> RResult<bcachefs::bch_sb_handle> {
let opts = bcachefs::bch_opts::default(); //unsafe {std::mem::MaybeUninit::zeroed().assume_init()};
read_super_opts(path, opts)