feat: route gtk logging through tracing

Yay consistent logging, especially now the tray uses a library that likes to vomit warnings
This commit is contained in:
Jake Stanger 2024-11-18 21:25:57 +00:00
parent 5afe5c19b9
commit e1f3b1bb72
No known key found for this signature in database
GPG Key ID: C51FC8F9CB0BEA61

View File

@ -1,8 +1,9 @@
use color_eyre::Result;
use dirs::data_dir;
use glib::{LogLevel, LogWriterOutput};
use std::{env, panic};
use strip_ansi_escapes::Writer;
use tracing::error;
use tracing::{debug, error, info, warn};
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
use tracing_appender::rolling::Rotation;
use tracing_error::ErrorLayer;
@ -89,5 +90,33 @@ fn install_tracing() -> Result<WorkerGuard> {
)
.init();
glib::log_set_writer_func(|level, fields| {
const KEY_DOMAIN: &str = "GLIB_DOMAIN";
const KEY_MESSAGE: &str = "MESSAGE";
let domain = fields
.iter()
.find(|f| f.key() == KEY_DOMAIN)
.and_then(|f| f.value_str())
.unwrap_or("Glib Unknown");
let message = fields
.iter()
.find(|f| f.key() == KEY_MESSAGE)
.and_then(|f| f.value_str())
.unwrap_or_default();
match level {
LogLevel::Error => error!(target: "GTK", "[{domain}] {message}"),
LogLevel::Critical => error!(target: "GTK", "[{domain}] CRITICAL: {message}"),
LogLevel::Warning => warn!(target: "GTK", "[{domain}] {message}"),
LogLevel::Message => info!(target: "GTK", "[{domain}] MESSAGE: {message}"),
LogLevel::Info => info!(target: "GTK", "[{domain}] MESSAGE: {message}"),
LogLevel::Debug => debug!(target: "GTK", "[{domain}] {message}"),
}
LogWriterOutput::Handled
});
Ok(guard)
}