Search code examples
node.jsrustwasi

Calling console.log from rust wasm32-wasi without the need for ssvm/ssvmup


I would like to debug using messages to the console.log.

We have a rust wasm32-wasi function being called from javascript running in nodejs. We can not use ssvm/ssvmup due to other restrictions.

Is there anything we can do to see messages from our wasm code in the console?


Solution

  • This is answered in The wasm-bindgen Guide: Using console.log:

    • Method #1, binding it manually:

      #[wasm_bindgen]
      extern "C" {
          // Use `js_namespace` here to bind `console.log(..)` instead of just
          // `log(..)`
          #[wasm_bindgen(js_namespace = console)]
          fn log(s: &str);
      
          // The `console.log` is quite polymorphic, so we can bind it with multiple
          // signatures. Note that we need to use `js_name` to ensure we always call
          // `log` in JS.
          #[wasm_bindgen(js_namespace = console, js_name = log)]
          fn log_u32(a: u32);
      
          // Multiple arguments too!
          #[wasm_bindgen(js_namespace = console, js_name = log)]
          fn log_many(a: &str, b: &str);
      }
      
      fn bare_bones() {
          log("Hello from Rust!");
          log_u32(42);
          log_many("Logging", "many values!");
      }
      
    • Method #2, using the web-sys crate:

      fn using_web_sys() {
          use web_sys::console;
      
          console::log_1(&"Hello using web-sys".into());
      
          let js: JsValue = 4.into();
          console::log_2(&"Logging arbitrary values looks like".into(), &js);
      }
      

    Another potential alternative, if you're doing a lot of logging you may want to consider using the log crate with the wasm-logger facade:

    wasm_logger::init(wasm_logger::Config::default());
    
    // Logging
    log::info!("Some info");
    log::error!("Error message");