From f96b40cff2e05961fa243c27ad5140d5ad1bf592 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Tue, 30 Sep 2014 15:15:12 +0100 Subject: [PATCH 1/3] Updated debug node to output other msg properties --- nodes/core/core/58-debug.html | 90 +++++++++++++++++++++++++---------- nodes/core/core/58-debug.js | 67 +++++++++++++++++--------- 2 files changed, 110 insertions(+), 47 deletions(-) diff --git a/nodes/core/core/58-debug.html b/nodes/core/core/58-debug.html index 04aa50789..5957db6df 100644 --- a/nodes/core/core/58-debug.html +++ b/nodes/core/core/58-debug.html @@ -16,12 +16,16 @@ diff --git a/nodes/core/core/58-debug.js b/nodes/core/core/58-debug.js index 7436bf2cb..77963d039 100644 --- a/nodes/core/core/58-debug.js +++ b/nodes/core/core/58-debug.js @@ -20,40 +20,63 @@ module.exports = function(RED) { var debuglength = RED.settings.debugMaxLength||1000; var useColors = false; // util.inspect.styles.boolean = "red"; - + function DebugNode(n) { RED.nodes.createNode(this,n); this.name = n.name; this.complete = n.complete; this.console = n.console; - this.active = (n.active == null)||n.active; + this.active = (n.active === null || typeof n.active === "undefined") || n.active; var node = this; - + this.on("input",function(msg) { - if (this.complete == "true") { // debug complete msg object - if (this.console == "true") { + if (this.complete === "true") { + // debug complete msg object + if (this.console === "true") { node.log("\n"+util.inspect(msg, {colors:useColors, depth:10})); } if (this.active) { sendDebug({id:this.id,name:this.name,topic:msg.topic,msg:msg,_path:msg._path}); } - } else { // debug just the msg.payload - if (this.console == "true") { - if (typeof msg.payload === "string") { - node.log((msg.payload.indexOf("\n") != -1 ? "\n" : "") + msg.payload); + } else { + // debug user defined msg property + var property = "payload"; + var output = msg[property]; + if (this.complete !== "false" && typeof this.complete !== "undefined") { + property = this.complete; + var propertyParts = property.split("."); + try { + output = propertyParts.reduce(function (obj, i) { + return obj[i]; + }, msg); + } catch (err) { + node.warn(err); + return; + } + + if (!output) { + node.warn("msg." + this.complete + " does not exist"); + return; + } + } + if (this.console === "true") { + if (typeof output === "string") { + node.log((output.indexOf("\n") !== -1 ? "\n" : "") + output); + } else if (typeof output === "object") { + node.log("\n"+util.inspect(output, {colors:useColors, depth:10})); + } else { + node.log(util.inspect(output, {colors:useColors})); } - else if (typeof msg.payload === "object") { node.log("\n"+util.inspect(msg.payload, {colors:useColors, depth:10})); } - else { node.log(util.inspect(msg.payload, {colors:useColors})); } } if (this.active) { - sendDebug({id:this.id,name:this.name,topic:msg.topic,msg:msg.payload,_path:msg._path}); + sendDebug({id:this.id,name:this.name,topic:msg.topic,property:property,msg:output,_path:msg._path}); } } }); } - + RED.nodes.registerType("debug",DebugNode); - + function sendDebug(msg) { if (msg.msg instanceof Error) { msg.msg = msg.msg.toString(); @@ -75,29 +98,29 @@ module.exports = function(RED) { msg.msg = "(boolean) "+msg.msg.toString(); } else if (msg.msg === 0) { msg.msg = "0"; - } else if (msg.msg == null) { + } else if (msg.msg === null || typeof msg.msg === "undefined") { msg.msg = "(undefined)"; } - + if (msg.msg.length > debuglength) { msg.msg = msg.msg.substr(0,debuglength) +" ...."; } - + RED.comms.publish("debug",msg); } - + DebugNode.logHandler = new events.EventEmitter(); DebugNode.logHandler.on("log",function(msg) { - if (msg.level == "warn" || msg.level == "error") { + if (msg.level === "warn" || msg.level === "error") { sendDebug(msg); } }); RED.log.addHandler(DebugNode.logHandler); - + RED.httpAdmin.post("/debug/:id/:state", function(req,res) { var node = RED.nodes.getNode(req.params.id); var state = req.params.state; - if (node != null) { + if (node !== null && typeof node !== "undefined" ) { if (state === "enable") { node.active = true; res.send(200); @@ -111,4 +134,4 @@ module.exports = function(RED) { res.send(404); } }); -} +}; From 2a753c9d22b9282993a4a074602904ee27285e19 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Wed, 1 Oct 2014 09:50:47 +0100 Subject: [PATCH 2/3] Updated debug node test --- test/nodes/core/core/58-debug_spec.js | 55 +++++++++++++++++++++------ 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/test/nodes/core/core/58-debug_spec.js b/test/nodes/core/core/58-debug_spec.js index dc667a69f..1fff58767 100644 --- a/test/nodes/core/core/58-debug_spec.js +++ b/test/nodes/core/core/58-debug_spec.js @@ -24,7 +24,7 @@ describe('debug node', function() { before(function(done) { helper.startServer(done); }); - + afterEach(function() { helper.unload(); }); @@ -47,7 +47,8 @@ describe('debug node', function() { n1.emit("input", {payload:"test"}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",name:"Debug",msg:"test"} + topic:"debug",data:{id:"n1",name:"Debug",msg:"test", + property:"payload"} }); }, done); }); @@ -69,7 +70,7 @@ describe('debug node', function() { n1.emit("input", {payload:"test"}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg:"test"} + topic:"debug",data:{id:"n1",msg:"test",property:"payload"} }); count++; }, function() { @@ -95,6 +96,34 @@ describe('debug node', function() { }); }); + it('should publish other property', function(done) { + var flow = [{id:"n1", type:"debug", complete: "foo" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload:"test", foo:"bar"}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug",data:{id:"n1",msg:"bar",property:"foo"} + }); + }, done); + }); + }); + + it('should publish multi-level properties', function(done) { + var flow = [{id:"n1", type:"debug", complete: "foo.bar" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload:"test", foo: {bar: "bar"}}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug",data:{id:"n1",msg:"bar",property:"foo.bar"} + }); + }, done); + }); + }); + it('should publish an Error', function(done) { var flow = [{id:"n1", type:"debug" }]; helper.load(debugNode, flow, function() { @@ -103,7 +132,7 @@ describe('debug node', function() { n1.emit("input", {payload: new Error("oops")}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg:"Error: oops"} + topic:"debug",data:{id:"n1",msg:"Error: oops",property:"payload"} }); }, done); }); @@ -117,7 +146,7 @@ describe('debug node', function() { n1.emit("input", {payload: true}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg: '(boolean) true'} + topic:"debug",data:{id:"n1",msg: '(boolean) true',property:"payload"} }); }, done); }); @@ -131,7 +160,7 @@ describe('debug node', function() { n1.emit("input", {}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg: '(undefined)'} + topic:"debug",data:{id:"n1",msg: '(undefined)',property:"payload"} }); }, done); }); @@ -146,7 +175,7 @@ describe('debug node', function() { }, function(msg) { JSON.parse(msg).should.eql({ topic:"debug", - data:{id:"n1",msg:'(Object) {\n "type": "foo"\n}'} + data:{id:"n1",msg:'(Object) {\n "type": "foo"\n}',property:"payload"} }); }, done); }); @@ -161,7 +190,8 @@ describe('debug node', function() { }, function(msg) { JSON.parse(msg).should.eql({ topic:"debug", - data:{id:"n1",msg: '(Array) [\n 0,\n 1,\n 2,\n 3\n]'} + data:{id:"n1",msg: '(Array) [\n 0,\n 1,\n 2,\n 3\n]', + property:"payload"} }); }, done); }); @@ -180,7 +210,8 @@ describe('debug node', function() { topic:"debug", data:{ id:"n1", - msg:'(Object) {\n "name": "bar",\n "o": "[circular]"\n}' + msg:'(Object) {\n "name": "bar",\n "o": "[circular]"\n}', + property:"payload" } }); }, done); @@ -198,7 +229,8 @@ describe('debug node', function() { topic:"debug", data:{ id:"n1", - msg: Array(1001).join("X")+' ....' + msg: Array(1001).join("X")+' ....', + property:"payload" } }); }, done); @@ -217,6 +249,7 @@ describe('debug node', function() { data:{ id:"n1", msg: '(Buffer) 48454c4c4f', + property:"payload" } }); }, done); @@ -236,7 +269,7 @@ describe('debug node', function() { }); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg:"message 2"} + topic:"debug",data:{id:"n1",msg:"message 2",property:"payload"} }); }, done); }); From 17e4bf1a119d8ff5f9cbf72e4e588c4a42934c15 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Mon, 20 Oct 2014 11:39:19 +0100 Subject: [PATCH 3/3] Tidy up node label and edit dialog --- nodes/core/core/58-debug.html | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nodes/core/core/58-debug.html b/nodes/core/core/58-debug.html index 5957db6df..1d26a4aed 100644 --- a/nodes/core/core/58-debug.html +++ b/nodes/core/core/58-debug.html @@ -23,8 +23,7 @@
- msg. - + msg.
@@ -87,9 +86,9 @@ }, label: function() { if (this.complete === "true") { - return this.name||"debug msg"; + return this.name||"msg"; } else { - return this.name||"debug msg." + this.complete; + return this.name || "msg." + (this.complete === "false" ? "payload" : this.complete); } }, labelStyle: function() {