aboutsummaryrefslogtreecommitdiffstats
path: root/dgbuilder/core_nodes/core/58-debug.html
blob: 04aa50789e6ea5c2cbcd41e86f5b1fce36354b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!--
  Copyright 2013 IBM Corp.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<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>
            <option value="true">complete msg object</option>
        </select>
    </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;">
            <option value="false">debug tab</option>
            <option value="true">debug tab and console</option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
</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 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>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>In addition any calls to node.warn or node.error will appear here.</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType('debug',{
        category: 'output',
        defaults: {
            name: {value:""},
            active: {value:true},
            console: {value:"false"},
            complete: {value:"false"}
        },
        label: function() {
            return this.name||"debug";
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        color:"#87a980",
        inputs:1,
        outputs:0,
        icon: "debug.png",
        align: "right",
        button: {
            toggle: "active",
            onclick: function() {
                var label = this.name||"debug";
                d3.xhr("debug/"+this.id+"/"+(this.active?"enable":"disable")).post(function(err,resp) {
                    if (err) {
                        if (err.status == 404) {
                            RED.notify("<strong>Error</strong>: debug node not deployed","error");
                        } else if (err.status == 0) {
                            RED.notify("<strong>Error</strong>: no response from server","error");
                        } else {
                            RED.notify("<strong>Error</strong>: unexpected error: ("+err.status+")"+err.response,"error");
                        }
                    } else if (resp.status == 200) {
                        RED.notify("Successfully activated: "+label,"success");
                    } else if (resp.status == 201) {
                        RED.notify("Successfully deactivated: "+label,"success");
                    } else {
                        RED.notify("<strong>Error</strong>: unexpected response: ("+resp.status+") "+resp.response,"error");
                    }
                });
            }
        },
        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) {
                that.handleDebugMessage("",{
                    name:"debug",
                    msg:msg
                });
            }
    
            this.handleDebugMessage = function(t,o) {
                var msg = document.createElement("div");
                msg.onmouseover = function() {
                    msg.style.borderRightColor = "#999";
                    var n = RED.nodes.node(o.id);
                    if (n) {
                        n.highlighted = true;
                        n.dirty = true;
                    }
                    RED.view.redraw();
                };
                msg.onmouseout = function() {
                    msg.style.borderRightColor = "";
                    var n = RED.nodes.node(o.id);
                    if (n) {
                        n.highlighted = false;
                        n.dirty = true;
                    }
                    RED.view.redraw();
                };
                msg.onclick = function() {
                    var node = RED.nodes.node(o.id);
                    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 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>'+
                                '<span class="debug-message-name">['+name+']</span>'+
                                (o.topic?'<span class="debug-message-topic">'+topic+'</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--;
                }
                if (atBottom) {
                    $(sbc).scrollTop(sbc.scrollHeight);
                }
            };
            RED.comms.subscribe("debug",this.handleDebugMessage);
    
            $("#debug-tab-clear").click(function() {
                $(".debug-message").remove();
                messageCount = 0;
                RED.nodes.eachNode(function(node) {
                    node.highlighted = false;
                    node.dirty = true;
                });
                RED.view.redraw();
            });
        },
        onpaletteremove: function() {
            RED.comms.unsubscribe("debug",this.handleDebugMessage);
            RED.sidebar.removeTab("debug");
            delete RED._debug;
        }
    });
</script>

<style>
    #debug-content {
        position: absolute;
        top: 30px;
        bottom: 0px;
        left:0px;
        right: 0px;
        overflow-y: scroll;
    }
    #debug-toolbar {
        padding: 3px 10px;
        height: 24px;
        background: #f3f3f3;
    }
    .debug-message {
        cursor: pointer;
        border-bottom: 1px solid #eee;
        border-left: 8px solid #eee;
        border-right: 8px solid #eee;
        padding: 2px;
    }
    .debug-message-date {
        background: #fff;
        font-size: 9px;
        color: #aaa;
        padding: 1px 5px 1px 1px;
    }
    .debug-message-topic {
        display: block;
        background: #fff;
        padding: 1px 5px;
        font-size: 9px;
        color: #a66;
    }
    .debug-message-name {
        background: #fff;
        padding: 1px 5px;
        font-size: 9px;
        color: #aac;
    }
    .debug-message-payload {
        display: block;
        padding: 2px;
        background: #fff;
    }
    .debug-message-level-log {
        border-left-color: #eee;
        border-right-color: #eee;
    }
    .debug-message-level-warn {
        border-left-color: #ffdf9d;
        border-right-color: #ffdf9d;
    }
    .debug-message-level-error {
        border-left-color: #f99;
        border-right-color: #f99;
    }
</style>