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.

175 lines
3.7 KiB

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