Skip to content

Commit

Permalink
log: hide malformed message by default
Browse files Browse the repository at this point in the history
  • Loading branch information
surban committed Nov 3, 2024
1 parent 0c718d9 commit 45a96de
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion openemc-log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "openemc-log"
description = "OpenEMC Logger"
authors = ["Sebastian Urban <surban@surban.net>"]
license = "GPL-3.0"
version = "0.7.0"
version = "0.7.1"
publish = false
edition = "2021"

Expand Down
82 changes: 60 additions & 22 deletions openemc-log/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use std::{
mod syslog;

const READ_BUFFER_SIZE: usize = 128;
const UNPROCESSED_SIZE: usize = 16_777_216;
const POLL_TIMEOUT: c_int = 60_000;
const FIRMWARE_DIR: &str = "/lib/firmware";

Expand Down Expand Up @@ -71,6 +72,9 @@ struct Opts {
/// Log to standard output instead of syslog.
#[arg(short, long)]
foreground: bool,
/// Show malformed log messages.
#[arg(long)]
malformed: bool,
/// Gzipped ELF image of OpenEMC firmware.
firmware_elf: Option<PathBuf>,
/// OpenEMC log file or device.
Expand All @@ -82,30 +86,46 @@ struct Opts {
probe: bool,
}

/// Write defmt log message to Rust logger.
fn forward_to_logger(frame: &Frame, loc: Option<&Location>, lines: bool) {
let mut msg = String::with_capacity(256);
/// Log message for Rust logger.
#[derive(Debug, Clone, PartialEq, Eq)]
struct LogMsg {
/// Log level.
pub level: Level,
/// Message.
pub msg: String,
}

if let Some(loc) = loc {
write!(msg, "[{}", &loc.module).unwrap();
if lines {
write!(msg, " @ {}:{}", loc.file.display(), loc.line).unwrap();
impl LogMsg {
/// Format defmt log message for Rust logger.
pub fn from_defmt(frame: &Frame, loc: Option<&Location>, lines: bool) -> Self {
let mut msg = String::with_capacity(256);

if let Some(loc) = loc {
write!(msg, "[{}", &loc.module).unwrap();
if lines {
write!(msg, " @ {}:{}", loc.file.display(), loc.line).unwrap();
}
write!(msg, "] ").unwrap();
}
write!(msg, "] ").unwrap();
}

write!(msg, "{}", frame.display_message()).unwrap();
write!(msg, "{}", frame.display_message()).unwrap();

let level = match frame.level() {
Some(DefmtLevel::Error) => Level::Error,
Some(DefmtLevel::Warn) => Level::Warn,
Some(DefmtLevel::Info) => Level::Info,
Some(DefmtLevel::Debug) => Level::Debug,
Some(DefmtLevel::Trace) => Level::Trace,
None => Level::Info,
};
let level = match frame.level() {
Some(DefmtLevel::Error) => Level::Error,
Some(DefmtLevel::Warn) => Level::Warn,
Some(DefmtLevel::Info) => Level::Info,
Some(DefmtLevel::Debug) => Level::Debug,
Some(DefmtLevel::Trace) => Level::Trace,
None => Level::Info,
};

Self { level, msg }
}

log::log!(level, "{msg}");
/// Log the message to the Rust logger.
pub fn log(&self) {
log::log!(self.level, "{}", &self.msg);
}
}

/// Gets the log file or log device path.
Expand Down Expand Up @@ -314,6 +334,7 @@ fn perform(opts: &Opts) -> Result<bool> {

let mut buf = [0; READ_BUFFER_SIZE];
let mut stream_decoder = table.new_stream_decoder();
let mut unprocessed = 0;

while !STOP.load(Ordering::SeqCst) {
// Read from start of device file.
Expand All @@ -323,6 +344,7 @@ fn perform(opts: &Opts) -> Result<bool> {

// Read data.
match log.read(&mut buf) {
Ok(0) if unprocessed > 0 => (),
Ok(0) if is_dev && !opts.oneshot && !opts.bootloader => {
// Wait for data to arrive.
let log_fd = log.as_fd();
Expand All @@ -334,7 +356,13 @@ fn perform(opts: &Opts) -> Result<bool> {
}
}
Ok(0) => return Ok(true),
Ok(n) => stream_decoder.received(&buf[..n]),
Ok(n) => {
stream_decoder.received(&buf[..n]);
unprocessed += n;
if unprocessed < UNPROCESSED_SIZE {
continue;
}
}
Err(err) if is_dev && err.kind() == ErrorKind::BrokenPipe => {
log::warn!("======== log data was lost ========");
stream_decoder = table.new_stream_decoder();
Expand All @@ -344,22 +372,32 @@ fn perform(opts: &Opts) -> Result<bool> {
}

// Decode the received data.
let mut log_msgs = Vec::new();
loop {
match stream_decoder.decode() {
Ok(frame) => {
let loc = locs.as_ref().and_then(|locs| locs.get(&frame.index()));
forward_to_logger(&frame, loc, opts.lines);
let log_msg = LogMsg::from_defmt(&frame, loc, opts.lines);
log_msgs.push(log_msg);
}
Err(DecodeError::UnexpectedEof) => break,
Err(DecodeError::Malformed) => {
log::warn!("======== malformed log data ========");
if !table.encoding().can_recover() {
return Err(DecodeError::Malformed.into());
}
break;
if !opts.malformed {
log_msgs.clear();
}
}
}
}
unprocessed = 0;

// Log to system log.
for log_msg in log_msgs {
log_msg.log();
}
}

Ok(false)
Expand Down

0 comments on commit 45a96de

Please sign in to comment.