Skip to content

Commit

Permalink
tapo-py: Surface Rust logs in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-dinculescu committed Oct 21, 2024
1 parent f5b045d commit 740d793
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 23 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tapo-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
7 changes: 7 additions & 0 deletions tapo-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod errors;
mod handlers;
mod runtime;

use log::LevelFilter;
use pyo3::prelude::*;

use api_client::PyApiClient;
Expand All @@ -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,
Expand All @@ -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")?;

Expand Down
12 changes: 6 additions & 6 deletions tapo/src/api/protocol/klap_protocol.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<R> = serde_json::from_str(&response_decrypted)?;
debug!("Device inner response: {inner_response:?}");
let response: TapoResponse<R> = 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)
}
Expand Down
4 changes: 2 additions & 2 deletions tapo/src/api/protocol/passthrough_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<R> = serde_json::from_str(&inner_response_decrypted)?;

Expand Down

0 comments on commit 740d793

Please sign in to comment.