-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/logging #86
Merged
Merged
Feature/logging #86
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d1f872
Adding a USB-based log output
ryan-summers 4e4d6b9
Adding USB serial logger
ryan-summers 14b66b4
Simplifying logger with mpmc
ryan-summers 2aba08f
Removing debug prints
ryan-summers 256d3d7
Updating log overflow message
ryan-summers 9eb70c0
Fixing format
ryan-summers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//! Booster NGFW logging utilities | ||
//! | ||
//! # Copyright | ||
//! Copyright (C) 2020 QUARTIQ GmbH - All Rights Reserved | ||
//! Unauthorized usage, editing, or copying is strictly prohibited. | ||
//! Proprietary and confidential. | ||
use heapless::{consts, String}; | ||
|
||
use super::SerialTerminal; | ||
use core::fmt::Write; | ||
|
||
/// A logging buffer for storing serialized logs pending transmission. | ||
/// | ||
/// # Notes | ||
/// The LoBufferedLog contains a character buffer of the log data waiting to be written. It is | ||
/// intended to be consumed asynchronously. In the case of booster, this log data is consumed in the | ||
/// USB task. | ||
pub struct BufferedLog { | ||
buffer: core::cell::UnsafeCell<LogBuffer>, | ||
} | ||
|
||
// Critical sections are used to manage the buffer access. This type is safe to share across contexts. | ||
unsafe impl Sync for BufferedLog {} | ||
|
||
impl BufferedLog { | ||
/// Construct a new buffered log object. | ||
pub const fn new() -> Self { | ||
Self { | ||
buffer: core::cell::UnsafeCell::new(LogBuffer::new()), | ||
} | ||
} | ||
|
||
/// Process all of the available log data. | ||
/// | ||
/// # Args | ||
/// * `terminal` - The serial terminal to write log data into. | ||
pub fn process(&self, terminal: &mut SerialTerminal) { | ||
cortex_m::interrupt::free(|_cs| { | ||
let buffer = unsafe { &mut *self.buffer.get() }; | ||
terminal.write(buffer.data()); | ||
buffer.clear(); | ||
}); | ||
} | ||
} | ||
|
||
impl log::Log for BufferedLog { | ||
fn enabled(&self, _metadata: &log::Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
let source_file = record.file().unwrap_or("Unknown"); | ||
let source_line = record.line().unwrap_or(u32::MAX); | ||
|
||
// Print the record into the buffer. | ||
let mut string: String<consts::U128> = String::new(); | ||
match write!( | ||
&mut string, | ||
"[{}] {}:{} - {}\n", | ||
record.level(), | ||
source_file, | ||
source_line, | ||
record.args() | ||
) { | ||
Err(_) => warn!("Log buffer overflow"), | ||
ryan-summers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ => {} | ||
}; | ||
|
||
cortex_m::interrupt::free(|_cs| { | ||
let buffer = unsafe { &mut *self.buffer.get() }; | ||
buffer.append(&string.into_bytes()); | ||
}); | ||
} | ||
|
||
// The log is not capable of being flushed as it does not own the data consumer. | ||
fn flush(&self) {} | ||
} | ||
|
||
/// An internal buffer for storing serialized log data. This is essentially a vector of u8. | ||
struct LogBuffer { | ||
data: [u8; 1024], | ||
index: usize, | ||
} | ||
|
||
impl LogBuffer { | ||
/// Construct the buffer. | ||
pub const fn new() -> Self { | ||
Self { | ||
data: [0u8; 1024], | ||
index: 0, | ||
} | ||
} | ||
|
||
/// Append data into the buffer. | ||
/// | ||
/// # Args | ||
/// * `data` - The data to append. If space isn't available, as much data will be appended as | ||
/// possible. | ||
pub fn append(&mut self, data: &[u8]) { | ||
let tail = &mut self.data[self.index..]; | ||
self.index += if data.len() > tail.len() { | ||
tail.copy_from_slice(&data[..tail.len()]); | ||
tail.len() | ||
} else { | ||
tail[..data.len()].copy_from_slice(data); | ||
data.len() | ||
} | ||
} | ||
|
||
/// Get the data in the buffer. | ||
pub fn data(&self) -> &[u8] { | ||
&self.data[..self.index] | ||
} | ||
|
||
/// Clear contents of the buffer. | ||
pub fn clear(&mut self) { | ||
self.index = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this take quite a while?
It's in a critical section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This boils down into a copy into the USB serial output port buffer, so the actual blocking time should just be the amount of time to copy the data - the output isn't actually transmitted over USB here.