From 08eca1a2d01bc7e48dabc507f15dd5b1ae0fc17f Mon Sep 17 00:00:00 2001 From: MCorange Date: Mon, 17 Jun 2024 14:10:59 +0300 Subject: [PATCH] Added logger --- Cargo.toml | 4 ++++ src/display.rs | 2 +- src/main.rs | 19 ++++++++++++++++++- src/plugman/mod.rs | 9 ++++++--- src/plugman/plugin.rs | 11 ++++++----- 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8784698..c6553bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,9 @@ chrono = "0.4.38" clap = { version = "4.5.7", features = ["derive"] } dlopen = "0.1.8" dlopen_derive = "0.1.4" +env_logger = "0.11.3" libc = "0.2.155" +log = "0.4.21" +serde = { version = "1.0.203", features = ["derive"] } +toml = "0.8.14" x11 = { version = "2.21.0", features = ["xlib"] } diff --git a/src/display.rs b/src/display.rs index 4ac7073..7783053 100644 --- a/src/display.rs +++ b/src/display.rs @@ -16,7 +16,7 @@ impl Display { let display = xlib::XOpenDisplay(std::ptr::null()); if display.is_null() { - eprintln!("ERROR: Could not open x11 display"); + log::error!("Could not open x11 display"); return Err(()); } diff --git a/src/main.rs b/src/main.rs index 3501cb2..f2f7643 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,22 @@ struct CliArgs { fn main() -> ExitCode { // let ca = CliArgs::parse(); + if ca.debug { + env_logger::builder() + .filter(None, log::LevelFilter::Debug) + .init(); + } else { + env_logger::builder() + .filter(None, log::LevelFilter::Info) + .init(); + let config; + match config::Config::new(ca.config_dir.as_std_path()) { + Ok(v) => config = v, + Err(e) => { + log::error!("Failed to parse config: {e:?}"); + return ExitCode::from(2); + } + }; let Ok(mut disp) = display::Display::new() else { return ExitCode::from(1); }; @@ -23,7 +39,7 @@ fn main() -> ExitCode { if let Err(e) = pm.load(PathBuf::from("./plugins") //ca.plugin_dir.as_std_path().to_path_buf() ){ - eprintln!("ERROR: Failed to load plugins: {e:?}"); + return ExitCode::from(3); } pm.init_plugins(); @@ -35,6 +51,7 @@ fn main() -> ExitCode { let _ = disp.write_display_name(&vals.join(" | ")); + log::warn!("Failed to write too bar: {e}"); std::thread::sleep(Duration::from_millis(250)); } } diff --git a/src/plugman/mod.rs b/src/plugman/mod.rs index 9f14b3b..fbdc0c4 100644 --- a/src/plugman/mod.rs +++ b/src/plugman/mod.rs @@ -20,13 +20,15 @@ impl PlugMan { } pub fn load(&mut self, dir: PathBuf) -> Result<(), PluginError>{ + log::debug!("Loading plugins from {:?}", dir); + let files = std::fs::read_dir(dir)?; for file in files { let entry = file?; if entry.file_type()?.is_file() { let plugin = Plugin::load(entry.path().to_path_buf())?; - println!("INFO: Loaded plugin {} {} licensed with {}", + log::info!("Loaded plugin '{}' ({}) licensed with {}", plugin.name().clone(), plugin.version().clone().unwrap_or("None".to_string()), plugin.license().clone().unwrap_or("None".to_string()) @@ -37,7 +39,8 @@ impl PlugMan { self.load(entry.path().to_path_buf())?; } } - println!("INFO: Loaded {} plugins", self.plugins.len()); + log::error!("No plugins found"); + log::info!("Loaded {} plugins", self.plugins.len()); Ok(()) } @@ -52,7 +55,7 @@ impl PlugMan { pub fn reload_plugins(&mut self) { for plugin in &mut self.plugins { if let Err(e) = plugin.reload() { - eprintln!("ERROR: Failed to reload plugin {:?}: {e}", plugin.name()); + log::error!("Failed to reload plugin {:?}: {e}", plugin.name()); } } } diff --git a/src/plugman/plugin.rs b/src/plugman/plugin.rs index 208c792..ec70d59 100644 --- a/src/plugman/plugin.rs +++ b/src/plugman/plugin.rs @@ -1,4 +1,4 @@ -use std::{alloc::Layout, ffi::{c_char, c_uint, CStr, CString}, path::PathBuf}; +use std::{alloc::Layout, ffi::{c_char, CStr}, path::PathBuf}; use dlopen::raw::Library; @@ -38,7 +38,7 @@ impl Plugin { } fn reload_symbols(&mut self) -> Result<(), dlopen::Error> { - println!("INFO: Loading {:?}", self.path); + log::debug!("Loading {:?}", self.path); let lib = Library::open(&self.path)?; let symbols = PluginSyms { init: unsafe { lib.symbol("plug_init")? }, @@ -51,14 +51,13 @@ impl Plugin { }; unsafe { if (symbols.get_info)().is_null() { - eprintln!("ERROR: Info fields for plugin {:?} are null", self.path); + log::error!("Info fields for plugin {:?} are null", self.path); self.disable(); } } self.syms = Some(symbols); self.lib = Some(lib); - println!("INFO: Loading OK"); Ok(()) } pub fn syms(&self) -> PluginSyms { @@ -97,7 +96,9 @@ impl Plugin { String::from_raw_parts(buf, len, cap) }; - println!("Polled: {}", s); + if !s.is_empty() { + log::debug!("Polled: {}", s); + } Ok(s) }