Skip to content

Commit eaf14ae

Browse files
Add log-format argument (#493)
* feat: Add defmt flag * feat: Add log-format argument * feat: Update default value of log-format * feat: Pin defmt deps * Fix typo Co-authored-by: Dániel Buga <[email protected]> --------- Co-authored-by: Dániel Buga <[email protected]>
1 parent 8ee6fbc commit eaf14ae

File tree

8 files changed

+62
-63
lines changed

8 files changed

+62
-63
lines changed

Cargo.lock

Lines changed: 9 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-espflash/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,3 @@ cargo = { version = "0.73.1", features = ["vendored-openssl"] }
4141

4242
[target.'cfg(windows)'.dependencies]
4343
cargo = "0.73.1"
44-
45-
[features]
46-
defmt = ["espflash/defmt"]

cargo-espflash/src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use std::{
66

77
use cargo_metadata::Message;
88
use clap::{Args, CommandFactory, Parser, Subcommand};
9-
use espflash::cli::{erase_flash, erase_region, EraseFlashArgs, EraseRegionArgs};
109
use espflash::{
1110
cli::{
12-
self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image,
13-
monitor::monitor, parse_partition_table, partition_table, print_board_info,
14-
save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress,
15-
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
11+
self, board_info, completions, config::Config, connect, erase_flash, erase_partitions,
12+
erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table,
13+
partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs,
14+
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
15+
MonitorArgs, PartitionTableArgs,
1616
},
1717
error::Error as EspflashError,
1818
image_format::ImageFormatKind,
@@ -341,11 +341,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
341341
115_200
342342
};
343343

344-
monitor(
344+
monitor_with(
345345
flasher.into_interface(),
346346
Some(&elf_data),
347347
pid,
348348
args.flash_args.monitor_baud.unwrap_or(default_baud),
349+
args.flash_args.log_format,
349350
)
350351
.into_diagnostic()?;
351352
}

espflash/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ clap_complete = { version = "4.4.3", optional = true }
4040
comfy-table = { version = "7.0.1", optional = true }
4141
crossterm = { version = "0.25.0", optional = true } # 0.26.x causes issues on Windows
4242
ctrlc = { version = "3.4.0", optional = true }
43-
defmt-decoder = { version = "=0.3.8", features = ["unstable"], optional = true }
43+
# defmt dependencies are pinned since defmt does not guarantee MSRV even for patch releases
44+
defmt-decoder = { version = "=0.3.9", features = ["unstable"], optional = true }
4445
defmt-parser = { version = "=0.3.3", features = ["unstable"], optional = true }
4546
dialoguer = { version = "0.10.4", optional = true }
4647
directories = { version = "5.0.1", optional = true }
@@ -74,6 +75,8 @@ cli = [
7475
"dep:comfy-table",
7576
"dep:crossterm",
7677
"dep:ctrlc",
78+
"dep:defmt-decoder",
79+
"dep:defmt-parser",
7780
"dep:dialoguer",
7881
"dep:directories",
7982
"dep:env_logger",
@@ -84,8 +87,5 @@ cli = [
8487
"dep:regex",
8588
"dep:update-informer",
8689
]
87-
defmt = [
88-
"dep:defmt-decoder",
89-
"dep:defmt-parser",
90-
]
90+
9191
raspberry = ["dep:rppal"]

espflash/src/bin/espflash.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clap::{Args, CommandFactory, Parser, Subcommand};
88
use espflash::{
99
cli::{
1010
self, board_info, completions, config::Config, connect, erase_flash, erase_partitions,
11-
erase_region, flash_elf_image, monitor::monitor, parse_partition_table, parse_uint32,
11+
erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table, parse_uint32,
1212
partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs,
1313
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
1414
MonitorArgs, PartitionTableArgs,
@@ -263,11 +263,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
263263
115_200
264264
};
265265

266-
monitor(
266+
monitor_with(
267267
flasher.into_interface(),
268268
Some(&elf_data),
269269
pid,
270270
args.flash_args.monitor_baud.unwrap_or(default_baud),
271+
args.flash_args.log_format,
271272
)
272273
.into_diagnostic()?;
273274
}

espflash/src/cli/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ use log::{debug, info};
2424
use miette::{IntoDiagnostic, Result, WrapErr};
2525
use serialport::{SerialPortType, UsbPortInfo};
2626

27-
use self::{config::Config, monitor::monitor, serial::get_serial_port_info};
27+
use self::{
28+
config::Config,
29+
monitor::{monitor_with, LogFormat},
30+
serial::get_serial_port_info,
31+
};
2832
use crate::{
2933
elf::ElfFirmwareImage,
3034
error::{Error, MissingPartition, MissingPartitionTable},
@@ -134,6 +138,9 @@ pub struct FlashArgs {
134138
/// Load the application to RAM instead of Flash
135139
#[arg(long)]
136140
pub ram: bool,
141+
/// Logging format.
142+
#[arg(long, short = 'f', default_value = "serial", requires = "monitor")]
143+
pub log_format: LogFormat,
137144
}
138145

139146
/// Operations for partitions tables
@@ -185,6 +192,9 @@ pub struct MonitorArgs {
185192
/// Connection configuration
186193
#[clap(flatten)]
187194
connect_args: ConnectArgs,
195+
/// Logging format.
196+
#[arg(long, short = 'f', default_value = "serial")]
197+
pub log_format: LogFormat,
188198
}
189199

190200
/// Select a serial port and establish a connection with a target device
@@ -291,11 +301,12 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
291301
115_200
292302
};
293303

294-
monitor(
304+
monitor_with(
295305
flasher.into_interface(),
296306
elf.as_deref(),
297307
pid,
298308
args.connect_args.baud.unwrap_or(default_baud),
309+
args.log_format,
299310
)
300311
.into_diagnostic()?;
301312

espflash/src/cli/monitor/mod.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crossterm::{
2121
};
2222
use log::error;
2323
use miette::{IntoDiagnostic, Result};
24+
use strum::{Display, EnumIter, EnumString, EnumVariantNames};
2425

2526
use crate::{
2627
cli::monitor::parser::{InputParser, ResolvingPrinter},
@@ -33,6 +34,17 @@ pub mod parser;
3334
mod line_endings;
3435
mod symbols;
3536

37+
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
38+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumIter, EnumString, EnumVariantNames)]
39+
#[non_exhaustive]
40+
#[strum(serialize_all = "lowercase")]
41+
pub enum LogFormat {
42+
/// defmt
43+
Defmt,
44+
/// serial
45+
Serial,
46+
}
47+
3648
/// Type that ensures that raw mode is disabled when dropped.
3749
struct RawModeGuard;
3850

@@ -58,22 +70,16 @@ pub fn monitor(
5870
pid: u16,
5971
baud: u32,
6072
) -> serialport::Result<()> {
61-
#[cfg(feature = "defmt")]
62-
let parser = parser::esp_defmt::EspDefmt::new(elf);
63-
64-
#[cfg(not(feature = "defmt"))]
65-
let parser = parser::serial::Serial;
66-
67-
monitor_with(serial, elf, pid, baud, parser)
73+
monitor_with(serial, elf, pid, baud, LogFormat::Serial)
6874
}
6975

7076
/// Open a serial monitor on the given interface, using the given input parser.
71-
pub fn monitor_with<L: InputParser>(
77+
pub fn monitor_with(
7278
mut serial: Interface,
7379
elf: Option<&[u8]>,
7480
pid: u16,
7581
baud: u32,
76-
mut parser: L,
82+
log_format: LogFormat,
7783
) -> serialport::Result<()> {
7884
println!("Commands:");
7985
println!(" CTRL+R Reset chip");
@@ -102,7 +108,16 @@ pub fn monitor_with<L: InputParser>(
102108
err => err,
103109
}?;
104110

105-
parser.feed(&buff[0..read_count], &mut stdout);
111+
match log_format {
112+
LogFormat::Defmt => {
113+
let mut parser = parser::esp_defmt::EspDefmt::new(elf);
114+
parser.feed(&buff[0..read_count], &mut stdout);
115+
}
116+
LogFormat::Serial => {
117+
let mut parser = parser::serial::Serial;
118+
parser.feed(&buff[0..read_count], &mut stdout);
119+
}
120+
}
106121

107122
// Don't forget to flush the writer!
108123
stdout.flush().ok();

espflash/src/cli/monitor/parser/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[cfg(feature = "defmt")]
21
pub mod esp_defmt;
32
pub mod serial;
43

0 commit comments

Comments
 (0)