mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user