Skip to content

Commit 391e2eb

Browse files
committed
feat: add bindgen to console log
1 parent 7e72ec1 commit 391e2eb

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/lib.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,41 @@ pub mod pages;
66

77
use wasm_bindgen::prelude::wasm_bindgen;
88

9+
#[wasm_bindgen]
10+
extern "C" {
11+
#[wasm_bindgen(js_namespace = console)]
12+
fn log(msg: String);
13+
14+
#[wasm_bindgen(js_namespace = console)]
15+
fn warn(msg: String);
16+
17+
#[wasm_bindgen(js_namespace = console)]
18+
fn error(msg: String);
19+
}
20+
21+
#[macro_export]
22+
macro_rules! log {
23+
($($t:tt)*) => {
24+
if cfg!(debug_assertions) {
25+
log(format_args!($($t)*).to_string())
26+
}
27+
}
28+
}
29+
30+
#[macro_export]
31+
macro_rules! warn {
32+
($($t:tt)*) => (warn(format_args!($($t)*).to_string()))
33+
}
34+
35+
#[macro_export]
36+
macro_rules! error {
37+
($($t:tt)*) => (error(format_args!($($t)*).to_string()))
38+
}
39+
940
#[wasm_bindgen]
1041
pub fn hydrate() {
11-
console_error_panic_hook::set_once();
42+
#[cfg(target_arch = "wasm32")]
43+
std::panic::set_hook(Box::new(|info: &std::panic::PanicInfo| error!("{info}")));
1244

1345
leptos::leptos_dom::HydrationCtx::stop_hydrating();
1446
}

0 commit comments

Comments
 (0)