use crate::log as glib_log;
use crate::translate::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlibLoggerFormat {
Plain,
LineAndFile,
Structured,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlibLoggerDomain {
None,
CrateTarget,
CratePath,
}
#[derive(Debug)]
pub struct GlibLogger {
format: GlibLoggerFormat,
domain: GlibLoggerDomain,
}
impl GlibLogger {
pub const fn new(format: GlibLoggerFormat, domain: GlibLoggerDomain) -> Self {
Self { format, domain }
}
fn level_to_glib(level: rs_log::Level) -> crate::LogLevel {
match level {
rs_log::Level::Error => crate::LogLevel::Critical,
rs_log::Level::Warn => crate::LogLevel::Warning,
rs_log::Level::Info => crate::LogLevel::Info,
rs_log::Level::Debug => crate::LogLevel::Debug,
rs_log::Level::Trace => crate::LogLevel::Debug,
}
}
#[doc(alias = "g_log")]
fn write_log(domain: Option<&str>, level: rs_log::Level, message: &std::fmt::Arguments<'_>) {
unsafe {
use std::fmt::Write;
let mut message_builder = crate::GStringBuilder::default();
if write!(&mut message_builder, "{}", message).is_err() {
return;
}
let message = message_builder.into_string();
crate::ffi::g_log(
domain.to_glib_none().0,
GlibLogger::level_to_glib(level).into_glib(),
b"%s\0".as_ptr() as *const _,
ToGlibPtr::<*const std::os::raw::c_char>::to_glib_none(&message).0,
);
}
}
fn write_log_structured(
domain: Option<&str>,
level: rs_log::Level,
file: Option<&str>,
line: Option<u32>,
func: Option<&str>,
message: &str,
) {
let line = line.map(|l| l.to_string());
let line = line.as_ref().map(|s| s.as_str());
crate::log_structured!(
domain.unwrap_or("default"),
GlibLogger::level_to_glib(level),
{
"CODE_FILE" => file.unwrap_or("<unknown file>");
"CODE_LINE" => line.unwrap_or("<unknown line>");
"CODE_FUNC" => func.unwrap_or("<unknown module path>");
"MESSAGE" => message;
}
);
}
}
impl rs_log::Log for GlibLogger {
fn enabled(&self, _: &rs_log::Metadata) -> bool {
true
}
fn log(&self, record: &rs_log::Record) {
if !self.enabled(record.metadata()) {
return;
}
let domain = match &self.domain {
GlibLoggerDomain::None => None,
GlibLoggerDomain::CrateTarget => Some(record.metadata().target()),
GlibLoggerDomain::CratePath => record.module_path(),
};
match self.format {
GlibLoggerFormat::Plain => {
GlibLogger::write_log(domain, record.level(), record.args());
}
GlibLoggerFormat::LineAndFile => {
match (record.file(), record.line()) {
(Some(file), Some(line)) => {
GlibLogger::write_log(
domain,
record.level(),
&format_args!("{}:{}: {}", file, line, record.args()),
);
}
(Some(file), None) => {
GlibLogger::write_log(
domain,
record.level(),
&format_args!("{}: {}", file, record.args()),
);
}
_ => {
GlibLogger::write_log(domain, record.level(), record.args());
}
};
}
GlibLoggerFormat::Structured => {
let args = record.args();
if let Some(s) = args.as_str() {
GlibLogger::write_log_structured(
domain,
record.level(),
record.file(),
record.line(),
record.module_path(),
s,
);
} else {
GlibLogger::write_log_structured(
domain,
record.level(),
record.file(),
record.line(),
record.module_path(),
&args.to_string(),
);
}
}
};
}
fn flush(&self) {}
}
pub fn rust_log_handler(domain: Option<&str>, level: glib_log::LogLevel, message: &str) {
let level = match level {
glib_log::LogLevel::Error | glib_log::LogLevel::Critical => rs_log::Level::Error,
glib_log::LogLevel::Warning => rs_log::Level::Warn,
glib_log::LogLevel::Message | glib_log::LogLevel::Info => rs_log::Level::Info,
glib_log::LogLevel::Debug => rs_log::Level::Debug,
};
rs_log::log!(target: domain.unwrap_or("<null>"), level, "{}", message);
}
#[macro_export]
#[cfg(any(feature = "dox", feature = "log_macros"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "log_macros")))]
macro_rules! error {
(target: $target:expr, $($arg:tt)+) => (
$crate::rs_log::log!(target: $target, $crate::rs_log::Level::Error, $($arg)+);
);
($($arg:tt)+) => (
$crate::rs_log::log!(target: G_LOG_DOMAIN, $crate::rs_log::Level::Error, $($arg)+);
)
}
#[macro_export]
#[cfg(any(feature = "dox", feature = "log_macros"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "log_macros")))]
macro_rules! warn {
(target: $target:expr, $($arg:tt)+) => (
$crate::rs_log::log!(target: $target, $crate::rs_log::Level::Warn, $($arg)+);
);
($($arg:tt)+) => (
$crate::rs_log::log!(target: G_LOG_DOMAIN, $crate::rs_log::Level::Warn, $($arg)+);
)
}
#[macro_export]
#[cfg(any(feature = "dox", feature = "log_macros"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "log_macros")))]
macro_rules! info {
(target: $target:expr, $($arg:tt)+) => (
$crate::rs_log::log!(target: $target, $crate::rs_log::Level::Info, $($arg)+);
);
($($arg:tt)+) => (
$crate::rs_log::log!(target: G_LOG_DOMAIN, $crate::rs_log::Level::Info, $($arg)+);
)
}
#[macro_export]
#[cfg(any(feature = "dox", feature = "log_macros"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "log_macros")))]
macro_rules! debug {
(target: $target:expr, $($arg:tt)+) => (
$crate::rs_log::log!(target: $target, $crate::rs_log::Level::Debug, $($arg)+);
);
($($arg:tt)+) => (
$crate::rs_log::log!(target: G_LOG_DOMAIN, $crate::rs_log::Level::Debug, $($arg)+);
)
}
#[macro_export]
#[cfg(any(feature = "dox", feature = "log_macros"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "log_macros")))]
macro_rules! trace {
(target: $target:expr, $($arg:tt)+) => (
$crate::rs_log::log!(target: $target, $crate::rs_log::Level::Trace, $($arg)+);
);
($($arg:tt)+) => (
$crate::rs_log::log!(target: G_LOG_DOMAIN, $crate::rs_log::Level::Trace, $($arg)+);
)
}