Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder: add support for customizable logger formatting #765

Merged
merged 20 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
- [#765]: `defmt-decoder`: Add support for customizable logger formatting

[#765]: https://github.com/knurling-rs/defmt/pull/765

- [#766] `decoder::log`: Rename `PrettyLogger` to `StdoutLogger`

Expand Down
1 change: 1 addition & 0 deletions decoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ byteorder = "1"
colored = "2"
defmt-parser = { version = "=0.3.3", path = "../parser", features = ["unstable"] }
ryu = "1"
nom = "7"

# display
time = { version = "0.3", default-features = false, features = [
Expand Down
106 changes: 106 additions & 0 deletions decoder/src/log/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use nom::{
branch::alt,
bytes::complete::{take, take_till1},
character::complete::char,
combinator::{map, map_res},
multi::many0,
sequence::delimited,
IResult, Parser,
};

#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub(super) enum LogSegment {
FileName,
FilePath,
LineNumber,
Log,
LogLevel,
ModulePath,
String(String),
Timestamp,
}

fn parse_argument(input: &str) -> IResult<&str, LogSegment, ()> {
let parse_enclosed = delimited(char('{'), take(1u32), char('}'));
let mut parse_type = map_res(parse_enclosed, move |s| match s {
"f" => Ok(LogSegment::FileName),
"F" => Ok(LogSegment::FilePath),
"l" => Ok(LogSegment::LineNumber),
"s" => Ok(LogSegment::Log),
"L" => Ok(LogSegment::LogLevel),
"m" => Ok(LogSegment::ModulePath),
"t" => Ok(LogSegment::Timestamp),
_ => Err(()),
});

parse_type.parse(input)
}

fn parse_string_segment(input: &str) -> IResult<&str, LogSegment, ()> {
map(take_till1(|c| c == '{'), |s: &str| {
LogSegment::String(s.to_string())
})
.parse(input)
}

pub(super) fn parse(input: &str) -> Result<Vec<LogSegment>, String> {
let mut parse_all = many0(alt((parse_argument, parse_string_segment)));

parse_all(input)
.map(|(_, output)| output)
.map_err(|e| e.to_string())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_log_template() {
let log_template = "{t} [{L}] {s}\n└─ {m} @ {F}:{l}";

let expected_output = vec![
LogSegment::Timestamp,
LogSegment::String(" [".to_string()),
LogSegment::LogLevel,
LogSegment::String("] ".to_string()),
LogSegment::Log,
LogSegment::String("\n└─ ".to_string()),
LogSegment::ModulePath,
LogSegment::String(" @ ".to_string()),
LogSegment::FilePath,
LogSegment::String(":".to_string()),
LogSegment::LineNumber,
];

let result = parse(log_template);
assert_eq!(result, Ok(expected_output));
}

#[test]
fn test_parse_string_segment() {
let result = parse_string_segment("Log: {t}");
let (input, output) = result.unwrap();
assert_eq!(input, "{t}");
assert_eq!(output, LogSegment::String("Log: ".to_string()));
}

#[test]
fn test_parse_empty_string_segment() {
let result = parse_string_segment("");
assert!(result.is_err());
}

#[test]
fn test_parse_timestamp_argument() {
let result = parse_argument("{t}");
assert_eq!(result, Ok(("", LogSegment::Timestamp)));
}

#[test]
fn test_parse_invalid_argument() {
let result = parse_argument("{foo}");
assert_eq!(result, Result::Err(nom::Err::Error(())));
}
}
10 changes: 7 additions & 3 deletions decoder/src/log/json_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use time::OffsetDateTime;

use std::io::{self, Write};

use super::{DefmtRecord, StdoutLogger};
use super::{stdout_logger::StdoutLogger, DefmtRecord};
andresovela marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) struct JsonLogger {
should_log: Box<dyn Fn(&Metadata) -> bool + Sync + Send>,
Expand Down Expand Up @@ -41,10 +41,14 @@ impl Log for JsonLogger {
}

impl JsonLogger {
pub fn new(should_log: impl Fn(&Metadata) -> bool + Sync + Send + 'static) -> Box<Self> {
pub fn new(
log_format: Option<&str>,
andresovela marked this conversation as resolved.
Show resolved Hide resolved
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(true, |_| true),
host_logger: StdoutLogger::new_unboxed(log_format, host_log_format, |_| true),
})
}

Expand Down
27 changes: 21 additions & 6 deletions decoder/src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! [`log`]: https://crates.io/crates/log
//! [`defmt`]: https://crates.io/crates/defmt

mod format;
mod json_logger;
mod stdout_logger;

Expand Down Expand Up @@ -119,19 +120,33 @@ impl<'a> DefmtRecord<'a> {
/// The caller has to provide a `should_log` closure that determines whether a log record should be
/// printed.
///
/// If `always_include_location` is `true`, a second line containing location information will be
/// printed for *all* records, not just for defmt frames (defmt frames always get location info
/// included if it is available, regardless of this setting).
/// An optional `log_format` string can be provided to format the way
/// logs are printed. A format string could look as follows:
/// "{t} [{L}] Location<{f}:{l}> {s}"
///
/// The arguments between curly braces are placeholders for log metadata.
/// The following arguments are supported:
/// - {t} : log timestamp
/// - {f} : file name (e.g. "main.rs")
/// - {F} : file path (e.g. "home/app/main.rs")
/// - {m} : module path (e.g. "foo::bar::some_function")
/// - {l} : line number
/// - {L} : log level (e.g. "INFO", "DEBUG", etc)
/// - {s} : the actual log
///
/// For example, with the log format shown above, a log would look like this:
/// "23124 [INFO] Location<main.rs:23> Hello, world!"
pub fn init_logger(
always_include_location: bool,
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(always_include_location, should_log),
false => StdoutLogger::new(log_format, host_log_format, should_log),
true => {
JsonLogger::print_schema_version();
JsonLogger::new(should_log)
JsonLogger::new(log_format, host_log_format, should_log)
}
})
.unwrap();
Expand Down
Loading
Loading