add logs macro

This commit is contained in:
histausse 2022-11-20 14:09:33 +01:00
parent 1ce4ed7fe9
commit adc45f6aa2
Signed by: histausse
GPG key ID: 67486F107F62E9E9
4 changed files with 143 additions and 12 deletions

View file

@ -44,7 +44,7 @@ impl TimeManager for Timer {
}
fn sleep(&self, duration: Duration) {
if duration.as_nanos() == 0 {
if duration.is_zero() {
return;
}

121
src/log.rs Normal file
View file

@ -0,0 +1,121 @@
//! Module implementing the `print!`/`println!` macro.
/// Log an debug info
#[macro_export]
macro_rules! debug {
($string:expr) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] DEBUG: ", $string),
timestamp.as_secs(),
timestamp.subsec_micros(),
));
});
($format_string:expr, $($arg:tt)*) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] DEBUG: ", $format_string),
timestamp.as_secs(),
timestamp.subsec_micros(),
$($arg)*
));
})
}
/// Log an info
#[macro_export]
macro_rules! info {
($string:expr) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] INFO: ", $string),
timestamp.as_secs(),
timestamp.subsec_micros(),
));
});
($format_string:expr, $($arg:tt)*) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] INFO: ", $format_string),
timestamp.as_secs(),
timestamp.subsec_micros(),
$($arg)*
));
})
}
/// Log an warning
#[macro_export]
macro_rules! warn {
($string:expr) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] WARN: ", $string),
timestamp.as_secs(),
timestamp.subsec_micros(),
));
});
($format_string:expr, $($arg:tt)*) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] WARN: ", $format_string),
timestamp.as_secs(),
timestamp.subsec_micros(),
$($arg)*
));
})
}
/// Log an error
#[macro_export]
macro_rules! error {
($string:expr) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] ERR: ", $string),
timestamp.as_secs(),
timestamp.subsec_micros(),
));
});
($format_string:expr, $($arg:tt)*) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] ERR: ", $format_string),
timestamp.as_secs(),
timestamp.subsec_micros(),
$($arg)*
));
})
}
/// Log a fatal error
#[macro_export]
macro_rules! fatal {
($string:expr) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] FATAL: ", $string),
timestamp.as_secs(),
timestamp.subsec_micros(),
));
});
($format_string:expr, $($arg:tt)*) => ({
use $crate::traits::time::TimeManager;
let timestamp = $crate::time_manager().uptime();
$crate::print::_print(format_args_nl!(
concat!("[{:>6}.{:06}] FATAL: ", $format_string),
timestamp.as_secs(),
timestamp.subsec_micros(),
$($arg)*
));
})
}

View file

@ -20,6 +20,8 @@ mod utils;
mod panic;
#[cfg(not(test))]
mod print;
#[cfg(not(test))]
mod log;
#[cfg(not(test))]
use core::arch::global_asm;
@ -74,6 +76,12 @@ pub unsafe fn _start_rust() -> ! {
bsp::rpi3::uart::init();
println!("Hello there");
debug!("debug");
info!("info");
warn!("warn");
error!("error");
fatal!("fatal");
let mut buffer = ['X'; 200];
let mut i = 0;
let mut c = console().read_char();
@ -95,22 +103,23 @@ pub unsafe fn _start_rust() -> ! {
println!("");
match gpio::set_pin_fonction(21, gpio::PinFunction::Output) {
Ok(()) => println!("Successfully set pin to output"),
Err(err) => println!("Failled to set pin: {err}"),
Ok(()) => info!("Successfully set pin to output"),
Err(err) => warn!("Failled to set pin: {}", err),
}
loop {
let uptime = time_manager().uptime();
match gpio::set_pin_output_state(21, gpio::PinOutputState::High) {
Ok(()) => println!("[{:>6}.{:06}] OUTPUT 21 UP", uptime.as_secs(), uptime.subsec_micros()),
Err(_err) => println!("Failled to set pin 21 to High"),
Ok(()) => info!("OUTPUT 21 UP"),
Err(_err) => warn!("Failled to set pin 21 to High"),
}
time_manager().sleep(Duration::from_secs(1));
let uptime = time_manager().uptime();
match gpio::set_pin_output_state(21, gpio::PinOutputState::Low) {
Ok(()) => println!("[{:>6}.{:06}] OUTPUT 21 DOWN", uptime.as_secs(), uptime.subsec_micros()),
Err(_err) => println!("Failled to set pin 21 to Low"),
Ok(()) => info!("OUTPUT 21 DOWN"),
Err(_err) => warn!("Failled to set pin 21 to Low"),
}
time_manager().sleep(Duration::from_secs(1));
if time_manager().uptime().as_secs() >= 60 {
panic!("Times out");
}
}
/*
println!("Hello there");

View file

@ -2,7 +2,7 @@
use core::panic::PanicInfo;
use crate::println;
use crate::fatal;
use crate::wait_forever;
/// Avoid nested panic
@ -32,10 +32,11 @@ fn panic(info: &PanicInfo) -> ! {
_ => ("???", 0, 0),
};
println!(
fatal!(
"Kernel panic!\n\n\
Panic location:\n File: '{location}', line {line}, column {column}\n\n\
Panic location:\n File: '{}', line {}, column {}\n\n\
{}",
location, line, column,
info.message().unwrap_or(&format_args!(""))
);
wait_forever()