UNPKG

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