integrate propper logging system

This commit is contained in:
Marc Planard 2023-06-05 13:07:43 +02:00
parent f20838e022
commit bb28bb0c5e
2 changed files with 29 additions and 6 deletions

View File

@ -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"] }

View File

@ -22,6 +22,8 @@ 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);
@ -30,7 +32,7 @@ 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<Conf>) {
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<NativeHeliosDac> {
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<helios_dac::Point> = line.into_iter().map(| p | p.into()).collect();
Ok(Frame::new(speed, line2))