Skip to content

Commit d821980

Browse files
committed
add log framework, stub decode tests
1 parent fc5643b commit d821980

File tree

9 files changed

+208
-34
lines changed

9 files changed

+208
-34
lines changed

Cargo.lock

Lines changed: 105 additions & 0 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ pkg-fmt = "zip"
3232
[dependencies]
3333
cargo_metadata = "0.15"
3434
cargo_toml = "0.11"
35-
clap = { version = "3.2", features = ["derive"] }
35+
clap = { version = "3.2", features = ["derive", "env"] }
3636
espflash = { version = "=1.6.1-dev", path = "../espflash" }
37+
log = "0.4.17"
3738
miette = { version = "5.2", features = ["fancy"] }
3839
serde = { version = "1.0", features = ["derive"] }
3940
strum = "0.24"
4041
thiserror = "1.0"
42+
tracing-subscriber = { version = "0.3.11", features = [ "env-filter" ] }
4143
toml = "0.5"

cargo-espflash/src/main.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ use espflash::{
1515
},
1616
Chip, Config, ImageFormatId,
1717
};
18+
use log::debug;
1819
use miette::{IntoDiagnostic, Result, WrapErr};
1920
use strum::VariantNames;
21+
use tracing_subscriber::{filter::LevelFilter, EnvFilter};
2022

2123
use crate::{
2224
cargo_config::{parse_cargo_config, CargoConfig},
@@ -28,19 +30,23 @@ mod cargo_config;
2830
mod error;
2931
mod package_metadata;
3032

31-
#[derive(Parser)]
33+
#[derive(Debug, Clone, Parser)]
3234
#[clap(bin_name = "cargo", version, propagate_version = true)]
3335
struct Opts {
3436
#[clap(subcommand)]
3537
subcommand: CargoSubCommand,
38+
39+
/// Log level
40+
#[clap(long, default_value = "info", env)]
41+
log_level: LevelFilter,
3642
}
3743

38-
#[derive(Parser)]
44+
#[derive(Debug, Clone, Parser)]
3945
enum CargoSubCommand {
4046
Espflash(EspFlashOpts),
4147
}
4248

43-
#[derive(Parser)]
49+
#[derive(Debug, Clone, Parser)]
4450
struct EspFlashOpts {
4551
#[clap(flatten)]
4652
flash_opts: FlashOpts,
@@ -52,7 +58,7 @@ struct EspFlashOpts {
5258
subcommand: Option<SubCommand>,
5359
}
5460

55-
#[derive(Parser)]
61+
#[derive(Debug, Clone, Parser)]
5662
pub enum SubCommand {
5763
/// Display information about the connected board and exit without flashing
5864
BoardInfo(ConnectOpts),
@@ -64,7 +70,7 @@ pub enum SubCommand {
6470
PartitionTable(PartitionTableOpts),
6571
}
6672

67-
#[derive(Parser)]
73+
#[derive(Debug, Clone, Parser)]
6874
pub struct BuildOpts {
6975
/// Build the application using the release profile
7076
#[clap(long)]
@@ -100,7 +106,7 @@ pub struct BuildOpts {
100106
pub flash_config_opts: FlashConfigOpts,
101107
}
102108

103-
#[derive(Parser)]
109+
#[derive(Debug, Clone, Parser)]
104110
pub struct SaveImageOpts {
105111
#[clap(flatten)]
106112
pub build_opts: BuildOpts,
@@ -125,8 +131,20 @@ fn main() -> Result<()> {
125131

126132
check_for_updates(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
127133

128-
let CargoSubCommand::Espflash(opts) = Opts::parse().subcommand;
134+
// Parse options
135+
let opts = Opts::parse();
136+
137+
// Setup logging
138+
let _ = tracing_subscriber::fmt()
139+
.with_env_filter(EnvFilter::from_default_env().add_directive(opts.log_level.into()))
140+
.init();
141+
142+
// Extract subcommand
143+
let CargoSubCommand::Espflash(opts) = opts.subcommand;
144+
145+
debug!("subcommand options: {:?}", opts);
129146

147+
// Load configuration and metadata
130148
let config = Config::load()?;
131149
let metadata = CargoEspFlashMeta::load("Cargo.toml")?;
132150
let cargo_config = parse_cargo_config(".")?;

espflash/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pkg-fmt = "zip"
3232
base64 = "0.13.0"
3333
binread = "2.2"
3434
bytemuck = { version = "1.11", features = ["derive"] }
35-
clap = { version = "3.2", features = ["derive"] }
35+
clap = { version = "3.2", features = ["derive", "env"] }
3636
comfy-table = "6.0"
3737
crossterm = "0.24"
3838
csv = "1.1"
@@ -41,6 +41,7 @@ directories-next = "2.0"
4141
espmonitor = "0.10"
4242
flate2 = "1.0"
4343
indicatif = "0.17"
44+
log = "0.4.17"
4445
maplit = "1.0"
4546
md5 = "0.7"
4647
miette = { version = "5.2", features = ["fancy"] }
@@ -51,6 +52,7 @@ serde-hex = "0.1"
5152
serde_json = "1.0.83"
5253
serde_plain = "1.0"
5354
serialport = "4.2"
55+
tracing-subscriber = { version = "0.3.11", features = [ "env-filter" ] }
5456
sha2 = "0.10"
5557
slip-codec = "0.3"
5658
strum = "0.24"

espflash/src/chip/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, ops::Range, str::FromStr};
22

33
use maplit::hashmap;
4-
use strum_macros::{Display, EnumVariantNames};
4+
use strum_macros::{Display, EnumIter, EnumVariantNames};
55

66
pub use self::{
77
esp32::{Esp32, Esp32Params, Esp32c2, Esp32c3, Esp32s2, Esp32s3},
@@ -143,7 +143,7 @@ impl SpiRegisters {
143143
}
144144
}
145145

146-
#[derive(Debug, Copy, Clone, Eq, PartialEq, Display, EnumVariantNames)]
146+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Display, EnumVariantNames, EnumIter)]
147147
pub enum Chip {
148148
#[strum(serialize = "ESP32")]
149149
Esp32,

espflash/src/cli/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub mod monitor;
3131

3232
mod serial;
3333

34-
#[derive(Parser, Debug)]
34+
#[derive(Clone, Debug, Parser)]
3535
pub struct ConnectOpts {
3636
/// Serial port connected to target device
3737
pub serial: Option<String>,
@@ -45,7 +45,7 @@ pub struct ConnectOpts {
4545
pub use_stub: bool,
4646
}
4747

48-
#[derive(Parser, Debug)]
48+
#[derive(Clone, Debug, Parser)]
4949
pub struct FlashOpts {
5050
/// Load the application to RAM instead of Flash
5151
#[clap(long)]
@@ -61,7 +61,7 @@ pub struct FlashOpts {
6161
pub monitor: bool,
6262
}
6363

64-
#[derive(Parser, Debug)]
64+
#[derive(Debug, Clone, Parser)]
6565
pub struct FlashConfigOpts {
6666
/// Flash mode to use
6767
#[clap(short = 'm', long, possible_values = FlashMode::VARIANTS, value_name = "MODE")]
@@ -74,7 +74,7 @@ pub struct FlashConfigOpts {
7474
pub flash_freq: Option<FlashFrequency>,
7575
}
7676

77-
#[derive(Parser, Debug)]
77+
#[derive(Clone, Debug, Parser)]
7878
pub struct PartitionTableOpts {
7979
/// Convert CSV parition table to binary representation
8080
#[clap(long, required_unless_present_any = ["info", "to-csv"])]
@@ -92,7 +92,7 @@ pub struct PartitionTableOpts {
9292
output: Option<PathBuf>,
9393
}
9494

95-
#[derive(Parser, Debug)]
95+
#[derive(Clone, Debug, Parser)]
9696
pub struct WriteBinToFlashOpts {
9797
/// Address at which to write the binary file
9898
#[clap(value_parser = parse_u32)]
@@ -138,7 +138,12 @@ pub fn connect(opts: &ConnectOpts, config: &Config) -> Result<Flasher> {
138138
_ => unreachable!(),
139139
};
140140

141-
Ok(Flasher::connect(serial, port_info, opts.speed, opts.use_stub)?)
141+
Ok(Flasher::connect(
142+
serial,
143+
port_info,
144+
opts.speed,
145+
opts.use_stub,
146+
)?)
142147
}
143148

144149
pub fn board_info(opts: ConnectOpts, config: Config) -> Result<()> {

0 commit comments

Comments
 (0)