public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH v2 proxmox-perl-rs] initialize logging when shared library is loaded
@ 2023-02-21  9:29 Lukas Wagner
  2023-03-07 11:49 ` [pve-devel] applied: " Wolfgang Bumiller
  0 siblings, 1 reply; 2+ messages in thread
From: Lukas Wagner @ 2023-02-21  9:29 UTC (permalink / raw)
  To: pve-devel

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





^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pve-devel] applied: [PATCH v2 proxmox-perl-rs] initialize logging when shared library is loaded
  2023-02-21  9:29 [pve-devel] [PATCH v2 proxmox-perl-rs] initialize logging when shared library is loaded Lukas Wagner
@ 2023-03-07 11:49 ` Wolfgang Bumiller
  0 siblings, 0 replies; 2+ messages in thread
From: Wolfgang Bumiller @ 2023-03-07 11:49 UTC (permalink / raw)
  To: Lukas Wagner; +Cc: pve-devel

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-07 11:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-21  9:29 [pve-devel] [PATCH v2 proxmox-perl-rs] initialize logging when shared library is loaded Lukas Wagner
2023-03-07 11:49 ` [pve-devel] applied: " Wolfgang Bumiller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal