|
-
- const debug = require('debug');
-
- var ENABLE_STATE_INFO = true;
- var ENABLE_STATE_DEBUG = true;
- var ENABLE_STATE_WARNING = true;
- var ENABLE_STATE_ERROR = true;
- 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){
- if (AddToLogList(this, namespace)){
- this.__d = debug(namespace);
- } else {
- this.__d = null;
- }
- //console.log(debug);
- this.__fnc = null;
- this.__ienabled = ENABLE_STATE_INFO;
- this.__denabled = ENABLE_STATE_DEBUG;
- this.__wenabled = ENABLE_STATE_WARNING;
- this.__eenabled = ENABLE_STATE_ERROR;
- this.__cenabled = ENABLE_STATE_CRITICAL;
- }
-
- get valid(){return (this.__d !== null);}
- get namespace(){return (this.__d) ? this.__d.namespace : "";}
-
- get enabled(){return (this.__d && this.__d.enabled);}
- set enabled(e){
- if (this.__d)
- this.__d.enabled = (e === true);
- }
-
- get infoEnabled(){return this.__ienabled;}
- set infoEnabled(e){this.__ienabled = (e === true);}
-
- get debugEnabled(){return this.__denabled;}
- set debugEnabled(e){this.__denabled = (e === true);}
-
- get warningEnabled(){return this.__wenabled;}
- set warningEnabled(e){this.__wenabled = (e === true);}
-
- get errorEnabled(){return this.__eenabled;}
- set errorEnabled(e){this.__eenabled = (e === true);}
-
- get criticalEnabled(){return this.__cenabled;}
- set criticalEnabled(e){this.__cenabled = (e === true);}
-
- set logFunc(f){
- this.__fnc = f;
- this.__d.log = f;
- }
-
- log(){
- 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;}
-
- switch(type){
- case "d":
- if (!ENABLE_STATE_DEBUG || !this.__denabled){return;}
- args[0] = "[ DEBUG ]: " + args[0];
- break;
- case "w":
- if (!ENABLE_STATE_WARNING || !this.__wenabled){return;}
- args[0] = "[ WARNING ]: " + args[0];
- break;
- case "e":
- if (!ENABLE_STATE_ERROR || !this.__eenabled){return;}
- args[0] = "[ ERROR ]: " + args[0];
- break;
- case "c":
- if (!ENABLE_STATE_CRITICAL || !this.__cenabled){return;}
- args[0] = "[ CRITICAL ]: " + args[0];
- break;
- case "i":
- default:
- if (!ENABLE_STATE_INFO || !this.__ienabled){return;}
- args[0] = "[ INFO ]: " + args[0];
- }
-
- this.__d.apply(this.__d, args);
- }
-
- info(){
- this.log.apply(this, ['i'].concat(Array.prototype.slice.call(arguments)));
- }
-
- debug(){
- this.log.apply(this, ['d'].concat(Array.prototype.slice.call(arguments)));
- }
-
- warning(){
- this.log.apply(this, ['w'].concat(Array.prototype.slice.call(arguments)));
- }
-
- error(){
- this.log.apply(this, ['e'].concat(Array.prototype.slice.call(arguments)));
- }
-
- critical(){
- this.log.apply(this, ['c'].concat(Array.prototype.slice.call(arguments)));
- }
-
- extend(ens){
- 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);
- }
- }
-
-
-
- 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(namespace){
- let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;});
- return (l) ? l.enabled : false;
- }
-
- disable(){
- LOG_LIST.forEach((l)=>{l.enabled=false;});
- }
-
- infoEnabled(){
- if (arguments.length > 0)
- ENABLE_STATE_INFO = (arguments[0] === true);
- return ENABLE_STATE_INFO;
- }
-
- debugEnabled(){
- if (arguments.length > 0)
- ENABLE_STATE_DEBUG = (arguments[0] === true);
- return ENABLE_STATE_DEBUG;
- }
-
- warningEnabled(){
- if (arguments.length > 0)
- ENABLE_STATE_WARNING = (arguments[0] === true);
- return ENABLE_STATE_WARNING;
- }
-
- errorEnabled(){
- if (arguments.length > 0)
- ENABLE_STATE_ERROR = (arguments[0] === true);
- return ENABLE_STATE_ERROR;
- }
-
- 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;
-
-
|