initialize logging when shared library is loaded

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@proxmox.com>
This commit is contained in:
Lukas Wagner 2023-02-21 10:29:46 +01:00 committed by Wolfgang Bumiller
parent f6b244dfb3
commit c7c3940718
7 changed files with 34 additions and 1 deletions

View File

@ -66,6 +66,10 @@ sub bootstrap {
$boot->();
}
BEGIN { __PACKAGE__->load(); }
BEGIN {
__PACKAGE__->load();
__PACKAGE__->bootstrap();
init();
}
1;

6
common/src/logger.rs Normal file
View File

@ -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}");
}
}

View File

@ -1,3 +1,4 @@
pub mod apt;
mod calendar_event;
pub mod logger;
mod subscription;

View File

@ -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"

View File

@ -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();
}
}

View File

@ -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"

View File

@ -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();
}
}