|
|
@@ -9,11 +9,22 @@ var ENABLE_STATE_CRITICAL = true; |
|
|
|
|
|
|
|
var LOG_LIST = []; |
|
|
|
|
|
|
|
function AddToLogList(lg, namespace){ |
|
|
|
idx = LOG_LIST.findIndex((i)=>{return i.namespace === namespace;}); |
|
|
|
if (idx < 0){ |
|
|
|
LOG_LIST.push(lg); |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
class Log { |
|
|
|
constructor(namespace){ |
|
|
|
this.__ns = namespace; |
|
|
|
this.__d = debug(namespace); |
|
|
|
if (AddToLogList(this, namespace)){ |
|
|
|
this.__d = debug(namespace); |
|
|
|
} else { |
|
|
|
this.__d = null; |
|
|
|
} |
|
|
|
//console.log(debug); |
|
|
|
this.__fnc = null; |
|
|
|
this.__ienabled = ENABLE_STATE_INFO; |
|
|
@@ -21,14 +32,15 @@ class Log { |
|
|
|
this.__wenabled = ENABLE_STATE_WARNING; |
|
|
|
this.__eenabled = ENABLE_STATE_ERROR; |
|
|
|
this.__cenabled = ENABLE_STATE_CRITICAL; |
|
|
|
LOG_LIST.push(this); |
|
|
|
} |
|
|
|
|
|
|
|
get namespace(){return this.__ns;} |
|
|
|
get valid(){return (this.__d !== null);} |
|
|
|
get namespace(){return (this.__d) ? this.__d.namespace : "";} |
|
|
|
|
|
|
|
get enabled(){return this.__d.enabled} |
|
|
|
get enabled(){return (this.__d && this.__d.enabled);} |
|
|
|
set enabled(e){ |
|
|
|
this.__d.enabled = (e === true); |
|
|
|
if (this.__d) |
|
|
|
this.__d.enabled = (e === true); |
|
|
|
} |
|
|
|
|
|
|
|
get infoEnabled(){return this.__ienabled;} |
|
|
@@ -52,7 +64,7 @@ class Log { |
|
|
|
} |
|
|
|
|
|
|
|
log(){ |
|
|
|
if (!this.__d.enabled || arguments.length < 2){return;} |
|
|
|
if (!this.enabled || arguments.length < 2){return;} |
|
|
|
var type = arguments[0]; |
|
|
|
var args = Array.prototype.slice.call(arguments, 1); |
|
|
|
if (typeof(args[0]) !== 'string'){return;} |
|
|
@@ -104,66 +116,102 @@ class Log { |
|
|
|
} |
|
|
|
|
|
|
|
extend(ens){ |
|
|
|
var nlog = new LOG(this.__ns + ":" + ens); |
|
|
|
nlog.enabled = this.__d.enable; |
|
|
|
nlog.infoEnabled = this.__ienabled; |
|
|
|
nlog.debugEnabled = this.__denabled; |
|
|
|
nlog.warningEnabled = this.__wenabled; |
|
|
|
nlog.errorEnabled = this.__eenabled; |
|
|
|
nlog.criticalEnabled = this.__cenabled; |
|
|
|
if (this.__fnc !== null) |
|
|
|
nlog.logFunc = this.__fnc; |
|
|
|
return nlog; |
|
|
|
if (this.valid){ |
|
|
|
var nlog = new LOG(this.__ns + ":" + ens); |
|
|
|
nlog.enabled = this.__d.enable; |
|
|
|
nlog.infoEnabled = this.__ienabled; |
|
|
|
nlog.debugEnabled = this.__denabled; |
|
|
|
nlog.warningEnabled = this.__wenabled; |
|
|
|
nlog.errorEnabled = this.__eenabled; |
|
|
|
nlog.criticalEnabled = this.__cenabled; |
|
|
|
if (this.__fnc !== null) |
|
|
|
nlog.logFunc = this.__fnc; |
|
|
|
return nlog; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
destroy(){ |
|
|
|
if (this.valid){ |
|
|
|
this.__d.destroy(); |
|
|
|
this.__d = null; |
|
|
|
} |
|
|
|
var idx = LOG_LIST.findIndex((i)=>{return i.namespace === this.namespace;}); |
|
|
|
if (idx >= 0) |
|
|
|
LOG_LIST.splice(idx, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
Log:Log, |
|
|
|
enable: function(namespace){ |
|
|
|
class LoggingSys{ |
|
|
|
constructor(){} |
|
|
|
|
|
|
|
get length(){return LOG_LIST.length;} |
|
|
|
|
|
|
|
createLog(namespace){ |
|
|
|
var log = LOG_LIST.find((i)=>{return i.namespace === namespace;}); |
|
|
|
if (!log) |
|
|
|
log = new Log(namespace); |
|
|
|
return log; |
|
|
|
} |
|
|
|
|
|
|
|
enable(namespace){ |
|
|
|
let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;}); |
|
|
|
if (l) |
|
|
|
l.enabled = true; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
enabled: function(namespace){ |
|
|
|
enabled(namespace){ |
|
|
|
let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;}); |
|
|
|
return (l) ? l.enabled : false; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
disable: function(){ |
|
|
|
disable(){ |
|
|
|
LOG_LIST.forEach((l)=>{l.enabled=false;}); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
infoEnabled: function(){ |
|
|
|
infoEnabled(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_INFO = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_INFO; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
debugEnabled: function(){ |
|
|
|
debugEnabled(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_DEBUG = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_DEBUG; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
warningEnabled: function(){ |
|
|
|
warningEnabled(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_WARNING = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_WARNING; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
errorEnabled: function(){ |
|
|
|
errorEnabled(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_ERROR = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_ERROR; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
criticalEnabled: function(){ |
|
|
|
criticalEnabled(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_CRITICAL = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_CRITICAL; |
|
|
|
} |
|
|
|
|
|
|
|
clear(){ |
|
|
|
LOG_LIST.forEach((e)=>{e.destroy();}); |
|
|
|
LOG_LIST = []; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var lsys = new LoggingSys(); |
|
|
|
module.exports = lsys; |
|
|
|
|
|
|
|
|
|
|
|
|