summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2016-12-04 10:38:35 -0800
committerJames M Snell <jasnell@gmail.com>2017-01-30 11:11:47 -0800
commit03e89b3ff298c63e9620f1547094f7fca76edde7 (patch)
treeb8bb5b52dfbfa437ca4c0df490131b184ccf9d33 /lib
parent5e1f32fd94f53acbec6c5f6eb4d2de5fdef2cd67 (diff)
downloadnode-new-03e89b3ff298c63e9620f1547094f7fca76edde7.tar.gz
process: add --redirect-warnings command line argument
The --redirect-warnings command line argument allows process warnings to be written to a specified file rather than printed to stderr. Also adds an equivalent NODE_REDIRECT_WARNINGS environment variable. If the specified file cannot be opened or written to for any reason, the argument is ignored and the warning is printed to stderr. If the file already exists, it will be appended to. PR-URL: https://github.com/nodejs/node/pull/10116 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Michal Zasso <targos@protonmail.com> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/process/warning.js82
1 files changed, 76 insertions, 6 deletions
diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js
index fd9e7e72a4..bf487f38a4 100644
--- a/lib/internal/process/warning.js
+++ b/lib/internal/process/warning.js
@@ -1,9 +1,80 @@
'use strict';
+const config = process.binding('config');
const prefix = `(${process.release.name}:${process.pid}) `;
exports.setup = setupProcessWarnings;
+var fs;
+var cachedFd;
+var acquiringFd = false;
+function nop() {}
+
+function lazyFs() {
+ if (!fs)
+ fs = require('fs');
+ return fs;
+}
+
+function writeOut(message) {
+ if (console && typeof console.error === 'function')
+ return console.error(message);
+ process._rawDebug(message);
+}
+
+function onClose(fd) {
+ return function() {
+ lazyFs().close(fd, nop);
+ };
+}
+
+function onOpen(cb) {
+ return function(err, fd) {
+ acquiringFd = false;
+ if (fd !== undefined) {
+ cachedFd = fd;
+ process.on('exit', onClose(fd));
+ }
+ cb(err, fd);
+ process.emit('_node_warning_fd_acquired', err, fd);
+ };
+}
+
+function onAcquired(message) {
+ // make a best effort attempt at writing the message
+ // to the fd. Errors are ignored at this point.
+ return function(err, fd) {
+ if (err)
+ return writeOut(message);
+ lazyFs().appendFile(fd, `${message}\n`, nop);
+ };
+}
+
+function acquireFd(cb) {
+ if (cachedFd === undefined && !acquiringFd) {
+ acquiringFd = true;
+ lazyFs().open(config.warningFile, 'a', onOpen(cb));
+ } else if (cachedFd !== undefined && !acquiringFd) {
+ cb(null, cachedFd);
+ } else {
+ process.once('_node_warning_fd_acquired', cb);
+ }
+}
+
+function output(message) {
+ if (typeof config.warningFile === 'string') {
+ acquireFd(onAcquired(message));
+ return;
+ }
+ writeOut(message);
+}
+
+function doEmitWarning(warning) {
+ return function() {
+ process.emit('warning', warning);
+ };
+}
+
function setupProcessWarnings() {
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
process.on('warning', (warning) => {
@@ -14,19 +85,18 @@ function setupProcessWarnings() {
(isDeprecation && process.traceDeprecation);
if (trace && warning.stack) {
if (warning.code) {
- console.error(`${prefix}[${warning.code}] ${warning.stack}`);
+ output(`${prefix}[${warning.code}] ${warning.stack}`);
} else {
- console.error(`${prefix}${warning.stack}`);
+ output(`${prefix}${warning.stack}`);
}
} else {
const toString =
typeof warning.toString === 'function' ?
warning.toString : Error.prototype.toString;
if (warning.code) {
- console.error(
- `${prefix}[${warning.code}] ${toString.apply(warning)}`);
+ output(`${prefix}[${warning.code}] ${toString.apply(warning)}`);
} else {
- console.error(`${prefix}${toString.apply(warning)}`);
+ output(`${prefix}${toString.apply(warning)}`);
}
}
});
@@ -63,6 +133,6 @@ function setupProcessWarnings() {
if (process.throwDeprecation)
throw warning;
}
- process.nextTick(() => process.emit('warning', warning));
+ process.nextTick(doEmitWarning(warning));
};
}