Skip to content

Commit

Permalink
Board: emit logging as events, pt. 2 clean up and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Jul 7, 2015
1 parent a655be3 commit 65cc715
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
30 changes: 21 additions & 9 deletions lib/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ var logging = {
}
};

Board.prototype.log = function( /* type, klass, details [, long description] */ ) {
Board.prototype.log = function( /* type, klass, message [, long description] */ ) {
var args = [].slice.call(arguments);

// If this was a direct call to `log(...)`, make sure
Expand All @@ -589,27 +589,39 @@ Board.prototype.log = function( /* type, klass, details [, long description] */

var type = args.shift();
var klass = args.shift();
var details = args.shift();
var message = args.shift();
var color = logging.colors[type];
var now = Date.now();
var event = {
type: type,
timestamp: now,
class: klass,
message: "",
data: null,
};

if (typeof args[args.length - 1] === "object") {
event.data = args.pop();
}

message += " " + args.join(", ");
event.message = message.trim();

if (this.debug) {
console.log([
// Timestamp
chalk.grey(Date.now()),
chalk.grey(now),
// Module, color matches type of log
chalk.magenta(klass),
// Details
chalk[color](details),
chalk[color](message),
// Miscellaneous args
args.join(", ")
].join(" "));
}

this.emit(type, {
timestamp: Date.now(),
class: klass,
message: [details, args.join(", ")].join(" ").trim()
});
this.emit(type, event);
this.emit("message", event);
};


Expand Down
15 changes: 11 additions & 4 deletions test/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ exports["Board"] = {
},

emitsLogsAsEvents: function(test) {
test.expect(11);
test.expect(19);

var spy = sinon.spy(Board.prototype, "log");
var io = new MockFirmata();
Expand All @@ -170,17 +170,20 @@ exports["Board"] = {

this.on("info", function(event) {
test.equal(event.class, "Board");
test.equal(event.message, "message");
test.equal(event.message, "message 1");
test.deepEqual(event.data, { foo: 2 });
});

this.on("fail", function(event) {
test.equal(event.class, "Board");
test.equal(event.message, "message");
test.deepEqual(event.data, null);
});

this.on("warn", function(event) {
test.equal(event.class, "Board");
test.equal(event.message, "message");
test.deepEqual(event.data, [1, 2, 3]);
});

this.on("log", function(event) {
Expand All @@ -193,9 +196,13 @@ exports["Board"] = {
test.equal(event.message, "message");
});

this.info("Board", "message");
this.on("message", function(event) {
test.ok(true);
});

this.info("Board", "message", 1, { foo: 2 });
this.fail("Board", "message");
this.warn("Board", "message");
this.warn("Board", "message", [1, 2, 3]);
this.log("Board", "message");
this.error("Board", "message");

Expand Down

0 comments on commit 65cc715

Please sign in to comment.