This commit is contained in:
vms 2020-10-06 12:32:15 +03:00
parent ab438b33d0
commit c53f03ad06
2 changed files with 16 additions and 14 deletions

View File

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

View File

@ -144,12 +144,7 @@ impl log::Log for WasmLogger {
record.args()
);
// this allows building sdk for x86_64
if cfg!(target_arch = "wasm32") {
unsafe { log_utf8_string(log_msg.as_ptr() as i32, log_msg.len() as i32) };
} else {
println!("{}", log_msg);
}
log_utf8_string(log_msg.as_ptr() as _, log_msg.len() as _);
}
// in our case flushing is performed by the VM itself
@ -157,10 +152,22 @@ impl log::Log for WasmLogger {
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) {
let msg = unsafe { from_utf8_unchecked(core::slice::from_raw_parts(ptr as _, size as _)) };
println!("{}", msg);
}
/// log_utf8_string should be provided directly by a host.
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "host")]
extern "C" {
// 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);
}