2017-10-03 15:35:31 +02:00
|
|
|
/* eslint no-console: off */
|
2017-07-27 19:14:04 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2019-04-07 23:22:56 +02:00
|
|
|
scope = require.register("./logging", {});
|
2017-07-27 19:14:04 +02:00
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
|
2018-09-15 21:13:25 +02:00
|
|
|
let settings = false;
|
2019-04-07 23:22:56 +02:00
|
|
|
scope.setSettings = function(realSettings){
|
|
|
|
if (!settings){
|
|
|
|
settings = realSettings;
|
|
|
|
settings.loaded.then(scope.clearQueue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
warning("logging: Settings can only be set once.");
|
|
|
|
}
|
|
|
|
};
|
2017-07-27 19:14:04 +02:00
|
|
|
|
|
|
|
var prefix = "";
|
|
|
|
|
|
|
|
function leftPad(str, char, pad){
|
|
|
|
str = "" + str;
|
|
|
|
return char.repeat(pad - str.length) + str;
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:47:24 +01:00
|
|
|
var colors = {
|
|
|
|
1: "color: red",
|
|
|
|
25: "color: orange",
|
|
|
|
50: "",
|
|
|
|
75: "color: darkgreen",
|
|
|
|
100: "color: blue",
|
|
|
|
999: "background-color: lightgray"
|
|
|
|
};
|
|
|
|
|
2017-07-27 19:14:04 +02:00
|
|
|
var queue = [];
|
|
|
|
function performLog(level, args, date){
|
|
|
|
if (!date){
|
|
|
|
date = new Date();
|
|
|
|
}
|
2018-09-15 21:13:25 +02:00
|
|
|
if (!settings || !settings.isInitialized() || queue.length){
|
2017-07-27 19:14:04 +02:00
|
|
|
queue.push({level, args, date});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (settings.logLevel >= level){
|
2017-12-19 22:59:30 +01:00
|
|
|
var pre = "%c[CanvasBlocker] ";
|
2017-07-27 19:14:04 +02:00
|
|
|
if (prefix){
|
2017-12-18 23:47:24 +01:00
|
|
|
pre += prefix + ": ";
|
2017-07-27 19:14:04 +02:00
|
|
|
}
|
2017-09-29 00:29:34 +02:00
|
|
|
pre += "[" +
|
2017-07-27 19:14:04 +02:00
|
|
|
date.getFullYear() + "-" +
|
|
|
|
leftPad(date.getMonth() + 1, "0", 2) + "-" +
|
|
|
|
leftPad(date.getDate(), "0", 2) + " " +
|
|
|
|
leftPad(date.getHours(), "0", 2) + ":" +
|
|
|
|
leftPad(date.getMinutes(), "0", 2) + ":" +
|
2017-10-03 15:35:31 +02:00
|
|
|
leftPad(date.getSeconds(), "0", 2) + "." +
|
2017-07-27 19:14:04 +02:00
|
|
|
leftPad(date.getMilliseconds(), "0", 3) +
|
2017-10-03 15:35:31 +02:00
|
|
|
"]";
|
2017-09-29 00:29:34 +02:00
|
|
|
if (typeof args[0] === "string"){
|
|
|
|
args[0] = pre + " " + args[0];
|
2017-12-18 23:57:25 +01:00
|
|
|
args.splice(1, 0, colors[level] || "");
|
2017-09-29 00:29:34 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-12-18 23:57:25 +01:00
|
|
|
args.unshift(colors[level] || "");
|
2017-09-29 00:29:34 +02:00
|
|
|
args.unshift(pre);
|
|
|
|
}
|
2017-07-27 19:14:04 +02:00
|
|
|
console.log.apply(console, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 19:00:00 +02:00
|
|
|
function error (...args){performLog(1, args);}
|
|
|
|
function warning(...args){performLog(25, args);}
|
|
|
|
function message(...args){performLog(50, args);}
|
|
|
|
function notice (...args){performLog(75, args);}
|
2017-07-27 19:14:04 +02:00
|
|
|
function verbose(...args){performLog(100, args);}
|
|
|
|
function metaLog(...args){performLog(999, args);}
|
|
|
|
|
|
|
|
scope.setPrefix = function(newPrefix){
|
|
|
|
if (!prefix){
|
|
|
|
prefix = newPrefix;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
warning("logging prefix already set (%s) cannot be set to %s", prefix, newPrefix);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
scope.clearQueue = function(){
|
|
|
|
if (queue.length){
|
|
|
|
metaLog("clear logging queue");
|
|
|
|
var tmp = queue;
|
|
|
|
queue = [];
|
|
|
|
tmp.forEach(function(item){
|
|
|
|
performLog(item.level, item.args, item.date);
|
|
|
|
});
|
|
|
|
metaLog("logging queue cleared");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
scope.error = error;
|
|
|
|
scope.warning = warning;
|
|
|
|
scope.message = message;
|
|
|
|
scope.notice = notice;
|
|
|
|
scope.verbose = verbose;
|
2017-11-08 17:46:41 +01:00
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
metaLog("logging available");
|
2017-07-27 19:14:04 +02:00
|
|
|
}());
|