leptos_dom/logging.rs
1//! Utilities for simple isomorphic logging to the console or terminal.
2
3use wasm_bindgen::JsValue;
4
5/// Uses `println!()`-style formatting to log something to the console (in the browser)
6/// or via `println!()` (if not in the browser).
7#[macro_export]
8macro_rules! log {
9 ($($t:tt)*) => ($crate::logging::console_log(&format_args!($($t)*).to_string()))
10}
11
12/// Uses `println!()`-style formatting to log warnings to the console (in the browser)
13/// or via `eprintln!()` (if not in the browser).
14#[macro_export]
15macro_rules! warn {
16 ($($t:tt)*) => ($crate::logging::console_warn(&format_args!($($t)*).to_string()))
17}
18
19/// Uses `println!()`-style formatting to log errors to the console (in the browser)
20/// or via `eprintln!()` (if not in the browser).
21#[macro_export]
22macro_rules! error {
23 ($($t:tt)*) => ($crate::logging::console_error(&format_args!($($t)*).to_string()))
24}
25
26/// Uses `println!()`-style formatting to log something to the console (in the browser)
27/// or via `println!()` (if not in the browser), but only if it's a debug build.
28#[macro_export]
29macro_rules! debug_log {
30 ($($x:tt)*) => {
31 {
32 if cfg!(debug_assertions) {
33 $crate::log!($($x)*)
34 }
35 }
36 }
37}
38
39/// Uses `println!()`-style formatting to log warnings to the console (in the browser)
40/// or via `eprintln!()` (if not in the browser), but only if it's a debug build.
41#[macro_export]
42macro_rules! debug_warn {
43 ($($x:tt)*) => {
44 {
45 if cfg!(debug_assertions) {
46 $crate::warn!($($x)*)
47 }
48 }
49 }
50}
51
52/// Uses `println!()`-style formatting to log errors to the console (in the browser)
53/// or via `eprintln!()` (if not in the browser), but only if it's a debug build.
54#[macro_export]
55macro_rules! debug_error {
56 ($($x:tt)*) => {
57 {
58 if cfg!(debug_assertions) {
59 $crate::error!($($x)*)
60 }
61 }
62 }
63}
64
65const fn log_to_stdout() -> bool {
66 cfg!(not(all(
67 target_arch = "wasm32",
68 not(any(target_os = "emscripten", target_os = "wasi"))
69 )))
70}
71
72/// Log a string to the console (in the browser)
73/// or via `println!()` (if not in the browser).
74pub fn console_log(s: &str) {
75 #[allow(clippy::print_stdout)]
76 if log_to_stdout() {
77 println!("{s}");
78 } else {
79 web_sys::console::log_1(&JsValue::from_str(s));
80 }
81}
82
83/// Log a warning to the console (in the browser)
84/// or via `eprintln!()` (if not in the browser).
85pub fn console_warn(s: &str) {
86 if log_to_stdout() {
87 eprintln!("{s}");
88 } else {
89 web_sys::console::warn_1(&JsValue::from_str(s));
90 }
91}
92
93/// Log an error to the console (in the browser)
94/// or via `eprintln!()` (if not in the browser).
95#[inline(always)]
96pub fn console_error(s: &str) {
97 if log_to_stdout() {
98 eprintln!("{s}");
99 } else {
100 web_sys::console::error_1(&JsValue::from_str(s));
101 }
102}
103
104/// Log a string to the console (in the browser)
105/// or via `println!()` (if not in the browser), but only in a debug build.
106#[inline(always)]
107pub fn console_debug_log(s: &str) {
108 if cfg!(debug_assertions) {
109 console_log(s)
110 }
111}
112
113/// Log a warning to the console (in the browser)
114/// or via `eprintln!()` (if not in the browser), but only in a debug build.
115#[inline(always)]
116pub fn console_debug_warn(s: &str) {
117 if cfg!(debug_assertions) {
118 console_warn(s)
119 }
120}
121
122/// Log an error to the console (in the browser)
123/// or via `eprintln!()` (if not in the browser), but only in a debug build.
124#[inline(always)]
125pub fn console_debug_error(s: &str) {
126 if cfg!(debug_assertions) {
127 console_error(s)
128 }
129}