pokemon-showdown/crashlogger.js
Guangcong Luo b665660608 Refactor for more ES6 features
Now that nodejs/node#3072 is mostly fixed, we can finally start using
Node 4+ features.

This refactor:

- uses arrow functions where appropriate

  Note that arrow functions still aren't used in Mocha, where `this`
  is sometimes meaningful.

  This also removes the need for .bind() nearly everywhere, as well
  as the `self = this` trick.

- refactors Validator and Connection into ES6 classes

- no longer uses Array#forEach for iterating arrays

  We strongly prefer for (let i = 0; i < arr.length; i++) because of
  performance reasons. Most forEaches have been replaced with for..of,
  though, which is 5x slower than the long-form loop but 2x faster
  than forEach, which is good enough outside of most inner loops.

  The only exception is tournaments, which is due for a more invasive
  refactor soon anyway.
2016-02-10 17:40:21 -06:00

64 lines
1.7 KiB
JavaScript

/**
* Crash logger
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Logs crashes, sends an e-mail notification if you've set up
* config.js to do that.
*
* @license MIT license
*/
'use strict';
const CRASH_EMAIL_THROTTLE = 5 * 60 * 1000; // 5 minutes
const logPath = require('path').resolve(__dirname, 'logs/errors.txt');
let lastCrashLog = 0;
let transport;
exports = module.exports = function (err, description, data) {
const datenow = Date.now();
let stack = (err.stack || err);
if (data) {
stack += '\n\nAdditional information:\n';
for (let k in data) {
stack += " " + k + " = " + data[k] + "\n";
}
}
console.error("\nCRASH: " + stack + "\n");
let out = require('fs').createWriteStream(logPath, {'flags': 'a'});
out.on("open", fd => {
out.write("\n" + stack + "\n");
out.end();
}).on("error", err => {
console.error("\nSUBCRASH: " + err.stack + "\n");
});
if (Config.crashguardemail && ((datenow - lastCrashLog) > CRASH_EMAIL_THROTTLE)) {
lastCrashLog = datenow;
try {
if (!transport) transport = require('nodemailer').createTransport(Config.crashguardemail.options);
} catch (e) {
console.error("Could not start nodemailer - try `npm install` if you want to use it");
}
if (transport) {
transport.sendMail({
from: Config.crashguardemail.from,
to: Config.crashguardemail.to,
subject: Config.crashguardemail.subject,
text: description + " crashed " + (exports.hadException ? "again " : "") + "with this stack trace:\n" + stack,
}, err => {
if (err) console.error("Error sending email: " + err);
});
}
}
exports.hadException = true;
if (process.uptime() < 60 * 60) {
// lock down the server
return 'lockdown';
}
};