From 8f0623b09b63a33dbd8741008a9833ac7981529b Mon Sep 17 00:00:00 2001
From: godzie44 <godzie@yandex.ru>
Date: Fri, 26 Jul 2024 17:17:30 +0300
Subject: [PATCH] refactor(plugin): add additional logging at plugin loading

Closes 813
---
 picoplugin/src/plugin/interface.rs | 12 ++++++++++++
 src/plugin/manager.rs              | 21 ++++++++++++++++++---
 src/plugin/mod.rs                  |  4 ++--
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/picoplugin/src/plugin/interface.rs b/picoplugin/src/plugin/interface.rs
index e463b3bebd..e93c474493 100644
--- a/picoplugin/src/plugin/interface.rs
+++ b/picoplugin/src/plugin/interface.rs
@@ -451,4 +451,16 @@ impl ServiceRegistry {
             Some(_) => Err(()),
         }
     }
+
+    /// Return a registered list of (service name, plugin version) pairs.
+    pub fn dump(&self) -> Vec<(String, String)> {
+        self.services
+            .keys()
+            .map(|key| {
+                let service = key.0.to_string();
+                let version = key.1.to_string();
+                (service, version)
+            })
+            .collect()
+    }
 }
diff --git a/src/plugin/manager.rs b/src/plugin/manager.rs
index c73b56805e..24a81dc989 100644
--- a/src/plugin/manager.rs
+++ b/src/plugin/manager.rs
@@ -118,8 +118,12 @@ impl PluginManager {
             }
 
             // trying to load a dynamic library
-            let Ok(lib) = (unsafe { LibraryWrapper::new(path) }) else {
-                continue;
+            let lib = match unsafe { LibraryWrapper::new(path.clone()) } {
+                Ok(lib) => lib,
+                Err(e) => {
+                    tlog!(Warning, "error while open plugin candidate: {e}");
+                    continue;
+                }
             };
             let lib = Rc::new(lib);
 
@@ -133,6 +137,12 @@ impl PluginManager {
                 registrar(&mut registry);
             });
 
+            tlog!(
+                Info,
+                "Plugin registry content from file {path:?}: {:?}",
+                registry.dump()
+            );
+
             // validate all services to possible factory collisions
             service_defs.iter().try_for_each(|svc| {
                 registry
@@ -174,7 +184,12 @@ impl PluginManager {
         }
 
         if !service_defs_to_load.is_empty() {
-            return Err(PluginError::PartialLoad);
+            return Err(PluginError::PartialLoad(
+                service_defs_to_load
+                    .into_iter()
+                    .map(|def| def.name)
+                    .collect(),
+            ));
         }
 
         if dry_run {
diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs
index a7aabe21f9..82651ca6fa 100644
--- a/src/plugin/mod.rs
+++ b/src/plugin/mod.rs
@@ -63,8 +63,8 @@ pub enum PluginError {
     ReadPluginDir(#[from] io::Error),
     #[error("Invalid shared object file: {0}")]
     InvalidSharedObject(#[from] libloading::Error),
-    #[error("Plugin partial load (some of services not found)")]
-    PartialLoad,
+    #[error("Plugin partial load (some of services not found: {0:?})")]
+    PartialLoad(Vec<String>),
     #[error("Callback: {0}")]
     Callback(#[from] PluginCallbackError),
     #[error("Attempt to call a disabled plugin")]
-- 
GitLab