All files / util logger.js

100% Statements 26/26
88.89% Branches 8/9
100% Functions 14/14
100% Lines 23/23
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      139x   139x     139x   139x 139x                                         139x     20x 16x 16x         21x       140x           10x   10x       10x             9x       13x   13x       13x             6x       7x   7x       7x             5x    
/*eslint no-console: 0*/
import _ from 'lodash';
 
export const isDevMode = (function isReactInDev() {
	// This property gets injected via Webpack.
	return process.env.NODE_ENV !== 'production'; // eslint-disable-line no-undef
})();
 
export const isNode = typeof process === 'object' && process.title === 'node';
 
export const logger = (function() {
	return checkIsDev()
		? {
				log,
				logOnce,
				warn,
				warnOnce,
				error,
				errorOnce,
				resetOnce,
			}
		: {
				log: _.noop,
				logOnce: _.noop,
				warn: _.noop,
				warnOnce: _.noop,
				error: _.noop,
				errorOnce: _.noop,
				resetOnce: _.noop,
			};
})();
 
const onceMap = {};
 
function once(key, fn) {
	if (!_.has(onceMap, key)) {
		_.set(onceMap, key, true);
		fn();
	}
}
 
function resetOnce(key) {
	_.unset(onceMap, key);
}
 
export function checkIsDev() {
	return (
		isDevMode && typeof window !== 'undefined' && typeof console !== 'undefined'
	);
}
 
function log(...args) {
	console.log(...args);
 
	try {
		// --- Welcome to debugging Lucid ---
		// This error was thrown as a convenience so that you can use this
		// stack to find the callsite that caused this warning to fire.
		throw new Error(args[0]);
	} catch (x) {
		/* */
	}
}
 
function logOnce(key, ...args) {
	once(key, () => log(...args));
}
 
function warn(...args) {
	console.warn(...args);
 
	try {
		// --- Welcome to debugging Lucid ---
		// This error was thrown as a convenience so that you can use this
		// stack to find the callsite that caused this warning to fire.
		throw new Error(args[0]);
	} catch (x) {
		/* */
	}
}
 
function warnOnce(key, ...args) {
	once(key, () => warn(...args));
}
 
function error(...args) {
	console.error(...args);
 
	try {
		// --- Welcome to debugging Lucid ---
		// This error was thrown as a convenience so that you can use this
		// stack to find the callsite that caused this warning to fire.
		throw new Error(args[0]);
	} catch (x) {
		/* */
	}
}
 
function errorOnce(key, ...args) {
	once(key, () => error(...args));
}