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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | 1 2 2 2 2 1 1 1 1 1 1 1 2 2 2 2 24 2 24 2 2 24 20 2 8 8 8 8 2 5 5 2 3 3 2 1 1 1 1 1 1 1 1 12 5 7 4 3 3 1 12 1 1 1 1 1 1 1 8 8 1 1 1 1 1 1 1 23 1 1 2 2 12 12 12 24 2 5 2 4 2 2 2 3 1 1 1 1 1 1 1 2 2 2 6 6 2 5 2 2 2 5 2 3 2 1 11 1 1 1 1 1 1 | /// <reference path='../../../third_party/freedom-typings/console.d.ts' /> /// <reference path='../../../third_party/freedom-typings/freedom-common.d.ts' /> /// <reference path='../../../third_party/freedom-typings/freedom-module-env.d.ts' /> var __extends = this.__extends || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.prototype; d.prototype = new __(); }; var logging = require('./loggingprovider.types'); // The freedom console provider. exports.freedomConsole = freedom['core.console'](); // Besides output to console, log can also be buffered for later retrieval // through "getLogs". This is the maximum number of buffered log before it is // trimmed. Assuming average log length is 80, the whole buffer size is about // 80k. That should be easy to send through email, not much memory usage, and // still enough to capture most issues. exports.MAX_BUFFERED_LOG = 1000; // Logs waiting for the logger to exist. exports.logBuffer = []; exports.enabled = true; // This represents a possible destination for log messages. To make use of // this, the class should be inherited from and the log_ method reimplemented // to record the message in whichever way is best for that transport. var AbstractLoggingDestination = (function () { function AbstractLoggingDestination(default_) { var _this = this; this.default_ = default_; // These filters control what is displayed/saved for the different log types. // Entries for each type should be of the form: // 'tag': LEVEL // where LEVEL is the minimum level of log that will be processed for the // module 'tag'. '*' is a wildcard tag that applies to any message that is not // specifically specified this.filters_ = {}; // This retrieves the minimum level that will cause some action on the part // of the logger for a given tag this.getLevelForTag = function (tag) { return (tag in _this.filters_) ? _this.filters_[tag] : _this.default_; }; this.checkFilter_ = function (level, tag) { return level >= _this.getLevelForTag(tag); }; this.log_ = function (level, tag, message) { throw Error('not implemented'); }; this.log = function (level, tag, message) { if (_this.checkFilter_(level, tag)) { _this.log_(level, tag, message); } }; // This method handles sending updates for the tags that have changed (and // only the tags that have changed) after calling the specified function // to do the change. this.doFilterChanges_ = function (doChange) { var oldLevels = {}; for (var tag in listeners) { oldLevels[tag] = getMinLevel(tag); } doChange(); for (var tag in oldLevels) { if (oldLevels[tag] !== getMinLevel(tag)) { updateTag(tag); } } }; this.setDefaultFilter = function (level) { _this.doFilterChanges_(function () { _this.default_ = level; }); }; this.setFilters = function (filters) { // while it would be possible to limit the scope of what tags should be // checked for changes, it's easier to just check all of them _this.doFilterChanges_(function () { _this.filters_ = filters; }); }; this.setFilter = function (tag, level) { var oldLevel = getMinLevel(tag); if (typeof (level) === 'undefined' || level === null) { delete _this.filters_[tag]; } else { _this.filters_[tag] = level; } if (getMinLevel(tag) !== oldLevel) { updateTag(tag); } }; } return AbstractLoggingDestination; })(); exports.AbstractLoggingDestination = AbstractLoggingDestination; // A logging destination for printing the message directly to the console var ConsoleLoggingDestination = (function (_super) { __extends(ConsoleLoggingDestination, _super); function ConsoleLoggingDestination() { var _this = this; _super.call(this, 0 /* debug */); this.log_ = function (level, tag, message) { if (level === 0 /* debug */) { exports.freedomConsole.debug(tag, _this.formatMessage_(message)); } else if (level === 1 /* info */) { exports.freedomConsole.info(tag, _this.formatMessage_(message)); } else Iif (level === 2 /* warn */) { exports.freedomConsole.warn(tag, _this.formatMessage_(message)); } else { exports.freedomConsole.error(tag, _this.formatMessage_(message)); } }; // Exports the date and message fields, yielding something like: // [Apr 23 15:07:12.586] listening on port 9999 // Since the Chrome and Firefox consoles provide some metadata support, // this ultimately results in something like this in the JavaScript // console: // (i) simple-socks [Apr 23 15:07:12.586] listening on port 9999 // (where (i) is a cute little symbol indicating the level and the // tag, simple-socks is in red. this.formatMessage_ = function (l) { return '[' + dateToString_(l.timestamp) + '] ' + l.message; }; } return ConsoleLoggingDestination; })(AbstractLoggingDestination); exports.ConsoleLoggingDestination = ConsoleLoggingDestination; var BufferedLoggingDestination = (function (_super) { __extends(BufferedLoggingDestination, _super); function BufferedLoggingDestination() { _super.call(this, 3 /* error */); this.log_ = function (level, tag, message) { Iif (exports.logBuffer.length > exports.MAX_BUFFERED_LOG) { exports.logBuffer.splice(0, exports.MAX_BUFFERED_LOG / 10); } exports.logBuffer.push(message); }; } return BufferedLoggingDestination; })(AbstractLoggingDestination); exports.BufferedLoggingDestination = BufferedLoggingDestination; var loggingDestinations = {}; loggingDestinations[0 /* console */] = new ConsoleLoggingDestination(); loggingDestinations[1 /* buffered */] = new BufferedLoggingDestination(); var MONTH_NAMES = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; // Generates current timestamp in form "M d H:m:s.S" function dateToString_(d) { return MONTH_NAMES[d.getMonth()] + ' ' + d.getDate() + ' ' + (d.getHours() < 10 ? '0' : '') + d.getHours() + ':' + (d.getMinutes() < 10 ? '0' : '') + d.getMinutes() + ':' + (d.getSeconds() < 10 ? '0' : '') + d.getSeconds() + '.' + d.getMilliseconds(); } // Interface for accumulating log messages. var Log = (function () { function Log() { var _this = this; this.log_ = function (level, tag, msg) { Iif (!exports.enabled) { return; } var message = { timestamp: new Date(), level: level, tag: tag, message: msg }; for (var i in loggingDestinations) { loggingDestinations[i].log(level, tag, message); } }; // Logs message in debug level. this.debug = function (source, msg) { _this.log_(0 /* debug */, source, msg); }; // Logs message in info level. this.info = function (source, msg) { _this.log_(1 /* info */, source, msg); }; this.log = function (source, msg) { _this.log_(1 /* info */, source, msg); }; // Logs message in warn level. this.warn = function (source, msg) { _this.log_(2 /* warn */, source, msg); }; // Logs message in error level. this.error = function (source, msg) { _this.log_(3 /* error */, source, msg); }; } return Log; })(); exports.Log = Log; var listeners = {}; function getMinLevel(tag) { var min = 3 /* error */; for (var i in loggingDestinations) { var level = loggingDestinations[i].getLevelForTag(tag); if (level < min) { min = level; } } return min; } function updateTag(tag) { if (!listeners[tag]) { return; } for (var i in listeners[tag]) { listeners[tag][i].update(); } } // Interface for managing & retreiving log messages. // Note: this is really a fake class: all data is in fact global. // TODO: rename this to LoggingManager or something sensible. var LoggingController = (function () { function LoggingController() { var _this = this; // Gets log as a encrypted blob, which can be transported in insecure // channel. this.getEncrypedLogBuffer = function (tags) { // TODO: to be implemented. return new ArrayBuffer(0); }; // Exports log in plaintext. // If specified, tags limits the exported messages to those having any of // the specified tags. this.getLogs = function (tags) { // TODO: use input to select log message. Eif (!tags || tags.length === 0) { return exports.logBuffer.map(_this.formatMessage_); } else { return exports.logBuffer.filter(function (m) { return tags.indexOf(m.tag) >= 0; }).map(_this.formatMessage_); } }; // Clears all the logs stored in buffer. this.clearLogs = function () { exports.logBuffer = []; }; // Enables/Disables log facility. this.enable = function () { exports.enabled = true; }; // Enables/Disables log facility. this.disable = function () { exports.enabled = false; }; this.setDefaultFilter = function (destination, level) { loggingDestinations[destination].setDefaultFilter(level); }; this.setFilters = function (destination, filters) { loggingDestinations[destination].setFilters(filters); }; this.setFilter = function (destination, tag, level) { loggingDestinations[destination].setFilter(tag, level); }; } // Exports all message fields, yielding something like: // simple-socks I [Apr 23 15:07:12.586] listening on port 9999 LoggingController.prototype.formatMessage_ = function (l) { return l.tag + ' ' + logging.Level[l.level][0].toUpperCase() + ' [' + dateToString_(l.timestamp) + '] ' + l.message; }; return LoggingController; })(); exports.LoggingController = LoggingController; // TODO - handle unbinding the listener if there is a disconnect event var LoggingListener = (function () { function LoggingListener(dispatchEvent_, tag_) { var _this = this; this.dispatchEvent_ = dispatchEvent_; this.tag_ = tag_; this.update = function () { _this.dispatchEvent_('levelchange', getMinLevel(_this.tag_)); }; if (!listeners[tag_]) { listeners[tag_] = []; } listeners[tag_].push(this); this.update(); } return LoggingListener; })(); exports.LoggingListener = LoggingListener; |