ConsoleReporter.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /** @license MIT License (c) copyright 2010-2014 original author or authors */
  2. /** @author Brian Cavalier */
  3. /** @author John Hann */
  4. (function(define) { 'use strict';
  5. define(function(require) {
  6. var error = require('./error');
  7. var unhandledRejectionsMsg = '[promises] Unhandled rejections: ';
  8. var allHandledMsg = '[promises] All previously unhandled rejections have now been handled';
  9. function ConsoleReporter() {
  10. this._previouslyReported = false;
  11. }
  12. ConsoleReporter.prototype = initDefaultLogging();
  13. ConsoleReporter.prototype.log = function(traces) {
  14. if(traces.length === 0) {
  15. if(this._previouslyReported) {
  16. this._previouslyReported = false;
  17. this.msg(allHandledMsg);
  18. }
  19. return;
  20. }
  21. this._previouslyReported = true;
  22. this.groupStart(unhandledRejectionsMsg + traces.length);
  23. try {
  24. this._log(traces);
  25. } finally {
  26. this.groupEnd();
  27. }
  28. };
  29. ConsoleReporter.prototype._log = function(traces) {
  30. for(var i=0; i<traces.length; ++i) {
  31. this.warn(error.format(traces[i]));
  32. }
  33. };
  34. function initDefaultLogging() {
  35. /*jshint maxcomplexity:7*/
  36. var log, warn, groupStart, groupEnd;
  37. if(typeof console === 'undefined') {
  38. log = warn = consoleNotAvailable;
  39. } else {
  40. // Alias console to prevent things like uglify's drop_console option from
  41. // removing console.log/error. Unhandled rejections fall into the same
  42. // category as uncaught exceptions, and build tools shouldn't silence them.
  43. var localConsole = console;
  44. if(typeof localConsole.error === 'function'
  45. && typeof localConsole.dir === 'function') {
  46. warn = function(s) {
  47. localConsole.error(s);
  48. };
  49. log = function(s) {
  50. localConsole.log(s);
  51. };
  52. if(typeof localConsole.groupCollapsed === 'function') {
  53. groupStart = function(s) {
  54. localConsole.groupCollapsed(s);
  55. };
  56. groupEnd = function() {
  57. localConsole.groupEnd();
  58. };
  59. }
  60. } else {
  61. // IE8 has console.log and JSON, so we can make a
  62. // reasonably useful warn() from those.
  63. // Credit to webpro (https://github.com/webpro) for this idea
  64. // typeof localConsole.log will return 'object' in IE8, so can't test it with === 'function'
  65. // Since this is more of a corner case for IE8, I'm ok to check it with !== 'undefined' to reduce complexity
  66. if (typeof localConsole.log !== 'undefined' && typeof JSON !== 'undefined') {
  67. log = warn = function(x) {
  68. if (typeof x !== 'string') {
  69. try {
  70. x = JSON.stringify(x);
  71. } catch (e) {
  72. }
  73. }
  74. localConsole.log(x);
  75. };
  76. } else {
  77. log = warn = consoleNotAvailable;
  78. }
  79. }
  80. }
  81. return {
  82. msg: log,
  83. warn: warn,
  84. groupStart: groupStart || warn,
  85. groupEnd: groupEnd || consoleNotAvailable
  86. };
  87. }
  88. function consoleNotAvailable() {}
  89. return ConsoleReporter;
  90. });
  91. }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));