logger: changed from RefCell to Mutex.

This commit is contained in:
pca006132 2020-07-15 17:04:16 +08:00
parent 2568d62865
commit 92405ffe91
4 changed files with 16 additions and 16 deletions

View File

@ -14,7 +14,7 @@ let
version = "0.1.0";
src = ./src;
cargoSha256 = "1q66h2avk0gvw8k07jjazzv1f3n1p4y11vkc15c6s31pcapz3d81";
cargoSha256 = "0q0xy5f1qzg6iifx6zqcfrs5l33ylhm4h2ii19l4c978l4vd3k5y";
nativeBuildInputs = [
pkgs.gnumake

10
src/Cargo.lock generated
View File

@ -200,7 +200,7 @@ dependencies = [
[[package]]
name = "libasync"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0"
dependencies = [
"embedded-hal",
"libcortex_a9",
@ -212,7 +212,7 @@ dependencies = [
[[package]]
name = "libboard_zynq"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0"
dependencies = [
"bit_field",
"embedded-hal",
@ -236,7 +236,7 @@ dependencies = [
[[package]]
name = "libcortex_a9"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0"
dependencies = [
"bit_field",
"libregister",
@ -245,7 +245,7 @@ dependencies = [
[[package]]
name = "libregister"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0"
dependencies = [
"bit_field",
"vcell",
@ -255,7 +255,7 @@ dependencies = [
[[package]]
name = "libsupport_zynq"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0"
dependencies = [
"compiler_builtins",
"libboard_zynq",

View File

@ -1,16 +1,17 @@
use core::cell::{Cell, RefCell, RefMut};
use core::cell::Cell;
use core::fmt::Write;
use log::{Log, LevelFilter};
use log_buffer::LogBuffer;
use libcortex_a9::mutex::{Mutex, MutexGuard};
use libboard_zynq::{println, timer::GlobalTimer};
pub struct LogBufferRef<'a> {
buffer: RefMut<'a, LogBuffer<&'static mut [u8]>>,
buffer: MutexGuard<'a, LogBuffer<&'static mut [u8]>>,
old_log_level: LevelFilter
}
impl<'a> LogBufferRef<'a> {
fn new(buffer: RefMut<'a, LogBuffer<&'static mut [u8]>>) -> LogBufferRef<'a> {
fn new(buffer: MutexGuard<'a, LogBuffer<&'static mut [u8]>>) -> LogBufferRef<'a> {
let old_log_level = log::max_level();
log::set_max_level(LevelFilter::Off);
LogBufferRef { buffer, old_log_level }
@ -36,7 +37,7 @@ impl<'a> Drop for LogBufferRef<'a> {
}
pub struct BufferLogger {
buffer: RefCell<LogBuffer<&'static mut [u8]>>,
buffer: Mutex<LogBuffer<&'static mut [u8]>>,
uart_filter: Cell<LevelFilter>
}
@ -45,7 +46,7 @@ static mut LOGGER: Option<BufferLogger> = None;
impl BufferLogger {
pub fn new(buffer: &'static mut [u8]) -> BufferLogger {
BufferLogger {
buffer: RefCell::new(LogBuffer::new(buffer)),
buffer: Mutex::new(LogBuffer::new(buffer)),
uart_filter: Cell::new(LevelFilter::Info),
}
}
@ -62,11 +63,10 @@ impl BufferLogger {
&mut LOGGER
}
pub fn buffer<'a>(&'a self) -> Result<LogBufferRef<'a>, ()> {
pub fn buffer<'a>(&'a self) -> Option<LogBufferRef<'a>> {
self.buffer
.try_borrow_mut()
.try_lock()
.map(LogBufferRef::new)
.map_err(|_| ())
}
pub fn uart_log_level(&self) -> LevelFilter {
@ -94,7 +94,7 @@ impl Log for BufferLogger {
let seconds = timestamp / 1_000_000;
let micros = timestamp % 1_000_000;
if let Ok(mut buffer) = self.buffer.try_borrow_mut() {
if let Some(mut buffer) = self.buffer.try_lock() {
writeln!(buffer, "[{:6}.{:06}s] {:>5}({}): {}", seconds, micros,
record.level(), record.target(), record.args()).unwrap();
}

View File

@ -17,7 +17,7 @@ where
poll_fn(|ctx| {
let logger = unsafe { BufferLogger::get_logger().as_mut().unwrap() };
match logger.buffer() {
Ok(buffer) if f(&buffer) => Poll::Ready(buffer),
Some(buffer) if f(&buffer) => Poll::Ready(buffer),
_ => {
ctx.waker().wake_by_ref();
Poll::Pending