node.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Module dependencies.
  3. */
  4. var tty = require('tty');
  5. var util = require('util');
  6. /**
  7. * This is the Node.js implementation of `debug()`.
  8. *
  9. * Expose `debug()` as the module.
  10. */
  11. exports = module.exports = require('./debug');
  12. exports.init = init;
  13. exports.log = log;
  14. exports.formatArgs = formatArgs;
  15. exports.save = save;
  16. exports.load = load;
  17. exports.useColors = useColors;
  18. /**
  19. * Colors.
  20. */
  21. exports.colors = [ 6, 2, 3, 4, 5, 1 ];
  22. try {
  23. var supportsColor = require('supports-color');
  24. if (supportsColor && supportsColor.level >= 2) {
  25. exports.colors = [
  26. 20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68,
  27. 69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113, 128, 129, 134,
  28. 135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
  29. 172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200, 201, 202, 203, 204,
  30. 205, 206, 207, 208, 209, 214, 215, 220, 221
  31. ];
  32. }
  33. } catch (err) {
  34. // swallow - we only care if `supports-color` is available; it doesn't have to be.
  35. }
  36. /**
  37. * Build up the default `inspectOpts` object from the environment variables.
  38. *
  39. * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
  40. */
  41. exports.inspectOpts = Object.keys(process.env).filter(function (key) {
  42. return /^debug_/i.test(key);
  43. }).reduce(function (obj, key) {
  44. // camel-case
  45. var prop = key
  46. .substring(6)
  47. .toLowerCase()
  48. .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
  49. // coerce string value into JS value
  50. var val = process.env[key];
  51. if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
  52. else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
  53. else if (val === 'null') val = null;
  54. else val = Number(val);
  55. obj[prop] = val;
  56. return obj;
  57. }, {});
  58. /**
  59. * Is stdout a TTY? Colored output is enabled when `true`.
  60. */
  61. function useColors() {
  62. return 'colors' in exports.inspectOpts
  63. ? Boolean(exports.inspectOpts.colors)
  64. : tty.isatty(process.stderr.fd);
  65. }
  66. /**
  67. * Map %o to `util.inspect()`, all on a single line.
  68. */
  69. exports.formatters.o = function(v) {
  70. this.inspectOpts.colors = this.useColors;
  71. return util.inspect(v, this.inspectOpts)
  72. .split('\n').map(function(str) {
  73. return str.trim()
  74. }).join(' ');
  75. };
  76. /**
  77. * Map %o to `util.inspect()`, allowing multiple lines if needed.
  78. */
  79. exports.formatters.O = function(v) {
  80. this.inspectOpts.colors = this.useColors;
  81. return util.inspect(v, this.inspectOpts);
  82. };
  83. /**
  84. * Adds ANSI color escape codes if enabled.
  85. *
  86. * @api public
  87. */
  88. function formatArgs(args) {
  89. var name = this.namespace;
  90. var useColors = this.useColors;
  91. if (useColors) {
  92. var c = this.color;
  93. var colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
  94. var prefix = ' ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';
  95. args[0] = prefix + args[0].split('\n').join('\n' + prefix);
  96. args.push(colorCode + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
  97. } else {
  98. args[0] = getDate() + name + ' ' + args[0];
  99. }
  100. }
  101. function getDate() {
  102. if (exports.inspectOpts.hideDate) {
  103. return '';
  104. } else {
  105. return new Date().toISOString() + ' ';
  106. }
  107. }
  108. /**
  109. * Invokes `util.format()` with the specified arguments and writes to stderr.
  110. */
  111. function log() {
  112. return process.stderr.write(util.format.apply(util, arguments) + '\n');
  113. }
  114. /**
  115. * Save `namespaces`.
  116. *
  117. * @param {String} namespaces
  118. * @api private
  119. */
  120. function save(namespaces) {
  121. if (null == namespaces) {
  122. // If you set a process.env field to null or undefined, it gets cast to the
  123. // string 'null' or 'undefined'. Just delete instead.
  124. delete process.env.DEBUG;
  125. } else {
  126. process.env.DEBUG = namespaces;
  127. }
  128. }
  129. /**
  130. * Load `namespaces`.
  131. *
  132. * @return {String} returns the previously persisted debug modes
  133. * @api private
  134. */
  135. function load() {
  136. return process.env.DEBUG;
  137. }
  138. /**
  139. * Init logic for `debug` instances.
  140. *
  141. * Create a new `inspectOpts` object in case `useColors` is set
  142. * differently for a particular `debug` instance.
  143. */
  144. function init (debug) {
  145. debug.inspectOpts = {};
  146. var keys = Object.keys(exports.inspectOpts);
  147. for (var i = 0; i < keys.length; i++) {
  148. debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  149. }
  150. }
  151. /**
  152. * Enable namespaces listed in `process.env.DEBUG` initially.
  153. */
  154. exports.enable(load());