From c50baef9c6297976cbd2b5889423136461343d0d Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sun, 28 Jul 2013 17:25:06 -0400 Subject: [PATCH] Change logger to stream, decode HTML messages - Using a WritableStream instead of manually buffering is a good idea - Chat messages are logged with HTML entities decoded (< instead of <) --- channel.js | 5 +++-- logger.js | 30 ++++++++++++------------------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/channel.js b/channel.js index cca26a27..c7bc8494 100644 --- a/channel.js +++ b/channel.js @@ -263,7 +263,7 @@ Channel.prototype.saveDump = function() { }; var text = JSON.stringify(dump); fs.writeFileSync("chandump/" + this.name, text); - this.logger.flush(); + this.logger.close(); } // Save channel dumps every 5 minutes, in case of crash @@ -1829,7 +1829,8 @@ Channel.prototype.sendMessage = function(username, msg, msgclass, data) { this.chatbuffer.push(msgobj); if(this.chatbuffer.length > 15) this.chatbuffer.shift(); - this.logger.log("<" + username + "." + msgclass + "> " + msg); + var unescaped = sanitize(msg).entityDecode(); + this.logger.log("<" + username + "." + msgclass + "> " + unescaped); }; /* REGION Rank stuff */ diff --git a/logger.js b/logger.js index f1129336..4bbc79fa 100644 --- a/logger.js +++ b/logger.js @@ -18,28 +18,22 @@ function getTimeString() { var Logger = function(filename) { this.filename = filename; - this.buffer = []; - - setInterval(function() { - this.flush(); - }.bind(this), 15000); + this.writer = fs.createWriteStream(filename, { + flags: "a", + encoding: "utf-8" + }); } -Logger.prototype.log = function(what) { - this.buffer.push("[" + getTimeString() + "] " + what); +Logger.prototype.log = function () { + var msg = ""; + for(var i in arguments) + msg += arguments[i]; + var str = "[" + getTimeString() + "] " + msg + "\n"; + this.writer.write(str); } -Logger.prototype.flush = function() { - if(this.buffer.length == 0) - return; - var text = this.buffer.join("\n") + "\n"; - this.buffer = []; - fs.appendFile(this.filename, text, function(err) { - if(err) { - errlog.log("Append to " + this.filename + " failed: "); - errlog.log(err); - } - }.bind(this)); +Logger.prototype.close = function () { + this.writer.end(); } var errlog = new Logger("error.log");