Skip to content

Commit

Permalink
reimpl Gautham's additions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjperez committed May 2, 2024
1 parent 15991b5 commit c4daf4b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
62 changes: 62 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
pid,
args.flash_args.monitor_baud.unwrap_or(default_baud),
args.flash_args.log_format,
args.flash_args.log_output,
true,
)
} else {
Expand Down
1 change: 1 addition & 0 deletions espflash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ required-features = ["cli", "serialport"]
addr2line = { version = "0.21.0", optional = true }
base64 = "0.22.0"
bytemuck = { version = "1.14.3", features = ["derive"] }
chrono = "0.4.38"
clap = { version = "4.5.2", features = [
"derive",
"env",
Expand Down
1 change: 1 addition & 0 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
pid,
args.flash_args.monitor_baud.unwrap_or(default_baud),
args.flash_args.log_format,
args.flash_args.log_output,
true,
)
} else {
Expand Down
7 changes: 7 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ pub struct FlashArgs {
/// Logging format.
#[arg(long, short = 'L', default_value = "serial", requires = "monitor")]
pub log_format: LogFormat,
/// Logging format.
#[arg(long, short = 'O', requires = "monitor")]
pub log_output: Option<String>,
/// Minimum chip revision supported by image, in format: major.minor
#[arg(long, default_value = "0.0", value_parser = parse_chip_rev)]
pub min_chip_rev: u16,
Expand Down Expand Up @@ -261,6 +264,9 @@ pub struct MonitorArgs {
/// Logging format.
#[arg(long, short = 'L', default_value = "serial", requires = "elf")]
pub log_format: LogFormat,
/// Logging format.
#[arg(long, short = 'O')]
pub log_output: Option<String>,
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -445,6 +451,7 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
pid,
args.connect_args.baud.unwrap_or(default_baud),
args.log_format,
args.log_output,
!args.non_interactive,
)
}
Expand Down
42 changes: 40 additions & 2 deletions espflash/src/cli/monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
//! call are not displayed until `println!()` is subsequently called, where as
//! in our monitor the output is displayed immediately upon reading.

use regex::Regex;
use std::{
io::{stdout, ErrorKind, Read, Write},
fs::{File, OpenOptions},
io::{stdout, BufWriter, ErrorKind, Read, Write},
time::Duration,
};

use chrono::Local;

use crossterm::{
event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
terminal::{disable_raw_mode, enable_raw_mode},
Expand Down Expand Up @@ -71,6 +75,7 @@ pub fn monitor(
pid: u16,
baud: u32,
log_format: LogFormat,
log_path: Option<String>,
interactive_mode: bool,
) -> miette::Result<()> {
if interactive_mode {
Expand Down Expand Up @@ -101,9 +106,30 @@ pub fn monitor(
};

let mut buff = [0; 1024];
let mut log_file: Option<BufWriter<File>> = if let Some(log_path) = log_path.as_ref() {
let log_file_obj = OpenOptions::new().create(true).append(true).open(log_path);
if let Err(err) = log_file_obj.as_ref() {
println!("error opening log_file: {:?}", err);
}
log_file_obj.map(BufWriter::new).ok()
} else {
None
};

loop {
let read_count = match serial.read(&mut buff) {
Ok(count) => Ok(count),
Ok(count) => {
if let Some(log_file) = log_file.as_mut() {
let line = String::from_utf8(buff.to_vec()).unwrap();
if let Err(err) = log_file
.write_all(strip_ansi_formatting_and_apply_timestamp(&line).as_bytes())
{
println!("could not write line {} to log file: {}", line, err);
}
log_file.write_all(b"\n").ok();
}
Ok(count)
}
Err(e) if e.kind() == ErrorKind::TimedOut => Ok(0),
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
err => err.into_diagnostic(),
Expand All @@ -112,6 +138,9 @@ pub fn monitor(
parser.feed(&buff[0..read_count], &mut stdout);

// Don't forget to flush the writer!
if let Some(log_file) = log_file.as_mut() {
log_file.flush().ok();
}
stdout.flush().ok();

if interactive_mode && poll(Duration::from_secs(0)).into_diagnostic()? {
Expand All @@ -138,6 +167,15 @@ pub fn monitor(
Ok(())
}

fn strip_ansi_formatting_and_apply_timestamp(line_str: &str) -> String {
let re = Regex::new(r"\x1b\[[0-9;]*m").unwrap();
let line_str = re.replace_all(line_str, "").to_string();
let re = Regex::new(r";[0-9]*m").unwrap();
let line_str = re.replace_all(&line_str, "").to_string();
let current_time = Local::now().format("%+");
format!("{current_time} - {line_str}")
}

// Converts key events from crossterm into appropriate character/escape
// sequences which are then sent over the serial connection.
//
Expand Down

0 comments on commit c4daf4b

Please sign in to comment.