common.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. "use strict";
  2. /**
  3. * This is the common logic for both the Node.js and web browser
  4. * implementations of `debug()`.
  5. */
  6. function setup(env) {
  7. createDebug.debug = createDebug;
  8. createDebug.default = createDebug;
  9. createDebug.coerce = coerce;
  10. createDebug.disable = disable;
  11. createDebug.enable = enable;
  12. createDebug.enabled = enabled;
  13. createDebug.humanize = require('ms');
  14. Object.keys(env).forEach(function (key) {
  15. createDebug[key] = env[key];
  16. });
  17. /**
  18. * Active `debug` instances.
  19. */
  20. createDebug.instances = [];
  21. /**
  22. * The currently active debug mode names, and names to skip.
  23. */
  24. createDebug.names = [];
  25. createDebug.skips = [];
  26. /**
  27. * Map of special "%n" handling functions, for the debug "format" argument.
  28. *
  29. * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
  30. */
  31. createDebug.formatters = {};
  32. /**
  33. * Selects a color for a debug namespace
  34. * @param {String} namespace The namespace string for the for the debug instance to be colored
  35. * @return {Number|String} An ANSI color code for the given namespace
  36. * @api private
  37. */
  38. function selectColor(namespace) {
  39. var hash = 0;
  40. for (var i = 0; i < namespace.length; i++) {
  41. hash = (hash << 5) - hash + namespace.charCodeAt(i);
  42. hash |= 0; // Convert to 32bit integer
  43. }
  44. return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
  45. }
  46. createDebug.selectColor = selectColor;
  47. /**
  48. * Create a debugger with the given `namespace`.
  49. *
  50. * @param {String} namespace
  51. * @return {Function}
  52. * @api public
  53. */
  54. function createDebug(namespace) {
  55. var prevTime;
  56. function debug() {
  57. // Disabled?
  58. if (!debug.enabled) {
  59. return;
  60. }
  61. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  62. args[_key] = arguments[_key];
  63. }
  64. var self = debug; // Set `diff` timestamp
  65. var curr = Number(new Date());
  66. var ms = curr - (prevTime || curr);
  67. self.diff = ms;
  68. self.prev = prevTime;
  69. self.curr = curr;
  70. prevTime = curr;
  71. args[0] = createDebug.coerce(args[0]);
  72. if (typeof args[0] !== 'string') {
  73. // Anything else let's inspect with %O
  74. args.unshift('%O');
  75. } // Apply any `formatters` transformations
  76. var index = 0;
  77. args[0] = args[0].replace(/%([a-zA-Z%])/g, function (match, format) {
  78. // If we encounter an escaped % then don't increase the array index
  79. if (match === '%%') {
  80. return match;
  81. }
  82. index++;
  83. var formatter = createDebug.formatters[format];
  84. if (typeof formatter === 'function') {
  85. var val = args[index];
  86. match = formatter.call(self, val); // Now we need to remove `args[index]` since it's inlined in the `format`
  87. args.splice(index, 1);
  88. index--;
  89. }
  90. return match;
  91. }); // Apply env-specific formatting (colors, etc.)
  92. createDebug.formatArgs.call(self, args);
  93. var logFn = self.log || createDebug.log;
  94. logFn.apply(self, args);
  95. }
  96. debug.namespace = namespace;
  97. debug.enabled = createDebug.enabled(namespace);
  98. debug.useColors = createDebug.useColors();
  99. debug.color = selectColor(namespace);
  100. debug.destroy = destroy;
  101. debug.extend = extend; // Debug.formatArgs = formatArgs;
  102. // debug.rawLog = rawLog;
  103. // env-specific initialization logic for debug instances
  104. if (typeof createDebug.init === 'function') {
  105. createDebug.init(debug);
  106. }
  107. createDebug.instances.push(debug);
  108. return debug;
  109. }
  110. function destroy() {
  111. var index = createDebug.instances.indexOf(this);
  112. if (index !== -1) {
  113. createDebug.instances.splice(index, 1);
  114. return true;
  115. }
  116. return false;
  117. }
  118. function extend(namespace, delimiter) {
  119. return createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
  120. }
  121. /**
  122. * Enables a debug mode by namespaces. This can include modes
  123. * separated by a colon and wildcards.
  124. *
  125. * @param {String} namespaces
  126. * @api public
  127. */
  128. function enable(namespaces) {
  129. createDebug.save(namespaces);
  130. createDebug.names = [];
  131. createDebug.skips = [];
  132. var i;
  133. var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  134. var len = split.length;
  135. for (i = 0; i < len; i++) {
  136. if (!split[i]) {
  137. // ignore empty strings
  138. continue;
  139. }
  140. namespaces = split[i].replace(/\*/g, '.*?');
  141. if (namespaces[0] === '-') {
  142. createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
  143. } else {
  144. createDebug.names.push(new RegExp('^' + namespaces + '$'));
  145. }
  146. }
  147. for (i = 0; i < createDebug.instances.length; i++) {
  148. var instance = createDebug.instances[i];
  149. instance.enabled = createDebug.enabled(instance.namespace);
  150. }
  151. }
  152. /**
  153. * Disable debug output.
  154. *
  155. * @api public
  156. */
  157. function disable() {
  158. createDebug.enable('');
  159. }
  160. /**
  161. * Returns true if the given mode name is enabled, false otherwise.
  162. *
  163. * @param {String} name
  164. * @return {Boolean}
  165. * @api public
  166. */
  167. function enabled(name) {
  168. if (name[name.length - 1] === '*') {
  169. return true;
  170. }
  171. var i;
  172. var len;
  173. for (i = 0, len = createDebug.skips.length; i < len; i++) {
  174. if (createDebug.skips[i].test(name)) {
  175. return false;
  176. }
  177. }
  178. for (i = 0, len = createDebug.names.length; i < len; i++) {
  179. if (createDebug.names[i].test(name)) {
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. /**
  186. * Coerce `val`.
  187. *
  188. * @param {Mixed} val
  189. * @return {Mixed}
  190. * @api private
  191. */
  192. function coerce(val) {
  193. if (val instanceof Error) {
  194. return val.stack || val.message;
  195. }
  196. return val;
  197. }
  198. createDebug.enable(createDebug.load());
  199. return createDebug;
  200. }
  201. module.exports = setup;