Skip to content

Commit

Permalink
* Allow for configurable logging option (basic callback function)
Browse files Browse the repository at this point in the history
* TODO: create level logging
  • Loading branch information
stelcheck committed Jan 24, 2013
1 parent 4911fc9 commit 0a9ef4f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
13 changes: 8 additions & 5 deletions api.js
@@ -1,11 +1,12 @@
var util = require('util'),
fs = require('fs'),
logger = require('util'),
_ = require('underscore')._,
humanized_time_span = require(__dirname + '/lib/humanized_time_span.js'),
EventEmitter = require('events').EventEmitter,
controller = new EventEmitter();

var log;

/**
Set this module as an express.js controller
*/
Expand All @@ -21,6 +22,8 @@ module.exports.setup = function (settings) {

module.exports.configure(settings);

log = settings.logger ? settings.logger : require('util').log;

/**
Load available plugins
*/
Expand All @@ -30,11 +33,11 @@ module.exports.setup = function (settings) {
if (fs.statSync(__dirname + '/plugins/' + directory).isDirectory() && fs.statSync(__dirname + '/plugins/' + directory + '/' + directory + '_plugin.js').isFile()) {
return require(__dirname + '/plugins/' + directory + '/' + directory + '_plugin.js').create(controller, settings);
} else {
logger.log("Excluding plugin: " + directory);
log("Excluding plugin: " + directory);
}
});
} else {
logger.log("Error when creating plugin: " + err);
log("Error when creating plugin: " + err);
}
});

Expand All @@ -58,7 +61,7 @@ module.exports.configure = function (s) {
};

module.exports.addService = function (serviceCfg) {
logger.log('Adding new service: ' + serviceCfg.name);
log('Adding new service: ' + serviceCfg.name);
module.exports.removeService(serviceCfg.name);
settings.services.push(serviceCfg);
};
Expand All @@ -85,7 +88,7 @@ status.services = [];
module.exports.checkAllServices = function() {
status.lastupdate = new Date().toUTCString();

logger.log('Refreshing status board...');
log('Refreshing status board...');

/**
Update the service status object, check the service
Expand Down
15 changes: 9 additions & 6 deletions plugins/irc/irc_plugin.js
@@ -1,9 +1,12 @@
var _ = require('underscore')._,
logger = require('util');
log;

exports.create = function(api, settings) {

log = settings.logger ? settings.logger : require('util').log;

if (settings.plugins && settings.plugins.irc && settings.plugins.irc.enable) {
logger.log('Creating the plugin: ' + __filename);
log('Creating the plugin: ' + __filename);

var irc = require('irc');
var channels = settings.plugins.irc.options.channels;
Expand Down Expand Up @@ -61,11 +64,11 @@ exports.create = function(api, settings) {
});

bot.on('join', function(channel, who) {
logger.log(who + ' has joined ' + channel);
log(who + ' has joined ' + channel);
if (who == settings.plugins.irc.nick) {

// TODO : Let's do it for each channel of the list...
logger.log('You are now connected to channel ' + channel + ' and ready to send messages');
log('You are now connected to channel ' + channel + ' and ready to send messages');
connected = true;
_.each(cache, function(message){
bot.say(channels, message);
Expand All @@ -76,7 +79,7 @@ exports.create = function(api, settings) {

// Add a private message listener to interact with the IRC bot
bot.addListener('pm', function(nick, message) {
logger.log('Got private message from ' + nick + ': ' + message);
log('Got private message from ' + nick + ': ' + message);
var callme = commands[trim(message)];
if (typeof callme !== 'undefined') {
bot.say(nick, callme.call(null, api));
Expand All @@ -88,7 +91,7 @@ exports.create = function(api, settings) {

// Send the message or cache it
var pushMessage = function(message) {
logger.log('Pushing message ' + message);
log('Pushing message ' + message);
if (connected) {
bot.say(channels, message);
} else {
Expand Down
20 changes: 11 additions & 9 deletions server.js
@@ -1,14 +1,16 @@
var path = require('path'),
util = require('util'),
url = require('url'),
_ = require('underscore')._;

var log;

exports.dashboard = function(settings) {
var rootPath = path.dirname(module.filename),
api = require('./api'),
express = require('express'),
app = express.createServer();

log = settings.logger ? settings.logger : require('util').log;
/**
Express app definition
Expand All @@ -18,7 +20,7 @@ exports.dashboard = function(settings) {
app.configure(function () {
var staticPath = path.join(rootPath, 'public');

util.log("Express server static directory: " + staticPath);
log("Express server static directory: " + staticPath);

app.use(express['static'].call(null, staticPath));
app.use(express.favicon(path.join(staticPath, 'favicon.ico')));
Expand All @@ -29,7 +31,7 @@ exports.dashboard = function(settings) {

app.listen(settings.port, settings.hostname);

util.log('Server running at http://' + settings.hostname + ':' + settings.port);
log('Server running at http://' + settings.hostname + ':' + settings.port);

/**
Routes are connected to the api, and an extra route is added for self-health checking
Expand Down Expand Up @@ -66,7 +68,7 @@ exports.dashboard = function(settings) {
io.sockets.on('connection', function(socket) {

count++;
util.log('New client connected! (' + count + ')');
log('New client connected! (' + count + ')');

socket.emit('title', settings.title);
socket.emit('status', api.getStatus());
Expand All @@ -75,7 +77,7 @@ exports.dashboard = function(settings) {

socket.on('disconnect', function() {
count--;
util.log('Client disconnect! (' + count + ')');
log('Client disconnect! (' + count + ')');
socket.broadcast.emit('count', count);
});
});
Expand All @@ -94,18 +96,18 @@ exports.dashboard = function(settings) {

api.on("routeContribution", function(route) {
app.get(route.path, route.binding);
util.log("Add GET route contribution: " + route.path);
log("Add GET route contribution: " + route.path);
});

api.on("postRouteContribution", function(route) {
app.post(route.path, route.binding);
util.log("Add POST route contribution: " + route.path);
log("Add POST route contribution: " + route.path);
});

api.on("staticContribution", function(plugin) {
var docRoot = __dirname + '/plugins/' + plugin + '/public';
app.use("/api/" + plugin, express['static'].call(null, docRoot));
util.log("Add static contribution: " + plugin + ", " + docRoot);
log("Add static contribution: " + plugin + ", " + docRoot);
});

api.on("refresh", function(status) {
Expand All @@ -116,5 +118,5 @@ exports.dashboard = function(settings) {

// We make the API available for external control
this.api = api;
util.log('Server started.');
log('Server started.');
};

0 comments on commit 0a9ef4f

Please sign in to comment.