Skip to content

Instantly share code, notes, and snippets.

@intrnl
Last active June 4, 2021 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intrnl/45fc1b3cfa6eddda5c34d73eb9d42a97 to your computer and use it in GitHub Desktop.
Save intrnl/45fc1b3cfa6eddda5c34d73eb9d42a97 to your computer and use it in GitHub Desktop.
// 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
Copy link
Author

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