You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.7 KiB

  1. /**
  2. * This is the web browser implementation of `debug()`.
  3. *
  4. * Expose `debug()` as the module.
  5. */
  6. exports = module.exports = require('./debug');
  7. exports.log = log;
  8. exports.formatArgs = formatArgs;
  9. exports.save = save;
  10. exports.load = load;
  11. exports.useColors = useColors;
  12. exports.storage = 'undefined' != typeof chrome
  13. && 'undefined' != typeof chrome.storage
  14. ? chrome.storage.local
  15. : localstorage();
  16. /**
  17. * Colors.
  18. */
  19. exports.colors = [
  20. 'lightseagreen',
  21. 'forestgreen',
  22. 'goldenrod',
  23. 'dodgerblue',
  24. 'darkorchid',
  25. 'crimson'
  26. ];
  27. /**
  28. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  29. * and the Firebug extension (any Firefox version) are known
  30. * to support "%c" CSS customizations.
  31. *
  32. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  33. */
  34. function useColors() {
  35. // is webkit? http://stackoverflow.com/a/16459606/376773
  36. return ('WebkitAppearance' in document.documentElement.style) ||
  37. // is firebug? http://stackoverflow.com/a/398120/376773
  38. (window.console && (console.firebug || (console.exception && console.table))) ||
  39. // is firefox >= v31?
  40. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  41. (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
  42. }
  43. /**
  44. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  45. */
  46. exports.formatters.j = function(v) {
  47. return JSON.stringify(v);
  48. };
  49. /**
  50. * Colorize log arguments if enabled.
  51. *
  52. * @api public
  53. */
  54. function formatArgs() {
  55. var args = arguments;
  56. var useColors = this.useColors;
  57. args[0] = (useColors ? '%c' : '')
  58. + this.namespace
  59. + (useColors ? ' %c' : ' ')
  60. + args[0]
  61. + (useColors ? '%c ' : ' ')
  62. + '+' + exports.humanize(this.diff);
  63. if (!useColors) return args;
  64. var c = 'color: ' + this.color;
  65. args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
  66. // the final "%c" is somewhat tricky, because there could be other
  67. // arguments passed either before or after the %c, so we need to
  68. // figure out the correct index to insert the CSS into
  69. var index = 0;
  70. var lastC = 0;
  71. args[0].replace(/%[a-z%]/g, function(match) {
  72. if ('%%' === match) return;
  73. index++;
  74. if ('%c' === match) {
  75. // we only are interested in the *last* %c
  76. // (the user may have provided their own)
  77. lastC = index;
  78. }
  79. });
  80. args.splice(lastC, 0, c);
  81. return args;
  82. }
  83. /**
  84. * Invokes `console.log()` when available.
  85. * No-op when `console.log` is not a "function".
  86. *
  87. * @api public
  88. */
  89. function log() {
  90. // this hackery is required for IE8/9, where
  91. // the `console.log` function doesn't have 'apply'
  92. return 'object' === typeof console
  93. && console.log
  94. && Function.prototype.apply.call(console.log, console, arguments);
  95. }
  96. /**
  97. * Save `namespaces`.
  98. *
  99. * @param {String} namespaces
  100. * @api private
  101. */
  102. function save(namespaces) {
  103. try {
  104. if (null == namespaces) {
  105. exports.storage.removeItem('debug');
  106. } else {
  107. exports.storage.debug = namespaces;
  108. }
  109. } catch(e) {}
  110. }
  111. /**
  112. * Load `namespaces`.
  113. *
  114. * @return {String} returns the previously persisted debug modes
  115. * @api private
  116. */
  117. function load() {
  118. var r;
  119. try {
  120. r = exports.storage.debug;
  121. } catch(e) {}
  122. return r;
  123. }
  124. /**
  125. * Enable namespaces listed in `localStorage.debug` initially.
  126. */
  127. exports.enable(load());
  128. /**
  129. * Localstorage attempts to return the localstorage.
  130. *
  131. * This is necessary because safari throws
  132. * when a user disables cookies/localstorage
  133. * and you attempt to access it.
  134. *
  135. * @return {LocalStorage}
  136. * @api private
  137. */
  138. function localstorage(){
  139. try {
  140. return window.localStorage;
  141. } catch (e) {}
  142. }