Skip to content

Commit

Permalink
Set web-client log level from web-server (#659)
Browse files Browse the repository at this point in the history
Fixes #588
  • Loading branch information
ia0 authored Oct 28, 2024
1 parent 4cf9d5a commit 0275557
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/runner-host/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/runner-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2021"
anyhow = "1.0.86"
clap = { version = "4.5.4", features = ["derive"] }
data-encoding = "2.6.0"
env_logger = "0.11.3"
env_logger = { version = "0.11.3", optional = true }
rand = "0.8.5"
tokio = { version = "1.40.0", features = ["full"] }
usb-device = "0.3.2"
Expand Down Expand Up @@ -54,10 +54,12 @@ features = [
[features]
# Exactly one is enabled by xtask.
debug = [
"dep:env_logger",
"wasefire-logger/log",
"wasefire-protocol-tokio/log",
"wasefire-protocol-usb/log",
"wasefire-scheduler/log",
"web-server/log",
]
release = []
# Exactly one is enabled by xtask.
Expand Down
14 changes: 13 additions & 1 deletion crates/runner-host/crates/web-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(try_blocks)]

use wasm_bindgen::JsValue;
use web_sys::console::{info_1, warn_1};

mod app;
mod board;
mod board_components;
mod console;
mod hooks;

fn main() {
wasm_logger::init(wasm_logger::Config::default());
match gloo_utils::window().location().search() {
Ok(x) if x.is_empty() => info_1(&JsValue::from_str("logging disabled")),
Ok(x) => match try { x.strip_prefix("?log=")?.parse().ok()? } {
None => warn_1(&JsValue::from_str("failed to parse location search for log")),
Some(level) => wasm_logger::init(wasm_logger::Config::new(level)),
},
Err(x) => warn_1(&x),
}
yew::Renderer::<app::App>::new().render();
}
4 changes: 4 additions & 0 deletions crates/runner-host/crates/web-server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/runner-host/crates/web-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.86"
futures-util = "0.3.30"
log = { version = "0.4.21", optional = true }
opener = "0.7.1"
serde_json = "1.0.117"
tokio = { version = "1.40.0", features = ["full", "rt-multi-thread", "sync"] }
Expand All @@ -17,6 +18,9 @@ wasefire-logger = { path = "../../../logger" }
wasefire-protocol = { path = "../../../protocol" }
web-common = { path = "../web-common" }

[features]
log = ["dep:log", "wasefire-logger/log"]

[lints]
clippy.unit-arg = "allow"
rust.unsafe-op-in-unsafe-fn = "warn"
Expand Down
6 changes: 5 additions & 1 deletion crates/runner-host/crates/web-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ impl Client {
let routes = warp::get().and(static_files.or(ws));

tokio::spawn(async move { warp::serve(routes).run(addr).await });
let url = format!("http://{addr}/");
#[cfg(feature = "log")]
let search = format!("?log={}", ::log::max_level());
#[cfg(not(feature = "log"))]
let search = "";
let url = format!("http://{addr}/{search}");

// Wait 2 seconds for a client to connect, otherwise open a browser. The client is supposed
// to connect every second. This should ensure that at most one client is open.
Expand Down
2 changes: 1 addition & 1 deletion crates/runner-host/crates/web-server/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ set -e
test_helper

cargo test --lib
cargo check --lib --features=wasefire-logger/log
cargo check --lib --features=log
1 change: 1 addition & 0 deletions crates/runner-host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum Interface {

#[tokio::main]
async fn main() -> Result<()> {
#[cfg(feature = "debug")]
env_logger::init();
LazyLock::force(&FLAGS);
std::panic::set_hook(Box::new(|info| {
Expand Down
2 changes: 2 additions & 0 deletions crates/runner-host/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tokio::io::AsyncWriteExt as _;
use tokio::process::Command;
use tokio::sync::mpsc::channel;
use wasefire_cli_tools::{cmd, fs};
use wasefire_logger as log;

use crate::{board, cleanup, with_state, FLAGS};

Expand All @@ -36,6 +37,7 @@ pub async fn init() -> Result<web_server::Client> {
});
let web_dir = FLAGS.dir.join("web");
if !fs::exists(&web_dir).await {
log::info!("Extracting web assets to {}", web_dir.display());
let mut tar = Command::new("tar");
tar.current_dir(&FLAGS.dir);
tar.arg("xz");
Expand Down
3 changes: 3 additions & 0 deletions crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ impl RunnerOptions {
if let Some(serial) = &self.serial {
cargo.env("WASEFIRE_HOST_SERIAL", serial);
}
if fs::exists("target/wasefire/web").await {
fs::remove_dir_all("target/wasefire/web").await?;
}
cmd::execute(Command::new("make").current_dir("crates/runner-host/crates/web-client"))
.await?;
}
Expand Down

0 comments on commit 0275557

Please sign in to comment.