1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #422 from anna2130/debug

Updated debug node to output other msg properties
This commit is contained in:
Nick O'Leary 2014-10-20 16:35:24 +01:00
commit 53a9a5fe93
3 changed files with 153 additions and 58 deletions

View File

@ -16,12 +16,15 @@
<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:208px" 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 +39,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||"msg";
} else {
return this.name || "msg." + (this.complete === "false" ? "payload" : this.complete);
}
},
labelStyle: function() {
return this.name?"node_label_italic":"";
@ -150,12 +183,17 @@
};
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);
@ -184,7 +222,8 @@
RED.comms.unsubscribe("debug",this.handleDebugMessage);
RED.sidebar.removeTab("debug");
delete RED._debug;
}
},
oneditprepare: oneditprepare
});
</script>

View File

@ -26,27 +26,50 @@ module.exports = function(RED) {
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});
}
}
});
@ -75,7 +98,7 @@ 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)";
}
@ -88,7 +111,7 @@ module.exports = function(RED) {
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);
}
});
@ -97,7 +120,7 @@ module.exports = function(RED) {
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);
}
});
}
};

View File

@ -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);
@ -237,7 +270,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);
});