From b44a7429bfd36fc139433b37c3dc90068da42e46 Mon Sep 17 00:00:00 2001 From: sea-grass Date: Wed, 20 Mar 2024 15:04:48 -0400 Subject: [PATCH 1/2] feat: Add `--log-level` option to wws --- Cargo.lock | 5 +++-- Cargo.toml | 1 + src/main.rs | 28 +++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a1209de..d22c82c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1677,9 +1677,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.20" +version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "mach" @@ -3150,6 +3150,7 @@ dependencies = [ "anyhow", "clap", "env_logger 0.10.2", + "log", "prettytable-rs", "reqwest", "wws-config", diff --git a/Cargo.toml b/Cargo.toml index d70d0d0b..bd65d811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ path = "src/main.rs" [dependencies] actix-web = { workspace = true } anyhow = "1.0.66" +log = "0.4.21" env_logger = "0.10.0" clap = { version = "4.0.10", features = ["derive"] } prettytable-rs = "0.10.0" diff --git a/src/main.rs b/src/main.rs index 9faa6862..b344c996 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,13 @@ use commands::main::Main; use commands::runtimes::RuntimesCommands; use std::io::{Error, ErrorKind}; use std::path::PathBuf; +use std::str::FromStr; use std::process::exit; use wws_config::Config; use wws_project::{identify_type, prepare_project, ProjectType}; use wws_router::Routes; use wws_server::{serve, ServeOptions}; +use log::LevelFilter; // Arguments #[derive(Parser, Debug)] @@ -45,6 +47,9 @@ pub struct Args { #[arg(long, short)] install_runtimes: bool, + #[arg(short, long, value_parser = LevelFilter::from_str, default_value = "info")] + log_level: LevelFilter, + /// Set the commit when using a git repository as project #[arg(long)] git_commit: Option, @@ -78,7 +83,20 @@ pub struct Args { async fn main() -> std::io::Result<()> { let args = Args::parse(); - std::env::set_var("RUST_LOG", "actix_web=info"); + match std::env::var("RUST_LOG") { + Ok(_) => {}, + Err(_) => { + match args.log_level { + LevelFilter::Off => std::env::set_var("RUST_LOG", ""), + LevelFilter::Error => std::env::set_var("RUST_LOG", "actix_web=error"), + LevelFilter::Warn => std::env::set_var("RUST_LOG", "actix_web=warn"), + LevelFilter::Info => std::env::set_var("RUST_LOG", "actix_web=info"), + LevelFilter::Debug => std::env::set_var("RUST_LOG", "actix_web=debug"), + LevelFilter::Trace => std::env::set_var("RUST_LOG", "actix_web=trace"), + } + }, + } + env_logger::init(); // Check the given subcommand @@ -185,14 +203,14 @@ async fn main() -> std::io::Result<()> { args.port, route.path, route.handler.display() - ); + ); } if args.enable_panel { println!( "🎛️ The admin panel is available at http://{}:{}/_panel/", &args.hostname, args.port - ); + ); } let server = serve(ServeOptions { @@ -204,12 +222,12 @@ async fn main() -> std::io::Result<()> { cors_origins: args.cors, }) .await - .map_err(|err| Error::new(ErrorKind::AddrInUse, err))?; + .map_err(|err| Error::new(ErrorKind::AddrInUse, err))?; println!( "🚀 Start serving requests at http://{}:{}\n", args.hostname, args.port - ); + ); // Run the server server.await From fba1f3c0ec8c0a76ffb03ced5ac01928e4a8910e Mon Sep 17 00:00:00 2001 From: sea-grass Date: Wed, 20 Mar 2024 15:18:04 -0400 Subject: [PATCH 2/2] chore: run cargo fmt --- src/main.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index b344c996..5c0dc9c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,15 +9,15 @@ use crate::utils::runtimes::install_missing_runtimes; use clap::Parser; use commands::main::Main; use commands::runtimes::RuntimesCommands; +use log::LevelFilter; use std::io::{Error, ErrorKind}; use std::path::PathBuf; -use std::str::FromStr; use std::process::exit; +use std::str::FromStr; use wws_config::Config; use wws_project::{identify_type, prepare_project, ProjectType}; use wws_router::Routes; use wws_server::{serve, ServeOptions}; -use log::LevelFilter; // Arguments #[derive(Parser, Debug)] @@ -84,16 +84,14 @@ async fn main() -> std::io::Result<()> { let args = Args::parse(); match std::env::var("RUST_LOG") { - Ok(_) => {}, - Err(_) => { - match args.log_level { - LevelFilter::Off => std::env::set_var("RUST_LOG", ""), - LevelFilter::Error => std::env::set_var("RUST_LOG", "actix_web=error"), - LevelFilter::Warn => std::env::set_var("RUST_LOG", "actix_web=warn"), - LevelFilter::Info => std::env::set_var("RUST_LOG", "actix_web=info"), - LevelFilter::Debug => std::env::set_var("RUST_LOG", "actix_web=debug"), - LevelFilter::Trace => std::env::set_var("RUST_LOG", "actix_web=trace"), - } + Ok(_) => {} + Err(_) => match args.log_level { + LevelFilter::Off => std::env::set_var("RUST_LOG", ""), + LevelFilter::Error => std::env::set_var("RUST_LOG", "actix_web=error"), + LevelFilter::Warn => std::env::set_var("RUST_LOG", "actix_web=warn"), + LevelFilter::Info => std::env::set_var("RUST_LOG", "actix_web=info"), + LevelFilter::Debug => std::env::set_var("RUST_LOG", "actix_web=debug"), + LevelFilter::Trace => std::env::set_var("RUST_LOG", "actix_web=trace"), }, } @@ -203,14 +201,14 @@ async fn main() -> std::io::Result<()> { args.port, route.path, route.handler.display() - ); + ); } if args.enable_panel { println!( "🎛️ The admin panel is available at http://{}:{}/_panel/", &args.hostname, args.port - ); + ); } let server = serve(ServeOptions { @@ -222,12 +220,12 @@ async fn main() -> std::io::Result<()> { cors_origins: args.cors, }) .await - .map_err(|err| Error::new(ErrorKind::AddrInUse, err))?; + .map_err(|err| Error::new(ErrorKind::AddrInUse, err))?; println!( "🚀 Start serving requests at http://{}:{}\n", args.hostname, args.port - ); + ); // Run the server server.await