adding call entrance/exit logging under streamline

29 views
Skip to first unread message

Seth Pollack

unread,
Jan 14, 2013, 9:07:13 PM1/14/13
to stream...@googlegroups.com
First of all, what a fantastic tool streamline looks to be! i'm quite excited about how this is going to simplify our node codebase. 

Here is the question: 

In our existing (non-streamline) node codebase, i have a wrapper for asynchronous functions that does entrance/exit logging. It logs the call parameters (formal argument names and actual argument values), as well as the error/results when the callback is later invoked. It also does some niceties like logging the time spent in each function call between invocation and async completion. I suppose it's poor man's AOP for function level logging and timing, and its proved quite useful to us. Anyhow, this is implemented by rooting around inside the function object, and by wrapping the function as well as its callback to get all the context we need. 

So, enter streamline. My existing logging wrapper does not play well with streamline's internal function wrapping. Does a solution for this type of entrance/exit logging already exist under streamline? if not, what would be the best way to go about adding this -- should i do this outside of streamline, or is forking a better approach? 

From the little bit of looking i did so far, i did see the F.fstreamlineFunction member of the streamline wrapper, which would at least allow me to get to the original function to query its formal arguments and such from the outside (although that's a bit of a hack to depend on it...)

Anyhow, any suggestions appreciated. 

Thanks,
Seth

Bruno Jouhier

unread,
Jan 17, 2013, 6:03:36 PM1/17/13
to stream...@googlegroups.com
Hello Seth,

I have created a small gist that demonstrates how you can wrap async functions and call them with streamline: https://gist.github.com/4560643

The first solution is to implement the wrapper directly with callbacks. This is probably the easiest and most flexible way to do it.

The second solution is to implement the wrapper as a streamline function. You have to use the apply_ function which comes bundled with streamline. This implementation preserves the sync style but it has one limitation: you have to know the index of the callback parameter (0 in my test function).

The before and after functions are sync in my gist but you can also make it work with async before and after functions.

Hope this helps.

Bruno

Seth Pollack

unread,
Jan 17, 2013, 11:52:53 PM1/17/13
to stream...@googlegroups.com
Bruno, thank you! This should get me started. 

Seth

Seth Pollack

unread,
Jun 18, 2013, 8:02:07 PM6/18/13
to stream...@googlegroups.com
Hi Bruno, a followup question here. I implemented a solution along the lines you outlined, which has been working great. The one issue i've hit is when we want to invoke a wrapped function via streamline futures, i.e. where we pass a falsy value for the callback function. In this case, the entrance/exit logger has no function to wrap, so we have to skip the logging. Any ideas on how to maintain the logging even when a function is invoked via a future? 

Thanks,
Seth

Bruno Jouhier

unread,
Jun 19, 2013, 5:42:14 PM6/19/13
to stream...@googlegroups.com
Hi Seth,

I've enhanced the gist a bit to make it work with futures. See the changes in https://gist.github.com/bjouhier/4560643/revisions

As the future is just a function with a callback parameter, the trick it to wrap the future when the callback is null:

  if (cb == null) return wrapper1(fn.apply(this, arguments), 0, true);

The only gotcha is that I have to pass true through an additional isFuture parameter to avoid printing the start message twice.

I hope it will work for you.

Bruno

Seth Pollack

unread,
Jun 19, 2013, 6:37:01 PM6/19/13
to stream...@googlegroups.com
Bruno, aha, clever. Let me give that a try. thanks!

Seth

--
You received this message because you are subscribed to a topic in the Google Groups "streamline.js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/streamlinejs/PTBLCTY7lVA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to streamlinejs...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages