1
votes

I have basically a C compiled binary wherein if an error is encountered during the execution, the error is dumped out to stderr. This C Binary is wrapped around NodeJS, where the binary is invoked via child process exec. But upon error, even though C code dumps out the information to stderr, I still seem to get the information in Nodejs on stdout, and not on stderr. So, essentially running console.log(stdout); dumps out the error information but console.log(stderr); dumps nothing. Does anyone have any idea on this, and if I need to redirect this information through a different medium so I get appropriate information on stdout and stderr on NodeJS script?

I created a test version of the code and it seems to display the information correctly on stderr and stdout:

#include <stdio.h>
int main(){
 fprintf(stderr, "Whoops, this is stderr");
 fprintf(stdout, "Whoops, this is stdout");
 return 0;
}

and corresponding NodeJS Code:

#!/usr/bin/env node
var exec = require('child_process').exec,
    path = require('path');
var bin = path.join(__dirname, 'a.out');
var proc = exec(bin, function (error, stdout, stderr) {
           console.log('stdout:', stdout);
           console.log('stderr:', stderr);
    });
proc.stdout.on('data', function (dat) { console.log(dat) });

and this is the output I get:

Whoops, this is stdout
stdout: Whoops, this is stdout
stderr: Whoops, this is stderr

Not sure why it would happen so in my code, May be because I am dumping a lot of information to stdout and stderr simultaneously or there is some buggy module I have included that may be causing this to happen. The actual code is quite big to be written here, but seems like I have to investigate where it might be going wrong.

1
can you show us your code? also, have you tried an event handler for stderr for the spawned process? eg: my_spawned_process.stderr.on('data', function (dat) { console.log(dat) }) - user2524973
Thanks @user2524973 for your help, See my posted answer :) - learn_develop
glad you got it sorted out :) - user2524973

1 Answers

0
votes

I seem to have figured out the problem. The legacy C Code that dumps out the information was never referring to the FILE * being passed onto it. That was the reason all the information appeared on stdout and not on stderr. Fixed the API to take FILE * as an argument and dump out the information to correct FILE pointer and now it works.