public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-perl-rs] initialize logging when shared library is loaded
Date: Thu, 16 Feb 2023 15:22:20 +0100	[thread overview]
Message-ID: <20230216142220.886551-1-l.wagner@proxmox.com> (raw)

Before, all calls to the log::* macros did not produce any output.
This commit sets up logging by hooking into module loading/bootstraping
process to call a single exported function `setup_logging`. The function
initializes env_logger with its default settings. The benefit of this
approach is that no changes of client-side Perl code is required.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
Not sure if this is the best/cleanest approach for this,
seems to work just fine though.

 Proxmox/Lib/template.pm | 7 +++++++
 common/src/logging.rs   | 8 ++++++++
 common/src/mod.rs       | 1 +
 pmg-rs/Cargo.toml       | 1 +
 pve-rs/Cargo.toml       | 1 +
 5 files changed, 18 insertions(+)
 create mode 100644 common/src/logging.rs

diff --git a/Proxmox/Lib/template.pm b/Proxmox/Lib/template.pm
index d7fbbf3..4942a64 100644
--- a/Proxmox/Lib/template.pm
+++ b/Proxmox/Lib/template.pm
@@ -47,6 +47,13 @@ sub load : prototype($) {
     $data->{$mod_name} = $lib;
     $data->{-current} //= $lib;
     $data->{-package} //= $pkg;
+
+    # Lookup up the 'setup_logger' function and call it.
+    # This sets up 'env_logger' so that 'log::*' can be used from Rust code.
+    my $sym  = DynaLoader::dl_find_symbol($lib, "setup_logging");
+    die "failed to locate 'setup_logging'\n" if !defined $sym;
+    my $setup_logging = DynaLoader::dl_install_xsub("setup_logging", $sym, "src/FIXME.rs");
+    $setup_logging->();
 }
 
 sub bootstrap {
diff --git a/common/src/logging.rs b/common/src/logging.rs
new file mode 100644
index 0000000..772b806
--- /dev/null
+++ b/common/src/logging.rs
@@ -0,0 +1,8 @@
+
+#[no_mangle]
+/// Should be called exactly once
+pub extern "C" fn setup_logging() {
+    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..749f494 100644
--- a/common/src/mod.rs
+++ b/common/src/mod.rs
@@ -1,3 +1,4 @@
 pub mod apt;
+pub mod logging;
 mod calendar_event;
 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/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"
-- 
2.30.2





             reply	other threads:[~2023-02-16 14:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-16 14:22 Lukas Wagner [this message]
2023-02-17 13:21 ` Wolfgang Bumiller

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=20230216142220.886551-1-l.wagner@proxmox.com \
    --to=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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal