This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
console.js
86 lines (66 loc) · 1.63 KB
/
console.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
'use strict';
/* global console, performance */
/**
* KeyboardConsole is an incomplete implementation of console so we could
* turn it on and off on build time.
*/
(function(exports) {
var KeyboardConsole = function KeyboardConsole() {
this._timers = null;
};
KeyboardConsole.prototype.start = function() {
var timers = this._timers = new Map();
timers.set('domLoading', performance.timing.domLoading);
this._startTime = Date.now();
};
/**
*
* Log level decides whether or not to print log.
*
* 0 - warn() and error()
* 1 - info()
* 2 - log() and time() and timeEnd()
* 3 - trace()
*
* Note that we want console.warn() and console.error() show up in
* production too so warn() and error() is not implemented here.
*
*/
KeyboardConsole.prototype.LOG_LEVEL = 0;
KeyboardConsole.prototype.trace = function() {
if (this.LOG_LEVEL < 3) {
return;
}
console.trace.apply(console, arguments);
};
KeyboardConsole.prototype.time = function(timerName) {
if (this.LOG_LEVEL < 2) {
return;
}
console.time(timerName);
};
KeyboardConsole.prototype.timeEnd = function(timerName) {
if (this.LOG_LEVEL < 2) {
return;
}
if (this._timers.has(timerName)) {
console.log(timerName + ': ' +
(Date.now() - this._timers.get(timerName)) + 'ms');
return;
}
console.timeEnd(timerName);
};
KeyboardConsole.prototype.log = function() {
if (this.LOG_LEVEL < 2) {
return;
}
console.log.apply(console, arguments);
};
KeyboardConsole.prototype.info = function() {
if (this.LOG_LEVEL < 1) {
return;
}
console.info.apply(console, arguments);
};
exports.KeyboardConsole = KeyboardConsole;
})(window);