refactor logger

This commit is contained in:
vms 2020-12-08 18:15:18 +03:00
parent 1f2355e6ef
commit de3d51ef8f
17 changed files with 241 additions and 61 deletions

108
Cargo.lock generated
View File

@ -35,9 +35,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.34"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7"
checksum = "2c0df63cb2955042487fad3aefd2c6e3ae7389ac5dc1beb28921de0b69f779d4"
[[package]]
name = "aquamarine-vm"
@ -189,9 +189,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.0.65"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15"
checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48"
[[package]]
name = "cfg-if"
@ -690,7 +690,7 @@ name = "fce-wit-generator"
version = "0.1.11"
dependencies = [
"fce-wit-parser",
"fluence-sdk-wit 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"fluence-sdk-wit 0.2.9",
"once_cell",
"serde",
"serde_json",
@ -737,17 +737,26 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1099731bb64286758969fb8dd6f903d1e4a7398d8e63bdec1be6387576f4d8ab"
dependencies = [
"fluence-sdk-macro 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"fluence-sdk-main 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"fluence-sdk-macro 0.2.9",
"fluence-sdk-main 0.2.9",
]
[[package]]
name = "fluence"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
source = "git+https://github.com/fluencelabs/rust-sdk?branch=logger_env#be250f9c1f75309b1d96d78dbfd380a8fae7fd3f"
dependencies = [
"fluence-sdk-macro 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence-sdk-main 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence-sdk-macro 0.2.10 (git+https://github.com/fluencelabs/rust-sdk?branch=logger_env)",
"fluence-sdk-main 0.2.10 (git+https://github.com/fluencelabs/rust-sdk?branch=logger_env)",
]
[[package]]
name = "fluence"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#ebf3e63aef8d4aafda41180eeb299c4138628174"
dependencies = [
"fluence-sdk-macro 0.2.10 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence-sdk-main 0.2.10 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]]
@ -772,7 +781,7 @@ dependencies = [
"env_logger 0.7.1",
"fce",
"fce-utils",
"fluence-sdk-main 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"fluence-sdk-main 0.2.9",
"itertools",
"log",
"once_cell",
@ -794,15 +803,23 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca5ffdf0ccf817b1e4e8438f6da7e8fa024679c706a69bde7aa8cad8b43e90ee"
dependencies = [
"fluence-sdk-wit 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"fluence-sdk-wit 0.2.9",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
version = "0.2.10"
source = "git+https://github.com/fluencelabs/rust-sdk?branch=logger_env#be250f9c1f75309b1d96d78dbfd380a8fae7fd3f"
dependencies = [
"fluence-sdk-wit 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence-sdk-wit 0.2.10 (git+https://github.com/fluencelabs/rust-sdk?branch=logger_env)",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.2.10"
source = "git+https://github.com/fluencelabs/rust-sdk#ebf3e63aef8d4aafda41180eeb299c4138628174"
dependencies = [
"fluence-sdk-wit 0.2.10 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]]
@ -811,17 +828,27 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4f81c3778c18d372fec6d96049f25e29fc4ff7ba4ab65ef4c2285f971e8670a"
dependencies = [
"fluence-sdk-macro 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"fluence-sdk-macro 0.2.9",
"log",
"serde",
]
[[package]]
name = "fluence-sdk-main"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
version = "0.2.10"
source = "git+https://github.com/fluencelabs/rust-sdk?branch=logger_env#be250f9c1f75309b1d96d78dbfd380a8fae7fd3f"
dependencies = [
"fluence-sdk-macro 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence-sdk-macro 0.2.10 (git+https://github.com/fluencelabs/rust-sdk?branch=logger_env)",
"log",
"serde",
]
[[package]]
name = "fluence-sdk-main"
version = "0.2.10"
source = "git+https://github.com/fluencelabs/rust-sdk#ebf3e63aef8d4aafda41180eeb299c4138628174"
dependencies = [
"fluence-sdk-macro 0.2.10 (git+https://github.com/fluencelabs/rust-sdk)",
"log",
"serde",
]
@ -842,8 +869,21 @@ dependencies = [
[[package]]
name = "fluence-sdk-wit"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
version = "0.2.10"
source = "git+https://github.com/fluencelabs/rust-sdk?branch=logger_env#be250f9c1f75309b1d96d78dbfd380a8fae7fd3f"
dependencies = [
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
"uuid",
]
[[package]]
name = "fluence-sdk-wit"
version = "0.2.10"
source = "git+https://github.com/fluencelabs/rust-sdk#ebf3e63aef8d4aafda41180eeb299c4138628174"
dependencies = [
"proc-macro2",
"quote",
@ -1323,15 +1363,15 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
[[package]]
name = "local_storage"
version = "0.1.0"
dependencies = [
"fluence 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence 0.2.9 (git+https://github.com/fluencelabs/rust-sdk?branch=logger_env)",
"log",
]
@ -2096,9 +2136,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.117"
version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800"
dependencies = [
"serde_derive",
]
@ -2124,9 +2164,9 @@ dependencies = [
[[package]]
name = "serde_derive"
version = "1.0.117"
version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df"
dependencies = [
"proc-macro2",
"quote",
@ -2135,9 +2175,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.59"
version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95"
checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779"
dependencies = [
"itoa",
"ryu",
@ -2195,9 +2235,9 @@ checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]]
name = "smallvec"
version = "1.5.0"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7acad6f34eb9e8a259d3283d1e8c1d34d7415943d4895f65cc73813c7396fc85"
checksum = "ae524f056d7d770e174287294f562e95044c68e88dec909a00d2094805db9d75"
[[package]]
name = "socket2"
@ -2237,9 +2277,9 @@ checksum = "343f3f510c2915908f155e94f17220b19ccfacf2a64a2a5d8004f2c3e311e7fd"
[[package]]
name = "syn"
version = "1.0.53"
version = "1.0.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
checksum = "9a2af957a63d6bd42255c359c93d9bfdb97076bd3b820897ce55ffbfbf107f44"
dependencies = [
"proc-macro2",
"quote",

View File

@ -19,14 +19,14 @@ mod path;
use crate::path::to_full_path;
use fluence::fce;
use fluence::WasmLogger;
use fluence::WasmLoggerBuilder;
const RESULT_FILE_PATH: &str = "/tmp/ipfs_rpc_file";
const IPFS_ADDR_ENV_NAME: &str = "IPFS_ADDR";
const TIMEOUT_ENV_NAME: &str = "timeout";
pub fn main() {
WasmLogger::new()
WasmLoggerBuilder::new()
.with_log_level(log::Level::Info)
.build()
.unwrap();

View File

@ -15,7 +15,7 @@
*/
use fluence::fce;
use fluence::WasmLogger;
use fluence::WasmLoggerBuilder;
use std::fs;
use std::path::PathBuf;
@ -23,7 +23,7 @@ use std::path::PathBuf;
const RPC_TMP_FILEPATH: &str = "/tmp/ipfs_rpc_file";
pub fn main() {
WasmLogger::new()
WasmLoggerBuilder::new()
.with_log_level(log::Level::Info)
.build()
.unwrap();

View File

@ -15,14 +15,11 @@
*/
use fluence::fce;
use fluence::WasmLogger;
use fluence::WasmLoggerBuilder;
/// Log level can be changed by `RUST_LOG` env as well.
pub fn main() {
WasmLogger::new()
.with_log_level(log::Level::Info)
.build()
.unwrap();
WasmLoggerBuilder::new().build().unwrap();
}
#[fce]

View File

@ -15,13 +15,10 @@
*/
use fluence::fce;
use fluence::WasmLogger;
use fluence::WasmLoggerBuilder;
pub fn main() {
WasmLogger::new()
.with_log_level(log::Level::Info)
.build()
.unwrap();
WasmLoggerBuilder::new().build().unwrap();
}
/// Combining of modules: `curl` and `local_storage`.

View File

@ -9,5 +9,5 @@ name = "local_storage"
path = "src/main.rs"
[dependencies]
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] }
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"], branch = "logger_env" }
log = "0.4.8"

View File

@ -16,17 +16,14 @@
use std::fs;
use fluence::fce;
use fluence::WasmLogger;
use fluence::WasmLoggerBuilder;
use std::path::PathBuf;
const SITES_DIR: &str = "/sites/";
/// Log level can be changed by `RUST_LOG` env as well.
pub fn main() {
WasmLogger::new()
.with_log_level(log::Level::Info)
.build()
.unwrap();
WasmLoggerBuilder::new().build().unwrap();
}
/// You can read or write files from the file system if there is permission to use directories described in `Config.toml`.
@ -46,7 +43,7 @@ pub fn put(name: String, file_content: Vec<u8>) -> String {
#[fce]
pub fn get(file_name: String) -> Vec<u8> {
log::info!("get called with file name: {}", file_name);
log::debug!("get called with file name: {}", file_name);
let tmp_filepath = format!("{}{}", SITES_DIR, file_name);

View File

@ -22,6 +22,8 @@ use crate::IValue;
use crate::IType;
use crate::misc::load_modules_from_fs;
use crate::misc::ModulesLoadStrategy;
use crate::logger::LoggerFilter;
use crate::logger::WASM_LOG_ENV_NAME;
use fce::FCE;
use fce::IFunctionArg;
@ -91,6 +93,11 @@ impl FluenceFaaS {
let call_parameters = Rc::new(RefCell::new(<_>::default()));
let modules_dir = config.modules_dir;
// LoggerFilter can be initialized with an empty string
let wasm_log_env = std::env::var(WASM_LOG_ENV_NAME).unwrap_or_default();
let logger_filter = LoggerFilter::from_env_string(&wasm_log_env);
for (module_name, module_config) in config.modules_config {
let module_bytes =
modules.remove(&module_name).ok_or_else(|| {
@ -104,6 +111,7 @@ impl FluenceFaaS {
module_name.clone(),
Some(module_config),
call_parameters.clone(),
&logger_filter
)?;
fce.load_module(module_name, &module_bytes, fce_module_config)?;
}

View File

@ -26,6 +26,7 @@
mod config;
mod errors;
mod logger;
mod faas;
mod faas_interface;
mod misc;

View File

@ -17,7 +17,7 @@
use wasmer_core::vm::Ctx;
use wasmer_core::memory::ptr::{Array, WasmPtr};
pub(super) fn log_utf8_string_closure(
pub(crate) fn log_utf8_string_closure(
logging_mask: i64,
module: String,
) -> impl Fn(&mut Ctx, i32, i64, i32, i32) {
@ -28,7 +28,7 @@ pub(super) fn log_utf8_string_closure(
}
}
pub(super) fn log_utf8_string(
pub(crate) fn log_utf8_string(
module: &str,
ctx: &mut Ctx,
level: i32,

View File

@ -0,0 +1,102 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use log::LevelFilter;
use std::collections::HashMap;
/// A logger filter.
///
/// This struct can be used to determine whether or not
/// a log record should be written to the output.
#[derive(Clone, Default, Eq, PartialEq, Debug)]
pub(crate) struct LoggerFilter<'env_string> {
default_log_level: Option<LevelFilter>,
module_levels: HashMap<&'env_string str, LevelFilter>,
}
impl<'env_string> LoggerFilter<'env_string> {
/// Parses a content of supplied variable in form of "module_name_1=log_level,module_name_2".
pub(crate) fn from_env_string(env: &'env_string str) -> Self {
let mut module_levels = HashMap::new();
let mut default_log_level: Option<LevelFilter> = None;
for module_log in env.split(',') {
if module_log.is_empty() {
continue;
}
let mut module_log_parts = module_log.split('=');
let part_0 = module_log_parts.next();
let part_1 = module_log_parts.next().map(|s| s.trim());
if let Some(part_3) = module_log_parts.next() {
eprintln!(
"logger warning: invalid directive '{}', ignoring it",
part_3
);
continue;
}
let (module_name, module_log_level) = match (part_0, part_1) {
// "info"
// "1"
(Some(part), None) => match part.parse() {
Ok(num) => (None, num),
Err(_) => (Some(part), LevelFilter::max()),
},
// "module_name="
(Some(module_name), Some("")) => (Some(module_name), LevelFilter::max()),
// "module_name=info"
(Some(module_name), Some(log_level)) => match log_level.parse() {
Ok(log_level) => (Some(module_name), log_level),
Err(e) => {
eprintln!(
"logger warning: invalid directive '{}', error '{}', ignoring it",
log_level, e
);
continue;
}
},
d => {
eprintln!("logger warning: invalid directive '{:?}', ignoring it", d);
continue;
}
};
match (module_name, &mut default_log_level) {
(Some(module_name), _) => {
module_levels.insert(module_name, module_log_level);
}
(None, Some(_)) => {
eprintln!(
"logger warning: can't set default level twice, '{}' ignored",
module_log_level
);
}
(None, w) => *w = Some(module_log_level),
}
}
Self {
default_log_level,
module_levels,
}
}
pub(crate) fn module_level(&self, module_name: &str) -> Option<LevelFilter> {
self.module_levels
.get(module_name)
.map_or_else(|| self.default_log_level, |l| Some(*l))
}
}

View File

@ -0,0 +1,23 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mod logger_filter;
mod log_utf8_string_impl;
pub const WASM_LOG_ENV_NAME: &str = "WASM_LOG";
pub(crate) use logger_filter::LoggerFilter;
pub(crate) use log_utf8_string_impl::log_utf8_string_closure;

View File

@ -16,7 +16,6 @@
mod ivalues_to_json;
mod json_to_ivalues;
mod log_utf8_string_impl;
mod modules_load_strategy;
mod utils;
@ -26,5 +25,3 @@ pub(crate) use modules_load_strategy::ModulesLoadStrategy;
pub(crate) use utils::create_host_import;
pub(crate) use utils::make_fce_config;
pub(crate) use utils::load_modules_from_fs;
pub(self) use log_utf8_string_impl::log_utf8_string_closure;

View File

@ -14,10 +14,12 @@
* limitations under the License.
*/
use super::log_utf8_string_closure;
use crate::Result;
use crate::config::FaaSModuleConfig;
use crate::errors::FaaSError;
use crate::logger::log_utf8_string_closure;
use crate::logger::LoggerFilter;
use crate::logger::WASM_LOG_ENV_NAME;
use fce::FCEModuleConfig;
use fce::HostImportDescriptor;
@ -83,6 +85,7 @@ pub(crate) fn make_fce_config(
module_name: String,
faas_module_config: Option<FaaSModuleConfig>,
call_parameters: Rc<RefCell<fluence_sdk_main::CallParameters>>,
logger_filter: &LoggerFilter<'_>
) -> Result<FCEModuleConfig> {
let mut fce_module_config = FCEModuleConfig::default();
@ -123,6 +126,20 @@ pub(crate) fn make_fce_config(
let mut namespace = Namespace::new();
if faas_module_config.logger_enabled {
if let Some(level_filter) = logger_filter.module_level(&module_name) {
let log_level = level_filter.to_level();
let log_level_str = match log_level {
Some(log_level) => log_level.to_string(),
None => String::from("off")
};
// overwrite possibly installed log variable in config
fce_module_config.wasi_envs.insert(
WASM_LOG_ENV_NAME.as_bytes().to_owned(),
log_level_str.into_bytes(),
);
}
let logging_mask = faas_module_config.logging_mask;
namespace.insert(
"log_utf8_string",
@ -190,3 +207,4 @@ pub(crate) fn load_modules_from_fs(
Ok(loaded)
}