summaryrefslogtreecommitdiff
path: root/rust-src/bch_bindgen
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
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')
-rw-r--r--rust-src/bch_bindgen/Cargo.lock4
-rw-r--r--rust-src/bch_bindgen/Cargo.toml4
-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
5 files changed, 69 insertions, 16 deletions
diff --git a/rust-src/bch_bindgen/Cargo.lock b/rust-src/bch_bindgen/Cargo.lock
index 7485b9be..747fe730 100644
--- a/rust-src/bch_bindgen/Cargo.lock
+++ b/rust-src/bch_bindgen/Cargo.lock
@@ -191,9 +191,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
-version = "7.1.2"
+version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e5507769c4919c998e69e49c839d9dc6e693ede4cc4290d6ad8b41d4f09c548c"
+checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
diff --git a/rust-src/bch_bindgen/Cargo.toml b/rust-src/bch_bindgen/Cargo.toml
index 7a619117..ce15fe0a 100644
--- a/rust-src/bch_bindgen/Cargo.toml
+++ b/rust-src/bch_bindgen/Cargo.toml
@@ -9,14 +9,14 @@ crate-type = ["lib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-tracing = "0.1.26"
+chrono = "0.4"
+colored = "2"
anyhow = "1.0"
udev = "0.7.0"
uuid = "1.2.2"
bitfield = "0.14.0"
memoffset = "0.8.0"
byteorder = "1.3"
-tracing-attributes = "0.1.15"
libc = "0.2.69"
gag = "1.0.0"
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)