Skip to content

Commit

Permalink
Add host_log_format option
Browse files Browse the repository at this point in the history
  • Loading branch information
andresovela committed Jul 17, 2023
1 parent 8a1c907 commit 812824d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
3 changes: 2 additions & 1 deletion decoder/src/log/json_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ impl Log for JsonLogger {
impl JsonLogger {
pub fn new(
log_format: Option<&str>,
host_log_format: Option<&str>,
should_log: impl Fn(&Metadata) -> bool + Sync + Send + 'static,
) -> Box<Self> {
Box::new(Self {
should_log: Box::new(should_log),
host_logger: StdoutLogger::new_unboxed(log_format, |_| true),
host_logger: StdoutLogger::new_unboxed(log_format, host_log_format, |_| true),
})
}

Expand Down
5 changes: 3 additions & 2 deletions decoder/src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ impl<'a> DefmtRecord<'a> {
/// "23124 [INFO] Location<main.rs:23> Hello, world!"
pub fn init_logger(
log_format: Option<&str>,
host_log_format: Option<&str>,
json: bool,
should_log: impl Fn(&Metadata) -> bool + Sync + Send + 'static,
) {
log::set_boxed_logger(match json {
false => StdoutLogger::new(log_format, should_log),
false => StdoutLogger::new(log_format, host_log_format, should_log),
true => {
JsonLogger::print_schema_version();
JsonLogger::new(log_format, should_log)
JsonLogger::new(log_format, host_log_format, should_log)
}
})
.unwrap();
Expand Down
28 changes: 17 additions & 11 deletions decoder/src/log/stdout_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ enum Record<'a> {
}

pub(crate) struct StdoutLogger {
format: Vec<LogSegment>,
log_format: Vec<LogSegment>,
host_log_format: Vec<LogSegment>,
should_log: Box<dyn Fn(&Metadata) -> bool + Sync + Send>,
/// Number of characters used by the timestamp.
/// This may increase over time and is used to align messages.
Expand Down Expand Up @@ -57,25 +58,29 @@ impl Log for StdoutLogger {
impl StdoutLogger {
pub fn new(
log_format: Option<&str>,
host_log_format: Option<&str>,
should_log: impl Fn(&Metadata) -> bool + Sync + Send + 'static,
) -> Box<Self> {
Box::new(Self::new_unboxed(log_format, should_log))
Box::new(Self::new_unboxed(log_format, host_log_format, should_log))
}

pub fn new_unboxed(
log_format: Option<&str>,
host_log_format: Option<&str>,
should_log: impl Fn(&Metadata) -> bool + Sync + Send + 'static,
) -> Self {
const DEFAULT_LOG_FORMAT: &str = "{t} {L} {s}\n└─ {m} @ {F}:{l}";
const DEFAULT_HOST_LOG_FORMAT: &str = "(HOST) {L} {s}";

let format = log_format.unwrap_or(DEFAULT_LOG_FORMAT);
let format = format::parse(format).unwrap_or_else(|_| {
// Use the default format if the user-provided format is invalid
format::parse(DEFAULT_LOG_FORMAT).unwrap()
});
let log_format = log_format.unwrap_or(DEFAULT_LOG_FORMAT);
let log_format = format::parse(log_format).unwrap();

let host_log_format = host_log_format.unwrap_or(DEFAULT_HOST_LOG_FORMAT);
let host_log_format = format::parse(host_log_format).unwrap();

Self {
format,
log_format,
host_log_format,
should_log: Box::new(should_log),
timing_align: AtomicUsize::new(0),
}
Expand All @@ -86,15 +91,15 @@ impl StdoutLogger {
self.timing_align.fetch_max(len, Ordering::Relaxed);
let min_timestamp_width = self.timing_align.load(Ordering::Relaxed);

Printer::new(Record::Defmt(&record), &self.format)
Printer::new(Record::Defmt(&record), &self.log_format)
.min_timestamp_width(min_timestamp_width)
.print_frame(&mut sink)
.ok();
}

pub(super) fn print_host_record(&self, record: &LogRecord, mut sink: StderrLock) {
let min_timestamp_width = self.timing_align.load(Ordering::Relaxed);
Printer::new(Record::Host(record), &self.format)
Printer::new(Record::Host(record), &self.host_log_format)
.min_timestamp_width(min_timestamp_width)
.print_frame(&mut sink)
.ok();
Expand Down Expand Up @@ -147,7 +152,8 @@ impl<'a> Printer<'a> {
fn print_timestamp<W: io::Write>(&self, sink: &mut W) -> io::Result<()> {
let timestamp = match self.record {
Record::Defmt(record) => record.timestamp().to_string(),
Record::Host(_) => String::from("(HOST)"),
// TODO: Is there a timestamp for host messages?
Record::Host(_) => String::from("<time>"),
};

write!(sink, "{timestamp:>0$}", self.min_timestamp_width,)
Expand Down
17 changes: 13 additions & 4 deletions print/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ struct Opts {
#[arg(long)]
log_format: Option<String>,

#[arg(long)]
host_log_format: Option<String>,

#[arg(long)]
show_skipped_frames: bool,

Expand Down Expand Up @@ -84,6 +87,7 @@ fn main() -> anyhow::Result<()> {
elf,
json,
log_format,
host_log_format,
show_skipped_frames,
verbose,
version,
Expand All @@ -94,10 +98,15 @@ fn main() -> anyhow::Result<()> {
return print_version();
}

defmt_decoder::log::init_logger(log_format.as_deref(), json, move |metadata| match verbose {
false => defmt_decoder::log::is_defmt_frame(metadata), // We display *all* defmt frames, but nothing else.
true => true, // We display *all* frames.
});
defmt_decoder::log::init_logger(
log_format.as_deref(),
host_log_format.as_deref(),
json,
move |metadata| match verbose {
false => defmt_decoder::log::is_defmt_frame(metadata), // We display *all* defmt frames, but nothing else.
true => true, // We display *all* frames.
},
);

// read and parse elf file
let bytes = fs::read(elf.unwrap())?;
Expand Down

0 comments on commit 812824d

Please sign in to comment.