all files / src/ Log.js

78.57% Statements 33/42
50% Branches 6/12
66.67% Functions 10/15
78.57% Lines 33/42
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                    10×   18×               1298×     1298×                       10×     18×     32× 32×   1173× 1048×   125× 125× 125×       10×     18×          
'use strict';
 
var log = {};
 
var prefLevel = 1;
 
var debugOut;
var warnOut;
var errorOut;
 
Iif(typeof console == 'undefined'){
	debugOut = function(){};
	warnOut = function(){};
	errorOut = function(){};
} else {
	debugOut = function(s){
		console.log(s);
	};
	warnOut = function(s){
		console.warn(s);
	};
	errorOut = function(s){
		console.error(s);
	};
}
 
 
log.SetLevel = function(level){
	prefLevel = level;
};
 
log.debug = function(debugstring, level){
	Iif(typeof(level) !== 'number'){
		level = 1;
	}
	if(level <= prefLevel) {
		debugOut(debugstring);
	}
};
 
log.debugObject = function(obj, level){
	if(typeof(level) !== 'number'){
		level = 1;
	}
	if(level <= prefLevel) {
		debugOut(obj);
	}
};
 
log.warn = function(warnstring){
	warnOut(warnstring);
};
 
log.error = function(errorstring){
	errorOut(errorstring);
};
 
log.Logger = function(prefix, llevel=2){
	prefLevel = llevel;
	return {
		debug: function(debugstring, level){
			if(typeof debugstring == 'string'){
				return log.debug(`${prefix}: ${debugstring}`, level);
			} else {
				log.debug(`${prefix}: \\`, level);
				log.debug(debugstring, level);
				return;
			}
		},
		warn: function(warnstring){
			return log.warn(`${prefix}: ${warnstring}`);
		},
		error: function(errorstring){
			return log.error(`${prefix}: ${errorstring}`);
		}
	};
};
 
module.exports = log;