2013-09-13 23:55:16 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2015-02-08 12:32:26 +01:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var util = require("util");
|
|
|
|
var events = require("events");
|
2016-06-20 22:35:20 +02:00
|
|
|
var path = require("path");
|
2018-01-13 17:14:03 +01:00
|
|
|
var debuglength = RED.settings.debugMaxLength || 1000;
|
2017-01-08 23:38:40 +01:00
|
|
|
var useColors = RED.settings.debugUseColors || false;
|
|
|
|
util.inspect.styles.boolean = "red";
|
2014-09-30 16:15:12 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function DebugNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.name = n.name;
|
2015-03-22 10:38:42 +01:00
|
|
|
this.complete = (n.complete||"payload").toString();
|
2018-01-13 17:14:03 +01:00
|
|
|
if (this.complete === "false") { this.complete = "payload"; }
|
|
|
|
this.console = ""+(n.console || false);
|
|
|
|
this.tostatus = n.tostatus || false;
|
|
|
|
this.tosidebar = n.tosidebar;
|
|
|
|
if (this.tosidebar === undefined) { this.tosidebar = true; }
|
|
|
|
this.severity = n.severity || 40;
|
2014-09-30 16:15:12 +02:00
|
|
|
this.active = (n.active === null || typeof n.active === "undefined") || n.active;
|
2018-08-31 17:31:08 +02:00
|
|
|
if (this.tostatus) {
|
2018-09-05 10:45:34 +02:00
|
|
|
this.oldStatus = {fill:"grey", shape:"ring"};
|
2018-08-31 17:31:08 +02:00
|
|
|
this.status(this.oldStatus);
|
|
|
|
}
|
|
|
|
else { this.status({}); }
|
2018-01-13 17:14:03 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2018-01-13 17:14:03 +01:00
|
|
|
var levels = {
|
|
|
|
off: 1,
|
|
|
|
fatal: 10,
|
|
|
|
error: 20,
|
|
|
|
warn: 30,
|
|
|
|
info: 40,
|
|
|
|
debug: 50,
|
|
|
|
trace: 60,
|
|
|
|
audit: 98,
|
|
|
|
metric: 99
|
|
|
|
};
|
|
|
|
var colors = {
|
|
|
|
"0": "grey",
|
|
|
|
"10": "grey",
|
|
|
|
"20": "red",
|
|
|
|
"30": "yellow",
|
|
|
|
"40": "grey",
|
|
|
|
"50": "green",
|
|
|
|
"60": "blue"
|
|
|
|
};
|
2014-09-30 16:15:12 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
this.on("input",function(msg) {
|
2014-09-30 16:15:12 +02:00
|
|
|
if (this.complete === "true") {
|
2018-01-13 17:14:03 +01:00
|
|
|
// debug complete msg object
|
2014-09-30 16:15:12 +02:00
|
|
|
if (this.console === "true") {
|
2014-05-04 00:32:04 +02:00
|
|
|
node.log("\n"+util.inspect(msg, {colors:useColors, depth:10}));
|
|
|
|
}
|
2018-01-13 17:14:03 +01:00
|
|
|
if (this.active && this.tosidebar) {
|
|
|
|
sendDebug({id:node.id, name:node.name, topic:msg.topic, msg:msg, _path:msg._path});
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2018-01-13 17:14:03 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// debug user defined msg property
|
2014-09-30 16:15:12 +02:00
|
|
|
var property = "payload";
|
|
|
|
var output = msg[property];
|
|
|
|
if (this.complete !== "false" && typeof this.complete !== "undefined") {
|
|
|
|
property = this.complete;
|
|
|
|
try {
|
2016-06-20 11:19:18 +02:00
|
|
|
output = RED.util.getMessageProperty(msg,this.complete);
|
|
|
|
} catch(err) {
|
2014-10-23 16:28:47 +02:00
|
|
|
output = undefined;
|
2014-09-30 16:15:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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}));
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.active) {
|
2018-01-13 17:14:03 +01:00
|
|
|
if (this.tosidebar == true) {
|
|
|
|
sendDebug({id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:output, _path:msg._path});
|
|
|
|
}
|
|
|
|
if (this.tostatus === true) {
|
|
|
|
var st = util.inspect(output);
|
|
|
|
if (st.length > 32) { st = st.substr(0,32) + "..."; }
|
|
|
|
node.oldStatus = {fill:colors[node.severity], shape:"dot", text:st};
|
|
|
|
node.status(node.oldStatus);
|
|
|
|
}
|
2014-04-08 12:31:22 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
2014-09-30 16:15:12 +02:00
|
|
|
|
2017-04-22 10:03:52 +02:00
|
|
|
RED.nodes.registerType("debug",DebugNode, {
|
|
|
|
settings: {
|
|
|
|
debugUseColors: {
|
|
|
|
value: false,
|
|
|
|
},
|
|
|
|
debugMaxLength: {
|
|
|
|
value: 1000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-09-30 16:15:12 +02:00
|
|
|
|
2014-05-07 21:47:25 +02:00
|
|
|
function sendDebug(msg) {
|
2017-04-24 21:36:27 +02:00
|
|
|
// don't put blank errors in sidebar (but do add to logs)
|
|
|
|
//if ((msg.msg === "") && (msg.hasOwnProperty("level")) && (msg.level === 20)) { return; }
|
2018-06-25 23:31:11 +02:00
|
|
|
msg = RED.util.encodeObject(msg,{maxLength:debuglength});
|
2014-05-07 21:47:25 +02:00
|
|
|
RED.comms.publish("debug",msg);
|
2014-04-08 12:31:22 +02:00
|
|
|
}
|
2014-09-30 16:15:12 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
DebugNode.logHandler = new events.EventEmitter();
|
|
|
|
DebugNode.logHandler.on("log",function(msg) {
|
2015-02-07 20:29:43 +01:00
|
|
|
if (msg.level === RED.log.WARN || msg.level === RED.log.ERROR) {
|
2017-07-05 00:30:51 +02:00
|
|
|
sendDebug(msg);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
RED.log.addHandler(DebugNode.logHandler);
|
2014-09-30 16:15:12 +02:00
|
|
|
|
2014-12-10 15:16:07 +01:00
|
|
|
RED.httpAdmin.post("/debug/:id/:state", RED.auth.needsPermission("debug.write"), function(req,res) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = RED.nodes.getNode(req.params.id);
|
|
|
|
var state = req.params.state;
|
2014-09-30 16:15:12 +02:00
|
|
|
if (node !== null && typeof node !== "undefined" ) {
|
2014-05-04 00:32:04 +02:00
|
|
|
if (state === "enable") {
|
|
|
|
node.active = true;
|
2015-07-15 23:43:24 +02:00
|
|
|
res.sendStatus(200);
|
2018-08-31 17:31:08 +02:00
|
|
|
if (node.tostatus) { node.status({fill:"grey", shape:"dot"}); }
|
2014-05-04 00:32:04 +02:00
|
|
|
} else if (state === "disable") {
|
|
|
|
node.active = false;
|
2015-07-15 23:43:24 +02:00
|
|
|
res.sendStatus(201);
|
2018-01-13 17:14:03 +01:00
|
|
|
if (node.tostatus && node.hasOwnProperty("oldStatus")) {
|
2018-09-05 10:45:34 +02:00
|
|
|
node.oldStatus.shape = "dot";
|
2018-01-13 17:14:03 +01:00
|
|
|
node.status(node.oldStatus);
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2015-07-15 23:43:24 +02:00
|
|
|
res.sendStatus(404);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-04-08 12:31:22 +02:00
|
|
|
} else {
|
2015-07-15 23:43:24 +02:00
|
|
|
res.sendStatus(404);
|
2014-04-08 12:31:22 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2016-06-20 22:35:20 +02:00
|
|
|
|
2017-01-10 10:43:46 +01:00
|
|
|
// As debug/view/debug-utils.js is loaded via <script> tag, it won't get
|
|
|
|
// the auth header attached. So do not use RED.auth.needsPermission here.
|
|
|
|
RED.httpAdmin.get("/debug/view/*",function(req,res) {
|
2016-06-20 22:35:20 +02:00
|
|
|
var options = {
|
|
|
|
root: __dirname + '/lib/debug/',
|
|
|
|
dotfiles: 'deny'
|
|
|
|
};
|
|
|
|
res.sendFile(req.params[0], options);
|
|
|
|
});
|
2014-09-30 16:15:12 +02:00
|
|
|
};
|