Force logging of panic messages

This commit is contained in:
Fabian 2018-08-20 17:38:27 -05:00
parent d67b91b895
commit d9282afec5
4 changed files with 53 additions and 12 deletions

View file

@ -258,6 +258,10 @@ function V86Starter(options)
const str = v86util.read_sized_string_from_mem(v86oxide.exports.memory, offset, len); const str = v86util.read_sized_string_from_mem(v86oxide.exports.memory, offset, len);
dbg_log(str, LOG_CPU); dbg_log(str, LOG_CPU);
}, },
"console_log_from_wasm": function(offset, len) {
const str = v86util.read_sized_string_from_mem(v86oxide.exports.memory, offset, len);
console.error(str);
},
"codegen_finalize": (wasm_table_index, start, end, first_opcode, state_flags) => cpu.codegen_finalize(wasm_table_index, start, end, first_opcode, state_flags), "codegen_finalize": (wasm_table_index, start, end, first_opcode, state_flags) => cpu.codegen_finalize(wasm_table_index, start, end, first_opcode, state_flags),
"__indirect_function_table": wasm_table, "__indirect_function_table": wasm_table,
"floor": Math.floor, "floor": Math.floor,

View file

@ -22,14 +22,14 @@ pub fn rust_setup() {
panic::set_hook(Box::new(|panic_info| { panic::set_hook(Box::new(|panic_info| {
if let Some(location) = panic_info.location() { if let Some(location) = panic_info.location() {
dbg_log!( console_log!(
"panic occurred in file '{}' at line {}", "panic occurred in file '{}' at line {}",
location.file(), location.file(),
location.line() location.line()
); );
} }
else { else {
dbg_log!("panic occurred but can't get location information..."); console_log!("panic occurred but can't get location information...");
} }
})); }));
} }

View file

@ -8,6 +8,16 @@ macro_rules! dbg_log {
} }
} }
#[allow(unused_macros)]
macro_rules! console_log {
($fmt:expr) => {
println!($fmt);
};
($fmt:expr, $($arg:tt)*) => {
println!($fmt, $($arg)*);
}
}
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! dbg_assert { macro_rules! dbg_assert {
($($arg:tt)*) => { ($($arg:tt)*) => {
@ -17,17 +27,34 @@ macro_rules! dbg_assert {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! dbg_log { macro_rules! console_log {
($fmt:expr) => { ($fmt:expr) => {
{ {
use ::util::{ DEBUG, _log_to_js_console }; use ::util::{ console_log_to_js_console };
if DEBUG { _log_to_js_console($fmt); } console_log_to_js_console($fmt);
} }
}; };
($fmt:expr, $($arg:tt)*) => { ($fmt:expr, $($arg:tt)*) => {
{ {
use ::util::{ DEBUG, _log_to_js_console }; use ::util::{ console_log_to_js_console };
if DEBUG { _log_to_js_console(format!($fmt, $($arg)*)); } console_log_to_js_console(format!($fmt, $($arg)*));
}
};
}
#[cfg(target_arch = "wasm32")]
#[allow(unused_macros)]
macro_rules! dbg_log {
($fmt:expr) => {
{
use ::util::{ DEBUG, log_to_js_console };
if DEBUG { log_to_js_console($fmt); }
}
};
($fmt:expr, $($arg:tt)*) => {
{
use ::util::{ DEBUG, log_to_js_console };
if DEBUG { log_to_js_console(format!($fmt, $($arg)*)); }
} }
}; };
} }
@ -36,9 +63,9 @@ macro_rules! dbg_log {
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! dbg_assert { macro_rules! dbg_assert {
($cond:expr) => {{ ($cond:expr) => {{
use util::{_log_to_js_console, abort, DEBUG}; use util::{abort, log_to_js_console, DEBUG};
if DEBUG && !$cond { if DEBUG && !$cond {
_log_to_js_console(format!( log_to_js_console(format!(
"Assertion failed at {}:{}:{}: '{}'", "Assertion failed at {}:{}:{}: '{}'",
file!(), file!(),
line!(), line!(),
@ -52,9 +79,9 @@ macro_rules! dbg_assert {
} }
}}; }};
($cond:expr, $desc:expr) => {{ ($cond:expr, $desc:expr) => {{
use util::{_log_to_js_console, abort, DEBUG}; use util::{abort, log_to_js_console, DEBUG};
if DEBUG && !$cond { if DEBUG && !$cond {
_log_to_js_console(format!( log_to_js_console(format!(
"Assertion failed at {}:{}:{}: '{}' - '{}'", "Assertion failed at {}:{}:{}: '{}' - '{}'",
file!(), file!(),
line!(), line!(),

View file

@ -77,6 +77,7 @@ pub const DEBUG: bool = cfg!(debug_assertions);
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
extern "C" { extern "C" {
pub fn log_from_wasm(ptr: *const u8, len: usize); pub fn log_from_wasm(ptr: *const u8, len: usize);
pub fn console_log_from_wasm(ptr: *const u8, len: usize);
pub fn abort(); pub fn abort();
} }
@ -84,10 +85,19 @@ extern "C" {
use std::string::ToString; use std::string::ToString;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub fn _log_to_js_console<T: ToString>(s: T) { pub fn log_to_js_console<T: ToString>(s: T) {
let s: String = s.to_string(); let s: String = s.to_string();
let len = s.len(); let len = s.len();
unsafe { unsafe {
log_from_wasm(s.as_bytes().as_ptr(), len); log_from_wasm(s.as_bytes().as_ptr(), len);
} }
} }
#[cfg(target_arch = "wasm32")]
pub fn console_log_to_js_console<T: ToString>(s: T) {
let s: String = s.to_string();
let len = s.len();
unsafe {
console_log_from_wasm(s.as_bytes().as_ptr(), len);
}
}