Skip to content

Instantly share code, notes, and snippets.

@intrnl

intrnl/patch_trace.js

Last active Jun 4, 2021
Embed
What would you like to do?
// monkey patches all class methods for tracing calls
// not made for async methods
function patch_trace (cl, depth = Infinity) {
let proto = cl.prototype;
do {
if (!depth-- || proto === Object.prototype) break;
let name = proto.constructor.name;
let fields = Object.getOwnPropertyNames(proto);
for (let field of fields) {
let log = `${name}.${field}`;
let prev = proto[field];
// ignore non-methods
// constructor cannot be patched
if (field === 'constructor' || typeof prev !== 'function') {
continue;
}
proto[field] = function (...args) {
try {
console.group(log);
return prev.apply(this, args);
} finally {
console.groupEnd();
}
};
}
} while (proto = Object.getPrototypeOf(proto))
}
@intrnl

This comment has been minimized.

Copy link
Owner Author

@intrnl intrnl commented Jun 4, 2021

Example call trace
Example call trace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment