diff options
Diffstat (limited to 'mingling_core')
| -rw-r--r-- | mingling_core/Cargo.toml | 4 | ||||
| -rw-r--r-- | mingling_core/src/debug.rs | 104 |
2 files changed, 108 insertions, 0 deletions
diff --git a/mingling_core/Cargo.toml b/mingling_core/Cargo.toml index 0d4baaa..f7f7c00 100644 --- a/mingling_core/Cargo.toml +++ b/mingling_core/Cargo.toml @@ -10,6 +10,7 @@ repository = "https://github.com/catilgrass/mingling" default = [] full = ["comp", "general_renderer"] comp = ["dep:just_template"] +debug = ["dep:log", "dep:env_logger"] general_renderer = [ "dep:serde", "dep:ron", @@ -31,3 +32,6 @@ ron = { version = "0.12.1", optional = true } serde_json = { version = "1", optional = true } serde_yaml = { version = "0.9", optional = true } toml = { version = "0.9.8", optional = true } + +log = { version = "0.4", optional = true } +env_logger = { version = "0.11", optional = true } diff --git a/mingling_core/src/debug.rs b/mingling_core/src/debug.rs new file mode 100644 index 0000000..9fd553c --- /dev/null +++ b/mingling_core/src/debug.rs @@ -0,0 +1,104 @@ +#[macro_export] +macro_rules! only_debug { + ($($expr:expr);* $(;)?) => { + #[cfg(feature = "debug")] + { + $($expr;)* + } + }; +} + +#[macro_export] +macro_rules! trace { + ($($arg:tt)*) => { + $crate::only_debug! { + log::trace!($($arg)*) + } + }; +} + +#[macro_export] +macro_rules! debug { + ($($arg:tt)*) => { + $crate::only_debug! { + log::debug!($($arg)*) + } + }; +} + +#[macro_export] +macro_rules! info { + ($($arg:tt)*) => { + $crate::only_debug! { + log::info!($($arg)*) + } + }; +} + +#[macro_export] +macro_rules! warn { + ($($arg:tt)*) => { + $crate::only_debug! { + log::warn!($($arg)*) + } + }; +} + +#[macro_export] +macro_rules! error { + ($($arg:tt)*) => { + $crate::only_debug! { + log::error!($($arg)*) + } + }; +} + +#[cfg(feature = "debug")] +pub fn init_env_logger() { + let mut log_path = std::env::current_exe() + .expect("Failed to get current executable path") + .parent() + .expect("Failed to get parent directory") + .to_path_buf(); + // Search for _log.txt in parent directories + let mut current_dir = log_path.parent().unwrap().to_path_buf(); + let mut found_log = false; + + while current_dir.parent().is_some() { + let log_file = current_dir.join("_log.txt"); + if log_file.exists() { + log_path = log_file; + found_log = true; + break; + } + current_dir = current_dir.parent().unwrap().to_path_buf(); + } + + // If not found, use "log.txt" in the original location + if !found_log { + log_path = std::env::current_exe() + .expect("Failed to get current executable path") + .parent() + .expect("Failed to get parent directory") + .to_path_buf(); + log_path.push("log.txt"); + } + + // Only initialize logger if log file exists + if log_path.exists() { + use env_logger::Target; + use log::LevelFilter; + use std::fs::OpenOptions; + + let log_file = OpenOptions::new() + .create(true) + .append(true) + .open(&log_path) + .expect("Failed to open log file"); + + env_logger::Builder::new() + .filter_level(LevelFilter::Trace) + .target(Target::Pipe(Box::new(log_file))) + .init(); + } +} |
