use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::OnceLock;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Level {
Error = 1,
Warn = 2,
Info = 3,
Debug = 4,
Trace = 5,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Record {
pub level: Level,
pub module: String,
pub message: String,
}
impl Level {
pub fn from_wire(s: &str) -> Option<Level> {
[
Level::Error,
Level::Warn,
Level::Info,
Level::Debug,
Level::Trace,
]
.into_iter()
.find(|l| l.as_str() == s)
}
pub fn as_str(self) -> &'static str {
match self {
Level::Error => "error",
Level::Warn => "warn",
Level::Info => "info",
Level::Debug => "debug",
Level::Trace => "trace",
}
}
pub fn label(self) -> &'static str {
match self {
Level::Error => "ERROR",
Level::Warn => "WARN ",
Level::Info => "INFO ",
Level::Debug => "DEBUG",
Level::Trace => "TRACE",
}
}
}
impl std::fmt::Display for Level {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl Record {
pub fn to_json(&self) -> String {
crate::events::Event::Log(self.clone()).to_json()
}
}
pub type Logger = dyn Fn(&Record) + Send + Sync;
static LEVEL: AtomicU8 = AtomicU8::new(0);
static LOGGER: OnceLock<Box<Logger>> = OnceLock::new();
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetLoggerError;
impl std::fmt::Display for SetLoggerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("a tvpipe logger is already installed")
}
}
impl std::error::Error for SetLoggerError {}
pub fn set_logger(
level: Level,
f: impl Fn(&Record) + Send + Sync + 'static,
) -> std::result::Result<(), SetLoggerError> {
LOGGER.set(Box::new(f)).map_err(|_| SetLoggerError)?;
LEVEL.store(level as u8, Ordering::Relaxed);
Ok(())
}
pub fn set_level(level: Level) {
if LOGGER.get().is_some() {
LEVEL.store(level as u8, Ordering::Relaxed);
}
}
#[inline]
pub fn enabled(level: Level) -> bool {
level as u8 <= LEVEL.load(Ordering::Relaxed)
}
#[cfg(test)]
pub fn to_stderr(level: Level) {
let _ = set_logger(level, |r| {
eprintln!("{} {}: {}", r.level.label(), r.module, r.message)
});
set_level(level);
}
#[doc(hidden)]
pub fn write(level: Level, module: &str, args: std::fmt::Arguments<'_>) {
let Some(logger) = LOGGER.get() else { return };
let module = module.strip_prefix("tvpipe::").unwrap_or(module);
let mut message = args.to_string();
let bad = |c: char| c.is_control() && c != '\n' && c != '\t';
if message.chars().any(bad) {
message = message
.chars()
.map(|c| if bad(c) { '?' } else { c })
.collect();
}
logger(&Record {
level,
module: module.to_owned(),
message,
});
}
#[macro_export]
#[doc(hidden)]
macro_rules! log_at {
($level:expr, $($arg:tt)+) => {
if $crate::log::enabled($level) {
$crate::log::write($level, module_path!(), format_args!($($arg)+));
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! error { ($($arg:tt)+) => { $crate::log_at!($crate::log::Level::Error, $($arg)+) }; }
#[macro_export]
#[doc(hidden)]
macro_rules! warn { ($($arg:tt)+) => { $crate::log_at!($crate::log::Level::Warn, $($arg)+) }; }
#[macro_export]
#[doc(hidden)]
macro_rules! info { ($($arg:tt)+) => { $crate::log_at!($crate::log::Level::Info, $($arg)+) }; }
#[macro_export]
#[doc(hidden)]
macro_rules! debug { ($($arg:tt)+) => { $crate::log_at!($crate::log::Level::Debug, $($arg)+) }; }
#[macro_export]
#[doc(hidden)]
macro_rules! trace { ($($arg:tt)+) => { $crate::log_at!($crate::log::Level::Trace, $($arg)+) }; }