diff --git a/Cargo.lock b/Cargo.lock index 17e3f340..ba110074 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,6 +227,15 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.19" @@ -273,6 +282,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + [[package]] name = "digest" version = "0.11.0-pre.8" @@ -471,6 +489,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "lazy_static" version = "1.4.0" @@ -564,6 +588,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num_cpus" version = "1.16.0" @@ -690,6 +720,12 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "proc-macro2" version = "1.0.79" @@ -799,6 +835,7 @@ dependencies = [ "thiserror", "tokio", "tracing", + "tracing-appender", "tracing-subscriber", "weak-table", ] @@ -1001,6 +1038,37 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.3.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tokio" version = "1.37.0" @@ -1041,6 +1109,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf" +dependencies = [ + "crossbeam-channel", + "thiserror", + "time", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.27" diff --git a/Cargo.toml b/Cargo.toml index 73bb3530..dec3531f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ mio = { version = "0.8.11", features = ["os-poll"] } bytes = "1.5" tracing = "0.1.40" tracing-subscriber = "0.3" +tracing-appender = "0.2.3" ctrlc = { version = "3.1.9" , features = ["termination"]} parking_lot = "0.12.1" pbkdf2 = "0.13.0-pre.0" diff --git a/src/encryptedfs_fuse3.rs b/src/encryptedfs_fuse3.rs index e913b857..d756c630 100644 --- a/src/encryptedfs_fuse3.rs +++ b/src/encryptedfs_fuse3.rs @@ -233,6 +233,8 @@ fn from_attr(from: FileAttr) -> fuse3::raw::prelude::FileAttr { impl Filesystem for EncryptedFsFuse3 { #[instrument(skip(self), err(level = Level::DEBUG))] async fn init(&self, _req: Request) -> Result { + trace!(""); + #[cfg(feature = "abi-7-26")] config.add_capabilities(FUSE_HANDLE_KILLPRIV).unwrap(); @@ -242,7 +244,9 @@ impl Filesystem for EncryptedFsFuse3 { } #[instrument(skip(self))] - async fn destroy(&self, _req: Request) {} + async fn destroy(&self, _req: Request) { + trace!(""); + } #[instrument(skip(self, name), fields(name = name.to_str().unwrap()))] async fn lookup(&self, req: Request, parent: u64, name: &OsStr) -> Result { @@ -310,6 +314,8 @@ impl Filesystem for EncryptedFsFuse3 { _fh: Option, _flags: u32, ) -> Result { + trace!(""); + match self.get_fs().borrow_mut().get_inode(inode) { Err(err) => { error!(err = %err); @@ -338,6 +344,7 @@ impl Filesystem for EncryptedFsFuse3 { set_attr: SetAttr, ) -> Result { + trace!(""); debug!("{set_attr:#?}"); let mut attr = if let Ok(attr) = self.get_fs().borrow_mut().get_inode(inode) { attr } else { @@ -491,6 +498,7 @@ impl Filesystem for EncryptedFsFuse3 { mode: u32, rdev: u32, ) -> Result { + trace!(""); debug!("mode={mode:o}"); let file_type = mode & libc::S_IFMT as u32; @@ -529,6 +537,7 @@ impl Filesystem for EncryptedFsFuse3 { mode: u32, umask: u32, ) -> Result { + trace!(""); debug!("mode={mode:o}"); let parent_attr = match self.get_fs().borrow_mut().get_inode(parent) { @@ -693,6 +702,8 @@ impl Filesystem for EncryptedFsFuse3 { new_parent: Inode, new_name: &OsStr, ) -> Result<()> { + trace!(""); + let attr = if let Ok(Some(attr)) = self.get_fs().borrow_mut().find_by_name(parent, name.to_str().unwrap()) { attr } else { @@ -863,6 +874,7 @@ impl Filesystem for EncryptedFsFuse3 { _flags: u32, ) -> Result { + trace!(""); debug!(size = data.len()); if let Err(err) = self.get_fs().borrow_mut().write_all(inode, offset, data, fh) { diff --git a/src/lib.rs b/src/lib.rs index 57a3145a..d6e60475 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,5 +108,31 @@ //! create_attr(0, file_type) //! } //! ``` +use tracing::Level; +use tracing_appender::non_blocking::WorkerGuard; + pub mod encryptedfs; pub mod encryptedfs_fuse3; + +pub fn is_debug() -> bool { + #[cfg(debug_assertions)] { + return true; + } + return false; +} + +pub fn log_init(level: Level) -> WorkerGuard { + let (writer, guard) = tracing_appender::non_blocking(std::io::stdout()); + let builder = tracing_subscriber::fmt() + .with_writer(writer) + .with_max_level(level); + if is_debug() { + builder + .pretty() + .init() + } else { + builder.init(); + } + + guard +} diff --git a/src/main.rs b/src/main.rs index 0577c56e..e95fb293 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use tracing::{error, info, instrument, Level}; use rencfs::encryptedfs::{Cipher, EncryptedFs, FsError}; use rencfs::encryptedfs_fuse3::EncryptedFsFuse3; +use rencfs::{is_debug, log_init}; #[tokio::main] async fn main() { @@ -143,12 +144,19 @@ fn async_main() { ) .get_matches(); - let log_level = matches.get_one::("log-level").unwrap().as_str(); - if Level::from_str(log_level).is_err() { - println!("Invalid log level"); - return; - } - log_init(log_level); + let log_level = if is_debug() { + Level::TRACE + } else { + let log_level_str = matches.get_one::("log-level").unwrap().as_str(); + let log_level = Level::from_str(log_level_str); + if log_level.is_err() { + println!("Invalid log level"); + return; + } + + log_level.unwrap() + }; + let _guard = log_init(log_level); let data_dir: String = matches .get_one::("data-dir") @@ -305,10 +313,3 @@ fn umount(mountpoint: &str, print_fail_status: bool) { println!("Cannot umount, maybe it was not mounted"); } } - -fn log_init(level: &str) { - let subscriber = tracing_subscriber::fmt() - .with_max_level(Level::from_str(level).unwrap()) - .finish(); - tracing::subscriber::set_global_default(subscriber).unwrap(); -}