bows
Version:
Rainbowed console logs for chrome, opera and firefox in development.
142 lines (119 loc) • 3.65 kB
JavaScript
(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());