gpt4 book ai didi

Elixir - 跟踪函数调用

转载 作者:行者123 更新时间:2023-12-05 00:48:03 25 4
gpt4 key购买 nike

Elixir 中是否有与 trace 相媲美的东西? lisp 中的宏,它显示函数调用的输入和返回值。取自 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

最佳答案

看看 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

关于Elixir - 跟踪函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50364530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com