From 812982556a2b21e12dc609748e4e16510ffd5efd Mon Sep 17 00:00:00 2001 From: Wildan M Date: Mon, 6 Jul 2026 05:35:13 +0700 Subject: [PATCH] Parse flags when logging openat --- src/macros.rs | 13 +++++++ src/platform/logger.rs | 14 ++++---- src/platform/redox/path.rs | 69 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index a058bccd04..555cbf8299 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -115,6 +115,19 @@ macro_rules! trace_expr { }); } +// log::trace! but functions inside them will +// not be evaluated or compiled unless enabled +macro_rules! trace_log { + ($($arg:tt)+) => { + #[cfg(not(feature = "no_trace"))] + { + if log::log_enabled!(log::Level::Trace) { + log::trace!($($arg)+); + } + } + }; +} + #[macro_export] macro_rules! skipws { ($ptr:expr) => { diff --git a/src/platform/logger.rs b/src/platform/logger.rs index a93e512457..a61de9a795 100644 --- a/src/platform/logger.rs +++ b/src/platform/logger.rs @@ -227,13 +227,15 @@ impl RedoxLogger { self } pub fn enable(self) -> Result<&'static Self, log::SetLoggerError> { - let leak = Box::leak(Box::new(self)); - log::set_logger(leak)?; - if let Some(max) = leak.max_level_in_use { - log::set_max_level(max); - } else { - log::set_max_level(DEFAULT_LOG_LEVEL); + let mut this = self; + if this.max_level_in_use.is_none() { + // to fix log::log_enabled! macro if + // DEFAULT_LOG_LEVEL changed to TRACE + this = this.with_max_level_override(DEFAULT_LOG_LEVEL); } + let leak = Box::leak(Box::new(this)); + log::set_logger(leak)?; + log::set_max_level(leak.max_level_in_use.unwrap()); Ok(leak) } fn write_record( diff --git a/src/platform/redox/path.rs b/src/platform/redox/path.rs index b08022e68b..55ae546cf8 100644 --- a/src/platform/redox/path.rs +++ b/src/platform/redox/path.rs @@ -221,9 +221,10 @@ pub fn openat(dirfd: c_int, path: RedoxStr<'_>, flags: usize) -> Result { redox_rt::sys::openat(dirfd as usize, &path, flags, fcntl_flags) }; - log::trace!( - "openat({dirfd:?}, {:?}, {flags:x}): {:?}", + trace_log!( + "openat({dirfd:?}, {:?}, {}): {:?}", &path, + decode_open_flags(flags), initial_res ); @@ -268,7 +269,12 @@ pub fn open(path: RedoxStr<'_>, flags: usize) -> Result { open_absolute(&path, flags) }; - log::trace!("open({:?}, {flags:x}): {:?}", &path, initial_res); + trace_log!( + "open({:?}, {}): {:?}", + &path, + decode_open_flags(flags), + initial_res + ); match initial_res { Ok(fd) => Ok(fd), @@ -400,3 +406,60 @@ pub(super) fn openat2( let oflags = at_flags_to_open_flags(at_flags) | fcntl::O_CLOEXEC | oflags; File::openat(dirfd, path, oflags) } + +#[cfg(any(debug_assertions, not(feature = "no_trace")))] +pub fn decode_open_flags(mut value: usize) -> alloc::string::String { + let flags = [ + ("O_NONBLOCK", O_NONBLOCK), + ("O_APPEND", O_APPEND), + ("O_SHLOCK", O_SHLOCK), + ("O_EXLOCK", O_EXLOCK), + ("O_ASYNC", O_ASYNC), + ("O_FSYNC", O_FSYNC), + ("O_CLOEXEC", O_CLOEXEC), + ("O_CREAT", O_CREAT), + ("O_TRUNC", O_TRUNC), + ("O_EXCL", O_EXCL), + ("O_DIRECTORY", O_DIRECTORY), + ("O_STAT", O_STAT), + ("O_SYMLINK", O_SYMLINK), + ("O_NOFOLLOW", O_NOFOLLOW), + ]; + + let mut result = alloc::string::String::new(); + + let accmode = value & O_ACCMODE; + match accmode { + O_RDONLY => result.push_str("O_RDONLY"), + O_WRONLY => result.push_str("O_WRONLY"), + O_RDWR => result.push_str("O_RDWR"), + _ => {} + } + value &= !O_ACCMODE; + for (name, flag) in flags.iter() { + if (value & flag) == *flag { + if !result.is_empty() { + result.push_str(" | "); + } + result.push_str(name); + value &= !flag; + } + } + + const PERM: usize = MODE_PERM as usize; + if (value & PERM) != 0 { + if !result.is_empty() { + result.push_str(" | "); + } + result.push_str(&format!("0o{:o}", value & PERM)); + } + value &= !PERM; + if value != 0 { + if !result.is_empty() { + result.push_str(" | "); + } + result.push_str(&format!("0x{:x}", value)); + } + + result +}