UNPKG

ajsfw

Version:
268 lines (267 loc) 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var LoggerStyleSheet_1 = require("./LoggerStyleSheet"); var LoggerToolbar_1 = require("./LoggerToolbar"); var LogBody_1 = require("./LogBody"); var LogType_1 = require("./LogType"); var Logger = (function () { function Logger(console, config) { this._initTime = (new Date()).getTime(); this._console = console; this._config = config; this._records = []; this._sameTypeCounter = {}; this._selectedItem = null; this._breakpoints = []; this._styleSheet = new LoggerStyleSheet_1.LoggerStyleSheet(this); this._toolBar = new LoggerToolbar_1.LoggerToolbar(this); this._body = new LogBody_1.LogBody(this); if (sessionStorage) { var bkpsJSON = sessionStorage.getItem("AJS_DEBUG_LOGGER_BREAKPOINTS"); if (bkpsJSON !== null) { this._breakpoints = JSON.parse(bkpsJSON); } } else { alert("Breakpoints not supported"); } console.registerModule(this); } Object.defineProperty(Logger.prototype, "initTime", { get: function () { return this._initTime; }, enumerable: true, configurable: true }); Object.defineProperty(Logger.prototype, "records", { get: function () { return this._records; }, enumerable: true, configurable: true }); Logger.prototype.setInfo = function (info) { this._console.setInfo(info); }; Logger.prototype.refresh = function () { this._console.hide(); this._console.show(); }; Logger.prototype.setBreakpoint = function () { if (this._selectedItem !== null && !this._selectedItem.breakpoint) { this._selectedItem.breakpoint = true; this._body.setBreakpoint(); this._breakpoints.push({ recordTypeId: this._selectedItem.sameTypeId, occurence: this._selectedItem.occurence }); sessionStorage.setItem("AJS_DEBUG_LOGGER_BREAKPOINTS", JSON.stringify(this._breakpoints)); } }; Logger.prototype.resetBreakpoint = function () { if (this._selectedItem !== null && this._selectedItem.breakpoint) { this._selectedItem.breakpoint = false; this._body.unsetBreakpoint(); for (var i = 0; i < this._breakpoints.length; i++) { if (this._breakpoints[i].recordTypeId === this._selectedItem.sameTypeId && this._breakpoints[i].occurence === this._selectedItem.occurence) { this._breakpoints.splice(i, 1); break; } } sessionStorage.setItem("AJS_DEBUG_LOGGER_BREAKPOINTS", JSON.stringify(this._breakpoints)); } }; Logger.prototype.clearBreakpoints = function () { if (sessionStorage) { this._breakpoints = []; sessionStorage.setItem("AJS_DEBUG_LOGGER_BREAKPOINTS", JSON.stringify(this._breakpoints)); this._body.clearBreakpoints(); } }; Logger.prototype.itemSelected = function (item) { this._selectedItem = item; if (sessionStorage) { this._toolBar.enableBreakpoints(); } }; Logger.prototype._checkBreakPoint = function (typeId, occurence) { for (var i = 0; i < this._breakpoints.length; i++) { if (this._breakpoints[i].recordTypeId === typeId && this._breakpoints[i].occurence === occurence) { return true; } } return false; }; Logger.prototype.getButtonLabel = function () { return "Log"; }; Logger.prototype.renderStyleSheet = function () { return this._styleSheet.render(); }; Logger.prototype.renderToolbar = function () { return this._toolBar.render(); }; Logger.prototype.renderBody = function () { this._bodyElement = this._body.render(); return this._body.render(); }; Logger.prototype.bodyRendered = function () { this._body.rendered(this._bodyElement.ownerDocument); }; Logger.prototype.log = function (type, level, sourceModule, object, message) { var data = []; for (var _i = 5; _i < arguments.length; _i++) { data[_i - 5] = arguments[_i]; } if (this._config.logTypes.indexOf(type) === -1 || level > this._config.maxLevel || !this._config.enabled) { return; } if (this._config.sourceModules.indexOf(sourceModule) === -1) { return; } var fnInfo = this.__getFunctionInfo(); var logRecord = { sameTypeId: "", time: new Date(), occurence: 0, type: type, level: level, module: sourceModule, object: object, function: fnInfo.name, caller: fnInfo.caller, message: message || "", data: data instanceof Array ? data.length : 0, breakpoint: false }; if (this._config.logDataToConsole) { var msg = message ? message : ""; if (data[0]) { window.console.log(this._records.length + ": " + LogType_1.LogType[type] + " " + msg + "[ " + logRecord.module + "." + logRecord.object + "." + logRecord.function + " ]", data[0]); } else { window.console.log(this._records.length + ": " + msg + "[ " + logRecord.module + "." + logRecord.object + "." + logRecord.function + " ]"); } } var sameTypeId = LogType_1.LogType[logRecord.type] + " " + level + " " + logRecord.module + " " + logRecord.object + " " + logRecord.function; sameTypeId = sameTypeId.replace(/\./g, "_"); sameTypeId = sameTypeId.replace(/ /g, "_"); sameTypeId = sameTypeId.replace(/\{/g, ""); sameTypeId = sameTypeId.replace(/\}/g, ""); sameTypeId = sameTypeId.replace(/\(/g, ""); sameTypeId = sameTypeId.replace(/\)/g, ""); sameTypeId = sameTypeId.replace(/\[/g, ""); sameTypeId = sameTypeId.replace(/\]/g, ""); sameTypeId = sameTypeId.replace(/\n/g, ""); logRecord.sameTypeId = sameTypeId; if (this._sameTypeCounter.hasOwnProperty(sameTypeId)) { this._sameTypeCounter[sameTypeId]++; logRecord.occurence = this._sameTypeCounter[sameTypeId]; } else { this._sameTypeCounter[sameTypeId] = 1; logRecord.occurence = 1; } logRecord.breakpoint = this._checkBreakPoint(sameTypeId, logRecord.occurence); this._records.push(logRecord); if (logRecord.breakpoint) { debugger; } }; Logger.prototype.__getFunctionInfo = function () { try { throw new Error("Error"); } catch (e) { if (e.stack) { var functions = e.stack.match(/(at ).*(\()/g); if (functions === null) { functions = e.stack.match(/.*@/g); if (functions && functions !== null) { for (var i = 0; i < functions.length; i++) { functions[i] = functions[i].substr(0, functions[i].length - 1); } return this.__getNameAndCaller(functions); } } return this.__getNameAndCaller(functions); } } return { name: "Unknown", caller: "Unknown" }; }; Logger.prototype.__getNameAndCaller = function (functions) { if (functions === undefined || functions === null) { return { name: "Unknown", caller: "Unknown" }; } this.__skipLogger(functions); this.__skipAsync(functions); if (functions.length === 0) { return { name: "Unknown", caller: "Unknown" }; } var fi = {}; if (functions[0].indexOf("at ") !== -1) { fi.name = functions[0].substring(3, functions[0].length - 2); } else { fi.name = functions[0].replace(/\//g, "").replace(/\</g, ""); } functions.shift(); this.__skipAsync(functions); if (functions.length > 0) { if (functions[0].indexOf("at ") !== -1) { fi.caller = functions[0].substring(3, functions[0].length - 2); } else { fi.caller = functions[0].replace(/\//g, "").replace(/\</g, ""); } } else { fi.caller = "unknown"; } return fi; }; Logger.prototype.__skipLogger = function (functions) { if (functions.length > 3) { functions.shift(); functions.shift(); functions.shift(); } }; Logger.prototype.__skipAsync = function (functions) { var tmp; if (functions.length > 0) { tmp = functions[0]; } while (functions.length > 0 && (functions[0].toLowerCase().indexOf("anonymous") !== -1 || this.__checkAsync(functions[0]))) { if (functions.length > 1 && functions[0].toLowerCase().indexOf("anonymous") !== -1 && this.__checkAsync(functions[1])) { functions.shift(); } else { if (functions.length > 0 && functions[0].toLowerCase().indexOf("anonymous") !== -1) { break; } } while (functions.length > 0 && this.__checkAsync(functions[0])) { functions.shift(); } } if (functions.length === 0 && tmp) { functions.push(tmp); } }; Logger.prototype.__checkAsync = function (stackRecord) { return stackRecord.indexOf("Generator.next") !== -1 || stackRecord.indexOf("__awaiter") !== -1 || stackRecord.indexOf("Promise") !== -1 || stackRecord.indexOf("fulfilled") !== -1 || stackRecord.indexOf("rejected") !== -1 || stackRecord.indexOf("step") !== -1; }; return Logger; }()); exports.Logger = Logger;