-
Notifications
You must be signed in to change notification settings - Fork 23
/
bows.js
141 lines (119 loc) · 3.57 KB
/
bows.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
(function() {
function checkColorSupport() {
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
return false;
}
var chrome = !!window.chrome,
firefox = /firefox/i.test(navigator.userAgent),
firefoxVersion,
electron = process && process.versions && process.versions.electron;
if (firefox) {
var match = navigator.userAgent.match(/Firefox\/(\d+\.\d+)/);
if (match && match[1] && Number(match[1])) {
firefoxVersion = Number(match[1]);
}
}
return chrome || firefoxVersion >= 31.0 || electron;
}
function getLocalStorageSafely() {
var localStorage;
try {
localStorage = window.localStorage;
} catch (e) {
// failed: access to localStorage is denied
}
return localStorage;
}
var yieldColor = function() {
var goldenRatio = 0.618033988749895;
hue += goldenRatio;
hue = hue % 1;
return hue * 360;
};
var inNode = typeof window === 'undefined',
ls = !inNode && getLocalStorageSafely(),
debugKey = ls && ls.andlogKey ? ls.andlogKey : 'debug',
debug = ls && ls[debugKey] ? ls[debugKey] : false,
logger = require('andlog'),
bind = Function.prototype.bind,
hue = 0,
padding = true,
separator = '|',
padLength = 15,
noop = function() {},
// if ls.debugColors is set, use that, otherwise check for support
colorsSupported =
ls && ls.debugColors ? ls.debugColors !== 'false' : checkColorSupport(),
bows = null,
debugRegex = null,
invertRegex = false,
moduleColorsMap = {};
if (debug && debug[0] === '!' && debug[1] === '/') {
invertRegex = true;
debug = debug.slice(1);
}
debugRegex =
debug &&
debug[0] === '/' &&
new RegExp(debug.substring(1, debug.length - 1));
var logLevels = ['log', 'debug', 'warn', 'error', 'info'];
//Noop should noop
for (var i = 0, ii = logLevels.length; i < ii; i++) {
noop[logLevels[i]] = noop;
}
bows = function(str) {
// If localStorage is not available just don't log
if (!ls) return noop;
var msg, colorString, logfn;
if (padding) {
msg = str.slice(0, padLength);
msg += Array(padLength + 3 - msg.length).join(' ') + separator;
} else {
msg = str + Array(3).join(' ') + separator;
}
if (debugRegex) {
var matches = str.match(debugRegex);
if ((!invertRegex && !matches) || (invertRegex && matches)) return noop;
}
if (!bind) return noop;
var logArgs = [logger];
if (colorsSupported) {
if (!moduleColorsMap[str]) {
moduleColorsMap[str] = yieldColor();
}
var color = moduleColorsMap[str];
msg = '%c' + msg;
colorString = 'color: hsl(' + color + ',99%,40%); font-weight: bold';
logArgs.push(msg, colorString);
} else {
logArgs.push(msg);
}
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
logArgs = logArgs.concat(args);
}
logfn = bind.apply(logger.log, logArgs);
logLevels.forEach(function(f) {
logfn[f] = bind.apply(logger[f] || logfn, logArgs);
});
return logfn;
};
bows.config = function(config) {
if (config.padLength) {
padLength = config.padLength;
}
if (typeof config.padding === 'boolean') {
padding = config.padding;
}
if (config.separator) {
separator = config.separator;
} else if (config.separator === false || config.separator === '') {
separator = '';
}
};
if (typeof module !== 'undefined') {
module.exports = bows;
} else {
window.bows = bows;
}
}.call());