Skip to content

Commit b028c52

Browse files
authored
Detect log format metadata (#809)
* Detect log format metadata * Match section name * Update HIL test * Changelog
1 parent ee954b2 commit b028c52

File tree

7 files changed

+69
-5
lines changed

7 files changed

+69
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Add `--monitor` option to `write-bin`. (#783)
1818
- Add `watchdog-reset` strategy to `--after` subcommand (#779)
1919
- Add `ROM` version of `read-flash` command (#812)
20+
- `espflash` can detect the log format automatically from ESP-HAL metadata. Reqires `esp-println` 0.14 (presumably, yet to be released) (#809)
2021

2122
### Changed
2223

espflash/src/cli/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ pub struct MonitorConfigArgs {
280280
#[arg(long, requires = "non_interactive")]
281281
no_reset: bool,
282282
/// Logging format.
283-
#[arg(long, short = 'L', default_value = "serial")]
284-
log_format: LogFormat,
283+
#[arg(long, short = 'L')]
284+
log_format: Option<LogFormat>,
285285
/// External log processors to use (comma separated executables)
286286
#[arg(long)]
287287
processors: Option<String>,

espflash/src/cli/monitor/mod.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ use crossterm::{
2020
terminal::{disable_raw_mode, enable_raw_mode},
2121
};
2222
use external_processors::ExternalProcessors;
23-
use log::{debug, error};
23+
use log::{debug, error, warn};
2424
use miette::{IntoDiagnostic, Result};
2525
#[cfg(feature = "serialport")]
2626
use serialport::SerialPort;
2727
use strum::{Display, EnumIter, EnumString, VariantNames};
2828

2929
use crate::{
3030
cli::{
31-
monitor::parser::{InputParser, ResolvingPrinter},
31+
monitor::{
32+
parser::{InputParser, ResolvingPrinter},
33+
symbols::Symbols,
34+
},
3235
MonitorConfigArgs,
3336
},
3437
connection::{reset::reset_after_flash, Port},
@@ -102,7 +105,10 @@ pub fn monitor(
102105
let stdout = stdout();
103106
let mut stdout = ResolvingPrinter::new(elf, stdout.lock());
104107

105-
let mut parser: Box<dyn InputParser> = match monitor_args.log_format {
108+
let mut parser: Box<dyn InputParser> = match monitor_args
109+
.log_format
110+
.unwrap_or_else(|| deduce_log_format(elf))
111+
{
106112
LogFormat::Defmt => Box::new(parser::esp_defmt::EspDefmt::new(elf)?),
107113
LogFormat::Serial => Box::new(parser::serial::Serial),
108114
};
@@ -151,6 +157,34 @@ pub fn monitor(
151157
Ok(())
152158
}
153159

160+
fn deduce_log_format(elf: Option<&[u8]>) -> LogFormat {
161+
let Some(elf) = elf else {
162+
return LogFormat::Serial;
163+
};
164+
165+
let Ok(symbols) = Symbols::try_from(elf) else {
166+
return LogFormat::Serial;
167+
};
168+
169+
let Some(format_symbol) =
170+
symbols.get_symbol_data(Some(".espressif.metadata"), b"espflash.LOG_FORMAT")
171+
else {
172+
return LogFormat::Serial;
173+
};
174+
175+
match format_symbol {
176+
b"defmt-espflash" => LogFormat::Defmt,
177+
b"serial" => LogFormat::Serial,
178+
other => {
179+
warn!(
180+
"Unknown log format symbol: {}. Defaulting to serial.",
181+
String::from_utf8_lossy(other),
182+
);
183+
LogFormat::Serial
184+
}
185+
}
186+
}
187+
154188
// Converts key events from crossterm into appropriate character/escape
155189
// sequences which are then sent over the serial connection.
156190
//

espflash/src/cli/monitor/symbols.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ impl<'sym> Symbols<'sym> {
9898
}
9999
})?
100100
}
101+
102+
pub(crate) fn get_symbol_data(
103+
&self,
104+
section_name: Option<&str>,
105+
name_bytes: &[u8],
106+
) -> Option<&'sym [u8]> {
107+
let sym = self.object.symbol_by_name_bytes(name_bytes)?;
108+
109+
let section = match section_name {
110+
Some(section_name) => self.object.section_by_name(section_name)?,
111+
None => self.object.section_by_index(sym.section_index()?).ok()?,
112+
};
113+
section.data_range(sym.address(), sym.size()).ok().flatten()
114+
}
101115
}

espflash/tests/data/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ cargo build --release
88

99
The `esp32c6_defmt` elf file under this folder has been generated using `esp-generate@04d69c9`:
1010

11+
> TODO: this part needs to be updated, and the elf re-created once ESP-HAL beta.1 is out.
12+
1113
```
1214
esp-generate --chip=esp32c6 -o defmt --headless esp32c6_defmt
1315
cd esp32c6_defmt

espflash/tests/data/esp32c6_defmt

3.25 MB
Binary file not shown.

espflash/tests/scripts/flash.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
app="espflash/tests/data/$1"
33

44
if [[ "$1" == "esp32c6" ]]; then
5+
# With manual log-format
56
app_defmt="${app}_defmt"
67
result=$(timeout 8s espflash flash --no-skip --monitor --non-interactive $app_defmt --log-format defmt 2>&1)
78
echo "$result"
@@ -13,6 +14,18 @@ if [[ "$1" == "esp32c6" ]]; then
1314
echo "Monitoring failed!"
1415
exit 1
1516
fi
17+
18+
# With auto-detected log-format
19+
result=$(timeout 8s espflash flash --no-skip --monitor --non-interactive $app_defmt 2>&1)
20+
echo "$result"
21+
if [[ ! $result =~ "Flashing has completed!" ]]; then
22+
echo "Flashing failed!"
23+
exit 1
24+
fi
25+
if ! echo "$result" | grep -q "Hello world!"; then
26+
echo "Monitoring failed!"
27+
exit 1
28+
fi
1629
fi
1730

1831
result=$(timeout 8s espflash flash --no-skip --monitor --non-interactive $app 2>&1)

0 commit comments

Comments
 (0)