node.js 4.3 KB

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