mirror of https://github.com/lukechilds/node.git
Browse Source
In several places throughout the code we write directly to stderr to report warnings (deprecation, possible eventemitter memory leak). The current design of simply dumping the text to stderr is less than ideal. This PR introduces a new "process warnings" mechanism that emits 'warning' events on the global process object. These are invoked with a `warning` argument whose value is an Error object. By default, these warnings will be printed to stderr. This can be suppressed using the `--no-warnings` and `--no-deprecation` command line flags. For warnings, the 'warning' event will still be emitted by the process, allowing applications to handle the warnings in custom ways. The existing `--no-deprecation` flag will continue to supress all deprecation output generated by the core lib. The `--trace-warnings` command line flag will tell Node.js to print the full stack trace of warnings as part of the default handling. The existing `--no-deprecation`, `--throw-deprecation` and `--trace-deprecation` flags continue to work as they currently do, but the exact output of the warning message is modified to occur on process.nextTick(). The stack trace for the warnings and deprecations preserve and point to the correct call site. A new `process.emitWarning()` API is provided to permit userland to emit warnings and deprecations using the same consistent mechanism. Test cases and documentation are included. PR-URL: https://github.com/nodejs/node/pull/4782 Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>process-exit-stdio-flushing
James M Snell
9 years ago
14 changed files with 393 additions and 54 deletions
@ -0,0 +1,49 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
const traceWarnings = process.traceProcessWarnings; |
||||
|
const noDeprecation = process.noDeprecation; |
||||
|
const traceDeprecation = process.traceDeprecation; |
||||
|
const throwDeprecation = process.throwDeprecation; |
||||
|
const prefix = `(${process.release.name}:${process.pid}) `; |
||||
|
|
||||
|
exports.setup = setupProcessWarnings; |
||||
|
|
||||
|
function setupProcessWarnings() { |
||||
|
if (!process.noProcessWarnings) { |
||||
|
process.on('warning', (warning) => { |
||||
|
if (!(warning instanceof Error)) return; |
||||
|
const isDeprecation = warning.name === 'DeprecationWarning'; |
||||
|
if (isDeprecation && noDeprecation) return; |
||||
|
const trace = traceWarnings || (isDeprecation && traceDeprecation); |
||||
|
if (trace && warning.stack) { |
||||
|
console.error(`${prefix}${warning.stack}`); |
||||
|
} else { |
||||
|
var toString = warning.toString; |
||||
|
if (typeof toString !== 'function') |
||||
|
toString = Error.prototype.toString; |
||||
|
console.error(`${prefix}${toString.apply(warning)}`); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
// process.emitWarning(error)
|
||||
|
// process.emitWarning(str[, name][, ctor])
|
||||
|
process.emitWarning = function(warning, name, ctor) { |
||||
|
if (typeof name === 'function') { |
||||
|
ctor = name; |
||||
|
name = 'Warning'; |
||||
|
} |
||||
|
if (warning === undefined || typeof warning === 'string') { |
||||
|
warning = new Error(warning); |
||||
|
warning.name = name || 'Warning'; |
||||
|
Error.captureStackTrace(warning, ctor || process.emitWarning); |
||||
|
} |
||||
|
if (!(warning instanceof Error)) { |
||||
|
throw new TypeError('\'warning\' must be an Error object or string.'); |
||||
|
} |
||||
|
if (throwDeprecation && warning.name === 'DeprecationWarning') |
||||
|
throw warning; |
||||
|
else |
||||
|
process.nextTick(() => process.emit('warning', warning)); |
||||
|
}; |
||||
|
} |
@ -0,0 +1,3 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
process.emitWarning('a bad practice warning'); |
@ -0,0 +1,29 @@ |
|||||
|
// Flags: --no-warnings
|
||||
|
// The flag suppresses stderr output but the warning event will still emit
|
||||
|
'use strict'; |
||||
|
|
||||
|
const common = require('../common'); |
||||
|
const assert = require('assert'); |
||||
|
const util = require('util'); |
||||
|
|
||||
|
process.on('warning', common.mustCall((warning) => { |
||||
|
assert(warning); |
||||
|
assert(/^(Warning|CustomWarning)/.test(warning.name)); |
||||
|
assert(warning.message, 'A Warning'); |
||||
|
}, 3)); |
||||
|
|
||||
|
process.emitWarning('A Warning'); |
||||
|
process.emitWarning('A Warning', 'CustomWarning'); |
||||
|
|
||||
|
function CustomWarning() { |
||||
|
Error.call(this); |
||||
|
this.name = 'CustomWarning'; |
||||
|
this.message = 'A Warning'; |
||||
|
Error.captureStackTrace(this, CustomWarning); |
||||
|
} |
||||
|
util.inherits(CustomWarning, Error); |
||||
|
process.emitWarning(new CustomWarning()); |
||||
|
|
||||
|
// TypeError is thrown on invalid output
|
||||
|
assert.throws(() => process.emitWarning(1), TypeError); |
||||
|
assert.throws(() => process.emitWarning({}), TypeError); |
@ -0,0 +1,33 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
require('../common'); |
||||
|
const assert = require('assert'); |
||||
|
const execFile = require('child_process').execFile; |
||||
|
const warnmod = require.resolve('../fixtures/warnings.js'); |
||||
|
const node = process.execPath; |
||||
|
|
||||
|
const normal = [warnmod]; |
||||
|
const noWarn = ['--no-warnings', warnmod]; |
||||
|
const traceWarn = ['--trace-warnings', warnmod]; |
||||
|
|
||||
|
execFile(node, normal, function(er, stdout, stderr) { |
||||
|
// Show Process Warnings
|
||||
|
assert.equal(er, null); |
||||
|
assert.equal(stdout, ''); |
||||
|
assert(/^\(.+\)\sWarning: a bad practice warning/.test(stderr)); |
||||
|
}); |
||||
|
|
||||
|
execFile(node, noWarn, function(er, stdout, stderr) { |
||||
|
// Hide Process Warnings
|
||||
|
assert.equal(er, null); |
||||
|
assert.equal(stdout, ''); |
||||
|
assert(!/^\(.+\)\sWarning: a bad practice warning/.test(stderr)); |
||||
|
}); |
||||
|
|
||||
|
execFile(node, traceWarn, function(er, stdout, stderr) { |
||||
|
// Show Warning Trace
|
||||
|
assert.equal(er, null); |
||||
|
assert.equal(stdout, ''); |
||||
|
assert(/^\(.+\)\sWarning: a bad practice warning/.test(stderr)); |
||||
|
assert(/at Object\.\<anonymous\>\s\(.+warnings.js:3:9\)/.test(stderr)); |
||||
|
}); |
Loading…
Reference in new issue