diff --git a/lib/robot.js b/lib/robot.js index f41bbed..02a77f5 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -38,7 +38,8 @@ var Robot = module.exports = function Robot(opts) { "start", "initRobot", "initDevices", - "initConnections" + "initConnections", + "log" ]; _.bindAll(this, methods); @@ -113,7 +114,7 @@ Robot.prototype.connection = function(name, conn) { str = "Connection names must be unique."; str += "Renaming '" + original + "' to '" + conn.name + "'"; - Logger.warn(str); + this.log("warn", str); } this.connections[conn.name] = initConnection(conn); @@ -137,7 +138,7 @@ Robot.prototype.initRobot = function(opts) { this.work = opts.work || opts.play; if (!this.work) { - this.work = function() { Logger.debug("No work yet."); }; + this.work = function() { this.log("debug", "No work yet."); }; } }; @@ -149,7 +150,7 @@ Robot.prototype.initRobot = function(opts) { Robot.prototype.initConnections = function(opts) { var str; - Logger.info("Initializing connections."); + this.log("info", "Initializing connections."); if (opts.connection == null && opts.connections == null) { return this.connections; @@ -159,7 +160,7 @@ Robot.prototype.initConnections = function(opts) { str = "Specifying a single connection with the 'connection' key "; str += "is deprecated. It will be removed in 1.0.0."; - Logger.warn(str); + this.log("warn", str); this.connection(opts.connection.name, opts.connection); return this.connections; @@ -205,13 +206,13 @@ Robot.prototype.device = function(name, device) { str = "Device names must be unique."; str += "Renaming '" + original + "' to '" + device.name + "'"; - Logger.warn(str); + this.log("warn", str); } if (typeof device.connection === "string") { if (this.connections[device.connection] == null) { str = "No connection found with the name " + device.connection + ".\n"; - Logger.fatal(str); + this.log("fatal", str); process.emit("SIGINT"); } @@ -233,7 +234,7 @@ Robot.prototype.device = function(name, device) { Robot.prototype.initDevices = function(opts) { var str; - Logger.info("Initializing devices."); + this.log("info", "Initializing devices."); if (opts.device == null && opts.devices == null) { return this.devices; @@ -248,7 +249,7 @@ Robot.prototype.initDevices = function(opts) { str = "Specifying a single device with the 'device' key is deprecated. "; str += "It will be removed in 1.0.0."; - Logger.warn(str); + this.log("warn", str); this.device(opts.device.name, opts.device); return this.devices; } @@ -292,8 +293,8 @@ Robot.prototype.start = function(callback) { start ], function(err, results) { if (!!err) { - Logger.fatal("An error occured while trying to start the robot:"); - Logger.fatal(err); + this.log("fatal", "An error occured while trying to start the robot:"); + this.log("fatal", err); this.halt(function() { if (typeof(this.error) === "function") { @@ -320,7 +321,7 @@ Robot.prototype.start = function(callback) { // // Returns nothing Robot.prototype.startWork = function() { - Logger.info("Working."); + this.log("info", "Working."); this.emit("ready", this); this.work.call(this, this); @@ -333,7 +334,7 @@ Robot.prototype.startWork = function() { // // Returns nothing Robot.prototype.startConnections = function(callback) { - Logger.info("Starting connections."); + this.log("info", "Starting connections."); var starters = _.map(this.connections, function(conn, name) { this[name] = conn; @@ -347,9 +348,9 @@ Robot.prototype.startConnections = function(callback) { str += " on port " + conn.port; } - Logger.debug(str + "."); + this.log("debug", str + "."); return conn.connect.call(conn, cb); - }; + }.bind(this); }, this); return Async.parallel(starters, callback); @@ -361,7 +362,9 @@ Robot.prototype.startConnections = function(callback) { // // Returns nothing Robot.prototype.startDevices = function(callback) { - Logger.info("Starting devices."); + var log = this.log; + + log("info", "Starting devices."); var starters = _.map(this.devices, function(device, name) { this[name] = device; @@ -373,7 +376,7 @@ Robot.prototype.startDevices = function(callback) { str += " on pin " + device.pin; } - Logger.debug(str + "."); + log("debug", str + "."); return device.start.call(device, cb); }; }, this); @@ -408,13 +411,19 @@ Robot.prototype.toString = function() { return "[Robot name='" + this.name + "']"; }; +Robot.prototype.log = function(level) { + var args = Array.prototype.slice.call(arguments, 1); + args.unshift("[" + this.name + "] -"); + Logger[level].apply(null, args); +}; + Robot.prototype.performArraySetup = function(things, typeOfThing, arrayName) { var str = "Specifying "; str += arrayName; str += " as an array is deprecated. "; str += "It will be removed in 1.0.0."; - Logger.warn(str); + this.log("warn", str); _.forEach(things, function(t, key) { var name = _.isString(key) ? key : t.name;