mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 20:46:39 +02:00
Added logging lib with setting to control log level.
This commit is contained in:
parent
65ad3a5814
commit
9715eb09d2
13 changed files with 299 additions and 108 deletions
88
lib/logging.js
Normal file
88
lib/logging.js
Normal file
|
@ -0,0 +1,88 @@
|
|||
/* jslint moz: true */
|
||||
/* 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 if (window.scope){
|
||||
window.scope.logging = {};
|
||||
scope = window.scope.logging;
|
||||
}
|
||||
else {
|
||||
window.logging = {};
|
||||
scope = window.logging;
|
||||
}
|
||||
|
||||
var prefix = "";
|
||||
|
||||
function leftPad(str, char, pad){
|
||||
str = "" + str;
|
||||
return char.repeat(pad - str.length) + str;
|
||||
}
|
||||
|
||||
var queue = [];
|
||||
function performLog(level, args, date){
|
||||
if (!date){
|
||||
date = new Date();
|
||||
}
|
||||
if (settings.isStillDefault){
|
||||
queue.push({level, args, date});
|
||||
}
|
||||
else {
|
||||
if (settings.logLevel >= level){
|
||||
if (prefix){
|
||||
args.unshift(prefix + ":");
|
||||
}
|
||||
args.unshift(
|
||||
"[" +
|
||||
date.getFullYear() + "-" +
|
||||
leftPad(date.getMonth() + 1, "0", 2) + "-" +
|
||||
leftPad(date.getDate(), "0", 2) + " " +
|
||||
leftPad(date.getHours(), "0", 2) + ":" +
|
||||
leftPad(date.getMinutes(), "0", 2) + ":" +
|
||||
leftPad(date.getSeconds(), "0", 2) + "." +
|
||||
leftPad(date.getMilliseconds(), "0", 3) +
|
||||
"]"
|
||||
);
|
||||
console.log.apply(console, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function error (...args){performLog( 1, args);}
|
||||
function warning(...args){performLog( 25, args);}
|
||||
function message(...args){performLog( 50, args);}
|
||||
function notice (...args){performLog( 75, args);}
|
||||
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;
|
||||
}());
|
Loading…
Add table
Add a link
Reference in a new issue