forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crashlogger.js
63 lines (55 loc) · 1.68 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
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 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';
}
};