Updated debug node to output other msg properties

This commit is contained in:
Anna Thomas 2014-09-30 15:15:12 +01:00
parent 4f496c37be
commit f96b40cff2
2 changed files with 110 additions and 47 deletions

View File

@ -16,12 +16,16 @@
<script type="text/x-red" data-template-name="debug">
<div class="form-row">
<label for="node-input-complete"><i class="fa fa-list"></i> Output</label>
<select type="text" id="node-input-complete" style="display: inline-block; width: 250px; vertical-align: top;">
<option value="false">payload only</option>
<label for="node-input-select-complete"><i class="fa fa-list"></i> Output</label>
<select type="text" id="node-input-select-complete" style="display: inline-block; width: 250px; vertical-align: top;">
<option value="false">message property</option>
<option value="true">complete msg object</option>
</select>
</div>
<div class="form-row" id="node-prop-row">
<label for="node-input-complete">&nbsp;</label>msg.
<input type="text" style="width:205px" id="node-input-complete">
</div>
<div class="form-row">
<label for="node-input-console"><i class="fa fa-random"></i> to</label>
<select type="text" id="node-input-console" style="display: inline-block; width: 250px; vertical-align: top;">
@ -36,27 +40,57 @@
</script>
<script type="text/x-red" data-help-name="debug">
<p>The Debug node can be connected to the output of any node. It will display the timestamp, <b>msg.topic</b> and <b>msg.payload</b> fields of any messages it receives in the debug tab of the sidebar.
<br/>The sidebar can be accessed under the options drop-down in the top right corner.</p>
<p>The Debug node can be connected to the output of any node. It can be used to display the output of any message property in the debug tab of the sidebar. The default is to display <b>msg.payload</b>.</p>
<p>Each message will also display the timestamp, <b>msg.topic</b> and the property chosen to output.</p>
<p>The sidebar can be accessed under the options drop-down in the top right corner.</p>
<p>The button to the right of the node will toggle it's output on and off so you can de-clutter the debug window.</p>
<p>If the payload is an object it will be stringified first for display and indicate that by saying "(Object) ".</p>
<p>If the payload is a buffer it will be stringified first for display and indicate that by saying "(Buffer) ".</p>
<p>If the payload is an object or buffer it will be stringified first for display and indicate that by saying "(Object)" or "(Buffer)".</p>
<p>Selecting any particular message will highlight (in red) the debug node that reported it. This is useful if you wire up multiple debug nodes.</p>
<p>Optionally can show the complete msg object - but the screen can get messy.</p>
<p>Optionally can show the complete <b>msg</b> object.</p>
<p>In addition any calls to node.warn or node.error will appear here.</p>
</script>
<script type="text/javascript">
function oneditprepare() {
if (this.complete === "true") {
// show complete message object
$("#node-input-select-complete").val(this.complete);
$("#node-prop-row").hide();
} else {
// show msg.[ ]
var property = (this.complete === "false") ? "payload" : this.complete;
$("#node-input-select-complete").val("false");
$("#node-input-complete").val(property);
$("#node-prop-row").show();
}
$("#node-input-select-complete").change(function() {
var v = $("#node-input-select-complete option:selected").val();
$("#node-input-complete").val(v);
if (v !== "true") {
$("#node-input-complete").val("payload");
$("#node-prop-row").show();
$("#node-input-complete").focus();
} else {
$("#node-prop-row").hide();
}
});
}
RED.nodes.registerType('debug',{
category: 'output',
defaults: {
name: {value:""},
active: {value:true},
console: {value:"false"},
complete: {value:"false"}
complete: {value:"false", required:true}
},
label: function() {
return this.name||"debug";
if (this.complete === "true") {
return this.name||"debug msg";
} else {
return this.name||"debug msg." + this.complete;
}
},
labelStyle: function() {
return this.name?"node_label_italic":"";
@ -92,26 +126,26 @@
onpaletteadd: function() {
var content = document.createElement("div");
content.id = "tab-debug";
var toolbar = document.createElement("div");
toolbar.id = "debug-toolbar";
content.appendChild(toolbar);
toolbar.innerHTML = '<div class="btn-group pull-right"><a id="debug-tab-clear" title="clear log" class="btn btn-mini" href="#"><i class="fa fa-trash"></i></a></div> ';
var messages = document.createElement("div");
messages.id = "debug-content";
content.appendChild(messages);
RED.sidebar.addTab("debug",content);
function getTimestamp() {
var d = new Date();
return d.toLocaleString();
}
var sbc = document.getElementById("debug-content");
var messageCount = 0;
var that = this;
RED._debug = function(msg) {
@ -120,7 +154,7 @@
msg:msg
});
}
this.handleDebugMessage = function(t,o) {
var msg = document.createElement("div");
msg.onmouseover = function() {
@ -146,20 +180,25 @@
if (node) {
RED.view.showWorkspace(node.z);
}
};
var name = (o.name?o.name:o.id).toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
var topic = (o.topic||"").toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
var property = (o.property?o.property:'').replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
var payload = (o.msg||"").toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
msg.className = 'debug-message'+(o.level?(' debug-message-level-'+o.level):'');
msg.innerHTML = '<span class="debug-message-date">'+getTimestamp()+'</span>'+
msg.innerHTML = '<span class="debug-message-date">'+
getTimestamp()+'</span>'+
'<span class="debug-message-name">['+name+']</span>'+
(o.topic?'<span class="debug-message-topic">'+topic+'</span>':'')+
'<span class="debug-message-payload">'+payload+'</span>';
'<span class="debug-message-topic">'+
(o.topic?topic+' : ':'')+
(o.property?'[msg.'+property+']':'[msg]')+
'</span>'+'<span class="debug-message-payload">'+
payload+'</span>';
var atBottom = (sbc.scrollHeight-messages.offsetHeight-sbc.scrollTop) < 5;
messageCount++;
$(messages).append(msg);
if (messageCount > 200) {
$("#debug-content .debug-message:first").remove();
messageCount--;
@ -169,7 +208,7 @@
}
};
RED.comms.subscribe("debug",this.handleDebugMessage);
$("#debug-tab-clear").click(function() {
$(".debug-message").remove();
messageCount = 0;
@ -184,7 +223,8 @@
RED.comms.unsubscribe("debug",this.handleDebugMessage);
RED.sidebar.removeTab("debug");
delete RED._debug;
}
},
oneditprepare: oneditprepare
});
</script>

View File

@ -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);
}
});
}
};