Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: Use Python's logging through pyo3_log #319

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -67,13 +66,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)`
geonnave marked this conversation as resolved.
Show resolved Hide resolved
/// 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
geonnave marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -90,7 +102,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