From 5c1b50ea51f32f1f7832229b2c74708fabd7ade7 Mon Sep 17 00:00:00 2001 From: Wiktor Mazur Date: Sat, 7 Mar 2020 13:49:51 +0100 Subject: [PATCH] + dynamic debug logging --- README.md | 13 ++++++++++++- backend/src/api.rs | 36 +++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index eaf2402..d2e5416 100644 --- a/README.md +++ b/README.md @@ -84,11 +84,22 @@ After first execution the app is going to create default configuration file name [sudo on linux] python application/albion-online-stats.py ``` +## Debugging + +Setting `BACKEND_LOG_LEVEL` to `DEBUG` value enables more advanced debug information + +E.g + +```shell +BACKEND_LOG_LEVEL=DEBUG python application/albion-online-stats.py +``` + + ## GUI testing - Seting TESTING variable runs application without backend dependency ```shell - TESTING=true python application/albion-online-stats.py + TESTING=simple python application/albion-online-stats.py ``` # License diff --git a/backend/src/api.rs b/backend/src/api.rs index daf8897..2310ee8 100644 --- a/backend/src/api.rs +++ b/backend/src/api.rs @@ -1,8 +1,9 @@ +use log::*; +use simplelog::*; +use std::env; use std::fs::File; use std::sync::mpsc::{channel, Receiver, Sender}; use std::thread; -use log::*; -use simplelog::*; use packet_sniffer::UdpPacket; use photon_decode::Photon; @@ -12,17 +13,14 @@ use crate::publisher::Publisher; pub use crate::publisher::Subscribers; - use crate::translate::udp_packet_to_game_events; - pub enum InitializationError { NetworkInterfaceListMissing, } pub fn initialize(subscribers: Subscribers) -> Result<(), InitializationError> { initialize_logging(); - if let Ok(interfaces) = packet_sniffer::network_interfaces() { thread::spawn(move || { let (tx, rx): (Sender, Receiver) = channel(); @@ -36,10 +34,10 @@ pub fn initialize(subscribers: Subscribers) -> Result<(), InitializationError> { loop { if let Ok(packet) = rx.recv() { udp_packet_to_game_events(&mut world, &mut photon, &packet) - .into_iter() - .for_each(|e| { - publisher.publish(&e); - }); + .into_iter() + .for_each(|e| { + publisher.publish(&e); + }); } } }); @@ -52,8 +50,20 @@ pub fn initialize(subscribers: Subscribers) -> Result<(), InitializationError> { fn initialize_logging() { CombinedLogger::init(vec![WriteLogger::new( - LevelFilter::Info, + get_logging_level(), Config::default(), - File::create("damage-meter.log").unwrap(), - )]).unwrap(); -} \ No newline at end of file + File::create("backend.log").unwrap(), + )]) + .unwrap(); +} + +fn get_logging_level() -> LevelFilter { + match env::var("BACKEND_LOG_LEVEL") + .unwrap_or("INFO".into()) + .to_lowercase() + .as_str() + { + "debug" => LevelFilter::Debug, + _ => LevelFilter::Info, + } +}