Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Does console.error writes to stderr ? #11961

Closed
maboiteaspam opened this issue Feb 8, 2014 · 3 comments
Closed

Does console.error writes to stderr ? #11961

maboiteaspam opened this issue Feb 8, 2014 · 3 comments

Comments

@maboiteaspam
Copy link

Hello,

Using lastest phantomjs 1 9 7, i can t manage to write to stderr using console.err.

In the front end script

  throw "some errors";

In thephantomjs script

  page.onConsoleMessage = function(msg, lineNum, sourceId) {
    console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
  };

  page.onError = function(msg, trace) {
    var msgStack = ['ERROR: ' + msg];
    if (trace && trace.length) {
      msgStack.push('TRACE:');
      trace.forEach(function(t) {
        msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
      });
    }
    console.error(msgStack.join('\n'));
  };

this works

 var phantomjsprocess = require('child_process').execFile(phantomjs.path, childArgs);
      phantomjsprocess.stdout
        .on('data', function (data) {
          grunt.verbose.writeln(data.trim());
        })
        // having some difficuties to pass phantomjs errors to stderr, so listens to stdout for errors
        .on('data', function (data) {
          if( data.match(/^(ERROR: )/) ){
            grunt.log.writeln(data.trim());
          }
        });

this does not, or seems not.

 var phantomjsprocess = require('child_process').execFile(phantomjs.path, childArgs);
      phantomjsprocess.stderr
        .on('data', function (data) {
          grunt.log.writeln(data.trim());
        });

I read the issue #10232 it is exactly my problem, but so far, i can t get it working.

Do you have any hints ?

thanks.

@JamesMGreene
Copy link
Collaborator

Closing as duplicate of #10150

@maboiteaspam
Copy link
Author

Oh sorry i mised it... Thanks for pointing.

For next people,
add this snippet into the phantomjs wrapper

console.error = function () {
    require("system").stderr.write(Array.prototype.join.call(arguments, ' ') + '\n');
};

Then listen to the stderr of your child process.

Tested on phantomJS1.9.7@ubuntu13

Works like a charm !

@seosgithub
Copy link

@maboiteaspam I stole your snippet and threw it in https://github.com/sotownsend/BooJS for stderr by default on console.error.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants