From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Lukas Wagner <l.wagner@proxmox.com>
Cc: pve-devel@lists.proxmox.com
Subject: [pve-devel] applied: [PATCH v2 proxmox-perl-rs] initialize logging when shared library is loaded
Date: Tue, 7 Mar 2023 12:49:29 +0100 [thread overview]
Message-ID: <20230307114929.zsvirs24dvhvecpe@fwblub> (raw)
In-Reply-To: <20230221092946.192347-1-l.wagner@proxmox.com>
applied, thanks
On Tue, Feb 21, 2023 at 10:29:46AM +0100, Lukas Wagner wrote:
> 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>
> ---
> 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
prev parent reply other threads:[~2023-03-07 11:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-21 9:29 [pve-devel] " Lukas Wagner
2023-03-07 11:49 ` Wolfgang Bumiller [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230307114929.zsvirs24dvhvecpe@fwblub \
--to=w.bumiller@proxmox.com \
--cc=l.wagner@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox