pub use std::cell::RefCell;
pub use std::collections::HashSet;
pub type Enabled = HashSet<String>;
pub type Disabled = HashSet<String>;
pub type Included = HashSet<String>;
pub type Excluded = HashSet<String>;
pub type ModList = RefCell<(Enabled, Disabled, Included, Excluded)>;
thread_local! {
pub static MOD_LIST: ModList = RefCell::new(Default::default());
}
#[macro_export] macro_rules!
logmod {
( $( $([$($n:tt)?])? $p:path, )* ) => {
let mut enabled = std::collections::HashSet::<String>::new();
let mut disabled = std::collections::HashSet::<String>::new();
let mut included = std::collections::HashSet::<String>::new();
let mut excluded = std::collections::HashSet::<String>::new();
$(
mod_path_parse!([$($($n)?)?], $p, enabled, disabled, included, excluded);
)*;
glob_replace!(
$crate::logmod::MOD_LIST,
(enabled, disabled, included, excluded)
);
};
( $( $([$($n:tt)?])? $p:path ),* ) => { logmod!($( $([$($n)?])? $p ),*,) };
}
#[macro_export] macro_rules!
mod_path_parse {
( [ ], $p:path, $enabled:expr, $disabled:expr, $included:expr, $excluded:expr ) => {
$enabled.insert(mod_path_normalyze!($p));
$included.insert(mod_path_normalyze!($p));
};
( [= ], $p:path, $enabled:expr, $disabled:expr, $included:expr, $excluded:expr ) => {
mod_path_parse!([ ], $p, $enabled, $disabled, $included, $excluded)
};
( [! ], $p:path, $enabled:expr, $disabled:expr, $included:expr, $excluded:expr ) => {
$disabled.insert(mod_path_normalyze!($p));
$excluded.insert(mod_path_normalyze!($p));
};
( [==], $p:path, $enabled:expr, $disabled:expr, $included:expr, $excluded:expr ) => {
$included.insert(mod_path_normalyze!($p));
};
( [!=], $p:path, $enabled:expr, $disabled:expr, $included:expr, $excluded:expr ) => {
$excluded.insert(mod_path_normalyze!($p));
};
}
#[macro_export] macro_rules!
mod_path_normalyze {
( $p:path ) => {
{
let app_name = option_env!("CARGO_PKG_NAME").unwrap_or("").trim().to_lowercase().replace("-", "_");
let mut path = stringify!($p).trim().to_lowercase().replace("crate", &app_name).replace("main", &app_name);
if path.starts_with("::") {
path = path.replacen("::", &format!("{app_name}::"), 1);
}
path
}
};
}
#[macro_export] macro_rules!
logmod_helper {
( $($msg:tt)* ) => {
glob_access!(
$crate::logmod::MOD_LIST,
mod_list,
{
let path = module_path!().to_lowercase();
if mod_list.3.contains(&path) {
return mod_list
}
if mod_list.2.contains(&path) {
$($msg)*; return mod_list;
}
let path_len = path.trim_matches(':').matches("::").count();
let mut path_tmp = path.clone();
for _ in 0..path_len {
path_tmp = match path_tmp.rsplit_once("::") {
Some(v) => { v.0.into() },
None => unreachable!(),
};
if mod_list.1.contains(&path_tmp) {
mod_list.3.insert(path);
mod_list.3.insert(path_tmp);
return mod_list;
} else if mod_list.0.contains(&path_tmp) {
$($msg)*; mod_list.2.insert(path);
mod_list.2.insert(path_tmp);
return mod_list;
} else if !mod_list.2.contains(&path_tmp) {
mod_list.3.insert(path_tmp.clone());
}
}
mod_list.3.insert(path);
mod_list
}
);
};
}