macro_log/
log.rs

1#![allow(unused)]
2
3#[allow(non_camel_case_types)]
4#[derive(Clone, Copy)]
5pub enum Level {
6    VERBOSE = 2,
7    DEBUG,
8    INFO,
9    WARN,
10    ERROR,
11    WTF,
12}
13
14impl Level {
15    fn tag(self: Self) -> &'static str {
16        let index = self as usize;
17        ["", "", "V", "D", "I", "W", "E", "WTF"][index]
18    }
19}
20
21#[cfg(not(target_os = "android"))]
22pub mod platform {
23    use super::Level;
24    use crate::time::get_time;
25
26    pub fn print(level: Level, file: &str, line: u32, str: String) {
27        let time = get_time();
28        match level {
29            Level::ERROR => {
30                eprintln!("{time} - [{tag}] - {}:{} -> {}", file, line, str, tag = level.tag());
31            },
32            _ => {
33                println!("{time} - [{tag}] - {}:{} -> {}", file, line, str, tag = level.tag());
34            },
35        }
36    }
37
38    #[cfg(debug_assertions)]
39    #[macro_export]
40    macro_rules! wtf {
41        ($($arg: expr $(,)?)+) => {
42            $(
43                $crate::log!($crate::log::Level::WTF, "{} = {:?}", stringify!($arg), $arg);
44            )+
45        }
46    }
47}
48
49#[cfg(target_os = "android")]
50pub mod platform {
51    use super::Level;
52    use crate::time::get_time;
53    pub const FMT: *const u8 = "%s\0".as_ptr();
54
55    extern {
56        // int __android_log_write(int prio, const char* tag, const char* text);
57        // int __android_log_print(int prio, const char* tag, const char* fmt, ...) __attribute__((__format__(printf, 3, 4)));
58        pub fn __android_log_print(level: i32, tag: *const u8, fmt: *const u8, ...);
59    }
60
61    pub fn print(level: Level, file: &str, line: u32, mut str: String) {
62        let tag = format!("{}:{}\0", file, line);
63        str.push('\0');
64        unsafe {
65            __android_log_print(level as i32, tag.as_ptr(), FMT, str.as_ptr());
66        }
67    }
68    
69    #[cfg(debug_assertions)]
70    #[macro_export]
71    macro_rules! wtf {
72        ($($arg: expr $(,)?)+) => {
73            $(
74                $crate::log!($crate::log::Level::ERROR, "{} = {:?}", stringify!($arg), $arg);
75            )+
76        }
77    }
78}
79
80#[cfg(debug_assertions)]
81mod debug {
82    #[macro_export]
83    macro_rules! d {
84        ($($arg: tt)+) => {
85            $crate::log!($crate::log::Level::DEBUG, $($arg)+);
86        }
87    }
88}
89
90#[cfg(not(debug_assertions))]
91mod release {
92    #[macro_export]
93    macro_rules! wtf {
94        ($($arg: tt)+) => (())
95    }
96
97    #[macro_export]
98    macro_rules! d {
99        ($($arg: tt)+) => (())
100    }
101}
102
103mod common {
104    #[macro_export]
105    macro_rules! log {
106        ($type: expr, $($arg: tt)+) => {
107            $crate::log::platform::print($type, file!(), line!(), format!($($arg)+));
108        }
109    }
110
111    #[macro_export]
112    macro_rules! i {
113        ($($arg: tt)+) => {
114            $crate::log!($crate::log::Level::INFO, $($arg)+);
115        }
116    }
117
118    #[macro_export]
119    macro_rules! w {
120        ($($arg: tt)+) => {
121            $crate::log!($crate::log::Level::WARN, $($arg)+);
122        }
123    }
124
125    #[macro_export]
126    macro_rules! e {
127        ($($arg: tt)+) => {
128            $crate::log!($crate::log::Level::ERROR, $($arg)+);
129        }
130    }
131}