1struct Logger {
2 to: Option<Box<dyn std::io::Write>>,
3}
4static mut LOGGER: Logger = Logger { to: None };
5#[derive(PartialEq, PartialOrd, Copy, Clone)]
6pub enum Level {
7 Trace,
8 Debug,
9 Info,
10 Warn,
11 Error,
12}
13static mut LEVEL: Level = Level::Info;
14
15impl Logger {
16 pub fn log(&mut self, args: std::fmt::Arguments<'_>) {
18 if let Some(ref mut to) = self.to {
19 let _ = to.write_fmt(args).unwrap();
20 }
21 }
23}
24pub fn init(to: impl std::io::Write + 'static, level: Level) {
33 unsafe {
35 LOGGER = Logger {
36 to: Some(Box::new(to)),
37 };
38 LEVEL = level;
39 }
40}
41pub fn with_file(name: &str) {
43 unsafe {
45 LOGGER = Logger {
46 to: Some(Box::new(
47 std::fs::File::options()
48 .create(true)
49 .append(true)
50 .open(name.to_string())
51 .unwrap(),
52 )),
53 };
54 }
55}
56#[inline]
65pub fn set_level(level: Level) {
66 unsafe {
67 LEVEL = level;
68 }
69}
70#[inline]
72pub fn level() -> Level {
73 unsafe { LEVEL }
74}
75#[inline]
77pub fn log(args: std::fmt::Arguments<'_>) {
78 unsafe {
79 LOGGER.log(args);
80 }
81}
82#[macro_export]
84macro_rules! log_dispatch {
85 ($lv:literal $level:ident opt,$val:expr,$($tail:tt)*) => {
86 {
87 let mut temp = $val;
88 if $crate::log::level() <= $crate::log::Level::$level && temp.is_none() {
89 $crate::log::log(format_args!("[{}][OPT] {}\n",$lv,format_args!($($tail)*)));
90 }
91 temp
92 }
93 };
94 ($lv:literal $level:ident res,$val:expr,$($tail:tt)*) => {
95 ($val).map_err(|e|{
96 if $crate::log::level() <= $crate::log::Level::$level{
97 $crate::log::log(format_args!("[{}][RES] {} [E]:{}\n",$lv,format_args!($($tail)*),e));
98 }
99 e
100 })
101 };
102 ($lv:literal $level:ident $($tail:tt)*) => {
103 if $crate::log::level() <= $crate::log::Level::$level{
104 $crate::log::log(format_args!("[{}] {}\n",$lv,format_args!($($tail)*)));
105 }
106 }
107}
108#[macro_export]
125macro_rules! trace {
126 ($($tail:tt)*) => {
127 $crate::log_dispatch!("TRACE" Trace $($tail)*);
128 };
129}
130#[macro_export]
132macro_rules! debug {
133 ($($tail:tt)*) => {
134 $crate::log_dispatch!("DEBUG" Debug $($tail)*);
135 };
136}
137#[macro_export]
139macro_rules! info {
140 ($($tail:tt)*) => {
141 $crate::log_dispatch!("INFO" Info $($tail)*);
142 };
143}
144#[macro_export]
146macro_rules! warn {
147 ($($tail:tt)*) => {
148 $crate::log_dispatch!("WARN" Warn $($tail)*);
149 };
150}
151#[macro_export]
153macro_rules! error {
154 ($($tail:tt)*) => {
155 $crate::log_dispatch!("ERROR" Error $($tail)*);
156 };
157}