Putting It Together—Tracing Method Calls

Let’s work through a larger example. We want to write a module called Tracer. If we use Tracer in another module, entry and exit tracing will be added to any subsequently defined function. For example, given the following:

 defmodule​ Test ​do
 use​ Tracer
 def​ puts_sum_three(a,b,c), ​do​: IO.inspect(a+b+c)
 def​ add_list(list), ​do​: Enum.reduce(list, 0, &(&1+&2))

we’d get this output:

 ==> call puts_sum_three(1, 2, 3)
 <== returns 6
 ==> call add_list([5,6,7,8])
 <== returns 26

My approach to writing this kind of code is to start by exploring what we have to work with, and then to generalize. The goal is to metaprogram ...

Get Programming Elixir 1.3 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.