forked from Zenovations/console-normalizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalizeconsole.js
95 lines (89 loc) · 3.18 KB
/
normalizeconsole.js
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
(function(console) {
/*********************************************************************************************
* Make sure console exists because IE blows up if it's not open and you attempt to access it
* Create some dummy functions if we need to, so we don't have to if/else everything
*********************************************************************************************/
console||(console = window.console = {
// all this "a, b, c, d, e" garbage is to make the IDEs happy, since they can't do variable argument lists
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
log: function(a, b, c, d, e) {},
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
info: function(a, b, c, d, e) {},
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
warn: function(a, b, c, d, e) {},
/**
* @param a
* @param [b]
* @param [c]
* @param [d]
* @param [e]
*/
error: function(a, b, c, d, e) {}
});
// le sigh, IE, oh IE, how we fight... fix Function.prototype.bind as needed
if (!Function.prototype.bind) {
//credits: taken from bind_even_never in this discussion: https://prototype.lighthouseapp.com/projects/8886/tickets/215-optimize-bind-bindaseventlistener#ticket-215-9
Function.prototype.bind = function(context) {
var fn = this, args = Array.prototype.slice.call(arguments, 1);
return function(){
return fn.apply(context, Array.prototype.concat.apply(args, arguments));
};
};
}
// IE 9 won't allow us to call console.log.apply (WTF IE!) It also reports typeof(console.log) as 'object' (UNH!)
// but together, those two errors can be useful in allowing us to fix stuff so it works right
if( typeof(console.log) === 'object' ) {
// Array.forEach doesn't work in IE 8 so don't try that :(
console.log = Function.prototype.call.bind(console.log, console);
console.info = Function.prototype.call.bind(console.info, console);
console.warn = Function.prototype.call.bind(console.warn, console);
console.error = Function.prototype.call.bind(console.error, console);
}
/**
* Support group and groupEnd functions
*/
('groupCollapsed' in console) ||
(console.groupCollapsed = function(msg) {
console.info("\n------------\n"+msg+"\n------------");
});
('group' in console) ||
(console.group = function(msg) {
console.info("\n------------\n"+msg+"\n------------");
});
('groupEnd' in console) ||
(console.groupEnd = function() {
//console.log("\n\n");
});
/**
* Support time and timeEnd functions
*/
('time' in console) ||
(function() {
var trackedTimes = {};
console.time = function(msg) {
trackedTimes[msg] = new Date().getTime();
};
console.timeEnd = function(msg) {
var end = new Date().getTime(), time = (msg in trackedTimes)? end - trackedTimes[msg] : 0;
console.info(msg+': '+time+'ms')
}
}());
})(window.console);