Skip to content

Commit

Permalink
feat: route gtk logging through tracing
Browse files Browse the repository at this point in the history
Yay consistent logging, especially now the tray uses a library that likes to vomit warnings
  • Loading branch information
JakeStanger committed Nov 18, 2024
1 parent 5afe5c1 commit e1f3b1b
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
}

0 comments on commit e1f3b1b

Please sign in to comment.