Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Redirect println! to console.log #292

Closed
MartinKavik opened this issue Nov 21, 2019 · 11 comments
Closed

Proposal: Redirect println! to console.log #292

MartinKavik opened this issue Nov 21, 2019 · 11 comments
Labels
enhancement New feature or request

Comments

@MartinKavik
Copy link
Member

MartinKavik commented Nov 21, 2019

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

@Ivshti
Copy link

Ivshti commented Nov 21, 2019

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

@David-OConnor
Copy link
Member

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.

@rebo
Copy link
Collaborator

rebo commented Nov 30, 2019

Agreed on this.

@MartinKavik
Copy link
Member Author

MartinKavik commented Dec 3, 2019

News:

@David-OConnor
Copy link
Member

David-OConnor commented Dec 3, 2019

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

@MartinKavik
Copy link
Member Author

@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.

@entropylost
Copy link

Any updates on this?

@MartinKavik
Copy link
Member Author

Any updates on this?

See DeMille/wasm-glue#3 and rust-lang/rust#31343 (comment).

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).

@kurtbuilds
Copy link

kurtbuilds commented May 19, 2022

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

@fosskers
Copy link
Collaborator

fosskers commented May 19, 2022

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) {}
}

@flosse
Copy link
Member

flosse commented Mar 7, 2023

obsolete since v0.10.0

@flosse flosse closed this as completed Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants