Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logger fmt and logging messages #375

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
);
}
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
25 changes: 6 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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,
}
}
2 changes: 1 addition & 1 deletion src/toolchain/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn get_gcc_name(target: &Target) -> String {

/// 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![ESP32_GCC, ESP32S2_GCC, ESP32S3_GCC, RISCV_GCC];

Expand Down
2 changes: 1 addition & 1 deletion src/toolchain/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 16 additions & 3 deletions src/toolchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand All @@ -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)?;
Expand Down Expand Up @@ -261,6 +270,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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/toolchain/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,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
);
Expand Down