arnaucube
arnaucube/commonroutesAppPublic Self-hosted repository
commonroutesApp/www/lib/angular-simple-logger/dist/index.js
www/lib/angular-simple-logger/dist/index.jsRaw
  1/**
  2 *  angular-simple-logger
  3 *
  4 * @version: 0.1.7
  5 * @author: Nicholas McCready
  6 * @date: Tue Jan 26 2016 10:15:01 GMT-0500 (EST)
  7 * @license: MIT
  8 */
  9var angular = require('angular');
 10
 11angular.module('nemLogging', []);
 12
 13angular.module('nemLogging').provider('nemDebug', function (){
 14  var ourDebug = null;
 15  ourDebug = require('debug');
 16
 17  this.$get =  function(){
 18    //avail as service
 19    return ourDebug;
 20  };
 21
 22  //avail at provider, config time
 23  this.debug = ourDebug;
 24
 25  return this;
 26});
 27var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
 28  slice = [].slice;
 29
 30angular.module('nemLogging').provider('nemSimpleLogger', [
 31  'nemDebugProvider', function(nemDebugProvider) {
 32    var LEVELS, Logger, _debugCache, _fns, _isValidLogObject, _maybeExecLevel, _wrapDebug, i, key, len, nemDebug, val;
 33    nemDebug = nemDebugProvider.debug;
 34    _debugCache = {};
 35    _fns = ['debug', 'info', 'warn', 'error', 'log'];
 36    LEVELS = {};
 37    for (key = i = 0, len = _fns.length; i < len; key = ++i) {
 38      val = _fns[key];
 39      LEVELS[val] = key;
 40    }
 41    _maybeExecLevel = function(level, current, fn) {
 42      if (level >= current) {
 43        return fn();
 44      }
 45    };
 46    _isValidLogObject = function(logObject) {
 47      var isValid, j, len1;
 48      isValid = false;
 49      if (!logObject) {
 50        return isValid;
 51      }
 52      for (j = 0, len1 = _fns.length; j < len1; j++) {
 53        val = _fns[j];
 54        isValid = (logObject[val] != null) && typeof logObject[val] === 'function';
 55        if (!isValid) {
 56          break;
 57        }
 58      }
 59      return isValid;
 60    };
 61
 62    /*
 63      Overide logeObject.debug with a nemDebug instance
 64      see: https://github.com/visionmedia/debug/blob/master/Readme.md
 65     */
 66    _wrapDebug = function(namespace, logObject) {
 67      var debugInstance, j, len1, newLogger;
 68      if (_debugCache[namespace] == null) {
 69        _debugCache[namespace] = nemDebug(namespace);
 70      }
 71      debugInstance = _debugCache[namespace];
 72      newLogger = {};
 73      for (j = 0, len1 = _fns.length; j < len1; j++) {
 74        val = _fns[j];
 75        newLogger[val] = val === 'debug' ? debugInstance : logObject[val];
 76      }
 77      return newLogger;
 78    };
 79    Logger = (function() {
 80      function Logger($log1) {
 81        var fn1, j, len1, level, logFns;
 82        this.$log = $log1;
 83        this.spawn = bind(this.spawn, this);
 84        if (!this.$log) {
 85          throw 'internalLogger undefined';
 86        }
 87        if (!_isValidLogObject(this.$log)) {
 88          throw '@$log is invalid';
 89        }
 90        this.doLog = true;
 91        logFns = {};
 92        fn1 = (function(_this) {
 93          return function(level) {
 94            logFns[level] = function() {
 95              var args;
 96              args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
 97              if (_this.doLog) {
 98                return _maybeExecLevel(LEVELS[level], _this.currentLevel, function() {
 99                  var ref;
100                  return (ref = _this.$log)[level].apply(ref, args);
101                });
102              }
103            };
104            return _this[level] = logFns[level];
105          };
106        })(this);
107        for (j = 0, len1 = _fns.length; j < len1; j++) {
108          level = _fns[j];
109          fn1(level);
110        }
111        this.LEVELS = LEVELS;
112        this.currentLevel = LEVELS.error;
113      }
114
115      Logger.prototype.spawn = function(newInternalLogger) {
116        if (typeof newInternalLogger === 'string') {
117          if (!_isValidLogObject(this.$log)) {
118            throw '@$log is invalid';
119          }
120          if (!nemDebug) {
121            throw 'nemDebug is undefined this is probably the light version of this library sep debug logggers is not supported!';
122          }
123          return _wrapDebug(newInternalLogger, this.$log);
124        }
125        return new Logger(newInternalLogger || this.$log);
126      };
127
128      return Logger;
129
130    })();
131    this.decorator = [
132      '$log', function($delegate) {
133        var log;
134        log = new Logger($delegate);
135        log.currentLevel = LEVELS.debug;
136        return log;
137      }
138    ];
139    this.$get = [
140      '$log', function($log) {
141        return new Logger($log);
142      }
143    ];
144    return this;
145  }
146]);