call_counter/
lib.rs

1use std::{collections::BTreeMap, sync::Mutex};
2
3static COUNTER: Mutex<BTreeMap<u32, u32>> = Mutex::new(BTreeMap::new());
4
5#[macro_export]
6macro_rules! count_calls {
7    () => {
8        call_counter::__increment_counter(file!(), line!());
9    };
10}
11
12pub fn __increment_counter(file: &'static str, line: u32) {
13    let mut lock = COUNTER.lock().unwrap();
14    let counter = lock.entry(line).or_default();
15
16    *counter += 1;
17
18    println!("{file}:{line} - {counter}");
19}
20
21#[cfg(test)]
22mod test {
23
24    use crate as call_counter;
25
26    fn call() {
27        count_calls!();
28    }
29
30    #[test]
31    fn test() {
32        for _ in 0..10 {
33            call();
34        }
35    }
36}