emscripten_functions/console.rs
1//! Select functions (with rust-native parameter types) from the emscripten [`console.h`] [header file].
2//!
3//! [`console.h`]: https://github.com/emscripten-core/emscripten/blob/main/site/source/docs/api_reference/console.h.rst
4//! [header file]: https://github.com/emscripten-core/emscripten/blob/main/system/include/emscripten/console.h
5
6use std::ffi::CString;
7
8use emscripten_functions_sys::console;
9
10/// Prints the given string using the [`console.log()`] JS function.
11///
12/// [`console.log()`]: https://developer.mozilla.org/en-US/docs/Web/API/console/log
13///
14/// # Arguments
15/// * `string` - The string to print.
16///
17/// # Examples
18/// ```rust
19/// log("Hello, world!");
20/// log(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
21/// ```
22pub fn log<T>(string: T)
23where
24 T: AsRef<str>,
25{
26 let cstring = CString::new(string.as_ref()).unwrap();
27 unsafe {
28 console::emscripten_console_log(cstring.as_ptr());
29 }
30}
31
32/// Prints the given string using the [`console.warn()`] JS function.
33///
34/// [`console.warn()`]: https://developer.mozilla.org/en-US/docs/Web/API/console/warn
35///
36/// # Arguments
37/// * `string` - The string to print.
38///
39/// # Examples
40/// ```rust
41/// warn("Hello, world!");
42/// warn(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
43/// ```
44pub fn warn<T>(string: T)
45where
46 T: AsRef<str>,
47{
48 let cstring = CString::new(string.as_ref()).unwrap();
49 unsafe {
50 console::emscripten_console_warn(cstring.as_ptr());
51 }
52}
53
54/// Prints the given string using the [`console.error()`] function.
55///
56/// [`console.error()`]: https://developer.mozilla.org/en-US/docs/Web/API/console/error
57///
58/// # Arguments
59/// * `string` - The string to print.
60///
61/// # Examples
62/// ```rust
63/// error("Hello, world!");
64/// error(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
65/// ```
66pub fn error<T>(string: T)
67where
68 T: AsRef<str>,
69{
70 let cstring = CString::new(string.as_ref()).unwrap();
71 unsafe {
72 console::emscripten_console_error(cstring.as_ptr());
73 }
74}
75
76/// Prints the given string using the emscripten-defined `out()` JS function.
77///
78/// # Arguments
79/// * `string` - The string to print.
80///
81/// # Examples
82/// ```rust
83/// out("Hello, world!");
84/// out(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
85/// ```
86pub fn out<T>(string: T)
87where
88 T: AsRef<str>,
89{
90 let cstring = CString::new(string.as_ref()).unwrap();
91 unsafe {
92 console::emscripten_out(cstring.as_ptr());
93 }
94}
95
96/// Prints the given string using the emscripten-defined `err()` JS function.
97///
98/// # Arguments
99/// * `string` - The string to print.
100///
101/// # Examples
102/// ```rust
103/// err("Hello, world!");
104/// err(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
105/// ```
106pub fn err<T>(string: T)
107where
108 T: AsRef<str>,
109{
110 let cstring = CString::new(string.as_ref()).unwrap();
111 unsafe {
112 console::emscripten_err(cstring.as_ptr());
113 }
114}
115
116/// Prints the given string using the emscripten-defined `dbg()` JS function.
117///
118/// # Arguments
119/// * `string` - The string to print.
120///
121/// # Examples
122/// ```rust
123/// dbg("Hello, world!");
124/// dbg(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
125/// ```
126pub fn dbg<T>(string: T)
127where
128 T: AsRef<str>,
129{
130 let cstring = CString::new(string.as_ref()).unwrap();
131 unsafe {
132 console::emscripten_dbg(cstring.as_ptr());
133 }
134}