wasm4fun_log/
debug.rs

1// Copyright Claudio Mattera 2022.
2//
3// Distributed under the MIT License or the Apache 2.0 License at your option.
4// See the accompanying files License-MIT.txt and License-Apache-2.0.txt, or
5// online at
6// https://opensource.org/licenses/MIT
7// https://opensource.org/licenses/Apache-2.0
8
9/// Format and write text to the WASM-4 debug console
10///
11/// # Panics
12///
13/// The macro panics if the formatted string is larger than 100 bytes.
14///
15///
16/// # Undefined Behaviour
17///
18/// The behaviour is undefined is the formatted string is not valid UTF-8.
19///
20///
21/// # Examples
22///
23/// ```no_run
24/// use wasm4fun_log::debug;
25///
26/// let h = 12;
27/// let pi = 3.14;
28/// debug!("There are {} hours in a day, and pi is {}", h, pi);
29/// ```
30#[cfg(feature = "debug")]
31#[macro_export]
32macro_rules! debug {
33    ( $format:expr $(, $arg:expr)* ) => {
34        {
35            use wasm4fun_log::{trace, Cursor, Write};
36
37            let mut string_buffer: [u8; 100] = [0; 100];
38            let mut cursor = Cursor::new(&mut string_buffer[..]);
39            write!(&mut cursor, $format, $($arg,)*)
40                .expect("!write");
41            let ending = cursor.position() as usize;
42            let raw = &string_buffer[..ending];
43            let s = unsafe { core::str::from_utf8_unchecked(raw) };
44            trace(s);
45        }
46    };
47}
48
49/// Pretend to format and write text to the WASM-4 debug console
50///
51/// This is the definition of `debug!` macro in case debugging is disabled.
52#[cfg(not(feature = "debug"))]
53#[macro_export]
54macro_rules! debug {
55    ( $format:expr $(, $arg:expr)* ) => {
56        {
57            $(
58                let _ = $arg;
59            )*
60        }
61    }
62}