process: add --redirect-warnings command line argument · nodejs/node@03e89b3

11

'use strict';

223+

const config = process.binding('config');

34

const prefix = `(${process.release.name}:${process.pid}) `;

4556

exports.setup = setupProcessWarnings;

678+

var fs;

9+

var cachedFd;

10+

var acquiringFd = false;

11+

function nop() {}

12+13+

function lazyFs() {

14+

if (!fs)

15+

fs = require('fs');

16+

return fs;

17+

}

18+19+

function writeOut(message) {

20+

if (console && typeof console.error === 'function')

21+

return console.error(message);

22+

process._rawDebug(message);

23+

}

24+25+

function onClose(fd) {

26+

return function() {

27+

lazyFs().close(fd, nop);

28+

};

29+

}

30+31+

function onOpen(cb) {

32+

return function(err, fd) {

33+

acquiringFd = false;

34+

if (fd !== undefined) {

35+

cachedFd = fd;

36+

process.on('exit', onClose(fd));

37+

}

38+

cb(err, fd);

39+

process.emit('_node_warning_fd_acquired', err, fd);

40+

};

41+

}

42+43+

function onAcquired(message) {

44+

// make a best effort attempt at writing the message

45+

// to the fd. Errors are ignored at this point.

46+

return function(err, fd) {

47+

if (err)

48+

return writeOut(message);

49+

lazyFs().appendFile(fd, `${message}\n`, nop);

50+

};

51+

}

52+53+

function acquireFd(cb) {

54+

if (cachedFd === undefined && !acquiringFd) {

55+

acquiringFd = true;

56+

lazyFs().open(config.warningFile, 'a', onOpen(cb));

57+

} else if (cachedFd !== undefined && !acquiringFd) {

58+

cb(null, cachedFd);

59+

} else {

60+

process.once('_node_warning_fd_acquired', cb);

61+

}

62+

}

63+64+

function output(message) {

65+

if (typeof config.warningFile === 'string') {

66+

acquireFd(onAcquired(message));

67+

return;

68+

}

69+

writeOut(message);

70+

}

71+72+

function doEmitWarning(warning) {

73+

return function() {

74+

process.emit('warning', warning);

75+

};

76+

}

77+778

function setupProcessWarnings() {

879

if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {

980

process.on('warning', (warning) => {

@@ -14,19 +85,18 @@ function setupProcessWarnings() {

1485

(isDeprecation && process.traceDeprecation);

1586

if (trace && warning.stack) {

1687

if (warning.code) {

17-

console.error(`${prefix}[${warning.code}] ${warning.stack}`);

88+

output(`${prefix}[${warning.code}] ${warning.stack}`);

1889

} else {

19-

console.error(`${prefix}${warning.stack}`);

90+

output(`${prefix}${warning.stack}`);

2091

}

2192

} else {

2293

const toString =

2394

typeof warning.toString === 'function' ?

2495

warning.toString : Error.prototype.toString;

2596

if (warning.code) {

26-

console.error(

27-

`${prefix}[${warning.code}] ${toString.apply(warning)}`);

97+

output(`${prefix}[${warning.code}] ${toString.apply(warning)}`);

2898

} else {

29-

console.error(`${prefix}${toString.apply(warning)}`);

99+

output(`${prefix}${toString.apply(warning)}`);

30100

}

31101

}

32102

});

@@ -63,6 +133,6 @@ function setupProcessWarnings() {

63133

if (process.throwDeprecation)

64134

throw warning;

65135

}

66-

process.nextTick(() => process.emit('warning', warning));

136+

process.nextTick(doEmitWarning(warning));

67137

};

68138

}