From d497414fec2b8e5b4a242f32b740113cd49b8398 Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Sun, 25 Jun 2023 15:05:14 +0800
Subject: [PATCH] fix: rename create_unit_obj_with_um to
 create_subunit_with_um, rename __unit_obj_create_with_params to
 __subunit_create_with_params, and we don't need to init logger when creating
 subunit, because we have already done that when we create UnitManagerObj

---
 coms/service/src/unit.rs      |  4 +--
 coms/target/src/unit.rs       |  4 +--
 core/bin/plugin/mod.rs        | 66 +++++++++++++++++++----------------
 core/bin/unit/entry/uentry.rs |  2 +-
 core/bin/unit/test/mod.rs     |  2 +-
 core/bin/unit/uload.rs        | 11 +++---
 core/lib/unit/base.rs         |  9 ++---
 9 files changed, 49 insertions(+), 58 deletions(-)

diff --git a/coms/service/src/unit.rs b/coms/service/src/unit.rs
index a4f6a79..2a217d4 100644
--- a/coms/service/src/unit.rs
+++ b/coms/service/src/unit.rs
@@ -12,13 +12,11 @@
 
 use crate::rentry::ServiceRestart;
 
-use super::base::PLUGIN_NAME;
 use super::comm::ServiceUnitComm;
 use super::config::ServiceConfig;
 use super::mng::RunningData;
 use super::mng::ServiceMng;
 use super::rentry::{NotifyAccess, ServiceCommand, ServiceType};
-use basic::logger;
 use basic::special::{BASIC_TARGET, SHUTDOWN_TARGET, SYSINIT_TARGET};
 use nix::sys::signal::Signal;
 use nix::sys::socket::UnixCredentials;
@@ -414,4 +412,4 @@ impl ServiceUnit {
 }*/
 
 use sysmaster::declure_unitobj_plugin_with_param;
-declure_unitobj_plugin_with_param!(ServiceUnit, ServiceUnit::new, PLUGIN_NAME);
+declure_unitobj_plugin_with_param!(ServiceUnit, ServiceUnit::new);
diff --git a/coms/target/src/unit.rs b/coms/target/src/unit.rs
index e4caf5d..bcd97c7 100644
--- a/coms/target/src/unit.rs
+++ b/coms/target/src/unit.rs
@@ -15,10 +15,8 @@
 //! Trait UnitObj defines the behavior of the sub unit.
 //! Trait UnitMngUtil is used to attach the Unitmanager to the sub unit.
 //! Trait UnitSubClass implement the convert from sub unit to UnitObj.
-use super::base::PLUGIN_NAME;
 use super::comm::TargetUnitComm;
 use super::mng::TargetMng;
-use basic::logger;
 use nix::sys::wait::WaitStatus;
 use std::{path::PathBuf, rc::Rc};
 use sysmaster::error::*;
@@ -193,4 +191,4 @@ impl UnitMngUtil for Target {
 }*/
 
 use sysmaster::declure_unitobj_plugin_with_param;
-declure_unitobj_plugin_with_param!(Target, Target::new, PLUGIN_NAME);
+declure_unitobj_plugin_with_param!(Target, Target::new);
diff --git a/core/bin/plugin/mod.rs b/core/bin/plugin/mod.rs
index a8f6f24..0b67f74 100644
--- a/core/bin/plugin/mod.rs
+++ b/core/bin/plugin/mod.rs
@@ -430,43 +430,47 @@ impl Plugin {
         }
     }
 
-    /// Create a  obj for subclasses of unit
+    /// Create the subunit trait of unit
     /// each sub unit need reference of declure_unitobj_plugin_with_param
     ///
-    pub fn create_unit_obj_with_um(
+    pub fn create_subunit_with_um(
         &self,
         unit_type: UnitType,
         um: Rc<dyn UmIf>,
     ) -> Result<Box<dyn SubUnit>> {
-        type SymType =
-            fn(um: Rc<dyn UmIf>, level: LevelFilter, target: &str, file: &str) -> *mut dyn SubUnit;
-        match self.get_lib(unit_type) {
-            Ok(dy_lib) => {
-                let sym: Result<Symbol<SymType>> = unsafe {
-                    dy_lib
-                        .lib
-                        .get(CONSTRUCTOR_NAME_WITH_PARAM)
-                        .map_err(|e| Error::PluginLoad { msg: e.to_string() })
-                };
-                log::debug!(
-                    "create unit obj with param level filter: {:?}",
-                    log::max_level()
-                );
-                let target = um.get_log_target();
-                let file = um.get_log_file();
-                if let Ok(fun) = sym {
-                    let boxed_raw = fun(um.clone(), log::max_level(), target, file);
-                    Ok(unsafe { Box::from_raw(boxed_raw) })
-                } else {
-                    Err(Error::PluginLoad {
-                        msg: format!("The library of {:?} is {:?}", unit_type, sym.err()),
-                    })
-                }
+        type FnType = fn(um: Rc<dyn UmIf>) -> *mut dyn SubUnit;
+
+        let dy_lib = match self.get_lib(unit_type) {
+            Err(_) => {
+                return Err(Error::PluginLoad {
+                    msg: format!("create unit, the {unit_type:?} plugin is not exist"),
+                })
             }
-            Err(_) => Err(Error::PluginLoad {
-                msg: format!("create unit, the {unit_type:?} plugin is not exist"),
-            }),
-        }
+            Ok(v) => v,
+        };
+
+        let sym: Result<Symbol<FnType>> = unsafe {
+            dy_lib
+                .lib
+                .get(b"__subunit_create_with_params")
+                .map_err(|e| Error::PluginLoad { msg: e.to_string() })
+        };
+
+        let fun = match sym {
+            Err(_) => {
+                return Err(Error::PluginLoad {
+                    msg: format!("The library of {:?} is {:?}", unit_type, sym.err()),
+                })
+            }
+            Ok(v) => v,
+        };
+
+        log::debug!(
+            "create unit obj with param level filter: {:?}",
+            log::max_level()
+        );
+        let boxed_raw = fun(um.clone());
+        Ok(unsafe { Box::from_raw(boxed_raw) })
     }
     /// Create a  obj for subclasses of unit manager
     /// each sub unit manager need reference of declure_umobj_plugin
@@ -583,7 +587,7 @@ mod tests {
     fn test_plugin_create_unit() {
         let plugin = init_test();
         let umifd = Rc::new(UmIfD);
-        let unitobj = plugin.create_unit_obj_with_um(UnitType::UnitService, umifd);
+        let unitobj = plugin.create_subunit_with_um(UnitType::UnitService, umifd);
         assert!(
             unitobj.is_ok(),
             "create unit [{:?}] failed",
diff --git a/core/bin/unit/entry/uentry.rs b/core/bin/unit/entry/uentry.rs
index 44a4af8..45f0e57 100644
--- a/core/bin/unit/entry/uentry.rs
+++ b/core/bin/unit/entry/uentry.rs
@@ -791,7 +791,7 @@ mod tests {
         let plugin = Plugin::get_instance();
         let umifd = Rc::new(UmIfD);
         let sub_obj = plugin
-            .create_unit_obj_with_um(UnitType::UnitService, umifd.clone())
+            .create_subunit_with_um(UnitType::UnitService, umifd.clone())
             .unwrap();
         sub_obj.attach_um(umifd);
         sub_obj.attach_reli(Rc::clone(&reli));
diff --git a/core/bin/unit/test/mod.rs b/core/bin/unit/test/mod.rs
index 1fc1965..8f2d120 100644
--- a/core/bin/unit/test/mod.rs
+++ b/core/bin/unit/test/mod.rs
@@ -39,7 +39,7 @@ pub(crate) mod test_utils {
         let unit_type = unit_name_to_type(name);
         let umifd = Rc::new(UmIfD);
         let plugins = Plugin::get_instance();
-        let subclass = plugins.create_unit_obj_with_um(unit_type, umifd).unwrap();
+        let subclass = plugins.create_subunit_with_um(unit_type, umifd).unwrap();
         subclass.attach_reli(Rc::clone(relir));
         Rc::new(UnitX::new(dmr, rentryr, &file, unit_type, name, subclass))
     }
diff --git a/core/bin/unit/uload.rs b/core/bin/unit/uload.rs
index d6fea43..382b2c0 100644
--- a/core/bin/unit/uload.rs
+++ b/core/bin/unit/uload.rs
@@ -157,14 +157,13 @@ impl UnitLoadData {
             return None;
         }
 
-        log::info!(
-            "begin create obj for type {:?}, name {} by plugin",
-            unit_type,
-            name
-        );
         let um = self.um();
         let um_rc = Rc::clone(&um);
-        let subclass = match Plugin::get_instance().create_unit_obj_with_um(unit_type, um_rc) {
+        log::info!(
+            "Creating SubUnit for {:?}: {name} by __subunit_create_with_params()",
+            unit_type
+        );
+        let subclass = match Plugin::get_instance().create_subunit_with_um(unit_type, um_rc) {
             Ok(sub) => sub,
             Err(_e) => {
                 log::error!("Failed to create unit_obj!{}", _e);
diff --git a/core/lib/unit/base.rs b/core/lib/unit/base.rs
index 555bb69..8aea031 100644
--- a/core/lib/unit/base.rs
+++ b/core/lib/unit/base.rs
@@ -191,19 +191,14 @@ macro_rules! declure_unitobj_plugin_default {
 /// which sub unit wants invoke um interface, about UmIf see doc of UmIf
 #[macro_export]
 macro_rules! declure_unitobj_plugin_with_param {
-    ($unit_type:ty, $constructor:path, $name:expr) => {
+    ($unit_type:ty, $constructor:path) => {
         use log::LevelFilter;
         /// method for create the unit instance
         #[no_mangle]
-        pub fn __unit_obj_create_with_params(
+        pub fn __subunit_create_with_params(
             um: Rc<dyn $crate::unit::UmIf>,
-            level: LevelFilter,
-            target: &str,
-            file: &str,
         ) -> *mut dyn $crate::unit::SubUnit {
-            logger::init_log_for_subum($name, level, target, file);
             let construcotr: fn(um: Rc<dyn $crate::unit::UmIf>) -> $unit_type = $constructor;
-
             let obj = construcotr(um);
             let boxed: Box<dyn $crate::unit::SubUnit> = Box::new(obj);
             Box::into_raw(boxed)
-- 
2.33.0

