* Create a debugger with the given `namespace`. * * @param {String} namespace * @return {Function} * @api public
(namespace)
| 3089 | */ |
| 3090 | |
| 3091 | function debug(namespace) { |
| 3092 | |
| 3093 | // define the `disabled` version |
| 3094 | function disabled() {} |
| 3095 | disabled.enabled = false; |
| 3096 | |
| 3097 | // define the `enabled` version |
| 3098 | function enabled() { |
| 3099 | |
| 3100 | var self = enabled; |
| 3101 | |
| 3102 | // set `diff` timestamp |
| 3103 | var curr = +new Date(); |
| 3104 | var ms = curr - (prevTime || curr); |
| 3105 | self.diff = ms; |
| 3106 | self.prev = prevTime; |
| 3107 | self.curr = curr; |
| 3108 | prevTime = curr; |
| 3109 | |
| 3110 | // add the `color` if not set |
| 3111 | if (null == self.useColors) |
| 3112 | self.useColors = exports.useColors(); |
| 3113 | if (null == self.color && self.useColors) |
| 3114 | self.color = selectColor(); |
| 3115 | |
| 3116 | var args = Array.prototype.slice.call(arguments); |
| 3117 | |
| 3118 | args[0] = exports.coerce(args[0]); |
| 3119 | |
| 3120 | if ('string' !== typeof args[0]) { |
| 3121 | // anything else let's inspect with %o |
| 3122 | args = ['%o'].concat(args); |
| 3123 | } |
| 3124 | |
| 3125 | // apply any `formatters` transformations |
| 3126 | var index = 0; |
| 3127 | args[0] = args[0].replace(/%([a-z%])/g, function(match, format) { |
| 3128 | // if we encounter an escaped % then don't increase the array index |
| 3129 | if (match === '%%') |
| 3130 | return match; |
| 3131 | index++; |
| 3132 | var formatter = exports.formatters[format]; |
| 3133 | if ('function' === typeof formatter) { |
| 3134 | var val = args[index]; |
| 3135 | match = formatter.call(self, val); |
| 3136 | |
| 3137 | // now we need to remove `args[index]` since it's inlined in the `format` |
| 3138 | args.splice(index, 1); |
| 3139 | index--; |
| 3140 | } |
| 3141 | return match; |
| 3142 | }); |
| 3143 | |
| 3144 | if ('function' === typeof exports.formatArgs) { |
| 3145 | args = exports.formatArgs.apply(self, args); |
| 3146 | } |
| 3147 | var logFn = enabled.log || exports.log || console.log.bind(console); |
| 3148 | logFn.apply(self, args); |
no outgoing calls
no test coverage detected