forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crashlogger.js
50 lines (49 loc) · 1.56 KB
/
crashlogger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 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
*/
exports = module.exports = (function () {
var lastCrashLog = 0;
var transport;
var logPath = require('path').resolve(__dirname, 'logs/errors.txt');
return function (err, description, isException) {
console.log("\nCRASH: " + (err.stack || err) + "\n");
require('fs').createWriteStream(logPath, {'flags': 'a'}).on("open", function (fd) {
this.write("\n" + err.stack + "\n");
this.end();
}).on("error", function (err) {
console.log("\nSUBCRASH: " + err.stack + "\n");
});
var datenow = Date.now();
if (Config.crashguardemail && ((datenow - lastCrashLog) > 1000 * 60 * 5)) {
lastCrashLog = datenow;
try {
if (!transport) transport = require('nodemailer').createTransport(Config.crashguardemail.options);
} catch (e) {
console.log("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" + (err.stack || err)
}, function (err) {
if (err) console.log("Error sending email: " + err);
});
}
}
if (isException) {
exports.hadException = true;
}
if (process.uptime() > 60 * 60) {
// no need to lock down the server
return true;
}
};
})();