From e1f3b1bb72f3aa6562f7c7d98a0ef8d131e86600 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Mon, 18 Nov 2024 21:25:57 +0000 Subject: [PATCH] feat: route gtk logging through tracing Yay consistent logging, especially now the tray uses a library that likes to vomit warnings --- src/logging.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/logging.rs b/src/logging.rs index bfa73c09..6ba97c7f 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -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 { ) .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) }