From 543cab46e4c1ec2c3c8d39ebe612bfb280e1ed30 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Fri, 20 Jun 2014 15:57:11 +0200 Subject: [PATCH] Added 'log' module --- lib/log.js | 34 ++++++++++++++++++++++++++++++++++ lib/models/chan.js | 35 ++++------------------------------- lib/models/network.js | 2 +- 3 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 lib/log.js diff --git a/lib/log.js b/lib/log.js new file mode 100644 index 00000000..c9594d2f --- /dev/null +++ b/lib/log.js @@ -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" + ); +}; diff --git a/lib/models/chan.js b/lib/models/chan.js index 67dcf096..40705887 100644 --- a/lib/models/chan.js +++ b/lib/models/chan.js @@ -1,6 +1,5 @@ var _ = require("lodash"); -var config = require("../../config") || {}; -var fs = require('fs'); +var log = require("../log"); var moment = require("moment"); module.exports = Chan; @@ -19,35 +18,9 @@ function Chan(attr) { Chan.prototype.addMsg = function(msg) { this.messages.push(msg); - if (config.log != true || this.type == "lobby") { - return; + if (this.type != "lobby") { + 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] 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) { @@ -74,7 +47,7 @@ Chan.prototype.sortUsers = function() { }; Chan.prototype.toJSON = function() { - var clone = _.clone(this); + var clone = _.omit(this, ["network"]); clone.count = clone.messages.length; clone.messages = clone.messages.slice(-100); return clone; diff --git a/lib/models/network.js b/lib/models/network.js index 428099cd..e31454f8 100644 --- a/lib/models/network.js +++ b/lib/models/network.js @@ -22,7 +22,7 @@ function Network(attr) { }; Network.prototype.addChan = function(chan) { - chan.network = this.name; + chan.network = this; this.channels.push(chan); };