kernel/
macros.rs

1// implement print! and println! macros
2#[macro_export]
3macro_rules! print {
4    ($($arg:tt)*) => {
5        $crate::io::_print(format_args!($($arg)*));
6    };
7}
8
9#[macro_export]
10macro_rules! println {
11    () => ($crate::print!("\n"));
12    ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
13}
14
15// implement eprint! and eprintln! macros
16#[macro_export]
17macro_rules! eprint {
18    ($($arg:tt)*) => {
19        $crate::io::_eprint(format_args!($($arg)*));
20    };
21}
22
23#[macro_export]
24macro_rules! eprintln {
25    () => ($crate::eprint!("\n"));
26    ($($arg:tt)*) => ($crate::eprint!("{}\n", format_args!($($arg)*)));
27}
28
29// copied from `std`
30#[macro_export]
31macro_rules! dbg {
32    // NOTE: We cannot use `concat!` to make a static string as a format argument
33    // of `eprintln!` because `file!` could contain a `{` or
34    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
35    // will be malformed.
36    () => {
37        $crate::println!("[{}:{}:{}]", ::core::file!(), ::core::line!(), ::core::column!())
38    };
39    ($val:expr $(,)?) => {
40        // Use of `match` here is intentional because it affects the lifetimes
41        // of temporaries - https://stackoverflow.com/a/48732525/1063961
42        match $val {
43            tmp => {
44                $crate::println!("[{}:{}:{}] {} = {:#?}",
45                    ::core::file!(), ::core::line!(), ::core::column!(), ::core::stringify!($val), &tmp);
46                tmp
47            }
48        }
49    };
50    ($($val:expr),+ $(,)?) => {
51        ($($crate::dbg!($val)),+,)
52    };
53}
54
55// same as above, but for eprintln!
56#[macro_export]
57macro_rules! edbg {
58    () => {
59        $crate::eprintln!("[{}:{}:{}]", ::core::file!(), ::core::line!(), ::core::column!())
60    };
61    ($val:expr $(,)?) => {
62        match $val {
63            tmp => {
64                $crate::eprintln!("[{}:{}:{}] {} = {:#?}",
65                    ::core::file!(), ::core::line!(), ::core::column!(), ::core::stringify!($val), &tmp);
66                tmp
67            }
68        }
69    };
70    ($($val:expr),+ $(,)?) => {
71        ($($crate::dbg!($val)),+,)
72    };
73}