From f96b40cff2e05961fa243c27ad5140d5ad1bf592 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Tue, 30 Sep 2014 15:15:12 +0100 Subject: [PATCH] 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); } }); -} +};