|
| 1 | +/* |
| 2 | +Copyright (c) 2012, Yahoo! Inc. All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use of this software in source and binary forms, |
| 5 | +with or without modification, are permitted provided that the following |
| 6 | +conditions are met: |
| 7 | +
|
| 8 | +* Redistributions of source code must retain the above |
| 9 | + copyright notice, this list of conditions and the |
| 10 | + following disclaimer. |
| 11 | +
|
| 12 | +* Redistributions in binary form must reproduce the above |
| 13 | + copyright notice, this list of conditions and the |
| 14 | + following disclaimer in the documentation and/or other |
| 15 | + materials provided with the distribution. |
| 16 | +
|
| 17 | +* Neither the name of Yahoo! Inc. nor the names of its |
| 18 | + contributors may be used to endorse or promote products |
| 19 | + derived from this software without specific prior |
| 20 | + written permission of Yahoo! Inc. |
| 21 | +
|
| 22 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 23 | +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 24 | +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 25 | +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | +*/ |
| 34 | + |
| 35 | +/** |
| 36 | +* An appender that displays log messages in a pop-up window. |
| 37 | +* @module comms-log4js-appenders-console |
| 38 | +*/ |
| 39 | +YUI.add('comms-log4js-appenders-console', function(Y) |
| 40 | +{ |
| 41 | + Y.comms.log4js.register("console", |
| 42 | + function(logger, show, name) |
| 43 | + { |
| 44 | + var w, doc, output, filterTimeout, timestamps, filter, |
| 45 | + regex = /.*/, |
| 46 | + firstIdx = 0, |
| 47 | + /** |
| 48 | + * This class implements an appender that will display log messages in a |
| 49 | + * pop-up window. The appender can be created and attached without opening |
| 50 | + * the window, if desired. The window can later be opened by calling show(). |
| 51 | + * @class consoleAppender |
| 52 | + */ |
| 53 | + a = |
| 54 | + { |
| 55 | + /** |
| 56 | + * Used by the logger to notify this appender of a new log entry. |
| 57 | + * @public |
| 58 | + * @method notify |
| 59 | + * @param {Object} entry A new log entry |
| 60 | + */ |
| 61 | + notify: function(entry) |
| 62 | + { |
| 63 | + if (output) |
| 64 | + output.value += format(entry, timestamps.checked); |
| 65 | + }, |
| 66 | + /** |
| 67 | + * Open the pop-up window |
| 68 | + * @public |
| 69 | + * @method show |
| 70 | + */ |
| 71 | + show: function() |
| 72 | + { |
| 73 | + w = window.open('', name, 'width=650,height=700,resizable'); |
| 74 | + doc = w.document; |
| 75 | + |
| 76 | + doc.body.innerHTML = |
| 77 | + '<div id="toolbar" style="border-bottom:1px solid #aca899;background-color:#f1efe7;height:22px;font-size:90%">' + |
| 78 | + '<button id="clear" style="float:right;margin-right:3px;font-size:95%;font-family:inherit">Clear</button>' + |
| 79 | + '<input id="filter" type="text" title="Type to Filter" style="font-size:95%;float:right;margin-right:20px;font-family:tahoma,verdana"></input>' + |
| 80 | + '<input id="timestamps" type="checkbox" style="font-size:95%"></input><label for="timestamps">Timestamps</label>' + |
| 81 | + '</div>' + |
| 82 | + '<div style="position:fixed;width:100%;top:22px;bottom:0;margin:0">'+ |
| 83 | + '<textarea id="output" readonly="readonly" style="resize:none;padding:0;width:100%;height:100%"></textarea>'+ |
| 84 | + '</div>'; |
| 85 | + |
| 86 | + doc.body.style.cssText="padding:0;margin:0;font-size:77%;font-family:tahoma,verdana"; |
| 87 | + |
| 88 | + output = doc.getElementById("output"); |
| 89 | + |
| 90 | + doc.getElementById("clear").onclick = clear; |
| 91 | + |
| 92 | + timestamps = doc.getElementById("timestamps"); |
| 93 | + timestamps.onclick = refresh; |
| 94 | + filter = doc.getElementById("filter"); |
| 95 | + filter.value = '.*'; |
| 96 | + |
| 97 | + filter.onkeyup = changeFilter; |
| 98 | + |
| 99 | + refresh(); |
| 100 | + }, |
| 101 | + /** |
| 102 | + * Redraw all log entries in the window. |
| 103 | + * @public |
| 104 | + * @method refresh |
| 105 | + */ |
| 106 | + refresh: function() |
| 107 | + { |
| 108 | + if (output) |
| 109 | + { |
| 110 | + var entry, |
| 111 | + out = [ ], |
| 112 | + i = 0, |
| 113 | + entries = logger.getEntries(), |
| 114 | + checked = timestamps.checked; |
| 115 | + |
| 116 | + if (logger.header && regex.test(logger.header)) |
| 117 | + out.push(logger.header); |
| 118 | + |
| 119 | + while (entry = entries[i++]) |
| 120 | + { |
| 121 | + if (entry.id > firstIdx && regex.test(entry.msg)) |
| 122 | + out.push(format(entry, checked)); |
| 123 | + } |
| 124 | + |
| 125 | + output.value = out.join(""); |
| 126 | + } |
| 127 | + } |
| 128 | + }, |
| 129 | + refresh = a.refresh; |
| 130 | + |
| 131 | + logger.show = a.show; |
| 132 | + show && a.show(); |
| 133 | + |
| 134 | + return a; |
| 135 | + |
| 136 | + /** |
| 137 | + * Clear the window. All this does is change the first index to display to be |
| 138 | + * the next log entry to be received. It's not implemented, but un-clearing is |
| 139 | + * possible. |
| 140 | + * @private |
| 141 | + * @method clear |
| 142 | + */ |
| 143 | + function clear() |
| 144 | + { |
| 145 | + var entries = logger.getEntries(); |
| 146 | + firstIdx = entries.length && entries[entries.length-1].id; |
| 147 | + refresh(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * The filter has been changed. Wait a short time and then redraw the window. |
| 152 | + * The wait is to avoid redrawing on every keystroke when the user is typing |
| 153 | + * a new filter. |
| 154 | + * @private |
| 155 | + * @method changeFilter |
| 156 | + */ |
| 157 | + function changeFilter() |
| 158 | + { |
| 159 | + if (filterTimeout) |
| 160 | + clearTimeout(filterTimeout); |
| 161 | + filterTimeout = setTimeout(updateFilter, 200); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * The filter has changed and the short wait in changeFilter() has expired. |
| 166 | + * Redraw the window using the new filter. |
| 167 | + * @private |
| 168 | + * @method updateFilter |
| 169 | + */ |
| 170 | + function updateFilter() |
| 171 | + { |
| 172 | + filterTimeout = 0; |
| 173 | + |
| 174 | + // Do this in a try/catch because the user may type in an invalid regex |
| 175 | + try |
| 176 | + { |
| 177 | + regex = new RegExp(filter.value, 'i'); |
| 178 | + } |
| 179 | + catch (e) { } |
| 180 | + |
| 181 | + refresh(); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Format a single log entry for display. |
| 186 | + * @private |
| 187 | + * @method format |
| 188 | + * @param entry {Object} The log entry to format |
| 189 | + * @param ts {Boolean} Whether or not to include the timestamp (if present) |
| 190 | + * @return {String} The log message, ready for display. |
| 191 | + */ |
| 192 | + function format(entry, ts) |
| 193 | + { |
| 194 | + var msg = ""; |
| 195 | + |
| 196 | + if (entry.id > firstIdx && regex.test(entry.msg)) |
| 197 | + { |
| 198 | + ts = ts && entry.ts; |
| 199 | + |
| 200 | + if (ts) |
| 201 | + { |
| 202 | + msg = ts.toTimeString().split(" ")[0]; |
| 203 | + ts = "" + ts.getMilliseconds(); |
| 204 | + while (ts.length < 3) |
| 205 | + ts = "0" + ts; |
| 206 | + |
| 207 | + msg = "[" + msg + "." + ts + "] "; |
| 208 | + } |
| 209 | + |
| 210 | + msg = entry.id + ". " + msg + entry.msg + "\n"; |
| 211 | + } |
| 212 | + |
| 213 | + return msg; |
| 214 | + } |
| 215 | + }); |
| 216 | + |
| 217 | +}, '1.0.0', { requires: [ 'comms-log4js' ] }); |
0 commit comments