Improve logging

This commit is contained in:
calzoneman 2013-03-27 14:28:51 -05:00
parent 34f20f423f
commit 124ec0080f
8 changed files with 169 additions and 66 deletions

View File

@ -13,6 +13,7 @@ var mysql = require("mysql-libmysqlclient");
var Config = require("./config.js");
var bcrypt = require("bcrypt");
var hashlib = require("node_hash");
var Logger = require("./logger.js");
// Check if a name is taken
exports.isRegistered = function(name) {
@ -20,7 +21,7 @@ exports.isRegistered = function(name) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Auth.isRegistered: DB connection failed");
return true;
}
var query = "SELECT * FROM registrations WHERE uname='{}'"
@ -50,7 +51,7 @@ exports.register = function(name, pw) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Auth.register: DB connection failed");
return false;
}
var hash = bcrypt.hashSync(pw, 10);
@ -68,7 +69,7 @@ exports.login = function(name, pw) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Auth.login: DB connection failed");
return false;
}
var query = "SELECT * FROM registrations WHERE uname='{1}'"
@ -92,7 +93,7 @@ exports.login = function(name, pw) {
var results = db.querySync(query);
db.closeSync();
if(!results) {
console.log("Failed to migrate password! user=", name);
Logger.errlog.log("Failed to migrate password! user=" + name);
return false;
}
return rows[0];

View File

@ -18,9 +18,10 @@ var Media = require("./media.js").Media;
var ChatCommand = require("./chatcommand.js");
var Server = require("./server.js");
var io = Server.io;
var Logger = require("./logger.js");
var Channel = function(name) {
console.log("Opening channel " + name);
Logger.syslog.log("Opening channel " + name);
this.name = name;
this.registered = false;
this.users = [];
@ -38,10 +39,11 @@ var Channel = function(name) {
qopen_allow_playnext: false,
qopen_allow_delete: false,
pagetitle: "Sync",
bgimage: ""
customcss: ""
};
this.ipbans = [];
this.ipbans = {};
this.logger = new Logger.Logger("chanlogs/" + this.name + ".log");
// Autolead stuff
// Accumulator
@ -59,7 +61,7 @@ Channel.prototype.loadMysql = function() {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Channel.loadMysql: MySQL Connection Failed");
return false;
}
// Check if channel exists
@ -67,12 +69,12 @@ Channel.prototype.loadMysql = function() {
.replace(/\{\}/, this.name);
var results = db.querySync(query);
if(!results) {
console.log("channel query failed");
Logger.errlog.log("Channel.loadMysql: Channel query failed");
return;
}
var rows = results.fetchAllSync();
if(rows.length == 0) {
console.log("Channel " + this.name + " is unregistered");
Logger.syslog.log("Channel " + this.name + " is unregistered.");
return;
}
this.registered = true;
@ -82,7 +84,7 @@ Channel.prototype.loadMysql = function() {
.replace(/\{\}/, this.name);
var results = db.querySync(query);
if(!results) {
console.log("channel library query failed");
Logger.errlog.log("Channel.loadMysql: failed to load library for " + this.name);
return;
}
var rows = results.fetchAllSync();
@ -95,7 +97,7 @@ Channel.prototype.loadMysql = function() {
.replace(/\{\}/, this.name);
var results = db.querySync(query);
if(!results) {
console.log("Channel banlist query failed");
Logger.errlog.log("Channel.loadMysql: failed to load banlist for " + this.name);
return;
}
var rows = results.fetchAllSync();
@ -103,7 +105,7 @@ Channel.prototype.loadMysql = function() {
this.ipbans[rows[i].ip] = [rows[i].name, rows[i].banner];
}
console.log("Loaded channel " + this.name + " from MySQL DB");
Logger.syslog.log("Loaded channel " + this.name + " from database");
db.closeSync();
}
@ -114,7 +116,7 @@ Channel.prototype.createTables = function() {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Channel.createTables: DB connection failed");
return false;
}
// Create library table
@ -187,6 +189,8 @@ Channel.prototype.tryRegister = function(user) {
user.socket.emit("registerChannel", {
success: true,
});
this.logger.log("*** " + user.name + " registered the channel");
Logger.syslog.log("Channel " + this.name + " was registered by " + user.name);
}
else {
user.socket.emit("registerChannel", {
@ -205,7 +209,7 @@ Channel.prototype.getRank = function(name) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Channel.getRank: DB connection failed");
return Rank.Guest;
}
var query = "SELECT * FROM chan_{1}_ranks WHERE name='{2}'"
@ -231,7 +235,7 @@ Channel.prototype.saveRank = function(user) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Channel.saveRank: DB connection failed");
return false;
}
var query = "UPDATE chan_{1}_ranks SET rank='{2}' WHERE name='{3}'"
@ -261,7 +265,7 @@ Channel.prototype.addToLibrary = function(media) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Channel.addToLibrary: DB connection failed");
return false;
}
var query = "INSERT INTO chan_{1}_library VALUES ('{2}', '{3}', {4}, '{5}', '{6}')"
@ -287,7 +291,7 @@ Channel.prototype.banIP = function(banner, bannee) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Channel.banIP: DB connection failed");
return false;
}
var query = "INSERT INTO chan_{1}_bans (`ip`, `name`, `banner`) VALUES ('{2}', '{3}', '{4}')"
@ -297,7 +301,10 @@ Channel.prototype.banIP = function(banner, bannee) {
.replace(/\{4\}/, banner.name);
results = db.querySync(query);
if(!results) {
console.log("Insert into ban table failed");
Logger.errlog.log("Channel.banIP: Insert failed");
}
else {
this.logger.log(bannee.ip + " (" + bannee.name + ") was banned by " + banner.name);
}
db.closeSync();
return results;
@ -313,7 +320,7 @@ Channel.prototype.unbanIP = function(ip) {
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
console.log("MySQL Connection Failed");
Logger.errlog.log("Channel.unbanIP: DB connection failed");
return false;
}
@ -323,7 +330,7 @@ Channel.prototype.unbanIP = function(ip) {
results = db.querySync(query);
if(!results) {
console.log("Delete from ban table failed");
Logger.errlog.log("Channel.unbanIP: Failed to delete entry");
return;
}
db.closeSync();
@ -351,7 +358,7 @@ Channel.prototype.searchLibrary = function(query) {
// Called when a new user enters the channel
Channel.prototype.userJoin = function(user) {
if(user.ip in this.ipbans && this.ipbans[user.ip] != null) {
console.log("/" + user.ip + " was disconnected - banned");
this.logger.log("--- Kicking /" + user.ip + " - banned");
user.socket.disconnect();
return;
}
@ -403,7 +410,8 @@ Channel.prototype.userJoin = function(user) {
user.socket.emit("banlist", {entries: ents});
if(user.playerReady)
this.sendMediaUpdate(user);
console.log("/" + user.ip + " joined channel " + this.name);
this.logger.log("+++ /" + user.ip + " joined");
Logger.syslog.log("/" + user.ip + " joined channel " + this.name);
}
// Called when a user leaves the channel
@ -428,6 +436,7 @@ Channel.prototype.userLeave = function(user) {
name: user.name
});
}
this.logger.log("--- /" + user.ip + " (" + user.name + ") left");
}
// Queues a new media
@ -440,6 +449,7 @@ Channel.prototype.enqueue = function(data) {
media: this.library[data.id].pack(),
pos: idx
});
this.logger.log("*** Queued from cache: id=" + data.id);
}
// Query metadata from YouTube
else if(data.type == "yt") {
@ -460,9 +470,11 @@ Channel.prototype.enqueue = function(data) {
pos: idx
});
chan.addToLibrary(vid);
chan.logger.log("*** Queued new YT video: " + id);
}
catch(e) {
console.log("YTQueue Fail: id=" + id);
Logger.errlog.log("YTQueue Fail: id=" + id);
console.log(e);
}
}})(this, data.id);
InfoGetter.getYTInfo(data.id, callback);
@ -491,9 +503,10 @@ Channel.prototype.enqueue = function(data) {
chan.addToLibrary(vid);
idx++;
}
chan.logger.log("*** Queued YT Playlist: id=" + id);
}
catch(e) {
console.log("YTPlaylist Failed: id=", id);
Logger.errlog.log("YTPlaylist Failed: id=", id);
console.log(e);
}
}})(this, data.id);
@ -507,6 +520,7 @@ Channel.prototype.enqueue = function(data) {
media: media.pack(),
pos: idx
});
this.logger.log("*** Queued Twitch channel: " + data.id);
}
else if(data.type == "li") {
var media = new Media(data.id, "Livestream ~ " + data.id, 0, "li");
@ -515,6 +529,7 @@ Channel.prototype.enqueue = function(data) {
media: media.pack(),
pos: idx
});
this.logger.log("*** Queued Livestream channel: " + data.id);
}
// Query metadata from Soundcloud
else if(data.type == "sc") {
@ -523,6 +538,7 @@ Channel.prototype.enqueue = function(data) {
return;
}
try {
var seconds = data.duration / 1000;
var title = data.title;
var vid = new Media(id, title, seconds, "sc");
@ -532,6 +548,12 @@ Channel.prototype.enqueue = function(data) {
pos: idx
});
chan.addToLibrary(vid);
chan.logger.log("*** Queued Soundcloud id=" + id);
}
catch(e) {
Logger.errlog.log("SCQueue fail: id=" + id);
Logger.errlog.log(e);
}
}})(this, data.id);
InfoGetter.getSCInfo(data.id, callback);
}
@ -542,6 +564,7 @@ Channel.prototype.enqueue = function(data) {
return;
}
try {
data = data[0];
var seconds = data.duration;
var title = data.title;
@ -552,6 +575,12 @@ Channel.prototype.enqueue = function(data) {
pos: idx
});
chan.addToLibrary(vid);
chan.logger.log("*** Queued Vimeo id=" + id);
}
catch(e) {
Logger.errlog.log("VIQueue fail: id=" + id);
Logger.errlog.log(e);
}
}})(this, data.id);
InfoGetter.getVIInfo(data.id, callback);
}
@ -563,6 +592,7 @@ Channel.prototype.enqueue = function(data) {
return;
}
try {
var seconds = data.duration;
var title = data.title;
var vid = new Media(id, title, seconds, "dm");
@ -572,6 +602,12 @@ Channel.prototype.enqueue = function(data) {
pos: idx
});
chan.addToLibrary(vid);
chan.logger.log("*** Queued Dailymotion id=" + id);
}
catch(e) {
Logger.errlog.log("DMQueue fail: id=" + id);
Logger.errlog.log(e);
}
}})(this, data.id);
InfoGetter.getDMInfo(data.id, callback);
}
@ -693,6 +729,7 @@ Channel.prototype.sendMessage = function(username, msg, msgclass) {
});
if(this.recentChat.length > 15)
this.recentChat.shift();
this.logger.log("<" + username + "." + msgclass + "> " + msg);
};
// Promotion! Actor is the client who initiated the promotion, name is the
@ -715,6 +752,7 @@ Channel.prototype.promoteUser = function(actor, name) {
if(receiver.loggedIn) {
this.saveRank(receiver);
}
this.logger.log("*** " + actor.name + " promoted " + receiver.name + " from " + (receiver.rank - 1) + " to " + receiver.rank);
this.broadcastRankUpdate(receiver);
}
}
@ -738,6 +776,7 @@ Channel.prototype.demoteUser = function(actor, name) {
if(receiver.loggedIn) {
this.saveRank(receiver);
}
this.logger.log("*** " + actor.name + " demoted " + receiver.name + " from " + (receiver.rank + 1) + " to " + receiver.rank);
this.broadcastRankUpdate(receiver);
}
}
@ -752,6 +791,7 @@ Channel.prototype.changeLeader = function(name) {
this.broadcastRankUpdate(old);
}
if(name == "") {
this.logger.log("*** Resuming autolead");
if(this.currentMedia != null) {
this.time = new Date().getTime();
this.i = 0;
@ -761,6 +801,7 @@ Channel.prototype.changeLeader = function(name) {
}
for(var i = 0; i < this.users.length; i++) {
if(this.users[i].name == name) {
this.logger.log("*** Assigned leader: " + name);
this.leader = this.users[i];
this.broadcastRankUpdate(this.leader);
}
@ -812,9 +853,6 @@ Channel.prototype.sendMediaUpdate = function(user) {
if(this.currentMedia != null) {
user.socket.emit("mediaUpdate", this.currentMedia.packupdate());
}
else {
console.log("currentMedia is null");
}
}
// Sent when someone logs in, to add them to the user list

View File

@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
var Rank = require("./rank.js");
var Poll = require("./poll.js").Poll;
var Logger = require("./logger.js");
function handle(chan, user, msg) {
if(msg.indexOf("/me ") == 0)
@ -46,6 +47,7 @@ function handleKick(chan, user, args) {
}
}
if(kickee) {
chan.logger.log("*** " + user.name + " kicked " + args[0]);
kickee.socket.disconnect();
}
}
@ -61,6 +63,7 @@ function handleBan(chan, user, args) {
}
}
if(kickee && kickee.rank < user.rank) {
chan.logger.log("*** " + user.name + " banned " + args[0]);
chan.banIP(user, kickee);
}
}
@ -68,6 +71,7 @@ function handleBan(chan, user, args) {
function handleUnban(chan, user, args) {
if(Rank.hasPermission(user, "ipban") && args.length > 0) {
chan.logger.log("*** " + user.name + " unbanned " + args[0]);
chan.unbanIP(args[0]);
}
}
@ -80,6 +84,7 @@ function handlePoll(chan, user, msg) {
var poll = new Poll(user.name, title, args);
chan.poll = poll;
chan.broadcastPoll();
chan.logger.log("*** " + user.name + " Opened Poll: '" + poll.title + "'");
}
}

View File

@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
var mysql = require("mysql-libmysqlclient");
var Config = require("./config.js");
var Logger = require("./logger.js");
var initialized = false;
@ -28,7 +29,7 @@ exports.init = function() {
ENGINE = MyISAM;";
var results = db.querySync(query);
if(!results) {
console.log("Database initialization failed! Could not create channel table");
Logger.errlog.log("database.init: channel table init failed!");
return false;
}
@ -41,7 +42,7 @@ exports.init = function() {
ENGINE = MyISAM;";
var results = db.querySync(query);
if(!results) {
console.log("Database initialization failed! Could not create registration table");
Logger.errlog.log("database.init: registration table init failed!");
return false;
}
@ -59,7 +60,7 @@ exports.listChannels = function() {
var query = "SELECT * FROM `channels`";
var results = db.querySync(query);
if(!results) {
console.log("Database channel listing failed!");
Logger.errlog.log("database.listChannels: query failed");
return false;
}
@ -79,7 +80,7 @@ exports.listUsers = function() {
var query = "SELECT * FROM `registrations`";
var results = db.querySync(query);
if(!results) {
console.log("Database user listing failed!");
Logger.errlog.log("database.listUsers: query failed");
return false;
}
@ -101,7 +102,7 @@ exports.listChannelRanks = function(chan) {
console.log(query);
var results = db.querySync(query);
if(!results) {
console.log("Database channel listing failed!");
Logger.errlog.log("database.listChannelRanks: query failed");
return false;
}

View File

@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
var http = require("http");
var https = require("https");
var Logger = require("./logger.js");
// Helper function for making an HTTP request and getting the result
// as JSON
@ -26,7 +27,7 @@ function getJSON(options, callback) {
var data = JSON.parse(buffer);
}
catch(e) {
console.log("JSON fail: ", options);
Logger.errlog.log("JSON fail: " + options.path);
return;
}
callback(res.statusCode, data);
@ -49,7 +50,7 @@ function getJSONHTTPS(options, callback) {
var data = JSON.parse(buffer);
}
catch(e) {
console.log("JSON fail: ", options);
Logger.errlog.log("JSON fail: " + options.path);
return;
}
callback(res.statusCode, data);

53
logger.js Normal file
View File

@ -0,0 +1,53 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var fs = require("fs");
function getTimeString() {
return new Date().toTimeString().split(" ")[0];
}
var Logger = function(filename) {
this.filename = filename;
this.buffer = [];
setInterval(function() {
this.flush();
}.bind(this), 15000);
}
Logger.prototype.log = function(what) {
this.buffer.push("[" + getTimeString() + "] " + what);
}
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));
}
var errlog = new Logger("error.log");
var syslog = new Logger("sys.log");
errlog.actualLog = errlog.log;
errlog.log = function(what) { console.log(what); this.actualLog(what); }
syslog.actualLog = syslog.log;
syslog.log = function(what) { console.log(what); this.actualLog(what); }
exports.Logger = Logger;
exports.errlog = errlog;
exports.syslog = syslog;

View File

@ -9,6 +9,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var Logger = require("./logger.js");
var Config = require("./config.js");
var connect = require("connect");
var app = connect.createServer(connect.static(__dirname+"/www")).listen(Config.IO_PORT);
@ -22,5 +23,5 @@ exports.channels = {};
exports.io.sockets.on("connection", function(socket) {
var user = new User(socket, socket.handshake.address.address);
console.log("New connection from /" + user.ip);
Logger.syslog.log("Accepted connection from /" + user.ip);
});

View File

@ -14,6 +14,7 @@ var Auth = require("./auth.js");
var Channel = require("./channel.js").Channel;
var Server = require("./server.js");
var Database = require("./database.js");
var Logger = require("./logger.js");
// Represents a client connected via socket.io
var User = function(socket, ip) {
@ -303,7 +304,7 @@ User.prototype.login = function(name, pw) {
}
// Woah, success!
else {
console.log(this.ip + " signed in as " + name);
Logger.syslog.log(this.ip + " signed in as " + name);
this.name = name;
this.loggedIn = false;
this.socket.emit("login", {
@ -313,6 +314,7 @@ User.prototype.login = function(name, pw) {
rank: this.rank
});
if(this.channel != null) {
this.channel.logger.log(this.ip + " signed in as " + name);
if(this.rank >= Rank.Moderator)
this.channel.sendPlaylist(this);
this.channel.broadcastNewUser(this);
@ -326,7 +328,7 @@ User.prototype.login = function(name, pw) {
this.socket.emit("login", {
success: true
});
console.log(this.ip + " logged in as " + name);
Logger.syslog.log(this.ip + " logged in as " + name);
// Sweet, let"s look up our rank
var chanrank = (this.channel != null) ? this.channel.getRank(name)
: Rank.Guest;
@ -338,6 +340,7 @@ User.prototype.login = function(name, pw) {
});
this.name = name;
if(this.channel != null) {
this.channel.logger.log(this.ip + " logged in as " + name);
if(this.rank >= Rank.Moderator)
this.channel.sendPlaylist(this);
this.channel.broadcastNewUser(this);