-
Notifications
You must be signed in to change notification settings - Fork 139
/
log.js
162 lines (143 loc) · 3.93 KB
/
log.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
(function() {
var exportedLog, ffSupport, formats, getOrderedMatches, hasMatches, isFF, isIE, isOpera, isSafari, log, makeArray, operaSupport, safariSupport, stringToArgs, _log;
if (!(window.console && window.console.log)) {
return;
}
log = function() {
var args;
args = [];
makeArray(arguments).forEach(function(arg) {
if (typeof arg === 'string') {
return args = args.concat(stringToArgs(arg));
} else {
return args.push(arg);
}
});
return _log.apply(window, args);
};
_log = function() {
return Function.prototype.apply.call(console.log, console, makeArray(arguments));
};
makeArray = function(arrayLikeThing) {
return Array.prototype.slice.call(arrayLikeThing);
};
formats = [
{
regex: /\*([^\*]+)\*/,
replacer: function(m, p1) {
return "%c" + p1 + "%c";
},
styles: function() {
return ['font-style: italic', ''];
}
}, {
regex: /\_([^\_]+)\_/,
replacer: function(m, p1) {
return "%c" + p1 + "%c";
},
styles: function() {
return ['font-weight: bold', ''];
}
}, {
regex: /\`([^\`]+)\`/,
replacer: function(m, p1) {
return "%c" + p1 + "%c";
},
styles: function() {
return ['background: rgb(255, 255, 219); padding: 1px 5px; border: 1px solid rgba(0, 0, 0, 0.1)', ''];
}
}, {
regex: /\[c\=(?:\"|\')?((?:(?!(?:\"|\')\]).)*)(?:\"|\')?\]((?:(?!\[c\]).)*)\[c\]/,
replacer: function(m, p1, p2) {
return "%c" + p2 + "%c";
},
styles: function(match) {
return [match[1], ''];
}
}
];
hasMatches = function(str) {
var _hasMatches;
_hasMatches = false;
formats.forEach(function(format) {
if (format.regex.test(str)) {
return _hasMatches = true;
}
});
return _hasMatches;
};
getOrderedMatches = function(str) {
var matches;
matches = [];
formats.forEach(function(format) {
var match;
match = str.match(format.regex);
if (match) {
return matches.push({
format: format,
match: match
});
}
});
return matches.sort(function(a, b) {
return a.match.index - b.match.index;
});
};
stringToArgs = function(str) {
var firstMatch, matches, styles;
styles = [];
while (hasMatches(str)) {
matches = getOrderedMatches(str);
firstMatch = matches[0];
str = str.replace(firstMatch.format.regex, firstMatch.format.replacer);
styles = styles.concat(firstMatch.format.styles(firstMatch.match));
}
return [str].concat(styles);
};
isSafari = function() {
return /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
};
isOpera = function() {
return /OPR/.test(navigator.userAgent) && /Opera/.test(navigator.vendor);
};
isFF = function() {
return /Firefox/.test(navigator.userAgent);
};
isIE = function() {
return /MSIE/.test(navigator.userAgent);
};
safariSupport = function() {
var m;
m = navigator.userAgent.match(/AppleWebKit\/(\d+)\.(\d+)(\.|\+|\s)/);
if (!m) {
return false;
}
return 537.38 <= parseInt(m[1], 10) + (parseInt(m[2], 10) / 100);
};
operaSupport = function() {
var m;
m = navigator.userAgent.match(/OPR\/(\d+)\./);
if (!m) {
return false;
}
return 15 <= parseInt(m[1], 10);
};
ffSupport = function() {
return window.console.firebug || window.console.exception;
};
if (isIE() || (isFF() && !ffSupport()) || (isOpera() && !operaSupport()) || (isSafari() && !safariSupport())) {
exportedLog = _log;
} else {
exportedLog = log;
}
exportedLog.l = _log;
if (typeof define === 'function' && define.amd) {
define(function() {
return exportedLog;
});
} else if (typeof exports !== 'undefined') {
module.exports = exportedLog;
} else {
window.log = exportedLog;
}
}).call(this);