This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconsole-shim.js
213 lines (189 loc) · 6.77 KB
/
console-shim.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @preserve console-shim 1.0.3
* https://github.com/kayahr/console-shim
* Copyright (C) 2011 Klaus Reimer <[email protected]>
* Licensed under the MIT license
* (See http://www.opensource.org/licenses/mit-license)
*/
(function(){
"use strict";
/**
* Returns a function which calls the specified function in the specified
* scope.
*
* @param {Function} func
* The function to call.
* @param {Object} scope
* The scope to call the function in.
* @param {...*} args
* Additional arguments to pass to the bound function.
* @returns {function(...[*]): undefined}
* The bound function.
*/
var bind = function(func, scope, args)
{
var fixedArgs = Array.prototype.slice.call(arguments, 2);
return function()
{
var args = fixedArgs.concat(Array.prototype.slice.call(arguments, 0));
(/** @type {Function} */ func).apply(scope, args);
};
};
// Create console if not present
if (!window["console"]) window.console = /** @type {Console} */ ({});
var console = (/** @type {Object} */ window.console);
// Implement console log if needed
if (!console["log"])
{
// Use log4javascript if present
if (window["log4javascript"])
{
var log = log4javascript.getDefaultLogger();
console.log = bind(log.info, log);
console.debug = bind(log.debug, log);
console.info = bind(log.info, log);
console.warn = bind(log.warn, log);
console.error = bind(log.error, log);
}
// Use empty dummy implementation to ignore logging
else
{
console.log = (/** @param {...*} args */ function(args) {});
}
}
// Implement other log levels to console.log if missing
if (!console["debug"]) console.debug = console.log;
if (!console["info"]) console.info = console.log;
if (!console["warn"]) console.warn = console.log;
if (!console["error"]) console.error = console.log;
// Wrap the log methods in IE (<=9) because their argument handling is wrong
// This wrapping is also done if the __consoleShimTest__ symbol is set. This
// is needed for unit testing.
if (window["__consoleShimTest__"] != null ||
eval("/*@cc_on @_jscript_version <= 9@*/"))
{
/**
* Wraps the call to a real IE logging method. Modifies the arguments so
* parameters which are not represented by a placeholder are properly
* printed with a space character as separator.
*
* @param {...*} args
* The function arguments. First argument is the log function
* to call, the other arguments are the log arguments.
*/
var wrap = function(args)
{
var i, max, match, log;
// Convert argument list to real array
args = Array.prototype.slice.call(arguments, 0);
// First argument is the log method to call
log = args.shift();
max = args.length;
if (max > 1 && window["__consoleShimTest__"] !== false)
{
// When first parameter is not a string then add a format string to
// the argument list so we are able to modify it in the next stop
if (typeof(args[0]) != "string")
{
args.unshift("%o");
max += 1;
}
// For each additional parameter which has no placeholder in the
// format string we add another placeholder separated with a
// space character.
match = args[0].match(/%[a-z]/g);
for (i = match ? match.length + 1 : 1; i < max; i += 1)
{
args[0] += " %o";
}
}
Function.apply.call(log, console, args);
};
// Wrap the native log methods of IE to fix argument output problems
console.log = bind(wrap, window, console.log);
console.debug = bind(wrap, window, console.debug);
console.info = bind(wrap, window, console.info);
console.warn = bind(wrap, window, console.warn);
console.error = bind(wrap, window, console.error);
}
// Implement console.assert if missing
if (!console["assert"])
{
console["assert"] = function()
{
var args = Array.prototype.slice.call(arguments, 0);
var expr = args.shift();
if (!expr)
{
args[0] = "Assertion failed: " + args[0];
console.error.apply(console, args);
}
};
}
// Linking console.dir and console.dirxml to the console.log method if
// missing. Hopefully the browser already logs objects and DOM nodes as a
// tree.
if (!console["dir"]) console["dir"] = console.log;
if (!console["dirxml"]) console["dirxml"] = console.log;
// Linking console.exception to console.error. This is not the same but
// at least some error message is displayed.
if (!console["exception"]) console["exception"] = console.error;
// Implement console.time and console.timeEnd if one of them is missing
if (!console["time"] || !console["timeEnd"])
{
var timers = {};
console["time"] = function(id)
{
timers[id] = new Date().getTime();
};
console["timeEnd"] = function(id)
{
var start = timers[id];
if (start)
{
console.log(id + ": " + (new Date().getTime() - start) + "ms");
delete timers[id];
}
};
}
// Implement console.table if missing
if (!console["table"])
{
console["table"] = function(data, columns)
{
var i, iMax, row, j, jMax, k;
// Do nothing if data has wrong type or no data was specified
if (!data || !(data instanceof Array) || !data.length) return;
// Auto-calculate columns array if not set
if (!columns || !(columns instanceof Array))
{
columns = [];
for (k in data[0])
{
if (!data[0].hasOwnProperty(k)) continue;
columns.push(k);
}
}
for (i = 0, iMax = data.length; i < iMax; i += 1)
{
row = [];
for (j = 0, jMax = columns.length; j < jMax; j += 1)
{
row.push(data[i][columns[j]]);
}
Function.apply.call(console.log, console, row);
}
};
}
// Dummy implementations of other console features to prevent error messages
// in browsers not supporting it.
if (!console["clear"]) console["clear"] = function() {};
if (!console["trace"]) console["trace"] = function() {};
if (!console["group"]) console["group"] = function() {};
if (!console["groupCollapsed"]) console["groupCollapsed"] = function() {};
if (!console["groupEnd"]) console["groupEnd"] = function() {};
if (!console["timeStamp"]) console["timeStamp"] = function() {};
if (!console["profile"]) console["profile"] = function() {};
if (!console["profileEnd"]) console["profileEnd"] = function() {};
if (!console["count"]) console["count"] = function() {};
})();