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"; version = "0.1.0";
src = ./src; src = ./src;
cargoSha256 = "1q66h2avk0gvw8k07jjazzv1f3n1p4y11vkc15c6s31pcapz3d81"; cargoSha256 = "0q0xy5f1qzg6iifx6zqcfrs5l33ylhm4h2ii19l4c978l4vd3k5y";
nativeBuildInputs = [ nativeBuildInputs = [
pkgs.gnumake pkgs.gnumake

10
src/Cargo.lock generated
View File

@ -200,7 +200,7 @@ dependencies = [
[[package]] [[package]]
name = "libasync" name = "libasync"
version = "0.0.0" 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 = [ dependencies = [
"embedded-hal", "embedded-hal",
"libcortex_a9", "libcortex_a9",
@ -212,7 +212,7 @@ dependencies = [
[[package]] [[package]]
name = "libboard_zynq" name = "libboard_zynq"
version = "0.0.0" 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 = [ dependencies = [
"bit_field", "bit_field",
"embedded-hal", "embedded-hal",
@ -236,7 +236,7 @@ dependencies = [
[[package]] [[package]]
name = "libcortex_a9" name = "libcortex_a9"
version = "0.0.0" 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 = [ dependencies = [
"bit_field", "bit_field",
"libregister", "libregister",
@ -245,7 +245,7 @@ dependencies = [
[[package]] [[package]]
name = "libregister" name = "libregister"
version = "0.0.0" 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 = [ dependencies = [
"bit_field", "bit_field",
"vcell", "vcell",
@ -255,7 +255,7 @@ dependencies = [
[[package]] [[package]]
name = "libsupport_zynq" name = "libsupport_zynq"
version = "0.0.0" 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 = [ dependencies = [
"compiler_builtins", "compiler_builtins",
"libboard_zynq", "libboard_zynq",

View File

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

View File

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