Add check to prevent write-after-end error

This commit is contained in:
calzoneman 2013-07-29 18:13:06 -04:00
parent 1df015b839
commit 93dd730bde
1 changed files with 8 additions and 1 deletions

View File

@ -17,6 +17,7 @@ function getTimeString() {
}
var Logger = function(filename) {
this.dead = false;
this.filename = filename;
this.writer = fs.createWriteStream(filename, {
flags: "a",
@ -25,6 +26,10 @@ var Logger = function(filename) {
}
Logger.prototype.log = function () {
if(this.dead) {
Logger.errlog.log("WARNING: Attempted write to dead logger: ", this.filename);
return;
}
var msg = "";
for(var i in arguments)
msg += arguments[i];
@ -33,7 +38,9 @@ Logger.prototype.log = function () {
}
Logger.prototype.close = function () {
this.writer.end();
this.writer.end("", null, function () {
this.dead = true;
});
}
var errlog = new Logger("error.log");