Added 'log' module

This commit is contained in:
Mattias Erming 2014-06-20 15:57:11 +02:00
parent 5736422a73
commit 543cab46e4
3 changed files with 39 additions and 32 deletions

34
lib/log.js Normal file
View File

@ -0,0 +1,34 @@
var config = require("../config") || {};
var fs = require("fs");
var moment = require("moment");
module.exports = function log(chan, msg) {
if (!config.log) {
return;
}
var network = chan.network.name;
var dir = "logs/" + network + "/";
if (!fs.existsSync(dir)) {
fs.mkdir(dir);
}
var date = moment().format("YYYY-MM-DD HH:mm");
var line = "[" + date + "] ";
if (msg.type == "normal") {
line += "<"
+ msg.from + "> "
+ msg.text;
} else {
line += "* "
+ msg.from + " "
+ msg.type + " "
+ msg.text;
}
var file = dir + chan.name + ".log";
fs.appendFile(
file,
line + "\n"
);
};

View File

@ -1,6 +1,5 @@
var _ = require("lodash"); var _ = require("lodash");
var config = require("../../config") || {}; var log = require("../log");
var fs = require('fs');
var moment = require("moment"); var moment = require("moment");
module.exports = Chan; module.exports = Chan;
@ -19,35 +18,9 @@ function Chan(attr) {
Chan.prototype.addMsg = function(msg) { Chan.prototype.addMsg = function(msg) {
this.messages.push(msg); this.messages.push(msg);
if (config.log != true || this.type == "lobby") { if (this.type != "lobby") {
return; log(this, msg);
} }
var dir = "logs/";
dir += this.network + "/";
if (!fs.existsSync(dir)) {
fs.mkdir(dir);
}
var line = "[" + msg.time + "] ";
if (msg.type == "normal") {
// Format:
// [00:00] <Arnold> Put that cookie down.. Now!!
line += "<" + msg.from + "> " + msg.text;
} else {
// Format:
// [00:00] * Arnold quit
line += "* " + msg.from + " " + msg.type;
if (msg.text) {
line += " " + msg.text;
}
}
var file = dir + this.name + ".log";
fs.appendFile(
file,
line + "\n"
);
}; };
Chan.prototype.addUser = function(user) { Chan.prototype.addUser = function(user) {
@ -74,7 +47,7 @@ Chan.prototype.sortUsers = function() {
}; };
Chan.prototype.toJSON = function() { Chan.prototype.toJSON = function() {
var clone = _.clone(this); var clone = _.omit(this, ["network"]);
clone.count = clone.messages.length; clone.count = clone.messages.length;
clone.messages = clone.messages.slice(-100); clone.messages = clone.messages.slice(-100);
return clone; return clone;

View File

@ -22,7 +22,7 @@ function Network(attr) {
}; };
Network.prototype.addChan = function(chan) { Network.prototype.addChan = function(chan) {
chan.network = this.name; chan.network = this;
this.channels.push(chan); this.channels.push(chan);
}; };