Blame view

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