Elixir中是否有类似于lisp中的trace
宏,它显示了函数调用的输入和返回值。取自Common Lisp HyperSpec网站:
(defun fact (n) (if (zerop n) 1 (* n (fact (- n 1)))))
=> FACT
(trace fact)
=> (FACT)
;; Of course, the format of traced output is implementation-dependent.
(fact 3)
>> 1 Enter FACT 3
>> | 2 Enter FACT 2
>> | 3 Enter FACT 1
>> | | 4 Enter FACT 0
>> | | 4 Exit FACT 1
>> | 3 Exit FACT 1
>> | 2 Exit FACT 2
>> 1 Exit FACT 6
=> 6
答案 0 :(得分:5)
看一下Erlang的dbg
模块(当我需要使用该模块时,请参阅https://stackoverflow.com/a/1954980/436853获取我的回答)。将其转换为Elixir式电话很简单:
iex(1)> :dbg.start()
{:ok, #PID<0.91.0>}
iex(2)> :dbg.tracer()
{:ok, #PID<0.91.0>}
iex(3)> :dbg.p(:all, :c)
{:ok, [{:matched, :nonode@nohost, 52}]}
iex(4)> :dbg.tp(IO, :puts, 1, [{:"_", [], [{:return_trace}]}])
{:ok, [{:matched, :nonode@nohost, 1}, {:saved, 1}]}
iex(5)> IO.puts("Hello!")
Hello!
(<0.89.0>) call 'Elixir.IO':puts(<<"Hello!">>)
(<0.89.0>) returned from 'Elixir.IO':puts/1 -> ok
:ok