-
Notifications
You must be signed in to change notification settings - Fork 2
/
crashlogger.js
50 lines (49 loc) · 1.36 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
*/
module.exports = (function () {
var lastCrashLog = 0;
return function (err, description) {
console.log("\nCRASH: " + (err.stack || err) + "\n");
require('fs').createWriteStream('logs/errors.txt', {'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;
var transport;
try {
transport = require('nodemailer').createTransport(
Config.crashguardemail.transport,
Config.crashguardemail.options
);
transport.sendMail({
from: Config.crashguardemail.from,
to: Config.crashguardemail.to,
subject: Config.crashguardemail.subject,
text: description + ' crashed with this stack trace:\n' + (err.stack || err)
});
} catch (e) {
// could not send an email...
console.log('Error sending email: ' + e);
} finally {
if (transport) {
transport.close();
}
}
}
if (process.uptime() > 60 * 60) {
// no need to lock down the server
return true;
}
};
})();