From 0ed9f6cc4f7deede85013bd77784b6b3e5ab71c3 Mon Sep 17 00:00:00 2001
From: Antoine Aflalo
Date: Sat, 28 Feb 2015 13:45:23 +0200
Subject: [PATCH] Adding an Object node to the sandbox of a function node
Permit the user of the sandbox to log using the Function Node. Test provided
and working.
Fix Display warning message in the debug log
Before they were displayed as error instead of warning
---
nodes/core/core/58-debug.html | 10 +++-
nodes/core/core/80-function.html | 7 +++
nodes/core/core/80-function.js | 12 ++++
test/nodes/core/core/80-function_spec.js | 70 +++++++++++++++++++++++-
4 files changed, 96 insertions(+), 3 deletions(-)
diff --git a/nodes/core/core/58-debug.html b/nodes/core/core/58-debug.html
index 093bf1f76..5349ce82d 100644
--- a/nodes/core/core/58-debug.html
+++ b/nodes/core/core/58-debug.html
@@ -196,8 +196,14 @@
'';
// NOTE: relying on function error to have a "type" that all other msgs don't
if (o.hasOwnProperty("type") && (o.type === "function")) {
- msg.className = 'debug-message debug-message-level-20';
- msg.innerHTML += '[function] : (error)';
+ var errorLvlType = 'error';
+ var errorLvl = 20;
+ if (o.hasOwnProperty("level") && o.level === 30) {
+ errorLvl = 30;
+ errorLvlType = 'warn'
+ }
+ msg.className = 'debug-message debug-message-level-' + errorLvl;
+ msg.innerHTML += '[function] : (' + errorLvlType + ')';
} else {
msg.innerHTML += ''+(o.topic?topic+' : ':'')+
(o.property?'[msg.'+property+']':'[msg]')+" : "+typ+'';
diff --git a/nodes/core/core/80-function.html b/nodes/core/core/80-function.html
index cef0809d8..f581574ac 100644
--- a/nodes/core/core/80-function.html
+++ b/nodes/core/core/80-function.html
@@ -39,6 +39,13 @@
The message is passed in as a JavaScript object called msg
.
By convention it will have a msg.payload
property containing
the body of the message.
+ If your node need to log anything use the node
object:
+
+ - Info:
node.log(msg);
+ - Warning:
node.warn(msg);
+ - Error:
node.error(msg);
+
+
The function should return the messages it wants to pass on to the next nodes
in the flow. It can return:
diff --git a/nodes/core/core/80-function.js b/nodes/core/core/80-function.js
index a333341bf..e45961e04 100644
--- a/nodes/core/core/80-function.js
+++ b/nodes/core/core/80-function.js
@@ -21,6 +21,7 @@ module.exports = function(RED) {
function FunctionNode(n) {
RED.nodes.createNode(this,n);
+ var node = this;
this.name = n.name;
this.func = n.func;
var functionText = "var results = null; results = (function(msg){\n"+this.func+"\n})(msg);";
@@ -29,6 +30,17 @@ module.exports = function(RED) {
console:console,
util:util,
Buffer:Buffer,
+ node: {
+ log : function() {
+ node.log.apply(node, arguments);
+ },
+ error: function(){
+ node.error.apply(node, arguments);
+ },
+ warn: function() {
+ node.warn.apply(node, arguments);
+ }
+ },
context: {
global:RED.settings.functionGlobalContext || {}
}
diff --git a/test/nodes/core/core/80-function_spec.js b/test/nodes/core/core/80-function_spec.js
index d08d03b2e..187b37a3d 100644
--- a/test/nodes/core/core/80-function_spec.js
+++ b/test/nodes/core/core/80-function_spec.js
@@ -23,7 +23,7 @@ describe('function node', function() {
before(function(done) {
helper.startServer(done);
});
-
+
afterEach(function() {
helper.unload();
});
@@ -153,5 +153,73 @@ describe('function node', function() {
}
});
});
+ describe('Logger', function () {
+ it('should log an Info Message', function (done) {
+ var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.log('test');"}];
+ helper.load(functionNode, flow, function () {
+ var n1 = helper.getNode("n1");
+ n1.receive({payload: "foo", topic: "bar"});
+ try {
+ helper.log().called.should.be.true;
+ var logEvents = helper.log().args.filter(function (evt) {
+ return evt[0].type == "function";
+ });
+ logEvents.should.have.length(1);
+ var msg = logEvents[0][0];
+ msg.should.have.property('level', helper.log().INFO);
+ msg.should.have.property('id', 'n1');
+ msg.should.have.property('type', 'function');
+ msg.should.have.property('msg', 'test');
+ done();
+ } catch (err) {
+ done(err);
+ }
+ });
+ });
+ it('should log a Warning Message', function (done) {
+ var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.warn('test');"}];
+ helper.load(functionNode, flow, function () {
+ var n1 = helper.getNode("n1");
+ n1.receive({payload: "foo", topic: "bar"});
+ try {
+ helper.log().called.should.be.true;
+ var logEvents = helper.log().args.filter(function (evt) {
+ return evt[0].type == "function";
+ });
+ logEvents.should.have.length(1);
+ var msg = logEvents[0][0];
+ msg.should.have.property('level', helper.log().WARN);
+ msg.should.have.property('id', 'n1');
+ msg.should.have.property('type', 'function');
+ msg.should.have.property('msg', 'test');
+ done();
+ } catch (err) {
+ done(err);
+ }
+ });
+ });
+ it('should log an Error Message', function (done) {
+ var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.error('test');"}];
+ helper.load(functionNode, flow, function () {
+ var n1 = helper.getNode("n1");
+ n1.receive({payload: "foo", topic: "bar"});
+ try {
+ helper.log().called.should.be.true;
+ var logEvents = helper.log().args.filter(function (evt) {
+ return evt[0].type == "function";
+ });
+ logEvents.should.have.length(1);
+ var msg = logEvents[0][0];
+ msg.should.have.property('level', helper.log().ERROR);
+ msg.should.have.property('id', 'n1');
+ msg.should.have.property('type', 'function');
+ msg.should.have.property('msg', 'test');
+ done();
+ } catch (err) {
+ done(err);
+ }
+ });
+ });
+ });
});