function NamedTimers(log, warningThreshold) {

    let timerScope;
    const namedTimer = {total: new Date()};
    const elapsed = {};

    this.startNamedTimer = function (name) {
        if (!name)
            return;

        delete elapsed[name];
        namedTimer[name] = new Date();
    };

    this.stopNamedTimer = function (name) {
        if ((!name) || (!namedTimer[name]))
            return;

        elapsed[name] = new Date() - namedTimer[name];
        delete namedTimer[name];
    };

    this.setScope = function(scope){
        timerScope = scope;
    };

    this.logElapsed = function() {

        if ((!log) || (!timerScope))
            return;

        elapsed.total = new Date() - namedTimer.total;
        log.trace({scope: timerScope, times: elapsed}, 'Elapsed time');

        if (!warningThreshold)
            return;

        if (elapsed.total > warningThreshold)
            log.warn({
                scope: timerScope,
                threshold: warningThreshold,
                actual: elapsed.total
            }, 'Exceeded time threshold');
    };
}

module.exports = NamedTimers;
