Skip to content

Commit

Permalink
Merge pull request openwsn-berkeley#319 from chrysn-pull-requests/log…
Browse files Browse the repository at this point in the history
…-to-python

python: Use Python's logging through pyo3_log
  • Loading branch information
geonnave authored Nov 21, 2024
2 parents 91f7d77 + 96e0286 commit b5ab5bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lakers-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lakers-ead-authz = { path = "../ead/lakers-ead-authz", features = [ "log" ] }
lakers-shared = { path = "../shared", features = ["python-bindings", "quadruple_sizes"] }
lakers-crypto = { path = "../crypto", default-features = false, features = ["rustcrypto"] }
log = "0.4"
env_logger = "0.9"
pyo3-log = "0.11.0"

[dev-dependencies]
# We don't need it to build, but it is listed in the manifest Cargo.toml, and
Expand Down
24 changes: 18 additions & 6 deletions lakers-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/// Note that this module is not restricted by no_std.
use lakers::*;
// use lakers_ead_authz::consts::*;
use env_logger;
use lakers_crypto::{default_crypto, CryptoTrait};
use log::trace;
use pyo3::wrap_pyfunction;
Expand Down Expand Up @@ -89,13 +88,26 @@ impl AutoCredential {
}
}

// this name must match `lib.name` in `Cargo.toml`
/// Lakers implementation of EDHOC.
///
/// The `EdhocInitiator` and `EdhocResponder` are entry points to this module.
///
/// Operations in this module produce logging entries on the `lakers.initiator` and
/// `lakers.responder` logger names. Due to implementation details of `pyo3_log`, Python's log
/// levels are cached in the Rust implementation. It is recommended that the full logging
/// is configured before creating Lakers objects. A setup with `logging.basicConfig(loglevel=5)`
/// will also show Lakers' trace level log messages, which have no equivalent Python level.
#[pymodule]
// this name must match `lib.name` in `Cargo.toml`
#[pyo3(name = "lakers")]
fn lakers_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
fn lakers_python(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
// initialize the logger once when the module is imported
if env_logger::try_init().is_ok() {
trace!("lakers-python initialized from Rust side.");
if let Err(e) = pyo3_log::Logger::new(py, pyo3_log::Caching::LoggersAndLevels)?
.filter(log::LevelFilter::Trace)
.install()
{
// Not logging anything in the successful case (see module level docs)
log::error!("lakers-python failed to set up: {e}");
}

m.add_function(wrap_pyfunction!(p256_generate_key_pair, m)?)?;
Expand All @@ -112,7 +124,7 @@ fn lakers_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ead_authz::PyAuthzEnrollmentServer>()?;
m.add_class::<ead_authz::PyAuthzServerUserAcl>()?;

let submodule = PyModule::new_bound(_py, "consts")?;
let submodule = PyModule::new_bound(py, "consts")?;
submodule.add("EAD_AUTHZ_LABEL", lakers_ead_authz::consts::EAD_AUTHZ_LABEL)?;
m.add_submodule(&submodule)?;
Ok(())
Expand Down

0 comments on commit b5ab5bd

Please sign in to comment.