Added logging

This commit is contained in:
Mattias Erming 2014-06-19 00:16:44 +02:00
parent a8aa3f59d1
commit 0d2da3b88d
4 changed files with 42 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
logs/
node_modules/ node_modules/
npm-debug.log npm-debug.log

View File

@ -1,6 +1,7 @@
module.exports = { module.exports = {
password: "",
port: 9000, port: 9000,
password: "",
log: true,
theme: "", theme: "",
defaults: { defaults: {
nick: "shout-user", nick: "shout-user",

View File

@ -1,5 +1,7 @@
var _ = require("lodash"); var _ = require("lodash");
var config = require("../../config") || {}; var config = require("../../config") || {};
var fs = require('fs');
var moment = require("moment");
module.exports = Chan; module.exports = Chan;
@ -16,11 +18,43 @@ 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") {
return;
}
var dir = "logs/";
dir += this.network + "/";
if (!fs.existsSync(dir)) {
fs.mkdir(dir);
console.log(dir);
}
var date = moment().format("YYYY-MM-DD");
var file = dir + this.name + ".log";
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;
}
}
fs.appendFile(
file,
line + "\n"
);
}; };
Chan.prototype.addUser = function(user) { Chan.prototype.addUser = function(user) {
this.users.push(user); this.users.push(user);
}; };
Chan.prototype.sortUsers = function() { Chan.prototype.sortUsers = function() {

View File

@ -13,6 +13,8 @@ function Network(attr) {
channels: [], channels: [],
}, attr)); }, attr));
this.name = this.host.split(".")[1] || clone.host;
// Add lobby // Add lobby
this.channels.unshift( this.channels.unshift(
new Chan({name: this.host, type: "lobby"}) new Chan({name: this.host, type: "lobby"})
@ -20,7 +22,7 @@ function Network(attr) {
}; };
Network.prototype.addChan = function(chan) { Network.prototype.addChan = function(chan) {
chan.network = this.host; chan.network = this.name;
this.channels.push(chan); this.channels.push(chan);
}; };
@ -29,6 +31,5 @@ Network.prototype.toJSON = function() {
"client", "client",
"connected", "connected",
]); ]);
clone.name = clone.host.split(".")[1] || clone.host;
return clone; return clone;
}; };