Skip to content

Commit

Permalink
Multi-threaded logging (#7)
Browse files Browse the repository at this point in the history
* Uart crate version upgrade
* Take lock with spin crate in order to make this logging multi-threaded
* Format change
  • Loading branch information
Azvanzed authored Jul 11, 2024
1 parent 53b6830 commit e75dbcd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ license = "MIT"
readme = "README.md"

[dependencies]
uart_16550 = "0.2"
log = { version = "0.4", default-features = false }
uart_16550 = "0.3.0"
log = { version = "0.4.22", default-features = false }
spin = "0.9.8"
25 changes: 11 additions & 14 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
use crate::serial::Serial;
use core::{
fmt::write,
format_args,
sync::atomic::{AtomicU16, Ordering},
};
use core::{fmt::write, format_args};
use log::*;
use spin::Mutex;

const COM1_PORT: u16 = 0x3f8;

static LOGGER: Logger = Logger(AtomicU16::new(COM1_PORT));
static LOGGER: Logger = Logger(Mutex::new(COM1_PORT));

struct Logger(AtomicU16);
struct Logger(Mutex<u16>);

impl log::Log for Logger {
fn enabled(&self, _m: &Metadata) -> bool {
true
}

fn log(&self, record: &Record) {
let mut serial = Serial::new(self.0.load(Ordering::Relaxed));
let port = self.0.lock();
let mut serial = Serial::new(*port);

let _ = write(
&mut serial,
format_args!(
"{:>8}: {} ({}, {}:{})\n",
"[{} {}] {}\n\0",
record.level(),
record.args(),
record.target(),
record.file().unwrap_or("<unknown>"),
record.line().unwrap_or(0),
record.module_path().unwrap_or(record.target()),
record.args()
),
);
}
Expand All @@ -37,7 +33,8 @@ impl log::Log for Logger {
}

fn set_logger_base(base: u16) {
LOGGER.0.store(base, Ordering::Relaxed);
let mut data = LOGGER.0.lock();
*data = base;
}

/// The builder for a serial port logger.
Expand Down

0 comments on commit e75dbcd

Please sign in to comment.