1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::ll_api::ll_cmd::*;

#[cfg(all(feature = "print-log", not(feature = "print-log-ufmt")))]
#[macro_export]
macro_rules! println {
    ($($arg:tt)*) => {{
        {
            use core::fmt::Write;
            writeln!($crate::print::Printer, $($arg)*).ok();
        }
    }};
}

#[cfg(all(feature = "print-log", feature = "print-log-ufmt"))]
#[macro_export]
macro_rules! println {
    ($($arg:tt)*) => {{
        {
            ufmt::uwriteln!($crate::print::Printer, $($arg)*).ok();
        }
    }};
}

#[cfg(all(feature = "print-log", not(feature = "print-log-ufmt")))]
#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {{
        {
            use core::fmt::Write;
            write!($crate::print::Printer, $($arg)*).ok();
        }
    }};
}

#[cfg(all(feature = "print-log", feature = "print-log-ufmt"))]
#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {{
        {
            ufmt::uwrite!($crate::print::Printer, $($arg)*).ok();
        }
    }};
}


#[cfg(not(feature = "print-log"))]
#[macro_export]
macro_rules! println {
    ($($arg:tt)*) => {{}};
}

#[cfg(not(feature = "print-log"))]
#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {{}};
}

pub struct Printer;

impl core::fmt::Write for Printer {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        let bytes = s.as_bytes();
        unsafe { ll_invoke(INVOKE_ID_LOG_PUTS, bytes.as_ptr(), bytes.len()) };

        Ok(())
    }
}

#[cfg(feature = "print-log-ufmt")]
impl ufmt::uWrite for Printer {
    type Error = core::convert::Infallible;

    fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
        let bytes = s.as_bytes();
        unsafe { ll_invoke(INVOKE_ID_LOG_PUTS, bytes.as_ptr(), bytes.len()) };
        
        Ok(())
    }
}