1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
struct Logger {
to: Option<Box<dyn std::io::Write>>,
}
static mut LOGGER: Logger = Logger { to: None };
#[derive(PartialEq, PartialOrd, Copy, Clone)]
pub enum Level {
Trace,
Debug,
Info,
Warn,
Error,
}
static mut LEVEL: Level = Level::Info;
impl Logger {
/// Logs a message at the specified level.
pub fn log(&mut self, args: std::fmt::Arguments<'_>) {
if let Some(ref mut to) = self.to {
let _ = to.write_fmt(args).unwrap();
}
// self.to.write(b"\n").unwrap();
}
}
/// Initializes the logger.
///
/// # Examples
///
/// ```
/// use xlog_rs::log;
/// log::init(std::io::stdout(), log::Level::Trace);
/// ```
pub fn init(to: impl std::io::Write + 'static, level: Level) {
// Box::new(to);
unsafe {
LOGGER = Logger {
to: Some(Box::new(to)),
};
LEVEL = level;
}
}
/// Initializes the logger with a filename.
pub fn with_file(name: &str) {
// Box::new(to);
unsafe {
LOGGER = Logger {
to: Some(Box::new(
std::fs::File::options()
.create(true)
.append(true)
.open(name.to_string())
.unwrap(),
)),
};
}
}
/// Sets the log level.
///
/// # Examples
///
/// ```
/// use xlog_rs::log;
/// log::set_level(log::Level::Trace);
/// ```
#[inline]
pub fn set_level(level: Level) {
unsafe {
LEVEL = level;
}
}
/// Returns the current log level.
#[inline]
pub fn level() -> Level {
unsafe { LEVEL }
}
/// Logs a message at the specified level.
#[inline]
pub fn log(args: std::fmt::Arguments<'_>) {
unsafe {
LOGGER.log(args);
}
}
/// Dispatch message with the type and level
#[macro_export]
macro_rules! log_dispatch {
($lv:literal $level:ident opt,$val:expr,$($tail:tt)*) => {
{
let mut temp = $val;
if $crate::log::level() <= $crate::log::Level::$level && temp.is_none() {
$crate::log::log(format_args!("[{}][OPT] {}\n",$lv,format_args!($($tail)*)));
}
temp
}
};
($lv:literal $level:ident res,$val:expr,$($tail:tt)*) => {
($val).map_err(|e|{
if $crate::log::level() <= $crate::log::Level::$level{
$crate::log::log(format_args!("[{}][RES] {} [E]:{}\n",$lv,format_args!($($tail)*),e));
}
e
})
};
($lv:literal $level:ident $($tail:tt)*) => {
if $crate::log::level() <= $crate::log::Level::$level{
$crate::log::log(format_args!("[{}] {}\n",$lv,format_args!($($tail)*)));
}
}
}
/// Logs a message at the trace level.
///
/// #Example
///
/// ```
/// use xlog_rs::log;
/// xlog_rs::trace!("{}", "abc");
/// let mut some = Some(());
/// let _ = xlog_rs::trace!(opt, some, "{}", "none");
/// some = None;
/// let _ = xlog_rs::trace!(opt, some, "{}", "none");
/// let mut ok = Ok(());
/// let _ = xlog_rs::trace!(res, ok, "{}", "error");
/// ok = Err("error");
/// let _ = xlog_rs::trace!(res, ok, "{}", "error");
/// ```
#[macro_export]
macro_rules! trace {
($($tail:tt)*) => {
$crate::log_dispatch!("TRACE" Trace $($tail)*);
};
}
/// Logs a message at the debug level.
#[macro_export]
macro_rules! debug {
($($tail:tt)*) => {
$crate::log_dispatch!("DEBUG" Debug $($tail)*);
};
}
/// Logs a message at the info level.
#[macro_export]
macro_rules! info {
($($tail:tt)*) => {
$crate::log_dispatch!("INFO" Info $($tail)*);
};
}
/// Logs a message at the warn level.
#[macro_export]
macro_rules! warn {
($($tail:tt)*) => {
$crate::log_dispatch!("WARN" Warn $($tail)*);
};
}
/// Logs a message at the error level.
#[macro_export]
macro_rules! error {
($($tail:tt)*) => {
$crate::log_dispatch!("ERROR" Error $($tail)*);
};
}