From bb28bb0c5e367a8c53b669928e0d85c02f8d5f48 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Mon, 5 Jun 2023 13:07:43 +0200 Subject: [PATCH] integrate propper logging system --- Cargo.toml | 2 ++ src/main.rs | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a9ba871..ccbd534 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,9 @@ edition = "2021" [dependencies] config = "0.13.3" ctrlc = "3.4.0" +env_logger = "0.10.0" helios-dac = { version = "0.1", default-features = false, features = ["native"] } +log = "0.4.18" redis = "0.23.0" ron = "0.8.0" serde = { version = "1.0.163", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 3b76adb..487ad7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,15 +22,17 @@ use conf::Conf; use errors::{LJError,LJResult}; use point::Point; use transformer::{Transformers,Translate,Replicate}; +use log::{LevelFilter,info,/* warn, */ error}; +use env_logger::Builder; const DEFAULT_CONF_FILE : &str = "settings.toml"; const CENTER : (f32,f32) = (2000.0, 2000.0); -pub fn main() { +pub fn main() { match run_all() { Ok(()) => {}, Err(err) => { - println!("Error: {}", err); + error!("Error: {}", err); } } } @@ -40,7 +42,11 @@ fn run_all() -> LJResult<()> { DEFAULT_CONF_FILE.to_string() }); - let config = Conf::new(&filename)?; + let config = Conf::new(&filename); + init_logging(&config); + let config = config?; + info!("Starting up"); + let mut rs = RedisCtrl::new(&config.redis_url)?; let running = Arc::new(AtomicBool::new(true)); @@ -59,7 +65,7 @@ fn run_all() -> LJResult<()> { while running.load(Ordering::SeqCst) { let order = rs.get_order(config.laser_id)?; if order != Order::Draw { - println!("{:?}", order); + info!("Order: {:?}", order); } let frame = get_next_frame(&config, 1000, &transformers, @@ -70,11 +76,26 @@ fn run_all() -> LJResult<()> { device.write_frame(frame)?; } - println!("Exiting, stoping device."); + info!("Exiting, stoping device."); device.stop()?; Ok(()) } +fn init_logging(config: &LJResult) { + if let Ok(ref config) = config { + if config.debug { + let mut builder = Builder::from_default_env(); + builder + .filter(None, LevelFilter::Info) + .init(); + } else { + env_logger::init(); + } + } else { + env_logger::init(); + } +} + fn get_helios_device() -> LJResult { let controller = NativeHeliosDacController::new()?; let devices = controller.list_devices()?; @@ -100,7 +121,7 @@ fn get_next_frame( line = transformer.apply(&line); } - println!("{:?}", line); + info!("Line: {:?}", line); let line2 : Vec = line.into_iter().map(| p | p.into()).collect(); Ok(Frame::new(speed, line2))