Robot log messages should contain the bot name

This commit is contained in:
Andrew Stewart 2015-01-20 19:29:40 -08:00
parent 5354102c2e
commit 8272efe060
1 changed files with 27 additions and 18 deletions

View File

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