Merge pull request #7 from fluencelabs/logging

Allow building for x86_64 target with logging enabled
This commit is contained in:
vms 2020-10-06 13:07:24 +03:00 committed by GitHub
commit 4d6c4f6b86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -53,8 +53,8 @@ pub use result::set_result_size;
pub(crate) fn log<S: AsRef<str>>(msg: S) { pub(crate) fn log<S: AsRef<str>>(msg: S) {
// logs will be printed only if debug feature is enabled // logs will be printed only if debug feature is enabled
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
unsafe { {
let msg = msg.as_ref(); let msg = msg.as_ref();
logger::log_utf8_string(msg.as_ptr() as _, msg.len() as _); logger::log_utf8_string(msg.as_ptr() as i32, msg.len() as i32);
} }
} }

View File

@ -144,7 +144,7 @@ impl log::Log for WasmLogger {
record.args() record.args()
); );
unsafe { log_utf8_string(log_msg.as_ptr() as i32, log_msg.len() as i32) }; log_utf8_string(log_msg.as_ptr() as _, log_msg.len() as _);
} }
// in our case flushing is performed by the VM itself // in our case flushing is performed by the VM itself
@ -152,9 +152,25 @@ impl log::Log for WasmLogger {
fn flush(&self) {} fn flush(&self) {}
} }
#[cfg(target_arch = "wasm32")]
pub fn log_utf8_string(ptr: i32, size: i32) {
unsafe { log_utf8_string_impl(ptr, size) };
}
#[cfg(not(target_arch = "wasm32"))]
pub fn log_utf8_string(ptr: i32, size: i32) {
use std::str::from_utf8_unchecked;
use core::slice::from_raw_parts;
let msg = unsafe { from_utf8_unchecked(from_raw_parts(ptr as _, size as _)) };
println!("{}", msg);
}
/// log_utf8_string should be provided directly by a host. /// log_utf8_string should be provided directly by a host.
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "host")] #[link(wasm_import_module = "host")]
extern "C" { extern "C" {
// Writes a byte string of size bytes that starts from ptr to a logger // Writes a byte string of size bytes that starts from ptr to a logger
pub fn log_utf8_string(ptr: i32, size: i32); #[link_name = "log_utf8_string"]
fn log_utf8_string_impl(ptr: i32, size: i32);
} }