2022-07-01 11:53:41 +00:00
|
|
|
use std::fmt::Arguments;
|
2022-07-06 08:16:08 +00:00
|
|
|
use std::io::{stderr, stdout, Write};
|
2022-07-01 11:53:41 +00:00
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
use crate::ffi::LogLevel;
|
|
|
|
|
2022-07-06 08:16:08 +00:00
|
|
|
// Ugly hack to avoid using enum
|
|
|
|
#[allow(non_snake_case, non_upper_case_globals)]
|
|
|
|
mod LogFlag {
|
|
|
|
pub const DisableError: u32 = 0x1;
|
|
|
|
pub const DisableWarn: u32 = 0x2;
|
|
|
|
pub const DisableInfo: u32 = 0x4;
|
|
|
|
pub const DisableDebug: u32 = 0x8;
|
|
|
|
pub const ExitOnError: u32 = 0x10;
|
|
|
|
}
|
|
|
|
|
2022-07-01 11:53:41 +00:00
|
|
|
// We don't need to care about thread safety, because all
|
|
|
|
// logger changes will only happen on the main thread.
|
|
|
|
pub static mut LOGGER: Logger = Logger {
|
2022-07-06 08:16:08 +00:00
|
|
|
fmt: |_, _| {},
|
|
|
|
write: |_, _| {},
|
|
|
|
flags: 0,
|
2022-07-01 11:53:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Logger {
|
2022-07-06 08:16:08 +00:00
|
|
|
pub fmt: fn(level: LogLevel, args: Arguments),
|
|
|
|
pub write: fn(level: LogLevel, msg: &[u8]),
|
|
|
|
pub flags: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_on_error(b: bool) {
|
|
|
|
unsafe {
|
|
|
|
if b {
|
|
|
|
LOGGER.flags |= LogFlag::ExitOnError;
|
|
|
|
} else {
|
|
|
|
LOGGER.flags &= !LogFlag::ExitOnError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LogLevel {
|
|
|
|
fn to_disable_flag(&self) -> u32 {
|
|
|
|
match *self {
|
|
|
|
LogLevel::Error => LogFlag::DisableError,
|
|
|
|
LogLevel::Warn => LogFlag::DisableWarn,
|
|
|
|
LogLevel::Info => LogFlag::DisableInfo,
|
|
|
|
LogLevel::Debug => LogFlag::DisableDebug,
|
2022-07-22 10:53:50 +00:00
|
|
|
_ => 0,
|
2022-07-06 08:16:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 08:16:08 +00:00
|
|
|
pub fn set_log_level_state(level: LogLevel, enabled: bool) {
|
|
|
|
let flag = level.to_disable_flag();
|
|
|
|
unsafe {
|
|
|
|
if enabled {
|
|
|
|
LOGGER.flags &= !flag
|
|
|
|
} else {
|
|
|
|
LOGGER.flags |= flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 11:53:41 +00:00
|
|
|
|
|
|
|
pub fn log_with_rs(level: LogLevel, msg: &str) {
|
2022-07-06 08:16:08 +00:00
|
|
|
let logger = unsafe { LOGGER };
|
|
|
|
if (logger.flags & level.to_disable_flag()) != 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(logger.write)(level, msg.as_bytes());
|
|
|
|
if level == LogLevel::Error && (logger.flags & LogFlag::ExitOnError) != 0 {
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 08:16:08 +00:00
|
|
|
pub fn log_impl(level: LogLevel, args: Arguments) {
|
|
|
|
let logger = unsafe { LOGGER };
|
|
|
|
if (logger.flags & level.to_disable_flag()) != 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(logger.fmt)(level, args);
|
|
|
|
if level == LogLevel::Error && (logger.flags & LogFlag::ExitOnError) != 0 {
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cmdline_logging() {
|
2022-07-06 08:16:08 +00:00
|
|
|
fn print(level: LogLevel, args: Arguments) {
|
|
|
|
if level == LogLevel::Info {
|
|
|
|
print!("{}", args);
|
|
|
|
} else {
|
|
|
|
eprint!("{}", args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(level: LogLevel, msg: &[u8]) {
|
|
|
|
if level == LogLevel::Info {
|
|
|
|
stdout().write_all(msg).ok();
|
|
|
|
} else {
|
|
|
|
stderr().write_all(msg).ok();
|
|
|
|
}
|
|
|
|
}
|
2022-07-06 04:13:09 +00:00
|
|
|
|
2022-07-01 11:53:41 +00:00
|
|
|
let logger = Logger {
|
2022-07-06 08:16:08 +00:00
|
|
|
fmt: print,
|
|
|
|
write,
|
|
|
|
flags: LogFlag::ExitOnError,
|
2022-07-01 11:53:41 +00:00
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
LOGGER = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! error {
|
2022-07-06 04:13:09 +00:00
|
|
|
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!($($arg)+)))
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! warn {
|
2022-07-06 04:13:09 +00:00
|
|
|
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Warn, format_args_nl!($($arg)+)))
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! info {
|
2022-07-06 04:13:09 +00:00
|
|
|
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Info, format_args_nl!($($arg)+)))
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! debug {
|
2022-07-06 04:13:09 +00:00
|
|
|
($($arg:tt)+) => ($crate::log_impl($crate::ffi::LogLevel::Debug, format_args_nl!($($arg)+)))
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! debug {
|
2022-07-22 10:53:50 +00:00
|
|
|
($($arg:tt)+) => {};
|
2022-07-01 11:53:41 +00:00
|
|
|
}
|