From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <l.wagner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 9B001938AE
 for <pve-devel@lists.proxmox.com>; Tue, 21 Feb 2023 10:29:49 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 79F8DC60
 for <pve-devel@lists.proxmox.com>; Tue, 21 Feb 2023 10:29:49 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Tue, 21 Feb 2023 10:29:48 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5D9D647DC1
 for <pve-devel@lists.proxmox.com>; Tue, 21 Feb 2023 10:29:48 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 21 Feb 2023 10:29:46 +0100
Message-Id: <20230221092946.192347-1-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.163 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [mod.rs, logger.rs, lib.rs, template.pm]
Subject: [pve-devel] [PATCH v2 proxmox-perl-rs] initialize logging when
 shared library is loaded
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 21 Feb 2023 09:29:49 -0000

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