diff --git a/CHANGELOG.md b/CHANGELOG.md index d7f8892..6a8e542 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ file. This change log follows the conventions of ### Added - Added support for the P300 and P304 power strips. +- Python logs can now capture entries from the underlying Rust library. ### Changed diff --git a/Cargo.lock b/Cargo.lock index 187a9c3..cef616e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,6 +58,12 @@ version = "1.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37bf3594c4c988a53154954629820791dde498571819ae4ca50ca811e060cc95" +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + [[package]] name = "async-trait" version = "0.1.83" @@ -140,9 +146,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.7.2" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" +checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" [[package]] name = "cbc" @@ -946,6 +952,17 @@ dependencies = [ "pyo3-build-config", ] +[[package]] +name = "pyo3-log" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ac84e6eec1159bc2a575c9ae6723baa6ee9d45873e9bebad1e3ad7e8d28a443" +dependencies = [ + "arc-swap", + "log", + "pyo3", +] + [[package]] name = "pyo3-macros" version = "0.22.5" @@ -1266,9 +1283,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.81" +version = "2.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "198514704ca887dd5a1e408c6c6cdcba43672f9b4062e1b24aa34e74e6d7faae" +checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" dependencies = [ "proc-macro2", "quote", @@ -1319,7 +1336,9 @@ version = "0.1.0" dependencies = [ "anyhow", "chrono", + "log", "pyo3", + "pyo3-log", "serde", "serde_json", "tapo", diff --git a/tapo-py/Cargo.toml b/tapo-py/Cargo.toml index c64873a..db3067b 100644 --- a/tapo-py/Cargo.toml +++ b/tapo-py/Cargo.toml @@ -15,12 +15,14 @@ default = [] [dependencies] anyhow = { workspace = true } chrono = { workspace = true, default-features = false } +log = { version = "0.4", default-features = false } pyo3 = { workspace = true, features = [ "chrono", "experimental-async", "extension-module", "py-clone", ] } +pyo3-log = { version = "0.11" } serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true, default-features = false, features = [ diff --git a/tapo-py/src/lib.rs b/tapo-py/src/lib.rs index 28d8338..344a938 100644 --- a/tapo-py/src/lib.rs +++ b/tapo-py/src/lib.rs @@ -3,6 +3,7 @@ mod errors; mod handlers; mod runtime; +use log::LevelFilter; use pyo3::prelude::*; use api_client::PyApiClient; @@ -13,6 +14,7 @@ use handlers::{ PyT300Handler, PyT31XHandler, TriggerLogsS200BResult, TriggerLogsT100Result, TriggerLogsT110Result, TriggerLogsT300Result, }; +use pyo3_log::{Caching, Logger}; use tapo::requests::Color; use tapo::responses::{ AutoOffStatus, ColorLightState, CurrentPowerResult, DefaultBrightnessState, @@ -30,6 +32,11 @@ use tapo::responses::{ #[pymodule] #[pyo3(name = "tapo")] fn tapo_py(py: Python, module: &Bound<'_, PyModule>) -> PyResult<()> { + Logger::new(py, Caching::LoggersAndLevels)? + .filter(LevelFilter::Trace) + .install() + .expect("Failed to install the logger"); + let requests = PyModule::new_bound(py, "tapo.requests")?; let responses = PyModule::new_bound(py, "tapo.responses")?; diff --git a/tapo/src/api/protocol/klap_protocol.rs b/tapo/src/api/protocol/klap_protocol.rs index 91b109c..30fe845 100644 --- a/tapo/src/api/protocol/klap_protocol.rs +++ b/tapo/src/api/protocol/klap_protocol.rs @@ -1,7 +1,7 @@ use std::fmt; use async_trait::async_trait; -use log::{debug, warn}; +use log::{debug, trace, warn}; use rand::rngs::StdRng; use rand::{RngCore, SeedableRng}; use reqwest::header::COOKIE; @@ -84,13 +84,13 @@ impl TapoProtocolExt for KlapProtocol { let response_body = response.bytes().await.map_err(anyhow::Error::from)?; let response_decrypted = cipher.decrypt(seq, response_body.to_vec())?; - debug!("Device responded with: {response_decrypted:?}"); + trace!("Device responded with (raw): {response_decrypted}"); - let inner_response: TapoResponse = serde_json::from_str(&response_decrypted)?; - debug!("Device inner response: {inner_response:?}"); + let response: TapoResponse = serde_json::from_str(&response_decrypted)?; + debug!("Device responded with: {response:?}"); - validate_response(&inner_response)?; - let result = inner_response.result; + validate_response(&response)?; + let result = response.result; Ok(result) } diff --git a/tapo/src/api/protocol/passthrough_protocol.rs b/tapo/src/api/protocol/passthrough_protocol.rs index ec613d7..795ee94 100644 --- a/tapo/src/api/protocol/passthrough_protocol.rs +++ b/tapo/src/api/protocol/passthrough_protocol.rs @@ -2,7 +2,7 @@ use std::fmt; use async_trait::async_trait; use base64::{engine::general_purpose, Engine as _}; -use log::debug; +use log::{debug, trace}; use rand::rngs::StdRng; use rand::SeedableRng; use reqwest::header::COOKIE; @@ -112,7 +112,7 @@ impl TapoProtocolExt for PassthroughProtocol { let inner_response_decrypted = session.cipher.decrypt(&inner_response_encrypted)?; - debug!("Device inner response decrypted: {inner_response_decrypted}"); + trace!("Device inner response (raw): {inner_response_decrypted}"); let inner_response: TapoResponse = serde_json::from_str(&inner_response_decrypted)?;