Last active
June 4, 2021 16:17
-
-
Save intrnl/45fc1b3cfa6eddda5c34d73eb9d42a97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example call trace