From 80205eddcd07f6fb39316ae23a64f444d2df8670 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Arcos Date: Tue, 17 Oct 2023 15:58:43 +0200 Subject: [PATCH] Improve logger fmt and logging messages (#375) * chore: Update logger fmt and export file log * chore: Update log messages * chore: Log level is now lowercase * refactor: Merge install and update methods --- src/env.rs | 12 +++++++----- src/lib.rs | 11 +++++++++-- src/main.rs | 25 ++++++------------------- src/toolchain/gcc.rs | 2 +- src/toolchain/llvm.rs | 2 +- src/toolchain/mod.rs | 19 ++++++++++++++++--- src/toolchain/rust.rs | 2 +- 7 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/env.rs b/src/env.rs index b126e9f1..d57d5750 100644 --- a/src/env.rs +++ b/src/env.rs @@ -2,7 +2,9 @@ use crate::{emoji, error::Error}; use directories::BaseDirs; -use log::{info, warn}; +use log::info; +#[cfg(windows)] +use log::warn; #[cfg(windows)] use std::env; use std::{ @@ -98,13 +100,13 @@ pub fn export_environment(export_file: &Path) -> Result<(), Error> { } #[cfg(unix)] if cfg!(unix) { - warn!( - "{} Please, set up the environment variables by running: '. {}'", + println!( + "\n\t{} To get started, you need to set up some environment variables by running: '. {}'", emoji::INFO, export_file.display() ); - warn!( - "{} This step must be done every time you open a new terminal", + println!( + "\t{} This step must be done every time you open a new terminal.\n\t See other methods for setting the environment in https://esp-rs.github.io/book/installation/riscv-and-xtensa.html#3-set-up-the-environment-variables", emoji::WARN ); } diff --git a/src/lib.rs b/src/lib.rs index 41ce7db4..45c3f572 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,8 +12,15 @@ pub mod logging { /// Initializes the logger pub fn initialize_logger(log_level: &str) { Builder::from_env(Env::default().default_filter_or(log_level)) - .format_target(false) - .format_timestamp_secs() + .format(|buf, record| { + use std::io::Write; + writeln!( + buf, + "[{}]: {}", + record.level().to_string().to_lowercase(), + record.args() + ) + }) .write_style(WriteStyle::Always) .init(); } diff --git a/src/main.rs b/src/main.rs index 9a6c01ca..6557d989 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use espup::{ logging::initialize_logger, toolchain::{ gcc::uninstall_gcc_toolchains, install as toolchain_install, llvm::Llvm, - rust::get_rustup_home, + rust::get_rustup_home, InstallMode, }, update::check_for_update, }; @@ -59,14 +59,12 @@ async fn completions(args: CompletionsOpts) -> Result<()> { Ok(()) } -/// Installs the Rust for ESP chips environment -async fn install(args: InstallOpts) -> Result<()> { +/// Installs or updates the Rust for ESP chips environment +async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()> { initialize_logger(&args.log_level); check_for_update(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); - info!("{} Installing the Espressif Rust ecosystem", emoji::DISC); - toolchain_install(args).await?; - info!("{} Installation successfully completed!", emoji::CHECK); + toolchain_install(args, install_mode).await?; Ok(()) } @@ -97,23 +95,12 @@ async fn uninstall(args: UninstallOpts) -> Result<()> { Ok(()) } -/// Updates Xtensa Rust toolchain. -async fn update(args: InstallOpts) -> Result<()> { - initialize_logger(&args.log_level); - check_for_update(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); - - info!("{} Updating Espressif Rust ecosystem", emoji::DISC); - toolchain_install(args).await?; - info!("{} Update successfully completed!", emoji::CHECK); - Ok(()) -} - #[tokio::main] async fn main() -> Result<()> { match Cli::parse().subcommand { SubCommand::Completions(args) => completions(args).await, - SubCommand::Install(args) => install(*args).await, - SubCommand::Update(args) => update(*args).await, + SubCommand::Install(args) => install(*args, InstallMode::Install).await, + SubCommand::Update(args) => install(*args, InstallMode::Update).await, SubCommand::Uninstall(args) => uninstall(args).await, } } diff --git a/src/toolchain/gcc.rs b/src/toolchain/gcc.rs index 187fe5c1..6893ec99 100644 --- a/src/toolchain/gcc.rs +++ b/src/toolchain/gcc.rs @@ -128,7 +128,7 @@ fn get_artifact_extension(host_triple: &HostTriple) -> &str { /// Checks if the toolchain is pressent, if present uninstalls it. pub fn uninstall_gcc_toolchains(toolchain_path: &Path) -> Result<(), Error> { - info!("{} Uninstalling GCC toolchain", emoji::WRENCH); + info!("{} Uninstalling GCC", emoji::WRENCH); let gcc_toolchains = vec![XTENSA_GCC, RISCV_GCC]; diff --git a/src/toolchain/llvm.rs b/src/toolchain/llvm.rs index d3ee661b..2ff7d225 100644 --- a/src/toolchain/llvm.rs +++ b/src/toolchain/llvm.rs @@ -156,7 +156,7 @@ impl Installable for Llvm { self.path.to_str().unwrap() ); } else { - info!("{} Installing Xtensa elf Clang", emoji::WRENCH); + info!("{} Installing Xtensa LLVM", emoji::WRENCH); download_file( self.repository_url.clone(), "idf_tool_xtensa_elf_clang.tar.xz", diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index fdac3fd4..090a97a8 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -35,6 +35,11 @@ pub mod gcc; pub mod llvm; pub mod rust; +pub enum InstallMode { + Install, + Update, +} + #[async_trait] pub trait Installable { /// Install some application, returning a vector of any required exports @@ -108,7 +113,7 @@ pub async fn download_file( } "gz" => { info!( - "{} Uncompressing tar.gz file to '{}'", + "{} Extracting tar.gz file to '{}'", emoji::WRENCH, output_directory ); @@ -120,7 +125,7 @@ pub async fn download_file( } "xz" => { info!( - "{} Uncompressing tar.xz file to '{}'", + "{} Extracting tar.xz file to '{}'", emoji::WRENCH, output_directory ); @@ -142,7 +147,11 @@ pub async fn download_file( } /// Installs or updates the Espressif Rust ecosystem. -pub async fn install(args: InstallOpts) -> Result<()> { +pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()> { + match install_mode { + InstallMode::Install => info!("{} Installing the Espressif Rust ecosystem", emoji::DISC), + InstallMode::Update => info!("{} Updating the Espressif Rust ecosystem", emoji::DISC), + } let export_file = get_export_file(args.export_file)?; let mut exports: Vec = Vec::new(); let host_triple = get_host_triple(args.default_host)?; @@ -262,6 +271,10 @@ pub async fn install(args: InstallOpts) -> Result<()> { } create_export_file(&export_file, &exports)?; + match install_mode { + InstallMode::Install => info!("{} Installation successfully completed!", emoji::CHECK), + InstallMode::Update => info!("{} Update successfully completed!", emoji::CHECK), + } export_environment(&export_file)?; Ok(()) } diff --git a/src/toolchain/rust.rs b/src/toolchain/rust.rs index eca47bad..8ca67c99 100644 --- a/src/toolchain/rust.rs +++ b/src/toolchain/rust.rs @@ -363,7 +363,7 @@ impl RiscVTarget { impl Installable for RiscVTarget { async fn install(&self) -> Result, Error> { info!( - "{} Installing RISC-V targets ('riscv32imc-unknown-none-elf' and 'riscv32imac-unknown-none-elf') for '{}' toolchain", + "{} Installing RISC-V Rust targets ('riscv32imc-unknown-none-elf' and 'riscv32imac-unknown-none-elf') for '{}' toolchain", emoji::WRENCH, &self.nightly_version );