lisn.js
Version:
Simply handle user gestures and actions. Includes widgets.
149 lines (145 loc) • 6.66 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* @module Debugging
*
* @categoryDescription Logging
* {@link Debugging.LocalConsole | LocalConsole} logs to the local browser
* console. On iOS devices it uses `console.info` for all levels because of a
* bug in WebKit whereby other log levels don't show in some remote debuggers.
* Also, iOS console only supports a single argument, so it joins the given
* arguments as a single string.
*
* {@link Debugging.RemoteConsole | RemoteConsole} connects to a remote
* {@link https://socket.io/ | socket.io} server and logs messages to it.
*
* {@link Console} holds a {@link LocalConsole} and optionally a
* {@link RemoteConsole} and logs to both.
*
* {@link Logger} holds a {@link Console} and implements debug at 10 different
* levels. The maximum logged level is configurable. Also emits a prefix in
* debug messages that identifies the instance.
*/
import * as MH from "../globals/minification-helpers.js";
import { settings } from "../globals/settings.js";
import { randId } from "../utils/text.js";
import { Console } from "./console.js";
/**
* Holds a {@link Console} and implements debug at 10 different levels. The
* maximum logged level is configurable. Also emits a prefix in debug messages
* that identifies the instance.
*
* @category Logging
*/
export class Logger {
constructor(config) {
var _myConfig$name, _myConfig$verbosityLe;
_defineProperty(this, "debug", void 0);
_defineProperty(this, "log", void 0);
_defineProperty(this, "info", void 0);
_defineProperty(this, "warn", void 0);
_defineProperty(this, "error", void 0);
_defineProperty(this, "debug1", void 0);
_defineProperty(this, "debug2", void 0);
_defineProperty(this, "debug3", void 0);
_defineProperty(this, "debug4", void 0);
_defineProperty(this, "debug5", void 0);
_defineProperty(this, "debug6", void 0);
_defineProperty(this, "debug7", void 0);
_defineProperty(this, "debug8", void 0);
_defineProperty(this, "debug9", void 0);
_defineProperty(this, "debug10", void 0);
_defineProperty(this, "getName", void 0);
_defineProperty(this, "getVerbosityLevel", void 0);
_defineProperty(this, "setVerbosityLevel", void 0);
config !== null && config !== void 0 ? config : config = {};
const myConfig = MH.merge({
// set defaults
verbosityLevel: settings.verbosityLevel,
remoteLoggerURL: settings.remoteLoggerURL,
remoteLoggerOnMobileOnly: settings.remoteLoggerOnMobileOnly,
debugID: randId()
}, config);
let remoteLoggerURL = "";
if (!getBooleanURLParam("disableRemoteLog") && (myConfig.remoteLoggerOnMobileOnly === false || isMobile())) {
var _myConfig$remoteLogge;
remoteLoggerURL = (_myConfig$remoteLogge = myConfig.remoteLoggerURL) !== null && _myConfig$remoteLogge !== void 0 ? _myConfig$remoteLogge : "";
}
const name = (_myConfig$name = myConfig.name) !== null && _myConfig$name !== void 0 ? _myConfig$name : "";
const myConsole = new Console(remoteLoggerURL, myConfig.remoteLoggerConnectTimeout);
// use setters bellow to validate value
let verbosityLevel = 0;
const logPrefix = `[LISN${name ? ": " + name : ""}]`;
const debugID = myConfig.debugID;
const debugPrefix = `[LISN${(name ? ": " + name : "") + (debugID ? "-" + debugID : "")}]`;
this.getName = () => name;
this.getVerbosityLevel = () => verbosityLevel;
this.setVerbosityLevel = l => {
verbosityLevel = l;
};
this.setVerbosityLevel((_myConfig$verbosityLe = myConfig.verbosityLevel) !== null && _myConfig$verbosityLe !== void 0 ? _myConfig$verbosityLe : 0);
this.debug1 = (...args) => logDebugN(this, 1, debugPrefix, ...args);
this.debug2 = (...args) => logDebugN(this, 2, debugPrefix, ...args);
this.debug3 = (...args) => logDebugN(this, 3, debugPrefix, ...args);
this.debug4 = (...args) => logDebugN(this, 4, debugPrefix, ...args);
this.debug5 = (...args) => logDebugN(this, 5, debugPrefix, ...args);
this.debug6 = (...args) => logDebugN(this, 6, debugPrefix, ...args);
this.debug7 = (...args) => logDebugN(this, 7, debugPrefix, ...args);
this.debug8 = (...args) => logDebugN(this, 8, debugPrefix, ...args);
this.debug9 = (...args) => logDebugN(this, 9, debugPrefix, ...args);
this.debug10 = (...args) => logDebugN(this, 10, debugPrefix, ...args);
this.debug = (...args) => myConsole.debug(debugPrefix, ...args);
this.log = (...args) => myConsole.log(logPrefix, ...args);
this.info = (...args) => myConsole.info(logPrefix, ...args);
this.warn = (...args) => myConsole.warn(logPrefix, ...args);
this.error = (...args) => {
myConsole.error(logPrefix, ...args);
};
// --------------------
if ("logAtCreation" in myConfig) {
this.debug6("New logger:", myConfig.logAtCreation);
}
}
}
// ----------------------------------------
const logDebugN = (logger, level, ...args) => {
if (!MH.isNumber(level)) {
args.unshift(level);
level = 1;
logger.error(MH.bugError("Missing logger.debug level"));
}
if (logger.getVerbosityLevel() < level) {
return;
}
logger.debug(`[DEBUG ${level}]`, ...args);
};
const isMobile = () => {
const regex = /Mobi|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
return regex.test(MH.userAgent);
};
const getBooleanURLParam = name => {
const value = getURLParameter(name);
return value && (value === "1" || MH.toLowerCase(value) === "true");
};
const getURLParameter = name => {
if (!MH.hasDOM()) {
return null;
}
const loc = MH.getDoc().location;
if (typeof URLSearchParams !== "undefined") {
const urlParams = new URLSearchParams(loc.search);
return urlParams.get(name);
}
name = MH.strReplace(name, /[[\]]/g, "\\$&");
const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
const match = loc.href.match(regex);
if (!match) {
return null;
}
if (!match[2]) {
return "";
}
return decodeURIComponent(MH.strReplace(match[2], /\+/g, " "));
};
//# sourceMappingURL=logger.js.map