A small 'live' web site which uses rest-api to represent a pseudo-termial-like interface.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.0KB

  1. const debug = require('debug');
  2. var ENABLE_STATE_INFO = true;
  3. var ENABLE_STATE_DEBUG = true;
  4. var ENABLE_STATE_WARNING = true;
  5. var ENABLE_STATE_ERROR = true;
  6. var ENABLE_STATE_CRITICAL = true;
  7. var LOG_LIST = [];
  8. function AddToLogList(lg, namespace){
  9. idx = LOG_LIST.findIndex((i)=>{return i.namespace === namespace;});
  10. if (idx < 0){
  11. LOG_LIST.push(lg);
  12. return true;
  13. }
  14. return false;
  15. }
  16. class Log {
  17. constructor(namespace){
  18. if (AddToLogList(this, namespace)){
  19. this.__d = debug(namespace);
  20. } else {
  21. this.__d = null;
  22. }
  23. //console.log(debug);
  24. this.__fnc = null;
  25. this.__ienabled = ENABLE_STATE_INFO;
  26. this.__denabled = ENABLE_STATE_DEBUG;
  27. this.__wenabled = ENABLE_STATE_WARNING;
  28. this.__eenabled = ENABLE_STATE_ERROR;
  29. this.__cenabled = ENABLE_STATE_CRITICAL;
  30. }
  31. get valid(){return (this.__d !== null);}
  32. get namespace(){return (this.__d) ? this.__d.namespace : "";}
  33. get enabled(){return (this.__d && this.__d.enabled);}
  34. set enabled(e){
  35. if (this.__d)
  36. this.__d.enabled = (e === true);
  37. }
  38. get infoEnabled(){return this.__ienabled;}
  39. set infoEnabled(e){this.__ienabled = (e === true);}
  40. get debugEnabled(){return this.__denabled;}
  41. set debugEnabled(e){this.__denabled = (e === true);}
  42. get warningEnabled(){return this.__wenabled;}
  43. set warningEnabled(e){this.__wenabled = (e === true);}
  44. get errorEnabled(){return this.__eenabled;}
  45. set errorEnabled(e){this.__eenabled = (e === true);}
  46. get criticalEnabled(){return this.__cenabled;}
  47. set criticalEnabled(e){this.__cenabled = (e === true);}
  48. set logFunc(f){
  49. this.__fnc = f;
  50. this.__d.log = f;
  51. }
  52. log(){
  53. if (!this.enabled || arguments.length < 2){return;}
  54. var type = arguments[0];
  55. var args = Array.prototype.slice.call(arguments, 1);
  56. if (typeof(args[0]) !== 'string'){return;}
  57. switch(type){
  58. case "d":
  59. if (!ENABLE_STATE_DEBUG || !this.__denabled){return;}
  60. args[0] = "[ DEBUG ]: " + args[0];
  61. break;
  62. case "w":
  63. if (!ENABLE_STATE_WARNING || !this.__wenabled){return;}
  64. args[0] = "[ WARNING ]: " + args[0];
  65. break;
  66. case "e":
  67. if (!ENABLE_STATE_ERROR || !this.__eenabled){return;}
  68. args[0] = "[ ERROR ]: " + args[0];
  69. break;
  70. case "c":
  71. if (!ENABLE_STATE_CRITICAL || !this.__cenabled){return;}
  72. args[0] = "[ CRITICAL ]: " + args[0];
  73. break;
  74. case "i":
  75. default:
  76. if (!ENABLE_STATE_INFO || !this.__ienabled){return;}
  77. args[0] = "[ INFO ]: " + args[0];
  78. }
  79. this.__d.apply(this.__d, args);
  80. }
  81. info(){
  82. this.log.apply(this, ['i'].concat(Array.prototype.slice.call(arguments)));
  83. }
  84. debug(){
  85. this.log.apply(this, ['d'].concat(Array.prototype.slice.call(arguments)));
  86. }
  87. warning(){
  88. this.log.apply(this, ['w'].concat(Array.prototype.slice.call(arguments)));
  89. }
  90. error(){
  91. this.log.apply(this, ['e'].concat(Array.prototype.slice.call(arguments)));
  92. }
  93. critical(){
  94. this.log.apply(this, ['c'].concat(Array.prototype.slice.call(arguments)));
  95. }
  96. extend(ens){
  97. if (this.valid){
  98. var nlog = new LOG(this.__ns + ":" + ens);
  99. nlog.enabled = this.__d.enable;
  100. nlog.infoEnabled = this.__ienabled;
  101. nlog.debugEnabled = this.__denabled;
  102. nlog.warningEnabled = this.__wenabled;
  103. nlog.errorEnabled = this.__eenabled;
  104. nlog.criticalEnabled = this.__cenabled;
  105. if (this.__fnc !== null)
  106. nlog.logFunc = this.__fnc;
  107. return nlog;
  108. }
  109. return null;
  110. }
  111. destroy(){
  112. if (this.valid){
  113. this.__d.destroy();
  114. this.__d = null;
  115. }
  116. var idx = LOG_LIST.findIndex((i)=>{return i.namespace === this.namespace;});
  117. if (idx >= 0)
  118. LOG_LIST.splice(idx, 1);
  119. }
  120. }
  121. class LoggingSys{
  122. constructor(){}
  123. get length(){return LOG_LIST.length;}
  124. createLog(namespace){
  125. var log = LOG_LIST.find((i)=>{return i.namespace === namespace;});
  126. if (!log)
  127. log = new Log(namespace);
  128. return log;
  129. }
  130. enable(namespace){
  131. let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;});
  132. if (l)
  133. l.enabled = true;
  134. }
  135. enabled(namespace){
  136. let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;});
  137. return (l) ? l.enabled : false;
  138. }
  139. disable(){
  140. LOG_LIST.forEach((l)=>{l.enabled=false;});
  141. }
  142. infoEnabled(){
  143. if (arguments.length > 0)
  144. ENABLE_STATE_INFO = (arguments[0] === true);
  145. return ENABLE_STATE_INFO;
  146. }
  147. debugEnabled(){
  148. if (arguments.length > 0)
  149. ENABLE_STATE_DEBUG = (arguments[0] === true);
  150. return ENABLE_STATE_DEBUG;
  151. }
  152. warningEnabled(){
  153. if (arguments.length > 0)
  154. ENABLE_STATE_WARNING = (arguments[0] === true);
  155. return ENABLE_STATE_WARNING;
  156. }
  157. errorEnabled(){
  158. if (arguments.length > 0)
  159. ENABLE_STATE_ERROR = (arguments[0] === true);
  160. return ENABLE_STATE_ERROR;
  161. }
  162. criticalEnabled(){
  163. if (arguments.length > 0)
  164. ENABLE_STATE_CRITICAL = (arguments[0] === true);
  165. return ENABLE_STATE_CRITICAL;
  166. }
  167. clear(){
  168. LOG_LIST.forEach((e)=>{e.destroy();});
  169. LOG_LIST = [];
  170. }
  171. };
  172. var lsys = new LoggingSys();
  173. module.exports = lsys;