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/
npm-debug.log

View file

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

View file

@ -1,5 +1,7 @@
var _ = require("lodash");
var config = require("../../config") || {};
var fs = require('fs');
var moment = require("moment");
module.exports = Chan;
@ -16,11 +18,43 @@ function Chan(attr) {
};
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) {
this.users.push(user);
this.users.push(user);
};
Chan.prototype.sortUsers = function() {

View file

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