From 4b318b35ce626956ad50c76668f71f0368eca84a Mon Sep 17 00:00:00 2001 From: Richard Chamberlain Date: Wed, 14 Dec 2016 10:26:52 +0000 Subject: [PATCH 1/2] Opt-in hooks and signal by default This changes the default action on require('nodereport') to opt-in to the exception and fatal-error hooks, and to enable the signal handler. So 'node -r nodereport application.js' will get all the nodereport functionality without any application code changes. For users programming to the API, a separate api-only entry point, require('nodereport/api') is provided. This does not enable the hooks and signal unless and until .setEvents() is called. --- .npmignore | 1 + README.md | 24 ++++++++++++++---------- binding.gyp | 6 +++--- index.js | 20 ++++++++++++++++++++ package.json | 1 - test/test-exception.js | 2 +- test/test-fatal-error.js | 2 +- test/test-signal.js | 2 +- 8 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 index.js diff --git a/.npmignore b/.npmignore index f5078ac..cc94b97 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ * !src/** !binding.gyp +!index.js diff --git a/README.md b/README.md index 8865428..665aeb6 100644 --- a/README.md +++ b/README.md @@ -15,15 +15,26 @@ Supports Node.js v4, v6 and v7 on Linux, MacOS, Windows and AIX. ```bash npm install nodereport +node -r nodereport app.js ``` +A NodeReport will be triggered automatically on unhandled exceptions and fatal +error events (for example out of memory errors), and can also be triggered +by sending a USR2 signal to a Node.js process (Linux/MacOS only). -This will allow a NodeReport to be triggered via an API -call from a JavaScript application. +A NodeReport can also be triggered via an API call from a JavaScript +application. ```js var nodereport = require('nodereport'); nodereport.triggerReport(); ``` +The API can be used without adding the automatic exception and fatal error +hooks and the signal handler, as follows: + +```js +var nodereport = require('nodereport/api'); +nodereport.triggerReport(); +``` Content of the NodeReport consists of a header section containing the event type, date, time, PID and Node version, sections containing JavaScript and @@ -50,19 +61,12 @@ can be specified as a parameter on the `triggerReport()` call. nodereport.triggerReport("myReportName"); ``` -A NodeReport can also be triggered automatically on unhandled exceptions, fatal -error events (for example out of memory errors), and signals (Linux/MacOS only). -Triggering on these events can be enabled using the following API call: - -```js -nodereport.setEvents("exception+fatalerror+signal+apicall"); -``` - ## Configuration Additional configuration is available using the following APIs: ```js +nodereport.setEvents("exception+fatalerror+signal+apicall"); nodereport.setSignal("SIGUSR2|SIGQUIT"); nodereport.setFileName("stdout|stderr|"); nodereport.setDirectory(""); diff --git a/binding.gyp b/binding.gyp index c665e5b..77272b1 100644 --- a/binding.gyp +++ b/binding.gyp @@ -1,7 +1,7 @@ { "targets": [ { - "target_name": "nodereport", + "target_name": "api", "sources": [ "src/node_report.cc", "src/module.cc" ], "include_dirs": [ ' process.exit(2)); From 010ecc09c879744e9275e4da5e773681c1740c93 Mon Sep 17 00:00:00 2001 From: Richard Chamberlain Date: Fri, 16 Dec 2016 11:36:00 +0000 Subject: [PATCH 2/2] Add test for api-only usage. Simplify index.js --- index.js | 11 +++-------- test/test-api-nohooks.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 test/test-api-nohooks.js diff --git a/index.js b/index.js index ed52c20..2860d19 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,9 @@ const api = require('./api'); -// Process NODEREPORT_EVENTS env var -const options = process.env.NODEREPORT_EVENTS; -if (options) { - api.setEvents(options); -} else { - // Default action - all events enabled - api.setEvents('exception+fatalerror+signal+apicall'); -} +// NODEREPORT_EVENTS env var overrides the defaults +const options = process.env.NODEREPORT_EVENTS || 'exception+fatalerror+signal+apicall'; +api.setEvents(options); exports.triggerReport = api.triggerReport; exports.setEvents = api.setEvents; diff --git a/test/test-api-nohooks.js b/test/test-api-nohooks.js new file mode 100644 index 0000000..3ca3dd4 --- /dev/null +++ b/test/test-api-nohooks.js @@ -0,0 +1,22 @@ +'use strict'; + +// Testcase to produce NodeReport via API call, using the no-hooks/no-signal +// interface - i.e. require('nodereport/api') +if (process.argv[2] === 'child') { + const nodereport = require('../api'); + nodereport.triggerReport(); +} else { + const common = require('./common.js'); + const spawn = require('child_process').spawn; + const tap = require('tap'); + + const child = spawn(process.execPath, [__filename, 'child']); + child.on('exit', (code) => { + tap.plan(3); + tap.equal(code, 0, 'Process exited cleanly'); + const reports = common.findReports(child.pid); + tap.equal(reports.length, 1, 'Found reports ' + reports); + const report = reports[0]; + common.validate(tap, report, {pid: child.pid}); + }); +}