Files
Magisk/native/src/base/logging.rs

150 lines
3.7 KiB
Rust
Raw Normal View History

2025-09-10 10:26:41 -07:00
use crate::ffi::LogLevelCxx;
use crate::{Utf8CStr, cstr};
use bitflags::bitflags;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use std::fmt;
2025-04-08 09:56:57 -07:00
use std::io::{Write, stderr, stdout};
2022-07-01 04:53:41 -07:00
use std::process::exit;
2025-09-10 10:26:41 -07:00
bitflags! {
#[derive(Copy, Clone)]
struct LogFlag : u32 {
const DISABLE_ERROR = 1 << 0;
const DISABLE_WARN = 1 << 1;
const DISABLE_INFO = 1 << 2;
const DISABLE_DEBUG = 1 << 3;
const EXIT_ON_ERROR = 1 << 4;
}
2022-07-06 01:16:08 -07:00
}
2023-10-13 16:59:54 -07:00
#[derive(Copy, Clone, FromPrimitive, ToPrimitive)]
#[repr(i32)]
pub enum LogLevel {
Error = LogLevelCxx::Error.repr,
Warn = LogLevelCxx::Warn.repr,
Info = LogLevelCxx::Info.repr,
Debug = LogLevelCxx::Debug.repr,
}
2022-07-01 04:53:41 -07: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 01:16:08 -07:00
write: |_, _| {},
2025-09-10 10:26:41 -07:00
flags: LogFlag::empty(),
2022-07-01 04:53:41 -07:00
};
2023-09-12 17:35:20 -07:00
type LogWriter = fn(level: LogLevel, msg: &Utf8CStr);
2024-03-05 01:48:35 -08:00
pub(crate) type Formatter<'a> = &'a mut dyn fmt::Write;
2022-07-01 04:53:41 -07:00
#[derive(Copy, Clone)]
pub struct Logger {
pub write: LogWriter,
2025-09-10 10:26:41 -07:00
flags: LogFlag,
2022-07-06 01:16:08 -07:00
}
2025-09-10 10:26:41 -07:00
pub fn update_logger(f: impl FnOnce(&mut Logger)) {
let mut logger = unsafe { LOGGER };
f(&mut logger);
2022-07-06 01:16:08 -07:00
unsafe {
2025-09-10 10:26:41 -07:00
LOGGER = logger;
2022-07-06 01:16:08 -07:00
}
}
2025-09-10 10:26:41 -07:00
pub fn exit_on_error(b: bool) {
update_logger(|logger| logger.flags.set(LogFlag::EXIT_ON_ERROR, b));
}
2022-07-06 01:16:08 -07:00
impl LogLevel {
2025-09-10 10:26:41 -07:00
fn as_disable_flag(&self) -> LogFlag {
2022-07-06 01:16:08 -07:00
match *self {
2025-09-10 10:26:41 -07:00
LogLevel::Error => LogFlag::DISABLE_ERROR,
LogLevel::Warn => LogFlag::DISABLE_WARN,
LogLevel::Info => LogFlag::DISABLE_INFO,
LogLevel::Debug => LogFlag::DISABLE_DEBUG,
2022-07-06 01:16:08 -07:00
}
}
2022-07-01 04:53:41 -07:00
}
2022-07-06 01:16:08 -07:00
pub fn set_log_level_state(level: LogLevel, enabled: bool) {
2025-09-10 10:26:41 -07:00
update_logger(|logger| logger.flags.set(level.as_disable_flag(), enabled));
2022-07-06 01:16:08 -07:00
}
2022-07-01 04:53:41 -07:00
fn log_with_writer<F: FnOnce(LogWriter)>(level: LogLevel, f: F) {
2022-07-06 01:16:08 -07:00
let logger = unsafe { LOGGER };
2025-09-10 10:26:41 -07:00
if logger.flags.contains(level.as_disable_flag()) {
2022-07-06 01:16:08 -07:00
return;
}
f(logger.write);
2025-09-10 10:26:41 -07:00
if logger.flags.contains(LogFlag::EXIT_ON_ERROR) {
2025-03-04 08:21:55 +08:00
exit(-1);
2022-07-06 01:16:08 -07:00
}
2022-07-01 04:53:41 -07:00
}
2023-12-26 23:08:06 +08:00
pub fn log_from_cxx(level: LogLevelCxx, msg: &Utf8CStr) {
2023-10-13 16:59:54 -07:00
if let Some(level) = LogLevel::from_i32(level.repr) {
log_with_writer(level, |write| write(level, msg));
}
}
pub fn log_with_formatter<F: FnOnce(Formatter) -> fmt::Result>(level: LogLevel, f: F) {
log_with_writer(level, |write| {
2025-04-22 02:55:53 -07:00
let mut buf = cstr::buf::default();
f(&mut buf).ok();
2023-09-12 17:35:20 -07:00
write(level, &buf);
});
}
2022-07-01 04:53:41 -07:00
pub fn cmdline_logging() {
2023-09-12 17:35:20 -07:00
fn cmdline_write(level: LogLevel, msg: &Utf8CStr) {
2023-10-13 16:59:54 -07:00
if matches!(level, LogLevel::Info) {
2023-09-12 17:35:20 -07:00
stdout().write_all(msg.as_bytes()).ok();
2022-07-06 01:16:08 -07:00
} else {
2023-09-12 17:35:20 -07:00
stderr().write_all(msg.as_bytes()).ok();
2022-07-06 01:16:08 -07:00
}
}
2025-09-10 10:26:41 -07:00
update_logger(|logger| logger.write = cmdline_write);
2022-07-01 04:53:41 -07:00
}
2025-05-30 11:11:36 -07:00
#[macro_export]
macro_rules! log_with_args {
($level:expr, $($args:tt)+) => {
$crate::log_with_formatter($level, |w| writeln!(w, $($args)+))
}
}
2022-07-01 04:53:41 -07:00
#[macro_export]
macro_rules! error {
($($args:tt)+) => {
2025-05-30 11:11:36 -07:00
$crate::log_with_formatter($crate::LogLevel::Error, |w| writeln!(w, $($args)+))
}
2022-07-01 04:53:41 -07:00
}
#[macro_export]
macro_rules! warn {
($($args:tt)+) => {
2025-05-30 11:11:36 -07:00
$crate::log_with_formatter($crate::LogLevel::Warn, |w| writeln!(w, $($args)+))
}
2022-07-01 04:53:41 -07:00
}
#[macro_export]
macro_rules! info {
($($args:tt)+) => {
2025-05-30 11:11:36 -07:00
$crate::log_with_formatter($crate::LogLevel::Info, |w| writeln!(w, $($args)+))
}
2022-07-01 04:53:41 -07:00
}
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug {
($($args:tt)+) => {
2025-05-30 11:11:36 -07:00
$crate::log_with_formatter($crate::LogLevel::Debug, |w| writeln!(w, $($args)+))
}
2022-07-01 04:53:41 -07:00
}
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug {
2022-08-08 22:53:37 -07:00
($($args:tt)+) => {};
2022-07-01 04:53:41 -07:00
}