wasm4fun-log 0.1.0

Logging functions and macros for WASM-4 fantasy console
Documentation
// Copyright Claudio Mattera 2022.
//
// Distributed under the MIT License or the Apache 2.0 License at your option.
// See the accompanying files License-MIT.txt and License-Apache-2.0.txt, or
// online at
// https://opensource.org/licenses/MIT
// https://opensource.org/licenses/Apache-2.0

/// Format and write text to the WASM-4 debug console
///
/// # Panics
///
/// The macro panics if the formatted string is larger than 100 bytes.
///
///
/// # Undefined Behaviour
///
/// The behaviour is undefined is the formatted string is not valid UTF-8.
///
///
/// # Examples
///
/// ```no_run
/// use wasm4fun_log::debug;
///
/// let h = 12;
/// let pi = 3.14;
/// debug!("There are {} hours in a day, and pi is {}", h, pi);
/// ```
#[cfg(feature = "debug")]
#[macro_export]
macro_rules! debug {
    ( $format:expr $(, $arg:expr)* ) => {
        {
            use wasm4fun_log::{trace, Cursor, Write};

            let mut string_buffer: [u8; 100] = [0; 100];
            let mut cursor = Cursor::new(&mut string_buffer[..]);
            write!(&mut cursor, $format, $($arg,)*)
                .expect("!write");
            let ending = cursor.position() as usize;
            let raw = &string_buffer[..ending];
            let s = unsafe { core::str::from_utf8_unchecked(raw) };
            trace(s);
        }
    };
}

/// Pretend to format and write text to the WASM-4 debug console
///
/// This is the definition of `debug!` macro in case debugging is disabled.
#[cfg(not(feature = "debug"))]
#[macro_export]
macro_rules! debug {
    ( $format:expr $(, $arg:expr)* ) => {
        {
            $(
                let _ = $arg;
            )*
        }
    }
}