1use std::sync::Mutex;
17use std::collections::HashMap;
18
19pub use log;
20pub use paste;
21
22lazy_static::lazy_static! {
23 static ref PRINT_COUNT: Mutex<HashMap<(log::Level, &'static str), u64>>
24 = Mutex::new(HashMap::new());
25}
26pub const MAX_PRINT_COUNT: u64 = 20;
27
28pub fn init_stderr_color_debug() {
31 use simplelog::*;
32 TermLogger::init(
33 LevelFilter::Debug,
34 ConfigBuilder::new()
35 .set_location_level(LevelFilter::Debug)
36 .set_thread_level(LevelFilter::Trace)
37 .build(),
38 TerminalMode::Stderr,
39 ColorChoice::Auto,
40 ).unwrap();
41}
42
43pub fn obtain_count(typ: log::Level, id: &'static str) -> u64 {
47 let mut print_counts = PRINT_COUNT.lock().unwrap();
48 match print_counts.get_mut(&(typ, id)) {
49 Some(v) => {
50 *v += 1;
51 *v
52 },
53 None => {
54 print_counts.insert((typ, id), 1);
55 1
56 }
57 }
58}
59
60#[macro_export]
64macro_rules! log_monitor {
65 ($typ:ident, $id:ident, $fmt:expr $(,$param:expr)*) => {{
66 let count = $crate::obtain_count(
67 $crate::paste::paste!($crate::log::Level::[<$typ:camel>]),
68 stringify!($id));
69 if count <= $crate::MAX_PRINT_COUNT {
70 $crate::log::$typ!(concat!("(", stringify!($id), ") ", $fmt)
71 $(,$param)*);
72 }
73 if count == $crate::MAX_PRINT_COUNT {
74 $crate::log::$typ!(
75 concat!("Further ",
76 stringify!($typ),
78 " (", stringify!($id), ") will be suppressed."));
79 }
80 }};
81 ($typ:ident, $fmt:expr $(,$param:expr)*) => {{
82 $crate::log::$typ!($fmt $(,$param)*);
83 }}
84}
85
86#[macro_export]
100macro_rules! info {
101 ($t:tt $(,$p:expr)*) => ($crate::log_monitor!(info, $t $(,$p)*))
102}
103
104#[macro_export]
105macro_rules! warn {
106 ($t:tt $(,$p:expr)*) => ($crate::log_monitor!(warn, $t $(,$p)*))
107}
108
109#[macro_export]
110macro_rules! error {
111 ($t:tt $(,$p:expr)*) => ($crate::log_monitor!(error, $t $(,$p)*))
112}
113
114#[macro_export]
115macro_rules! debug {
116 ($t:tt $(,$p:expr)*) => ($crate::log_monitor!(debug, $t $(,$p)*))
117}
118
119#[macro_export]
120macro_rules! trace {
121 ($t:tt $(,$p:expr)*) => ($crate::log_monitor!(trace, $t $(,$p)*))
122}
123
124mod logging_timer;
125pub use logging_timer::*;