From 056d935fcdf5da6747c60ed02fdd7c618d3ffdaa Mon Sep 17 00:00:00 2001 From: Jason K Hall <106277735+Jkhall81@users.noreply.github.com> Date: Thu, 15 Jan 2026 01:44:54 -0700 Subject: [PATCH] wasm: Require explicit logging initialization (#22587) Co-authored-by: Micha Reiser --- crates/ruff_wasm/src/lib.rs | 34 ++++++++++++++++++++++++--- crates/ty_wasm/src/lib.rs | 34 ++++++++++++++++++++++++--- playground/ruff/src/Editor/Chrome.tsx | 11 +++++++-- playground/ty/src/Playground.tsx | 7 ++++++ 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 326dc2b194..9a62704977 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -99,8 +99,6 @@ pub fn before_main() {} #[wasm_bindgen(start)] pub fn run() { - use log::Level; - before_main(); // When the `console_error_panic_hook` feature is enabled, we can call the @@ -111,8 +109,38 @@ pub fn run() { // https://github.com/rustwasm/console_error_panic_hook#readme #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); +} - console_log::init_with_level(Level::Debug).expect("Initializing logger went wrong."); +/// Initializes the logger with the given log level. +/// +/// ## Panics +/// If this function is called more than once. +#[wasm_bindgen(js_name = "initLogging")] +pub fn init_logging(level: LogLevel) { + console_log::init_with_level(level.into()) + .expect("`initLogging` to only be called at most once."); +} + +#[derive(Copy, Clone, Debug)] +#[wasm_bindgen] +pub enum LogLevel { + Trace, + Debug, + Info, + Warn, + Error, +} + +impl From for log::Level { + fn from(level: LogLevel) -> Self { + match level { + LogLevel::Trace => log::Level::Trace, + LogLevel::Debug => log::Level::Debug, + LogLevel::Info => log::Level::Info, + LogLevel::Warn => log::Level::Warn, + LogLevel::Error => log::Level::Error, + } + } } #[wasm_bindgen] diff --git a/crates/ty_wasm/src/lib.rs b/crates/ty_wasm/src/lib.rs index 05cd77da00..9cf4f130a3 100644 --- a/crates/ty_wasm/src/lib.rs +++ b/crates/ty_wasm/src/lib.rs @@ -58,8 +58,6 @@ pub fn before_main() {} #[wasm_bindgen(start)] pub fn run() { - use log::Level; - before_main(); ruff_db::set_program_version(version()).unwrap(); @@ -72,8 +70,38 @@ pub fn run() { // https://github.com/rustwasm/console_error_panic_hook#readme #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); +} - console_log::init_with_level(Level::Debug).expect("Initializing logger went wrong."); +/// Initializes the logger with the given log level. +/// +/// ## Panics +/// If this function is called more than once. +#[wasm_bindgen(js_name = "initLogging")] +pub fn init_logging(level: LogLevel) { + console_log::init_with_level(level.into()) + .expect("`initLogging` to only be called at most once."); +} + +#[derive(Copy, Clone, Debug)] +#[wasm_bindgen] +pub enum LogLevel { + Trace, + Debug, + Info, + Warn, + Error, +} + +impl From for log::Level { + fn from(level: LogLevel) -> Self { + match level { + LogLevel::Trace => log::Level::Trace, + LogLevel::Debug => log::Level::Debug, + LogLevel::Info => log::Level::Info, + LogLevel::Warn => log::Level::Warn, + LogLevel::Error => log::Level::Error, + } + } } #[wasm_bindgen] diff --git a/playground/ruff/src/Editor/Chrome.tsx b/playground/ruff/src/Editor/Chrome.tsx index be744f2412..d95e538a53 100644 --- a/playground/ruff/src/Editor/Chrome.tsx +++ b/playground/ruff/src/Editor/Chrome.tsx @@ -3,9 +3,9 @@ import { useCallback, useMemo, useRef, useState } from "react"; import { Header, useTheme, setupMonaco } from "shared"; import { persist, persistLocal, restore, stringify } from "./settings"; import { default as Editor, Source } from "./Editor"; -import initRuff, { Workspace } from "ruff_wasm"; import { loader } from "@monaco-editor/react"; import { DEFAULT_PYTHON_SOURCE } from "../constants"; +import { default as initRuff, LogLevel, Workspace } from "ruff_wasm"; export default function Chrome() { const initPromise = useRef>(null); @@ -110,7 +110,14 @@ async function startPlayground(): Promise<{ settings: string; ruffVersion: string; }> { - await initRuff(); + const ruff = await initRuff(); + + if (import.meta.env.DEV) { + ruff.initLogging(LogLevel.Debug); + } else { + ruff.initLogging(LogLevel.Info); + } + const monaco = await loader.init(); setupMonaco(monaco, { diff --git a/playground/ty/src/Playground.tsx b/playground/ty/src/Playground.tsx index f315b8538f..53dfb6ded8 100644 --- a/playground/ty/src/Playground.tsx +++ b/playground/ty/src/Playground.tsx @@ -507,6 +507,13 @@ export interface InitializedPlayground { async function startPlayground(): Promise { const ty = await import("ty_wasm"); await ty.default(); + + if (import.meta.env.DEV) { + ty.initLogging(ty.LogLevel.Debug); + } else { + ty.initLogging(ty.LogLevel.Info); + } + const version = ty.version(); const monaco = await loader.init();