From b61a250d58616dcd1d3eeaf27b0754951e449d84 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 20 Mar 2015 22:09:58 +0000 Subject: [PATCH] Debug message formatting mangling brackets --- nodes/core/core/58-debug.html | 27 +++++++++++------- nodes/core/core/58-debug.js | 25 +++++++++++----- public/red/ui/tab-info.js | 2 +- red/nodes/Flow.js | 11 +++++-- test/nodes/core/core/58-debug_spec.js | 41 ++++++++++++++------------- 5 files changed, 66 insertions(+), 40 deletions(-) diff --git a/nodes/core/core/58-debug.html b/nodes/core/core/58-debug.html index 5349ce82d..341d1a844 100644 --- a/nodes/core/core/58-debug.html +++ b/nodes/core/core/58-debug.html @@ -156,7 +156,9 @@ msg:msg }); } - + function sanitize(m) { + return m.replace(/&/g,"&").replace(//g,">"); + } this.handleDebugMessage = function(t,o) { var msg = document.createElement("div"); msg.onmouseover = function() { @@ -184,15 +186,17 @@ } }; - var name = (o.name?o.name:o.id).toString().replace(/&/g,"&").replace(//g,">"); - var topic = (o.topic||"").toString().replace(/&/g,"&").replace(//g,">"); - var property = (o.property?o.property:'').replace(/&/g,"&").replace(//g,">"); - var payload = (o.msg||"()").toString().replace(/&/g,"&").replace(//g,">"); - var typ = payload.substring(0,payload.indexOf(')')+1); - payload = payload.substring(payload.indexOf(')')+1); + console.log(o); + var name = sanitize(((o.name?o.name:o.id)||"").toString()); + var topic = sanitize((o.topic||"").toString()); + var property = sanitize(o.property?o.property:''); + var payload = sanitize((o.msg||"").toString()); + var format = sanitize((o.format||"").toString()); + msg.className = 'debug-message'+(o.level?(' debug-message-level-'+o.level):''); msg.innerHTML = ''+ - getTimestamp()+'['+name+']'+ + getTimestamp()+''+ + (name?'['+name+']':'')+ ''; // NOTE: relying on function error to have a "type" that all other msgs don't if (o.hasOwnProperty("type") && (o.type === "function")) { @@ -205,8 +209,11 @@ msg.className = 'debug-message debug-message-level-' + errorLvl; msg.innerHTML += '[function] : (' + errorLvlType + ')'; } else { - msg.innerHTML += ''+(o.topic?topic+' : ':'')+ - (o.property?'[msg.'+property+']':'[msg]')+" : "+typ+''; + msg.innerHTML += ''+ + (o.topic?topic+' : ':'')+ + (o.property?'[msg.'+property+']':'[msg]')+" : "+format+ + + ''; } msg.innerHTML += ''+ payload+ ''; var atBottom = (sbc.scrollHeight-messages.offsetHeight-sbc.scrollTop) < 5; diff --git a/nodes/core/core/58-debug.js b/nodes/core/core/58-debug.js index c30356cc3..8230b9f2f 100644 --- a/nodes/core/core/58-debug.js +++ b/nodes/core/core/58-debug.js @@ -82,14 +82,18 @@ module.exports = function(RED) { function sendDebug(msg) { if (msg.msg instanceof Error) { + msg.format = "error"; msg.msg = msg.msg.toString(); } else if (msg.msg instanceof Buffer) { - msg.msg = "(Buffer) "+msg.msg.toString('hex'); + msg.format = "buffer"; + msg.msg = msg.msg.toString('hex'); } else if (typeof msg.msg === 'object') { var seen = []; - var ty = "(Object) "; - if (util.isArray(msg.msg)) { ty = "(Array) "; } - msg.msg = ty + JSON.stringify(msg.msg, function(key, value) { + msg.format = "object"; + if (util.isArray(msg.msg)) { + msg.format = "array"; + } + msg.msg = JSON.stringify(msg.msg, function(key, value) { if (typeof value === 'object' && value !== null) { if (seen.indexOf(value) !== -1) { return "[circular]"; } seen.push(value); @@ -98,14 +102,21 @@ module.exports = function(RED) { }," "); seen = null; } else if (typeof msg.msg === "boolean") { - msg.msg = "(boolean) "+msg.msg.toString(); + msg.format = "boolean"; + msg.msg = msg.msg.toString(); } else if (typeof msg.msg === "number") { - msg.msg = "(number) "+msg.msg.toString(); + msg.format = "number"; + msg.msg = msg.msg.toString(); } else if (msg.msg === 0) { + msg.format = "number"; msg.msg = "0"; } else if (msg.msg === null || typeof msg.msg === "undefined") { + msg.format = (msg.msg === null)?"null":"undefined"; msg.msg = "(undefined)"; - } else { msg.msg = "(string) "+msg.msg; } + } else { + msg.format = "string"; + msg.msg = msg.msg; + } if (msg.msg.length > debuglength) { msg.msg = msg.msg.substr(0,debuglength) +" ...."; diff --git a/public/red/ui/tab-info.js b/public/red/ui/tab-info.js index e11fb655b..c5f62c5c9 100644 --- a/public/red/ui/tab-info.js +++ b/public/red/ui/tab-info.js @@ -97,7 +97,7 @@ RED.sidebar.info = (function() { var val = node[n]||""; var type = typeof val; if (type === "string") { - if (val.length == 0) { + if (val.length === 0) { val += 'blank'; } else { if (val.length > 30) { diff --git a/red/nodes/Flow.js b/red/nodes/Flow.js index 106e1a1fa..259b7debf 100644 --- a/red/nodes/Flow.js +++ b/red/nodes/Flow.js @@ -21,7 +21,7 @@ var typeRegistry = require("./registry"); var credentials = require("./credentials"); var redUtil = require("../util"); var events = require("../events"); -var log = require("../log"); +var Log = require("../log"); function getID() { return (1+Math.random()*4294967295).toString(16); @@ -35,10 +35,15 @@ function createNode(type,config) { nn = new nt(clone(config)); } catch (err) { - log.warn(type+" : "+err); + Log.log({ + level: Log.ERROR, + id:config.id, + type: type, + msg: err + }); } } else { - log.warn("unknown type: "+type); + Log.error("Unknown type: "+type); } return nn; } diff --git a/test/nodes/core/core/58-debug_spec.js b/test/nodes/core/core/58-debug_spec.js index 1a6b98bd6..7955f2614 100644 --- a/test/nodes/core/core/58-debug_spec.js +++ b/test/nodes/core/core/58-debug_spec.js @@ -47,8 +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:"(string) test", - property:"payload"} + topic:"debug",data:{id:"n1",name:"Debug",msg:"test", + format:"string",property:"payload"} }); }, done); }); @@ -63,7 +63,7 @@ describe('debug node', function() { n1.emit("input", {payload:"test"}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg:"(string) test",property:"payload"} + topic:"debug",data:{id:"n1",msg:"test",property:"payload",format:"string"} }); count++; }, function() { @@ -93,7 +93,7 @@ describe('debug node', function() { }, function(msg) { JSON.parse(msg).should.eql({ topic:"debug", - data:{id:"n1",msg:'(Object) {\n "payload": "test"\n}'} + data:{id:"n1",msg:'{\n "payload": "test"\n}',format:"object"} }); }, done); }); @@ -107,7 +107,7 @@ describe('debug node', function() { n1.emit("input", {payload:"test", foo:"bar"}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg:"(string) bar",property:"foo"} + topic:"debug",data:{id:"n1",msg:"bar",property:"foo",format:"string"} }); }, done); }); @@ -121,7 +121,7 @@ describe('debug node', function() { n1.emit("input", {payload:"test", foo: {bar: "bar"}}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg:"(string) bar",property:"foo.bar"} + topic:"debug",data:{id:"n1",msg:"bar",property:"foo.bar",format:"string"} }); }, done); }); @@ -135,7 +135,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",property:"payload"} + topic:"debug",data:{id:"n1",msg:"Error: oops",property:"payload",format:"error"} }); }, done); }); @@ -149,7 +149,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',property:"payload"} + topic:"debug",data:{id:"n1",msg: 'true',property:"payload",format:"boolean"} }); }, done); }); @@ -163,7 +163,7 @@ describe('debug node', function() { n1.emit("input", {}); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg: '(undefined)',property:"payload"} + topic:"debug",data:{id:"n1",msg: '(undefined)',property:"payload",format:"undefined"} }); }, done); }); @@ -178,7 +178,7 @@ describe('debug node', function() { }, function(msg) { JSON.parse(msg).should.eql({ topic:"debug", - data:{id:"n1",msg:'(Object) {\n "type": "foo"\n}',property:"payload"} + data:{id:"n1",msg:'{\n "type": "foo"\n}',property:"payload",format:"object"} }); }, done); }); @@ -193,7 +193,7 @@ 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: '[\n 0,\n 1,\n 2,\n 3\n]',format:"array", property:"payload"} }); }, done); @@ -213,8 +213,8 @@ describe('debug node', function() { topic:"debug", data:{ id:"n1", - msg:'(Object) {\n "name": "bar",\n "o": "[circular]"\n}', - property:"payload" + msg:'{\n "name": "bar",\n "o": "[circular]"\n}', + property:"payload",format:"object" } }); }, done); @@ -228,12 +228,14 @@ describe('debug node', function() { websocket_test(function() { n1.emit("input", {payload: Array(1002).join("X")}); }, function(msg) { - JSON.parse(msg).should.eql({ + var a = JSON.parse(msg); + a.should.eql({ topic:"debug", data:{ id:"n1", - msg: "(string) "+Array(992).join("X")+' ....', - property:"payload" + msg: Array(1001).join("X")+' ....', + property:"payload", + format:"string" } }); }, done); @@ -251,8 +253,9 @@ describe('debug node', function() { topic:"debug", data:{ id:"n1", - msg: '(Buffer) 48454c4c4f', - property:"payload" + msg: '48454c4c4f', + property:"payload", + format: "buffer" } }); }, done); @@ -273,7 +276,7 @@ describe('debug node', function() { }); }, function(msg) { JSON.parse(msg).should.eql({ - topic:"debug",data:{id:"n1",msg:"(string) message 2",property:"payload"} + topic:"debug",data:{id:"n1",msg:"message 2",property:"payload",format:"string"} }); }, done); });