From 7f89ba505443820255cc2b093a011ee74d06b024 Mon Sep 17 00:00:00 2001 From: folex <0xdxdy@gmail.com> Date: Fri, 23 Oct 2020 17:05:58 +0300 Subject: [PATCH] use module::target as module_path in log --- fluence-faas/src/faas.rs | 11 ++++-- fluence-faas/src/misc/log_utf8_string_impl.rs | 34 ++++++++++++++++--- fluence-faas/src/misc/mod.rs | 2 +- fluence-faas/src/misc/utils.rs | 8 +++-- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/fluence-faas/src/faas.rs b/fluence-faas/src/faas.rs index f84d8542..42596ae9 100644 --- a/fluence-faas/src/faas.rs +++ b/fluence-faas/src/faas.rs @@ -87,8 +87,11 @@ impl FluenceFaaS { )) })?; - let fce_module_config = - crate::misc::make_fce_config(Some(module_config), call_parameters.clone())?; + let fce_module_config = crate::misc::make_fce_config( + module_name.clone(), + Some(module_config), + call_parameters.clone(), + )?; fce.load_module(module_name, &module_bytes, fce_module_config)?; } @@ -233,8 +236,10 @@ impl FluenceFaaS { FaaSError: From, { let config = config.map(|c| c.try_into()).transpose()?; + let name = name.into(); - let fce_module_config = crate::misc::make_fce_config(config, self.call_parameters.clone())?; + let fce_module_config = + crate::misc::make_fce_config(name.clone(), config, self.call_parameters.clone())?; self.fce .load_module(name, &wasm_bytes, fce_module_config) .map_err(Into::into) diff --git a/fluence-faas/src/misc/log_utf8_string_impl.rs b/fluence-faas/src/misc/log_utf8_string_impl.rs index 9484ce41..c1afceda 100644 --- a/fluence-faas/src/misc/log_utf8_string_impl.rs +++ b/fluence-faas/src/misc/log_utf8_string_impl.rs @@ -17,7 +17,24 @@ use wasmer_core::vm::Ctx; use wasmer_core::memory::ptr::{Array, WasmPtr}; +pub(super) fn log_utf8_string_closure( + module: String, +) -> impl for<'a> Fn(&'a mut Ctx, i32, i32, i32, i32, i32) { + move |ctx, level, target_offset, target_size, msg_offset, msg_size| { + log_utf8_string( + &module, + ctx, + level, + target_offset, + target_size, + msg_offset, + msg_size, + ) + } +} + pub(super) fn log_utf8_string( + module: &str, ctx: &mut Ctx, level: i32, target_offset: i32, @@ -28,10 +45,19 @@ pub(super) fn log_utf8_string( let level = level_from_i32(level); let target = read_string(ctx, target_offset, target_size); let msg = read_string(ctx, msg_offset, msg_size); - match (msg, target) { - (Some(msg), Some(target)) => log::log!(target: target, level, "{}", msg), - (Some(msg), None) => log::log!(level, "{}", msg), - (None, _) => log::warn!("logger: incorrect UTF8 string's been supplied to logger"), + + let module_path = target.map(|t| format!("{}::{}", module, t)); + let module_path = module_path.as_deref().unwrap_or(module); + + match msg { + Some(msg) => log::logger().log( + &log::Record::builder() + .args(format_args!("{}", msg)) + .level(level) + .module_path(module_path.into()) + .build(), + ), + None => log::warn!("logger: incorrect UTF8 string's been supplied to logger"), } } diff --git a/fluence-faas/src/misc/mod.rs b/fluence-faas/src/misc/mod.rs index da6b17d6..0608babc 100644 --- a/fluence-faas/src/misc/mod.rs +++ b/fluence-faas/src/misc/mod.rs @@ -24,4 +24,4 @@ pub(crate) use modules_load_strategy::ModulesLoadStrategy; pub(crate) use utils::create_host_import; pub(crate) use utils::make_fce_config; -pub(self) use log_utf8_string_impl::log_utf8_string; +pub(self) use log_utf8_string_impl::log_utf8_string_closure; diff --git a/fluence-faas/src/misc/utils.rs b/fluence-faas/src/misc/utils.rs index 17976610..f7b3ab2d 100644 --- a/fluence-faas/src/misc/utils.rs +++ b/fluence-faas/src/misc/utils.rs @@ -15,7 +15,7 @@ */ use crate::config::FaaSModuleConfig; -use super::log_utf8_string; +use super::log_utf8_string_closure; use fce::FCEModuleConfig; use fce::HostImportDescriptor; @@ -77,6 +77,7 @@ fn create_call_parameters_import( /// Make FCE config from provided FaaS config. pub(crate) fn make_fce_config( + module_name: String, faas_module_config: Option, call_parameters: Rc>, ) -> crate::Result { @@ -119,7 +120,10 @@ pub(crate) fn make_fce_config( let mut namespace = Namespace::new(); if faas_module_config.logger_enabled { - namespace.insert("log_utf8_string", func!(log_utf8_string)); + namespace.insert( + "log_utf8_string", + func!(log_utf8_string_closure(module_name)), + ); } let mut raw_host_imports = ImportObject::new();