From 77623327000511e20d75a9dd70294d9fc4d0928f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Cerveau?= Date: Wed, 16 Feb 2022 17:07:57 +0100 Subject: [PATCH] preferences: add log level Can now set the log level in the application --- src/logger.rs | 22 ++++++++++++++++++ src/ui/preferences.rs | 54 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/logger.rs b/src/logger.rs index 129ef75..106d4bc 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -17,12 +17,28 @@ use std::fs::File; #[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] pub enum LogLevel { + Off, Error, Warning, Info, Debug, Trace, } + +impl LogLevel { + pub fn from_u32(value: u32) -> LogLevel { + match value { + 0 => LogLevel::Off, + 1 => LogLevel::Error, + 2 => LogLevel::Warning, + 3 => LogLevel::Info, + 4 => LogLevel::Debug, + 5 => LogLevel::Trace, + _ => panic!("Unknown value: {}", value), + } + } +} + impl fmt::Display for LogLevel { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) @@ -95,6 +111,7 @@ impl io::Write for WriteAdapter { fn translate_to_simple_logger(log_level: LogLevel) -> LevelFilter { match log_level { + LogLevel::Off => LevelFilter::Off, LogLevel::Error => LevelFilter::Error, LogLevel::Warning => LevelFilter::Warn, LogLevel::Info => LevelFilter::Info, @@ -128,6 +145,10 @@ pub fn init_logger(sender: Sender, log_file: &str) { .unwrap(); } +pub fn set_log_level(level: LogLevel) { + log::set_max_level(translate_to_simple_logger(level)); +} + pub fn print_log(log_level: LogLevel, msg: String) { match log_level { LogLevel::Error => { @@ -145,5 +166,6 @@ pub fn print_log(log_level: LogLevel, msg: String) { LogLevel::Trace => { trace!("{}", msg); } + _ => {} }; } diff --git a/src/ui/preferences.rs b/src/ui/preferences.rs index 9d08dde..a91f6a4 100644 --- a/src/ui/preferences.rs +++ b/src/ui/preferences.rs @@ -8,25 +8,31 @@ use crate::app::GPSApp; +use crate::logger; use crate::settings; use crate::ui as GPSUI; use gtk::glib; use gtk::prelude::*; +fn add_settings_widget(grid: >k::Grid, label_name: &str, widget: >k::Widget, row: i32) { + let label = gtk::Label::builder() + .label(label_name) + .hexpand(true) + .halign(gtk::Align::Start) + .margin_start(4) + .build(); + + grid.attach(&label, 0, row, 1, 1); + grid.attach(widget, 1, row, 1, 1); +} + pub fn display_settings(app: &GPSApp) { let grid = gtk::Grid::new(); grid.set_column_spacing(4); grid.set_row_spacing(4); grid.set_margin_bottom(12); - - let label = gtk::Label::builder() - .label("Use gtk4paintablesink element for video rendering:") - .hexpand(true) - .halign(gtk::Align::Start) - .margin_start(4) - .build(); - let widget = gtk::CheckButton::new(); let settings = settings::Settings::load_settings(); + let widget = gtk::CheckButton::new(); widget.set_active( settings .preferences @@ -41,8 +47,36 @@ pub fn display_settings(app: &GPSApp) { settings::Settings::save_settings(&settings); })); - grid.attach(&label, 0, 0, 1, 1); - grid.attach(&widget, 1, 0, 1, 1); + let widget = widget + .dynamic_cast::() + .expect("Should be a widget"); + add_settings_widget( + &grid, + "Use gtk4paintablesink element for video rendering:", + &widget, + 0, + ); + + let widget = gtk::SpinButton::with_range(0.0, 5.0, 1.0); + widget.set_value( + settings + .preferences + .get("log_level") + .unwrap_or(&"0.0".to_string()) + .parse::() + .expect("Should a f64 value"), + ); + widget.connect_value_changed(glib::clone!(@weak widget => move |c| { + let mut settings = settings::Settings::load_settings(); + settings.preferences.insert("log_level".to_string(), c.value().to_string()); + logger::set_log_level(logger::LogLevel::from_u32(c.value() as u32)); + settings::Settings::save_settings(&settings); + })); + + let widget = widget + .dynamic_cast::() + .expect("Should be a widget"); + add_settings_widget(&grid, "Log level", &widget, 1); let dialog = GPSUI::dialog::create_dialog("Preferences", app, &grid, move |_app, dialog| { dialog.close();