kleisauke / wasm-vips

libvips for the browser and Node.js, compiled to WebAssembly with Emscripten.
https://kleisauke.github.io/wasm-vips/
MIT License
463 stars 25 forks source link

Support custom `stdout` and `stderr` #23

Closed atjn closed 1 year ago

atjn commented 1 year ago

I am using wasm-vips as a dependency for a CLI tool. Currently, if vips encounters a warning, it will output it directly to the console. This is pretty annoying because the CLI tool already is outputting information to the console, so now there are random fragments of vips warnings cluttered in with the rest of the information.

I would like to be able to receive the warnings in a function, where I can write my own logic for when and where the output should be logged. If that is not possible, the next best thing would be to tell wasm-vips to never output anything.

Maybe this is already supported? But I can't seem to figure out how it works. I have already tried this, as an attempt to make it stop outputting to the CLI, but it did not work:

import newVips from "wasm-vips";
const vips = await newVips({
    print: (stdout) => {return ""},
    printErr: (stderr) => {return ""},
    preRun: module => {
        module.print = (stdout) => {return ""};
        module.printErr = (stderr) => {return ""};
    },
});
kleisauke commented 1 year ago

Hi @atjn,

It looks like a bug in Emscripten's NODERAWFS filesystem backend, on the web this works properly:

import Vips from 'wasm-vips';

const log = (type, text) => console.log(`${type}: ${text}`);
const vips = await Vips({
  print: (text) => log('stdout', text),
  printErr: (text) => log('stderr', text),
  preRun: (module) => {
    // Handy for debugging
    // module.ENV.VIPS_INFO = '1';
    // module.ENV.VIPS_LEAK = '1';

    // Hide warning messages
    module.ENV.VIPS_WARNING = '0';
  }
});

For now, I can recommend setting the VIPS_WARNING environment variable, as shown in the example above.

Upstream issue: https://github.com/emscripten-core/emscripten/issues/17688

atjn commented 1 year ago

Thank you so much for the absolutely excellent debug work here!

kleisauke commented 1 year ago

Upstream PR: https://github.com/emscripten-core/emscripten/pull/18163.

kleisauke commented 1 year ago

I just cherry-picked that PR in the https://github.com/emscripten-core/emscripten/compare/3.1.29...kleisauke:wasm-vips-3.1.29 changeset with commit https://github.com/kleisauke/emscripten/commit/cf6cdde22382590929698e570f26afce0d0c0a1f.

This will be in v0.0.5.

kleisauke commented 1 year ago

v0.0.5 now available.