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
This commit is contained in:
Sergio Gasquez Arcos 2023-10-17 15:58:43 +02:00 committed by GitHub
parent 3910ea36fd
commit 80205eddcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 32 deletions

View File

@ -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
);
}

View File

@ -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();
}

View File

@ -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,
}
}

View File

@ -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];

View File

@ -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",

View File

@ -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<String> = 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(())
}

View File

@ -363,7 +363,7 @@ impl RiscVTarget {
impl Installable for RiscVTarget {
async fn install(&self) -> Result<Vec<String>, 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
);