add level & target to log_utf8_string

This commit is contained in:
folex 2020-10-23 16:35:25 +03:00
parent f0c382e640
commit 18d435c643
2 changed files with 57 additions and 29 deletions

48
Cargo.lock generated
View File

@ -707,15 +707,6 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "fluence"
version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"fluence-sdk-macro 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence-sdk-main 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]] [[package]]
name = "fluence" name = "fluence"
version = "0.2.8" version = "0.2.8"
@ -726,6 +717,15 @@ dependencies = [
"fluence-sdk-main 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "fluence-sdk-main 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "fluence"
version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"fluence-sdk-macro 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
"fluence-sdk-main 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]] [[package]]
name = "fluence-app-service" name = "fluence-app-service"
version = "0.1.10" version = "0.1.10"
@ -763,14 +763,6 @@ dependencies = [
"wasmer-wasi-fl", "wasmer-wasi-fl",
] ]
[[package]]
name = "fluence-sdk-macro"
version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"fluence-sdk-wit 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]] [[package]]
name = "fluence-sdk-macro" name = "fluence-sdk-macro"
version = "0.2.8" version = "0.2.8"
@ -781,13 +773,11 @@ dependencies = [
] ]
[[package]] [[package]]
name = "fluence-sdk-main" name = "fluence-sdk-macro"
version = "0.2.8" version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd" source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [ dependencies = [
"fluence-sdk-macro 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)", "fluence-sdk-wit 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
"log",
"serde",
] ]
[[package]] [[package]]
@ -802,9 +792,20 @@ dependencies = [
] ]
[[package]] [[package]]
name = "fluence-sdk-wit" name = "fluence-sdk-main"
version = "0.2.8" version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd" source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"fluence-sdk-macro 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
"log",
"serde",
]
[[package]]
name = "fluence-sdk-wit"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "560baf91197ded38a99a5c94ff366a3dd971ebf33f5d987ecce31d3dedf86d17"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -817,8 +818,7 @@ dependencies = [
[[package]] [[package]]
name = "fluence-sdk-wit" name = "fluence-sdk-wit"
version = "0.2.8" version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
checksum = "560baf91197ded38a99a5c94ff366a3dd971ebf33f5d987ecce31d3dedf86d17"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -17,10 +17,38 @@
use wasmer_core::vm::Ctx; use wasmer_core::vm::Ctx;
use wasmer_core::memory::ptr::{Array, WasmPtr}; use wasmer_core::memory::ptr::{Array, WasmPtr};
pub(super) fn log_utf8_string(ctx: &mut Ctx, offset: i32, size: i32) { pub(super) fn log_utf8_string(
ctx: &mut Ctx,
level: i32,
target_offset: i32,
target_size: i32,
msg_offset: i32,
msg_size: i32,
) {
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"),
}
}
#[inline]
fn read_string(ctx: &Ctx, offset: i32, size: i32) -> Option<&str> {
let wasm_ptr = WasmPtr::<u8, Array>::new(offset as _); let wasm_ptr = WasmPtr::<u8, Array>::new(offset as _);
match wasm_ptr.get_utf8_string(ctx.memory(0), size as _) { wasm_ptr.get_utf8_string(ctx.memory(0), size as _)
Some(msg) => log::info!("{}", msg), }
None => log::warn!("logger: incorrect UTF8 string's been supplied to logger"),
#[inline]
fn level_from_i32(level: i32) -> log::Level {
match level {
1 => log::Level::Error,
2 => log::Level::Warn,
3 => log::Level::Info,
4 => log::Level::Debug,
5 => log::Level::Trace,
_ => log::Level::max(),
} }
} }