Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
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: #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>
- Loading branch information
Showing
with
186 additions
and 6 deletions.
- +21 −0 doc/api/cli.md
- +10 −0 doc/node.1
- +76 −6 lib/internal/process/warning.js
- +14 −0 src/node.cc
- +10 −0 src/node_config.cc
- +5 −0 src/node_internals.h
- +25 −0 test/parallel/test-process-redirect-warnings-env.js
- +25 −0 test/parallel/test-process-redirect-warnings.js
| @@ -0,0 +1,25 @@ | ||
| 'use strict'; | ||
|
|
||
| // Tests the NODE_REDIRECT_WARNINGS environment variable by spawning | ||
| // a new child node process that emits a warning into a temporary | ||
| // warnings file. Once the process completes, the warning file is | ||
| // opened and the contents are validated | ||
|
|
||
| const common = require('../common'); | ||
| const fs = require('fs'); | ||
| const fork = require('child_process').fork; | ||
| const path = require('path'); | ||
| const assert = require('assert'); | ||
|
|
||
| common.refreshTmpDir(); | ||
|
|
||
| const warnmod = require.resolve(common.fixturesDir + '/warnings.js'); | ||
| const warnpath = path.join(common.tmpDir, 'warnings.txt'); | ||
|
|
||
| fork(warnmod, {env: {NODE_REDIRECT_WARNINGS: warnpath}}) | ||
| .on('exit', common.mustCall(() => { | ||
| fs.readFile(warnpath, 'utf8', common.mustCall((err, data) => { | ||
| assert.ifError(err); | ||
| assert(/\(node:\d+\) Warning: a bad practice warning/.test(data)); | ||
| })); | ||
| })); |
| @@ -0,0 +1,25 @@ | ||
| 'use strict'; | ||
|
|
||
| // Tests the --redirect-warnings command line flag by spawning | ||
| // a new child node process that emits a warning into a temporary | ||
| // warnings file. Once the process completes, the warning file is | ||
| // opened and the contents are validated | ||
|
|
||
| const common = require('../common'); | ||
| const fs = require('fs'); | ||
| const fork = require('child_process').fork; | ||
| const path = require('path'); | ||
| const assert = require('assert'); | ||
|
|
||
| common.refreshTmpDir(); | ||
|
|
||
| const warnmod = require.resolve(common.fixturesDir + '/warnings.js'); | ||
| const warnpath = path.join(common.tmpDir, 'warnings.txt'); | ||
|
|
||
| fork(warnmod, {execArgv: [`--redirect-warnings=${warnpath}`]}) | ||
| .on('exit', common.mustCall(() => { | ||
| fs.readFile(warnpath, 'utf8', common.mustCall((err, data) => { | ||
| assert.ifError(err); | ||
| assert(/\(node:\d+\) Warning: a bad practice warning/.test(data)); | ||
| })); | ||
| })); |