Skip to content

Commit

Permalink
Add convenience constructors for FormatterConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
andresovela committed Oct 12, 2023
1 parent 0aaf7ab commit cefe2d6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
38 changes: 38 additions & 0 deletions decoder/src/log/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,54 @@ enum Record<'a> {
Host(&'a LogRecord<'a>),
}

#[derive(Debug)]
pub enum FormatterFormat<'a> {
Default { with_location: bool },
Custom(&'a str),
}

impl Default for FormatterFormat<'_> {
fn default() -> Self {
FormatterFormat::Default {
with_location: false,
}
}
}

#[derive(Debug, Default)]
pub struct FormatterConfig<'a> {
pub format: FormatterFormat<'a>,
pub is_timestamp_available: bool,
}

impl<'a> FormatterConfig<'a> {
pub fn custom(format: &'a str) -> Self {
FormatterConfig {
format: FormatterFormat::Custom(format),
is_timestamp_available: false,
}
}

pub fn with_timestamp(mut self) -> Self {
self.is_timestamp_available = true;
self
}

pub fn with_location(mut self) -> Self {
// TODO: Should we warn the user that trying to set a location
// for a custom format won't work?
match self.format {
FormatterFormat::Default { with_location: _ } => {
self.format = FormatterFormat::Default {
with_location: true,
};
self
}
_ => self,
}
}
}

impl InternalFormatter {
fn new(config: FormatterConfig, source: Source) -> Self {
const FORMAT: &str = "{L} {s}";
Expand Down
42 changes: 15 additions & 27 deletions print/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::anyhow;
use clap::{Parser, Subcommand};
use defmt_decoder::{
log::{
format::{Formatter, FormatterConfig, FormatterFormat, HostFormatter},
format::{Formatter, FormatterConfig, HostFormatter},
DefmtLoggerType,
},
DecodeError, Frame, Locations, Table, DEFMT_VERSIONS,
Expand Down Expand Up @@ -123,40 +123,28 @@ fn main() -> anyhow::Result<()> {
DefmtLoggerType::Stdout
};

// TODO: Figure out how to get around the temporary borrowed value error if
// you try to do FormatterFormat::Custom(log_format.as_str())
let cloned_format = log_format.clone().unwrap_or_default();
let defmt_format = if log_format.is_some() {
FormatterFormat::Custom(cloned_format.as_str())
let mut formatter_config = if log_format.is_some() {
FormatterConfig::custom(cloned_format.as_str())
} else if verbose {
FormatterConfig::default().with_location()
} else {
FormatterFormat::Default {
with_location: verbose,
}
FormatterConfig::default()
};

// TODO: Figure out how to get around the temporary borrowed value error if
// you try to do FormatterFormat::Custom(host_log_format.as_str())
formatter_config.is_timestamp_available = table.has_timestamp();

let cloned_host_format = host_log_format.clone().unwrap_or_default();
let host_format = if host_log_format.is_some() {
FormatterFormat::Custom(cloned_host_format.as_str())
let host_formatter_config = if host_log_format.is_some() {
FormatterConfig::custom(cloned_host_format.as_str())
} else if verbose {
FormatterConfig::default().with_location()
} else {
FormatterFormat::Default {
with_location: verbose,
}
};

let defmt_config = FormatterConfig {
format: defmt_format,
is_timestamp_available: table.has_timestamp(),
};

let host_config = FormatterConfig {
format: host_format,
is_timestamp_available: table.has_timestamp(),
FormatterConfig::default()
};

let formatter = Formatter::new(defmt_config);
let host_formatter = HostFormatter::new(host_config);
let formatter = Formatter::new(formatter_config);
let host_formatter = HostFormatter::new(host_formatter_config);

defmt_decoder::log::init_logger(formatter, host_formatter, logger_type, move |metadata| {
match verbose {
Expand Down

0 comments on commit cefe2d6

Please sign in to comment.