wasm: Require explicit logging initialization (#22587)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Jason K Hall
2026-01-15 01:44:54 -07:00
committed by GitHub
parent c3ba638ef5
commit 056d935fcd
4 changed files with 78 additions and 8 deletions

View File

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

View File

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

View File

@@ -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 | Promise<void>>(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, {

View File

@@ -507,6 +507,13 @@ export interface InitializedPlayground {
async function startPlayground(): Promise<InitializedPlayground> {
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();