1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/// A macro similar to [`dbg!`] that logs [`JsValue`][wasm_bindgen::JsValue]s to console.
///
/// See the [stdlib documentation][std::dbg] to learn more. This macro calls `console.log`
/// instead of `eprintln!` for `JsValue`s. The formatting is done by the browser. If you want
/// [`Debug`][std::fmt::Debug] implementation to be used instead, consider using [`console_dbg`]
#[macro_export]
macro_rules! console {
() => {
$crate::log!(
::std::format!("%c[{}:{}] ", ::std::file!(), ::std::line!()),
"font-weight: bold"
);
};
($val:expr $(,)?) => {
{
let v = $val;
$crate::__console_inner!(v $val)
}
};
($($val:expr),+ $(,)?) => {
($($crate::console!($val)),+,)
};
}
/// A macro similar to [`dbg!`] to log to browser console.
///
/// See the [stdlib documentation][std::dbg] to learn more. This macro calls `console.log`
/// instead of `eprintln!`. This macro passing the values to [`console`] after formatting them using
/// the [`Debug`][std::fmt::Debug] implementation.
#[macro_export]
macro_rules! console_dbg {
() => {
$crate::console!()
};
($val:expr $(,)?) => {
{
let v: $crate::__macro::JsValue = ::std::format!("{:?}", $val).into();
$crate::__console_inner!(v $val)
}
};
($($val:expr),+ $(,)?) => {
($($crate::console_dbg!($val)),+,)
};
}
/// This is an implementation detail and *should not* be called directly!
#[doc(hidden)]
#[macro_export]
macro_rules! __console_inner {
($js_value:ident $val:expr) => {{
$crate::log!(
::std::format!("%c[{}:{}] ", ::std::file!(), ::std::line!()),
"font-weight: bold",
::std::format!("{} = ", ::std::stringify!($val)),
&$js_value
);
$js_value
}};
}
#[cfg(test)]
mod tests {
#![allow(dead_code)]
//! These exist to ensure code compiles
use wasm_bindgen::JsValue;
fn console_works() {
console!();
{
let js_value = JsValue::from("test");
console!(js_value);
}
{
let js_value_1 = JsValue::from("test 1");
let js_value_2 = JsValue::from("test 2");
console!(js_value_1, js_value_2);
}
}
fn console_dbg_works() {
#[derive(Debug)]
struct Value(&'static str);
console_dbg!();
{
let value = Value("test");
console_dbg!(value);
}
{
let value_1 = Value("test 1");
let value_2 = Value("test 2");
console_dbg!(value_1, value_2);
}
}
}