Added logger

This commit is contained in:
Gvidas Juknevičius 2024-06-17 14:10:59 +03:00
parent 208eed348b
commit 08eca1a2d0
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
5 changed files with 35 additions and 10 deletions

View File

@ -12,5 +12,9 @@ chrono = "0.4.38"
clap = { version = "4.5.7", features = ["derive"] } clap = { version = "4.5.7", features = ["derive"] }
dlopen = "0.1.8" dlopen = "0.1.8"
dlopen_derive = "0.1.4" dlopen_derive = "0.1.4"
env_logger = "0.11.3"
libc = "0.2.155" 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"] } x11 = { version = "2.21.0", features = ["xlib"] }

View File

@ -16,7 +16,7 @@ impl Display {
let display = xlib::XOpenDisplay(std::ptr::null()); let display = xlib::XOpenDisplay(std::ptr::null());
if display.is_null() { if display.is_null() {
eprintln!("ERROR: Could not open x11 display"); log::error!("Could not open x11 display");
return Err(()); return Err(());
} }

View File

@ -14,6 +14,22 @@ struct CliArgs {
fn main() -> ExitCode { fn main() -> ExitCode {
// let ca = CliArgs::parse(); // 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 { let Ok(mut disp) = display::Display::new() else {
return ExitCode::from(1); return ExitCode::from(1);
}; };
@ -23,7 +39,7 @@ fn main() -> ExitCode {
if let Err(e) = pm.load(PathBuf::from("./plugins") if let Err(e) = pm.load(PathBuf::from("./plugins")
//ca.plugin_dir.as_std_path().to_path_buf() //ca.plugin_dir.as_std_path().to_path_buf()
){ ){
eprintln!("ERROR: Failed to load plugins: {e:?}"); return ExitCode::from(3);
} }
pm.init_plugins(); pm.init_plugins();
@ -35,6 +51,7 @@ fn main() -> ExitCode {
let _ = disp.write_display_name(&vals.join(" | ")); let _ = disp.write_display_name(&vals.join(" | "));
log::warn!("Failed to write too bar: {e}");
std::thread::sleep(Duration::from_millis(250)); std::thread::sleep(Duration::from_millis(250));
} }
} }

View File

@ -20,13 +20,15 @@ impl PlugMan {
} }
pub fn load(&mut self, dir: PathBuf) -> Result<(), PluginError>{ pub fn load(&mut self, dir: PathBuf) -> Result<(), PluginError>{
log::debug!("Loading plugins from {:?}", dir);
let files = std::fs::read_dir(dir)?; let files = std::fs::read_dir(dir)?;
for file in files { for file in files {
let entry = file?; let entry = file?;
if entry.file_type()?.is_file() { if entry.file_type()?.is_file() {
let plugin = Plugin::load(entry.path().to_path_buf())?; 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.name().clone(),
plugin.version().clone().unwrap_or("None".to_string()), plugin.version().clone().unwrap_or("None".to_string()),
plugin.license().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())?; 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(()) Ok(())
} }
@ -52,7 +55,7 @@ impl PlugMan {
pub fn reload_plugins(&mut self) { pub fn reload_plugins(&mut self) {
for plugin in &mut self.plugins { for plugin in &mut self.plugins {
if let Err(e) = plugin.reload() { if let Err(e) = plugin.reload() {
eprintln!("ERROR: Failed to reload plugin {:?}: {e}", plugin.name()); log::error!("Failed to reload plugin {:?}: {e}", plugin.name());
} }
} }
} }

View File

@ -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; use dlopen::raw::Library;
@ -38,7 +38,7 @@ impl Plugin {
} }
fn reload_symbols(&mut self) -> Result<(), dlopen::Error> { 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 lib = Library::open(&self.path)?;
let symbols = PluginSyms { let symbols = PluginSyms {
init: unsafe { lib.symbol("plug_init")? }, init: unsafe { lib.symbol("plug_init")? },
@ -51,14 +51,13 @@ impl Plugin {
}; };
unsafe { unsafe {
if (symbols.get_info)().is_null() { 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.disable();
} }
} }
self.syms = Some(symbols); self.syms = Some(symbols);
self.lib = Some(lib); self.lib = Some(lib);
println!("INFO: Loading OK");
Ok(()) Ok(())
} }
pub fn syms(&self) -> PluginSyms { pub fn syms(&self) -> PluginSyms {
@ -97,7 +96,9 @@ impl Plugin {
String::from_raw_parts(buf, len, cap) String::from_raw_parts(buf, len, cap)
}; };
println!("Polled: {}", s); if !s.is_empty() {
log::debug!("Polled: {}", s);
}
Ok(s) Ok(s)
} }