Skip to content

Carriage Return "\r" character on stderr #16108

Closed

Description

@jozefchutka

Can you help me understand why emscripten behaves differently on stderr message ending with \n vs. \r.

Here is a necessary code change in ffmpeg code changing \r for \n so emscripten properly calls printErr() callback for every single message. Using the original \r it would only call printErr() once at the very end with all the messages concatenated together.

Or is there a compile time flag in emscripten that can be used so no code change is needed?

Activity

  1. sbc100 commented on Jan 27, 2022

    @sbc100
    Collaborator

    The printErr callback mechanism doesn't currently have any way to represent a carriage return, or any kind of partial line of output. The only thing it supports is a complete line of output (that ends in a newline). IIUC this is due to the fact the its default implementation is based on console.error which is fundamentally line-based.. so it has to wait for a compete line.

    \r essentially erases the current line before it is complete and overwrites it with new content. It assumes that some kind of tty or console is attached, and this is not compatible with JavaScripts console.log/warning/error API. We would need to extent the printErr mechanism within emscripten to support something like that. Its not inconceivable but its also not obvious (to me) how to make this work.

  2. changed the title [-]newline character on stderr[/-] [+]Carriage Return "\r" character on stderr[/+] on Jan 27, 2022
  3. jozefchutka commented on Jan 27, 2022

    @jozefchutka
    Author

    Thanks for the insights @sbc100 . Is this something that can be considered a feature request? I guess there is plenty of libs that can benefit from reading carriage return line "updates"

  4. sbc100 commented on Jan 27, 2022

    @sbc100
    Collaborator

    Yup, I think it would be nice to have a better story for TTY/console output.

  5. jozefchutka commented on Apr 15, 2022

    @jozefchutka
    Author

    I ended up using stderr/stdout instead of print and printErr, this way my code gets notified asap with any incomming data, however byte-by-byte by default.

    Furher I modified compiled myModule.js and replaced its plain content:

    for(var i=0;i<length;i++){try{output(buffer[offset+i])}catch(e){throw new FS.ErrnoError(29)}}

    by:

    let i = length;try{output(buffer, offset, length)}catch(e){throw new FS.ErrnoError(29)}

    Now I can read a whole chunk of bytes at once, with some more logic for buffering, \n and \r detection etc.:

    stderr:(buffer, offset, length) => {
        const arrayBuffer = buffer.slice(offset, offset + length).buffer;
        const text = new TextDecoder("utf8").decode(arrayBuffer);

    Its a bit ugly but fast. I am wondering if there are options:

    • for more elegant solution without content replacing (stackoverflow)
    • or event better, mount my custom stderr/stdout and read bytes from some kind of callback without these data ever ending up in main memory buffer

    Or should I start another thread for seek help / requesting such design?

  6. jozefchutka commented on Apr 19, 2022

    @jozefchutka
    Author

    Closing this issue in favor of #16755

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions