use core::any::Any;
use axfs_ng_vfs::{NodeFlags, VfsResult};
use crate::pseudofs::DeviceOps;
const DEFAULT_LEVEL: u8 = 6;
pub(crate) struct Kmsg;
impl Kmsg {
fn parse(buf: &[u8]) -> (u8, &[u8]) {
if let [b'<', rest @ ..] = buf {
let digits = rest.iter().take_while(|c| c.is_ascii_digit()).count();
if digits > 0 && rest.get(digits) == Some(&b'>') {
let mut priority: u64 = 0;
for &c in &rest[..digits] {
priority = priority
.saturating_mul(10)
.saturating_add((c - b'0') as u64);
}
return ((priority & 7) as u8, &rest[digits + 1..]);
}
}
(DEFAULT_LEVEL, buf)
}
}
impl DeviceOps for Kmsg {
fn read_at(&self, _buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
Ok(0)
}
fn write_at(&self, buf: &[u8], _offset: u64) -> VfsResult<usize> {
let (severity, msg) = Self::parse(buf);
let msg = msg.strip_suffix(b"\n").unwrap_or(msg);
let text = core::str::from_utf8(msg).unwrap_or("<kmsg: invalid utf-8>");
match severity {
0..=3 => error!("{text}"),
4 => warn!("{text}"),
5 | 6 => info!("{text}"),
_ => debug!("{text}"),
}
Ok(buf.len())
}
fn as_any(&self) -> &dyn Any {
self
}
fn flags(&self) -> NodeFlags {
NodeFlags::NON_CACHEABLE | NodeFlags::STREAM
}
}