我是 Node.js 新手。我编写了一个脚本,可以生成 5 个子进程并从中捕获日志,截至目前,日志看起来一团糟。
但我想以更清晰的格式显示输出,就像每行都被重用并且数据被记录在进程 ID 旁边。像这样的东西:
下面是我的代码:
var state = "node stealth.js ";
var exec = require('child_process').exec;
workers = 5
p_list = [];
for (var i = 0; i < workers; i++) {
(function(i){
var child = exec(state+'user'+i);
console.log('started Worker '+i)
// Add the child process to the list for tracking
p_list.push({process:child, content:""});
// Listen for any response:
child.stdout.on('data', function (data) {
console.log(child.pid, data);
p_list[i].content += data;
process.stdout.cursorTo(i);
process.stdout.clearLine();
process.stdout.write(child.pid+data);
process.stdout.write("\n"); // end the line
});
// Listen for any errors:
child.stderr.on('data', function (data) {
console.log(child.pid, data);
p_list[i].content += data;
});
// Listen if the process closed
child.on('close', function(exit_code) {
console.log('Closed before stop: Closing code: ', exit_code);
});
})(i)
}
我是一名优秀的程序员,十分优秀!