process: add 'warning' event by jasnell · Pull Request #4782 · nodejs/node (original) (raw)

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 'warn' events on the global process object. These are
invoked with a Warning object that is similar in structure to
an Error in that they have a name, message and stack trace.

By default, these warnings will be printed to stderr. This can be
suppressed using the --no-warnings command line flag, however the
'warn' event will still be emitted by the process, allowing applications
to handle the warnings in custom ways.

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 warning, however,
is preserved and shows the correct call site.

Test cases and documentation are included.

Refs: nodejs/node-eps#4 (comment)