From adc45f6aa272e75be225395077b7e3c992de1796 Mon Sep 17 00:00:00 2001 From: Histausse Date: Sun, 20 Nov 2022 14:09:33 +0100 Subject: [PATCH] add logs macro --- src/bsp/aarch64/time.rs | 2 +- src/log.rs | 121 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 25 ++++++--- src/panic.rs | 7 ++- 4 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 src/log.rs diff --git a/src/bsp/aarch64/time.rs b/src/bsp/aarch64/time.rs index 679c742..0843a19 100644 --- a/src/bsp/aarch64/time.rs +++ b/src/bsp/aarch64/time.rs @@ -44,7 +44,7 @@ impl TimeManager for Timer { } fn sleep(&self, duration: Duration) { - if duration.as_nanos() == 0 { + if duration.is_zero() { return; } diff --git a/src/log.rs b/src/log.rs new file mode 100644 index 0000000..1218266 --- /dev/null +++ b/src/log.rs @@ -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)* + )); + }) +} diff --git a/src/main.rs b/src/main.rs index 1853252..b94c417 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); diff --git a/src/panic.rs b/src/panic.rs index bfc50f0..ab496c8 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -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()