seed icon indicating copy to clipboard operation
seed copied to clipboard

Proposal: Redirect println! to console.log

Open MartinKavik opened this issue 6 years ago • 10 comments

Redirect println! and similar functions to console.log.

Motivation:

  • We can use standard Rust functions for logging / debugging.
  • It will be easier for beginners to learn and play with Seed.
  • We don't need to maintain our custom console.log wrappers.

Inspiration for implementation: https://github.com/DeMille/wasm-glue

MartinKavik avatar Nov 21 '19 19:11 MartinKavik

This is a very good idea, it would simplify web development with seed a lot! Especially if the dbg!() macro works as well

Ivshti avatar Nov 21 '19 19:11 Ivshti

That'd be great; didn't think it was possible. The log! macro's still useful for showing multiple things, and error for showing red text in the console, but native rust println! is a better default.

David-OConnor avatar Nov 21 '19 20:11 David-OConnor

Agreed on this.

rebo avatar Nov 30 '19 06:11 rebo

News:

MartinKavik avatar Dec 03 '19 00:12 MartinKavik

Nice research. Want to close this one until a later later time? I don't have any insight on how to implement.

David-OConnor avatar Dec 03 '19 01:12 David-OConnor

@David-OConnor I suggest to leave it open for a few days so other guys can see this issue and Rust developers know that we (WASM app developers) want this feature.

MartinKavik avatar Dec 03 '19 01:12 MartinKavik

Any updates on this?

entropylost avatar Jul 18 '21 01:07 entropylost

Any updates on this?

See https://github.com/DeMille/wasm-glue/issues/3 and https://github.com/rust-lang/rust/issues/31343#issuecomment-812636785.

So I've implemented custom println & eprintln in MoonZoon. This way you get at least a compilation error when those custom methods are in scope imported by zoon::* to prevent silent fails. I'm not sure if we want it in Seed, log! is good enough I think.

Unfortunately writing a Rust RFC to make native println-like functions work isn't my priority. Also we should take into account std::fmt may increase Wasm file size a lot so I'm not sure how it should be actually implemented to be universal enough (to support more lightweight fmt machinery).

MartinKavik avatar Jul 18 '21 09:07 MartinKavik

I'd love to see this too. FWIW, it's possible to hack this redirection today in nightly Rust:

#![feature(internal_output_capture)]


#[wasm_bindgen]
pub fn start() {
    let z = Arc::new(Mutex::new(Vec::new()));
    let mut idx = 0usize;
    /// This redirects all stderr and stdout to the buffer contained in `z`.
    std::io::set_output_capture(Some(z.clone()));

    println!("Hello, world!");

    let x = z.lock().unwrap();
    let prev_idx = idx;
    idx = x.len();
    let z = std::str::from_utf8(&x[prev_idx..idx]).unwrap();
    log(&z);
}

I'm experimenting with a loop that would offload the de-buffering to a webworker so it happens continuously in the background.

That said, it feels like the proper solution is a wasm32-unknown-browser target that would (IMO correctly) direct println! to console.log

kurtbuilds avatar May 19 '22 20:05 kurtbuilds

Consider also supplying an implementation of the log crate traits, like this:

/// A simple implementation of [`log::Log`].
pub struct SeedLogger;

impl SeedLogger {
    /// Initialise the logger.
    ///
    /// You must only call this once in an application!
    pub fn init() -> Result<(), SetLoggerError> {
        log::set_logger(&SeedLogger).map(|_| log::set_max_level(FILTER))
    }
}

impl log::Log for SeedLogger {
    fn enabled(&self, metadata: &Metadata<'_>) -> bool {
        metadata.level() <= LEVEL
    }

    fn log(&self, record: &Record<'_>) {
        let m = record.metadata();

        if self.enabled(m) {
            let target = m.target();

            seed::log!(format!("[{}] ({}) {}", m.level(), target, record.args()));
        }
    }

    fn flush(&self) {}
}

fosskers avatar May 19 '22 20:05 fosskers

obsolete since v0.10.0

flosse avatar Mar 07 '23 00:03 flosse