rust-raspberrypi-OS-tutorials/11_virtual_mem_part1_identity_mapping/src/print.rs

107 lines
3.1 KiB
Rust
Raw Normal View History

2019-11-25 18:54:05 +00:00
// SPDX-License-Identifier: MIT OR Apache-2.0
2019-11-04 21:59:31 +00:00
//
2020-01-01 23:41:03 +00:00
// Copyright (c) 2018-2020 Andre Richter <andre.o.richter@gmail.com>
2019-11-04 21:59:31 +00:00
//! Printing facilities.
2020-03-28 12:24:53 +00:00
use crate::{bsp, console};
2019-11-04 21:59:31 +00:00
use core::fmt;
2020-03-28 12:24:53 +00:00
//--------------------------------------------------------------------------------------------------
2020-09-29 19:43:31 +00:00
// Public Code
2020-03-28 12:24:53 +00:00
//--------------------------------------------------------------------------------------------------
2019-12-17 12:42:20 +00:00
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
2020-03-28 12:24:53 +00:00
use console::interface::Write;
2020-03-28 12:24:53 +00:00
bsp::console::console().write_fmt(args).unwrap();
}
2019-11-04 21:59:31 +00:00
/// Prints without a newline.
///
/// Carbon copy from https://doc.rust-lang.org/src/std/macros.rs.html
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::print::_print(format_args!($($arg)*)));
}
/// Prints with a newline.
///
/// Carbon copy from https://doc.rust-lang.org/src/std/macros.rs.html
2019-11-04 21:59:31 +00:00
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ({
$crate::print::_print(format_args_nl!($($arg)*));
})
}
2020-03-28 12:24:53 +00:00
/// Prints an info, with a newline.
#[macro_export]
macro_rules! info {
2019-11-04 21:59:31 +00:00
($string:expr) => ({
#[allow(unused_imports)]
2020-03-28 12:24:53 +00:00
use crate::time::interface::TimeManager;
2019-11-04 21:59:31 +00:00
2020-03-28 12:24:53 +00:00
let timestamp = $crate::time::time_manager().uptime();
2019-11-04 21:59:31 +00:00
let timestamp_subsec_us = timestamp.subsec_micros();
$crate::print::_print(format_args_nl!(
concat!("[ {:>3}.{:03}{:03}] ", $string),
timestamp.as_secs(),
timestamp_subsec_us / 1_000,
timestamp_subsec_us % 1_000
));
});
($format_string:expr, $($arg:tt)*) => ({
#[allow(unused_imports)]
2020-03-28 12:24:53 +00:00
use crate::time::interface::TimeManager;
2019-11-04 21:59:31 +00:00
2020-03-28 12:24:53 +00:00
let timestamp = $crate::time::time_manager().uptime();
2019-11-04 21:59:31 +00:00
let timestamp_subsec_us = timestamp.subsec_micros();
$crate::print::_print(format_args_nl!(
concat!("[ {:>3}.{:03}{:03}] ", $format_string),
timestamp.as_secs(),
timestamp_subsec_us / 1_000,
timestamp_subsec_us % 1_000,
$($arg)*
));
})
}
2020-03-28 12:24:53 +00:00
/// Prints a warning, with a newline.
2019-11-04 21:59:31 +00:00
#[macro_export]
macro_rules! warn {
($string:expr) => ({
#[allow(unused_imports)]
2020-03-28 12:24:53 +00:00
use crate::time::interface::TimeManager;
2019-11-04 21:59:31 +00:00
2020-03-28 12:24:53 +00:00
let timestamp = $crate::time::time_manager().uptime();
2019-11-04 21:59:31 +00:00
let timestamp_subsec_us = timestamp.subsec_micros();
$crate::print::_print(format_args_nl!(
concat!("[W {:>3}.{:03}{:03}] ", $string),
timestamp.as_secs(),
timestamp_subsec_us / 1_000,
timestamp_subsec_us % 1_000
));
});
($format_string:expr, $($arg:tt)*) => ({
#[allow(unused_imports)]
2020-03-28 12:24:53 +00:00
use crate::time::interface::TimeManager;
2019-11-04 21:59:31 +00:00
2020-03-28 12:24:53 +00:00
let timestamp = $crate::time::time_manager().uptime();
2019-11-04 21:59:31 +00:00
let timestamp_subsec_us = timestamp.subsec_micros();
$crate::print::_print(format_args_nl!(
concat!("[W {:>3}.{:03}{:03}] ", $format_string),
timestamp.as_secs(),
timestamp_subsec_us / 1_000,
timestamp_subsec_us % 1_000,
$($arg)*
));
})
}