Emscripten: `printf` Does Not Get Output to the JavaScript `console.log`

Created on 9 Apr 2012  路  6Comments  路  Source: emscripten-core/emscripten

Hi,

The old version (April 6, 2012, 17:00 MDT) is working.

The latest version (April 9, 2012, 10:00 MDT) is not working with the printf.

Here are steps to reproduce it:

1. The source file main.cpp

plee@sos:~/Dev/emscripten/tests/test$ cat main.cpp 
extern "C"
{

#include <stdio.h>

void Test()
{
    printf("This should be output to JavaScript console.log if run on web.");
}

}

int main(int argc, char* argv[])
{
    Test();

    return 0;
}

2. The command to compile it

plee@sos:~/Dev/emscripten/tests/test$ emcc main.cpp -O3 -o main.js -s EXPORTED_FUNCTIONS='["_main", "_Test"]'

3. The test.html page

plee@sos:~/Dev/emscripten/tests/test$ cat test.html 
<html>
  <head>
    <title>Emscripten Test Harness</title>

  </head>
  <body>
    <textarea id='output' rows="2", cols="120" wrap="off"></textarea>
    <br />

<script type='text/javascript'>
var Module = {
    'noInitialRun' : true 
};
</script>
<script type='text/javascript' src='main.js'></script>


<script type='text/javascript'>
function startTest() {
    Module._Test();
    window.alert("Did you see any output at the console?");
}
</script>

<input type="button" onclick="startTest()" id="btnStartTest" name="btnStartTest" value="Start Test" /><br />

  </body>
</html>

Click the Start Test button, you will see the alert message, but no output at the console.

Most helpful comment

If you add a newline at the end of the printf, does it work?

We need to do buffering until newlines for js consoles, might be the cause of this issue.

All 6 comments

If you add a newline at the end of the printf, does it work?

We need to do buffering until newlines for js consoles, might be the cause of this issue.

Here is the \n test source file:

extern "C"
{

#include <stdio.h>

void Test()
{
    printf("This should be output to JavaScript console.log if run on web.");
    printf("1 with slash-n\n");
    printf("2 without slash-n");
}

}

int main(int argc, char* argv[])
{
    Test();

    return 0;
}

hmmm, it's now very confusing to me.

If I set noInitalRun to false, when I load the page and click the button, console output:

This should be output to JavaScript console.log if run on web.1 with slash-n
2 without slash-n
This should be output to JavaScript console.log if run on web.1 with slash-n

If I set noInitalRun to true, when I load the page and click the button, NO console output at all.

It seems the printf has something to do with the value of noInitialRun. Can you try this example and fix the weird behaviors? Thanks.

Should be fixed on incoming. We were not initializing the filesystem when noInitialRun is set, preventing proper printing.

Sounds cool. I suggest adding my simple code into the unit test if possible. Do leave a comment when it's merged to the trunk.

I added something which should properly test this case. But if you want to write a patch with an additional test that would be cool though.

Merged to master.

It works now. Thanks.

Was this page helpful?
0 / 5 - 0 ratings