sel4/
printing.rs

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
//
// Copyright 2023, Colias Group, LLC
// Copyright (c) 2020 Arm Limited
//
// SPDX-License-Identifier: MIT
//

use core::fmt;

use crate::sys;

/// Corresponds to `seL4_DebugPutChar`.
pub fn debug_put_char(c: u8) {
    sys::seL4_DebugPutChar(c)
}

/// Implements `core::fmt::Write` using [`debug_put_char`].
pub struct DebugWrite;

impl fmt::Write for DebugWrite {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        for &c in s.as_bytes() {
            debug_put_char(c)
        }
        Ok(())
    }
}

#[doc(hidden)]
pub fn debug_print_helper(args: fmt::Arguments) {
    fmt::write(&mut DebugWrite, args).unwrap_or_else(|err| {
        panic!("write error: {:?}", err)
    })
}

/// Like `std::debug_print!`, except backed by `seL4_DebugPutChar`.
#[macro_export]
macro_rules! debug_print {
    ($($arg:tt)*) => ($crate::_private::printing::debug_print_helper(format_args!($($arg)*)));
}

/// Like `std::debug_println!`, except backed by `seL4_DebugPutChar`.
#[macro_export]
macro_rules! debug_println {
    () => ($crate::debug_println!(""));
    ($($arg:tt)*) => ($crate::debug_print!("{}\n", format_args!($($arg)*)));
}

#[doc(hidden)]
pub mod _private {
    pub use super::debug_print_helper;
}