Fix logger being closed inappropriately

This commit is contained in:
calzoneman 2013-07-29 19:59:52 -04:00
parent 687a561079
commit ba26d9abbb
3 changed files with 12 additions and 11 deletions

View File

@ -268,7 +268,6 @@ Channel.prototype.saveDump = function() {
}; };
var text = JSON.stringify(dump); var text = JSON.stringify(dump);
fs.writeFileSync("chandump/" + this.name, text); fs.writeFileSync("chandump/" + this.name, text);
this.logger.close();
} }
// Save channel dumps every 5 minutes, in case of crash // Save channel dumps every 5 minutes, in case of crash

View File

@ -17,7 +17,6 @@ function getTimeString() {
} }
var Logger = function(filename) { var Logger = function(filename) {
this.dead = false;
this.filename = filename; this.filename = filename;
this.writer = fs.createWriteStream(filename, { this.writer = fs.createWriteStream(filename, {
flags: "a", flags: "a",
@ -31,23 +30,25 @@ Logger.prototype.log = function () {
msg += arguments[i]; msg += arguments[i];
if(this.dead) { if(this.dead) {
errlog.log("WARNING: Attempted write to dead logger: ", this.filename);
errlog.log("Message was: ", msg);
return; return;
} }
var str = "[" + getTimeString() + "] " + msg + "\n"; var str = "[" + getTimeString() + "] " + msg + "\n";
try {
this.writer.write(str); this.writer.write(str);
} catch(e) {
errlog.log("WARNING: Attempted logwrite failed: " + this.filename);
errlog.log("Message was: " + msg);
errlog.log(e);
}
} }
Logger.prototype.close = function () { Logger.prototype.close = function () {
if(this.dead) { try {
errlog.log("WARNING: Attempted closure on dead logger: ", this.filename); this.writer.end();
return; } catch(e) {
errlog.log("Log close failed: " + this.filename);
} }
this.writer.end("", null, function () {
this.dead = true;
}.bind(this));
} }
var errlog = new Logger("error.log"); var errlog = new Logger("error.log");

View File

@ -55,6 +55,7 @@ var Server = {
if(chan.registered) if(chan.registered)
chan.saveDump(); chan.saveDump();
chan.playlist.die(); chan.playlist.die();
chan.logger.close();
for(var i in this.channels) { for(var i in this.channels) {
if(this.channels[i].canonical_name == chan.canonical_name) { if(this.channels[i].canonical_name == chan.canonical_name) {
this.channels.splice(i, 1); this.channels.splice(i, 1);