mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
8 years ago
|
'use strict';
|
||
|
const _ = require('lodash');
|
||
|
|
||
|
class Debug {
|
||
|
constructor(moduleName){
|
||
|
this.name = moduleName || 'unnamed module';
|
||
|
}
|
||
|
|
||
|
push(req, debugMsg){
|
||
|
if (!_.isEmpty(req.clean) && req.clean.enableDebug){
|
||
|
req.debug = req.debug || [];
|
||
|
// remove the extra space character
|
||
|
req.debug.push({[this.name]: debugMsg});
|
||
|
// req.debug.push(`[${this.name}] ${debugMsg}`);
|
||
|
}
|
||
|
}
|
||
|
// optional debugMsg passed to timer
|
||
|
beginTimer(req, debugMsg){
|
||
|
if (!_.isEmpty(req.clean) && req.clean.enableDebug){
|
||
|
// internal object debugTimers. Doesn't get displayed in geocodeJSON
|
||
|
req.debugTimers = req.debugTimers || {};
|
||
|
req.debugTimers[this.name] = Date.now();
|
||
|
if (debugMsg){
|
||
|
this.push(req, `Timer Began: ${debugMsg}`);
|
||
|
} else {
|
||
|
this.push(req, `Timer Began`);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
stopTimer(req, debugMsg){
|
||
|
if (!_.isEmpty(req.clean) && req.clean.enableDebug){
|
||
|
let timeElapsed = Date.now() - req.debugTimers[this.name];
|
||
|
if (debugMsg){
|
||
|
this.push(req, `Timer Stopped: ${timeElapsed} ms: ${debugMsg}`);
|
||
|
} else {
|
||
|
this.push(req, `Timer Stopped: ${timeElapsed} ms`);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Debug;
|