xlog/
log.rs

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    /// Logs a message at the specified level.
17    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        // self.to.write(b"\n").unwrap();
22    }
23}
24/// Initializes the logger.
25///
26/// # Examples
27///
28/// ```
29/// use xlog::log;
30/// log::init(std::io::stdout(), log::Level::Trace);
31/// ```
32pub fn init(to: impl std::io::Write + 'static, level: Level) {
33    // Box::new(to);
34    unsafe {
35        LOGGER = Logger {
36            to: Some(Box::new(to)),
37        };
38        LEVEL = level;
39    }
40}
41/// Initializes the logger with a filename.
42pub fn with_file(name: &str) {
43    // Box::new(to);
44    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/// Sets the log level.
57///
58/// # Examples
59///
60/// ```
61/// use xlog::log;
62/// log::set_level(log::Level::Trace);
63/// ```
64#[inline]
65pub fn set_level(level: Level) {
66    unsafe {
67        LEVEL = level;
68    }
69}
70/// Returns the current log level.
71#[inline]
72pub fn level() -> Level {
73    unsafe { LEVEL }
74}
75/// Logs a message at the specified level.
76#[inline]
77pub fn log(args: std::fmt::Arguments<'_>) {
78    unsafe {
79        LOGGER.log(args);
80    }
81}
82/// Dispatch message with the type and level
83#[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/// Logs a message at the trace level.
109///
110/// #Example
111///
112/// ```
113/// use xlog::log;
114/// xlog::trace!("{}", "abc");
115/// let mut some = Some(());
116/// let _ = xlog::trace!(opt, some, "{}", "none");
117/// some = None;
118/// let _ = xlog::trace!(opt, some, "{}", "none");
119/// let mut ok = Ok(());
120/// let _ = xlog::trace!(res, ok, "{}", "error");
121/// ok = Err("error");
122/// let _ = xlog::trace!(res, ok, "{}", "error");
123/// ```
124#[macro_export]
125macro_rules! trace {
126    ($($tail:tt)*) => {
127        $crate::log_dispatch!("TRACE" Trace $($tail)*);
128    };
129}
130/// Logs a message at the debug level.
131#[macro_export]
132macro_rules! debug {
133    ($($tail:tt)*) => {
134        $crate::log_dispatch!("DEBUG" Debug $($tail)*);
135    };
136}
137/// Logs a message at the info level.
138#[macro_export]
139macro_rules! info {
140    ($($tail:tt)*) => {
141        $crate::log_dispatch!("INFO" Info $($tail)*);
142    };
143}
144/// Logs a message at the warn level.
145#[macro_export]
146macro_rules! warn {
147    ($($tail:tt)*) => {
148        $crate::log_dispatch!("WARN" Warn $($tail)*);
149    };
150}
151/// Logs a message at the error level.
152#[macro_export]
153macro_rules! error {
154    ($($tail:tt)*) => {
155        $crate::log_dispatch!("ERROR" Error $($tail)*);
156    };
157}