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 &lt;)
This commit is contained in:
calzoneman 2013-07-28 17:25:06 -04:00
parent 6899186600
commit c50baef9c6
2 changed files with 15 additions and 20 deletions

View file

@ -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 */

View file

@ -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");