Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Help Using Wrapper/Decorator to Log Function Calls?

1562752868

Edited 1562752923
I would like to log a message whenever any of my functions are called, which contains the name of the function, its arguments, and a run duration (by checking Date objects when the function begins and ends).  I've been looking up wrappers and decorators on the Internet, but I'm not quite sure how to adapt what I've found to the Roll20 scripting environment.  With my scripts all being of the form: const Handouts = (() => { const SCRIPTNAME = "Handouts" const checkInstall = () => { /* code */ }, regHandlers = () => { /* code */ } return { RegisterEventHandlers: regHandlers , CheckInstall: checkInstall } })() on ( "ready" , () => { Handouts . RegisterEventHandlers () Handouts . CheckInstall () }) ... containing both constant values and functions, is there a way to iterate through all of the functions and wrap them with something that logs the information I want?  (Maybe using _.mapObject() over the return variable?)  Being able to trace the path my code takes through all of my functions, and the time each function takes to run, would be incredibly helpful to pinning down a few annoying inefficiencies I've detected! Thanks for any help you can provide!
1562767622

Edited 1562767653
GiGs
Pro
Sheet Author
API Scripter
There's no easy way to do this, in roll20, as far as i know. You have to manually create your logging statements everywhere you think you need, and choose what you want to log. If you're concerned about the timing of functions, let me say that you shouldnt be. Any inefficiencies in actually working scripts will be dwarfed by other practical considerations (like the fact you';re running through a sandbox, in a very heavy web app that is managing so many things - maps, characters, hundreds or thousands of attributes, graphic objects, etc). Chasing after a few bits of speed improvement here and there just isnt worth it. If you are seeing any noticeable lagging, it's worth trying to chase that error, but otherwise, there's only so much you can do. Most sources of meaningful  delay will be outside of the scripting environment.
1562768956
The Aaron
Roll20 Production Team
API Scripter
Do, for wrapping the functions, you can look at the code I have in The Aaron Sheet:&nbsp; <a href="https://github.com/shdwjk/TheAaronSheet" rel="nofollow">https://github.com/shdwjk/TheAaronSheet</a> Look for wrapCallback(). It's not exactly what you're looking for, but should be close enough to get you moving.&nbsp; For automatically instrumenting all your interface functions, you could wrap the returned object as you are returning it with a function that takes an object and wraps all of its function properties with your wrapper, and returns a new object. That would catch interface functions. For functions you decompose into, you'd have to use a similar method, or wrap them manually.&nbsp;