From 192731dcd3ece01813f34b40d2d2f97123791ce8 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Tue, 9 Jan 2024 11:33:11 +1100 Subject: [PATCH] logging --- Cargo.lock | 39 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ debian/service | 3 ++- src/api_interface.rs | 12 ++++++------ src/main.rs | 5 +++-- src/types.rs | 5 +++++ watch.sh | 3 ++- 7 files changed, 59 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33cd5de..0eeb178 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -534,6 +534,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -812,6 +825,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.28" @@ -2153,6 +2172,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "termcolor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + [[package]] name = "terminal_size" version = "0.1.17" @@ -2171,7 +2199,9 @@ dependencies = [ "async-channel", "chrono", "clap", + "env_logger", "include_dir", + "log", "metrics", "metrics-prometheus", "prometheus", @@ -2698,6 +2728,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 59817d0..3abeef6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,5 @@ async-channel = "2.1" metrics = "0.22" metrics-prometheus = "0.6.0" prometheus = "0.13" +env_logger = "0.10" +log = "0.4" diff --git a/debian/service b/debian/service index 494cf11..2ae7795 100644 --- a/debian/service +++ b/debian/service @@ -6,8 +6,9 @@ StartLimitIntervalSec=0 [Service] Type=simple Restart=always -RestartSec=1 +RestartSec=10s User=tesla +Environment="RUST_LOG=error,warn,info" ExecStart=/usr/bin/tesla-charge-controller watch [Install] diff --git a/src/api_interface.rs b/src/api_interface.rs index bc0a3ad..53e5183 100644 --- a/src/api_interface.rs +++ b/src/api_interface.rs @@ -71,7 +71,7 @@ impl TeslaInterface { let mut api = FleetApi::new(key.access_token, key.refresh_token); api.refresh().await?; let last_refresh = Instant::now(); - println!("Refreshed auth key"); + info!("Refreshed auth key"); let vehicle = api .products() .await? @@ -106,12 +106,12 @@ impl TeslaInterface { refresh_token: self.api.refresh_token.clone(), })?, )?; - println!("Auth successfully saved"); + info!("Auth successfully saved"); Ok(()) } pub async fn refresh(&mut self) { - println!("refreshing..."); + info!("refreshing keys..."); self.refresh_keys().await; self.refresh_state().await; } @@ -145,7 +145,7 @@ impl TeslaInterface { state.climate_state = Some(new_climate_state); } } - Err(e) => eprintln!("Error getting state: {e:#?}"), + Err(e) => error!("Error getting state: {e:#?}"), } } @@ -156,10 +156,10 @@ impl TeslaInterface { let now = Instant::now(); match self.save_key() { Ok(_) => self.last_refresh = now, - Err(e) => eprintln!("error saving auth token: {e:?}"), + Err(e) => error!("error saving auth token: {e:?}"), } } - Err(e) => eprintln!("error refreshing auth token: {e:?}"), + Err(e) => error!("error refreshing auth token: {e:?}"), } } } diff --git a/src/main.rs b/src/main.rs index 2efe186..9c8072e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,7 @@ enum Commands { #[tokio::main] async fn main() { let args = Args::parse(); + env_logger::init(); let auth_path = args.config_dir.join("auth"); let config_path = args.config_dir.join("config"); @@ -66,7 +67,7 @@ async fn main() { _ = interval.tick() => interface.refresh().await, message = receiver.recv() => match message { Ok(message) => interface.process_request(message).await, - Err(e) => eprintln!("Error on receive channel: {e:#?}") + Err(e) => error!("Error on receive channel: {e:#?}") } } } @@ -74,7 +75,7 @@ async fn main() { server_handle.await; } - Err(e) => println!("{}", e.error_string()), + Err(e) => error!("{}", e.error_string()), }, } } diff --git a/src/types.rs b/src/types.rs index 5bb6130..b4c28df 100644 --- a/src/types.rs +++ b/src/types.rs @@ -18,6 +18,11 @@ impl TryFrom for ClimateState { type Error = anyhow::Error; fn try_from(value: teslatte::vehicles::ClimateState) -> Result { + log::info!( + "Current cabin overheat protection state: {}", + value.cabin_overheat_protection + ); + Ok(Self { inside_temp: value .inside_temp diff --git a/watch.sh b/watch.sh index 60b4cac..c6c6bcd 100755 --- a/watch.sh +++ b/watch.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash + ( trap 'kill 0' SIGINT cargo watch -w "src" -x check -s 'touch .trigger' & - cargo watch --no-vcs-ignores -w .trigger -x "run -- --config-dir test-config watch" + RUST_LOG=error,warn,info cargo watch --no-vcs-ignores -w .trigger -x "run -- --config-dir test-config watch" )