[pve-devel] [PATCH v2 proxmox-perl-rs] initialize logging when shared library is loaded
Lukas Wagner
l.wagner at proxmox.com
Tue Feb 21 10:29:46 CET 2023
This commit sets up logging by hooking into module loading/bootstraping
process to call a new `init` function exported by the `Proxmox::Lib::{PVE,PMG}`
module, which initializes `env_logger` with its default settings.
This allows us to use `log::*` macros from Rust code.
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
Changes v1 -> v2:
* Incorporate changes as requested by Wolfgang in v1:
* Use perlmod instead of exported 'extern "C"' function
Proxmox/Lib/template.pm | 6 +++++-
common/src/logger.rs | 6 ++++++
common/src/mod.rs | 1 +
pmg-rs/Cargo.toml | 1 +
pmg-rs/src/lib.rs | 10 ++++++++++
pve-rs/Cargo.toml | 1 +
pve-rs/src/lib.rs | 10 ++++++++++
7 files changed, 34 insertions(+), 1 deletion(-)
create mode 100644 common/src/logger.rs
diff --git a/Proxmox/Lib/template.pm b/Proxmox/Lib/template.pm
index d7fbbf3..9eb10cf 100644
--- a/Proxmox/Lib/template.pm
+++ b/Proxmox/Lib/template.pm
@@ -66,6 +66,10 @@ sub bootstrap {
$boot->();
}
-BEGIN { __PACKAGE__->load(); }
+BEGIN {
+ __PACKAGE__->load();
+ __PACKAGE__->bootstrap();
+ init();
+}
1;
diff --git a/common/src/logger.rs b/common/src/logger.rs
new file mode 100644
index 0000000..36dc856
--- /dev/null
+++ b/common/src/logger.rs
@@ -0,0 +1,6 @@
+/// Initialize logging. Should only be called once
+pub fn init() {
+ if let Err(e) = env_logger::try_init() {
+ eprintln!("could not set up env_logger: {e}");
+ }
+}
diff --git a/common/src/mod.rs b/common/src/mod.rs
index b8b843e..6c86ac0 100644
--- a/common/src/mod.rs
+++ b/common/src/mod.rs
@@ -1,3 +1,4 @@
pub mod apt;
mod calendar_event;
+pub mod logger;
mod subscription;
diff --git a/pmg-rs/Cargo.toml b/pmg-rs/Cargo.toml
index 6800f40..2d9ea29 100644
--- a/pmg-rs/Cargo.toml
+++ b/pmg-rs/Cargo.toml
@@ -20,6 +20,7 @@ crate-type = [ "cdylib" ]
[dependencies]
anyhow = "1.0"
+env_logger = "0.9"
hex = "0.4"
http = "0.2.7"
libc = "0.2"
diff --git a/pmg-rs/src/lib.rs b/pmg-rs/src/lib.rs
index af89416..5914bc9 100644
--- a/pmg-rs/src/lib.rs
+++ b/pmg-rs/src/lib.rs
@@ -5,3 +5,13 @@ pub mod acme;
pub mod apt;
pub mod csr;
pub mod tfa;
+
+#[perlmod::package(name = "Proxmox::Lib::PMG", lib = "pmg_rs")]
+mod export {
+ use crate::common;
+
+ #[export]
+ pub fn init() {
+ common::logger::init();
+ }
+}
diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml
index aa32aeb..6c921c4 100644
--- a/pve-rs/Cargo.toml
+++ b/pve-rs/Cargo.toml
@@ -18,6 +18,7 @@ crate-type = [ "cdylib" ]
anyhow = "1.0"
base32 = "0.4"
base64 = "0.13"
+env_logger = "0.9"
hex = "0.4"
http = "0.2.7"
libc = "0.2"
diff --git a/pve-rs/src/lib.rs b/pve-rs/src/lib.rs
index 562a4d4..671aad0 100644
--- a/pve-rs/src/lib.rs
+++ b/pve-rs/src/lib.rs
@@ -7,3 +7,13 @@ pub mod apt;
pub mod openid;
pub mod resource_scheduling;
pub mod tfa;
+
+#[perlmod::package(name = "Proxmox::Lib::PVE", lib = "pve_rs")]
+mod export {
+ use crate::common;
+
+ #[export]
+ pub fn init() {
+ common::logger::init();
+ }
+}
--
2.30.2
More information about the pve-devel
mailing list