Add in-memory logging, retrievable from sys:log

This commit is contained in:
Jeremy Soller
2019-03-17 09:31:34 -06:00
parent f7c9712977
commit d2095d8d0f
6 changed files with 70 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
use alloc::collections::VecDeque;
use spin::Mutex;
pub static LOG: Mutex<Option<Log>> = Mutex::new(None);
pub fn init() {
*LOG.lock() = Some(Log::new(1024 * 1024));
}
pub struct Log {
data: VecDeque<u8>,
size: usize,
}
impl Log {
pub fn new(size: usize) -> Log {
Log {
data: VecDeque::with_capacity(size),
size: size
}
}
pub fn read(&self) -> (&[u8], &[u8]) {
self.data.as_slices()
}
pub fn write(&mut self, buf: &[u8]) {
for &b in buf {
while self.data.len() + 1 >= self.size {
self.data.pop_front();
}
self.data.push_back(b);
}
}
}