From 4af76e3d179de32bdbaa08dac4a587f36a62ad87 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 30 Oct 2024 10:43:59 +0100 Subject: [PATCH] Use proper logging --- Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++- src/main.rs | 22 ++++++++++------ 3 files changed, 92 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 966a61f..10b84fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,6 +16,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.18" @@ -236,6 +245,29 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -258,7 +290,9 @@ version = "0.1.0" dependencies = [ "clap", "crossterm", + "env_logger", "esp-metadata", + "log", "quote", "ratatui", "rhai", @@ -318,6 +352,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "indoc" version = "2.0.5" @@ -401,6 +441,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "mio" version = "1.0.2" @@ -506,6 +552,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rhai" version = "1.19.0" diff --git a/Cargo.toml b/Cargo.toml index 76e7cf9..d747fc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,10 @@ edition = "2021" [dependencies] clap = { version = "4.5.20", features = ["derive"] } -crossterm = { version = "0.28.1" } # Maybe this gives Windows issues like espflash? +crossterm = "0.28.1" +env_logger = "0.11.5" esp-metadata = { version = "0.4.0", features = ["clap"] } +log = "0.4.22" ratatui = { version = "0.29.0", features = ["crossterm"] } rhai = "1.19.0" diff --git a/src/main.rs b/src/main.rs index d8f492e..9542041 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::{ }; use clap::Parser; +use env_logger::{Builder, Env}; use esp_metadata::Chip; mod template_files; @@ -178,6 +179,10 @@ struct Args { } fn main() { + Builder::from_env(Env::default().default_filter_or(log::LevelFilter::Info.as_str())) + .format_target(false) + .init(); + let args = Args::parse(); let path = &args @@ -186,12 +191,12 @@ fn main() { .unwrap_or_else(|| env::current_dir().unwrap()); if !path.is_dir() { - eprintln!("Output path must be a directory"); + log::error!("Output path must be a directory"); process::exit(-1); } if path.join(&args.name).exists() { - eprintln!("Directory already exists"); + log::error!("Directory already exists"); process::exit(-1); } @@ -271,7 +276,7 @@ fn main() { .output() .unwrap(); } else { - eprintln!("Current directory is already in a git repository, skipping git initialization"); + log::warn!("Current directory is already in a git repository, skipping git initialization"); } } @@ -336,7 +341,7 @@ fn process_file( // that's a bad workaround if trimmed == "#[rustfmt::skip]" { - println!("Skipping rustfmt"); + log::info!("Skipping rustfmt"); continue; } @@ -404,9 +409,10 @@ fn process_options(args: &Args) { if !option_item.chips().iter().any(|chip| chip == &args.chip) && !option_item.chips().is_empty() { - eprintln!( - "Error: Option '{}' is not supported for chip {}", - option, args.chip + log::error!( + "Option '{}' is not supported for chip {}", + option, + args.chip ); process::exit(-1); } @@ -414,7 +420,7 @@ fn process_options(args: &Args) { } if args.option.contains(&String::from("ble")) && args.option.contains(&String::from("wifi")) { - eprintln!("Error: Options 'ble' and 'wifi' are mutually exclusive"); + log::error!("Options 'ble' and 'wifi' are mutually exclusive"); process::exit(-1); } }