Code coverage report for src/Logger.js

Statements: 100% (27 / 27)      Branches: 100% (4 / 4)      Functions: 100% (6 / 6)      Lines: 100% (27 / 27)      Ignored: none     

All files » src/ » Logger.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      1 52 52 52     13 130   130 3 3 3 3   127     13 3 3   3   3     13 3 3 3   3     13 3     13 3       13
'use strict'
 
class Logger {
	constructor(options) {
		this._options = options
		this._count = 0
		this._stack = {}
	}
 
	log(message, func) {
		var result
 
		if (process.env.NODE_ENV !== 'production' && this._options.debug) {
			this._start(message)
			result = func()
			this._stop(message)
			return result
		}
		return func()
	}
 
	_start(message) {
		var indentation = this._indent()
		var now = new Date()
 
		this._stack[message] = now
 
		console.log(`[${now.toISOString()}] ${indentation}Starting ${message}...`)
	}
 
	_stop(message) {
		var indentation = this._unindent()
		var now = new Date()
		var elapsed = now - this._stack[message]
 
		console.log(`[${now.toISOString()}] ${indentation}Finished ${message} after ${elapsed} ms`)
	}
 
	_indent() {
		return Array(++this._count).join('  ')
	}
 
	_unindent() {
		return Array(this._count--).join('  ')
	}
}
 
export default Logger