-
Notifications
You must be signed in to change notification settings - Fork 156
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
Comments
This is a very good idea, it would simplify web development with seed a lot! Especially if the |
That'd be great; didn't think it was possible. The |
Agreed on this. |
News:
|
Nice research. Want to close this one until a later later time? I don't have any insight on how to implement. |
@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. |
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 Unfortunately writing a Rust RFC to make native |
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 |
Consider also supplying an implementation of the /// 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) {}
} |
obsolete since v0.10.0 |
Redirect
println!
and similar functions toconsole.log
.Motivation:
console.log
wrappers.Inspiration for implementation:
https://github.com/DeMille/wasm-glue
The text was updated successfully, but these errors were encountered: