sycamore_web/
macros.rs

1/// Log a message to the JavaScript console if on wasm32. Otherwise logs it to stdout.
2///
3/// Note: this does not work properly for server-side WASM since it will mistakenly try to log to
4/// the JS console.
5#[macro_export]
6macro_rules! console_log {
7    ($($arg:tt)*) => {
8        if $crate::is_not_ssr!() {
9            $crate::rt::web_sys::console::log_1(&::std::format!($($arg)*).into());
10        } else {
11            ::std::println!($($arg)*);
12        }
13    };
14}
15
16/// Log a warning to the JavaScript console if on wasm32. Otherwise logs it to stderr.
17///
18/// Note: this does not work properly for server-side WASM since it will mistakenly try to log to
19/// the JS console.
20#[macro_export]
21macro_rules! console_warn {
22    ($($arg:tt)*) => {
23        if $crate::is_not_ssr!() {
24            $crate::rt::web_sys::console::warn_1(&::std::format!($($arg)*).into());
25        } else {
26            ::std::eprintln!($($arg)*);
27        }
28    };
29}
30
31/// Prints an error message to the JavaScript console if on wasm32. Otherwise logs it to stderr.
32///
33/// Note: this does not work properly for server-side WASM since it will mistakenly try to log to
34/// the JS console.
35#[macro_export]
36macro_rules! console_error {
37    ($($arg:tt)*) => {
38        if $crate::is_not_ssr!() {
39            $crate::rt::web_sys::console::error_1(&::std::format!($($arg)*).into());
40        } else {
41            ::std::eprintln!($($arg)*);
42        }
43    };
44}
45
46/// Debug the value of a variable to the JavaScript console if on wasm32. Otherwise logs it to
47/// stdout.
48///
49/// Note: this does not work properly for server-side WASM since it will mistakenly try to log to
50/// the JS console.
51#[macro_export]
52macro_rules! console_dbg {
53    () => {
54        if $crate::is_not_ssr!() {
55            $crate::rt::web_sys::console::log_1(
56                &::std::format!("[{}:{}]", ::std::file!(), ::std::line!(),).into(),
57            );
58        } else {
59            ::std::dbg!($arg);
60        }
61    };
62    ($arg:expr $(,)?) => {
63        if $crate::is_not_ssr!() {
64            let arg = $arg;
65            $crate::rt::web_sys::console::log_1(
66                &::std::format!(
67                    "[{}:{}] {} = {:#?}",
68                    ::std::file!(),
69                    ::std::line!(),
70                    ::std::stringify!($arg),
71                    arg
72                )
73                .into(),
74            );
75            arg
76        } else {
77            ::std::dbg!($arg)
78        }
79    };
80    ($($arg:expr),+ $(,)?) => {
81        $($crate::console_dbg!($arg);)+
82    }
83}