Introduce DEBUG module which traces public API calls

This patch improves on DEBUG module to trace all puppeteer's
public API calls.

References #89.
This commit is contained in:
Andrey Lushnikov
2017-07-18 20:53:00 -07:00
parent f154d537f7
commit 55acae40fd
8 changed files with 55 additions and 1 deletions

View File

@@ -91,6 +91,41 @@ class Helper {
});
}
}
/**
* @param {!Object} classType
*/
static tracePublicAPI(classType) {
let className = classType.prototype.constructor.name;
className = className.substring(0, 1).toLowerCase() + className.substring(1);
const debug = require('debug')(`puppeteer:${className}`);
if (!debug.enabled)
return;
for (let methodName of Reflect.ownKeys(classType.prototype)) {
const method = Reflect.get(classType.prototype, methodName);
if (methodName === 'constructor' || methodName.startsWith('_') || typeof method !== 'function')
continue;
Reflect.set(classType.prototype, methodName, function(...args) {
let argsText = args.map(stringifyArgument).join(', ');
let callsite = `${className}.${methodName}(${argsText})`;
debug(callsite);
return method.call(this, ...args);
});
}
/**
* @param {!Object} arg
* @return {string}
*/
function stringifyArgument(arg) {
if (typeof arg !== 'function')
return JSON.stringify(arg);
let text = arg.toString().split('\n').map(line => line.trim()).join('');
if (text.length > 20)
text = text.substring(0, 20) + '…';
return `"${text}"`;
}
}
}
module.exports = Helper;