在 lager.elr (https://github.com/basho/lager的主模块)中没有名为“debug”的函数,但我有一个应用程序从 lager 模块调用调试函数,如: lager:debug(Str,参数)
我是 Erlang 的初学者,但我知道当我们从模块 lile “mymodule:myfunction”调用函数时,文件 mymodule.erl 中应该有一个名为“myfunction”的函数,但在这种情况下,当我在 lager.erl 中搜索函数时“调试”我找不到它。
在 lager.elr (https://github.com/basho/lager的主模块)中没有名为“debug”的函数,但我有一个应用程序从 lager 模块调用调试函数,如: lager:debug(Str,参数)
我是 Erlang 的初学者,但我知道当我们从模块 lile “mymodule:myfunction”调用函数时,文件 mymodule.erl 中应该有一个名为“myfunction”的函数,但在这种情况下,当我在 lager.erl 中搜索函数时“调试”我找不到它。
您没有看到提及的原因lager:debug/2是因为 lager 使用了parse transform。因此,在编译代码时,它通过 lagers parse 转换提供,并且调用lager:debug/2替换为对另一个模块函数的另一个调用。
如果您使用正确的解析转换选项编译您的代码,那么该代码将起作用。
“I GIVE CRAP ANSWERS”很好地解释了这种奇怪的行为。我在这里发布了一个代码,该代码应该向您展示在梁文件中生成的代码是什么:
在外壳中:
实用程序:反编译([yourfile.beam])。
%% Author: PCHAPIER
%% Created: 25 mai 2010
-module(utility).
%%
%% Include files
%%
%%
%% Exported Functions
%%
-export([decompile/1, decompdir/1]).
-export([shuffle/1]).
%%
%% API Functions
%%
decompdir(Dir) ->
Cmd = "cd " ++ Dir,
os:cmd(Cmd),
L = os:cmd("dir /B *.beam"),
L1 = re:split(L,"[\t\r\n+]",[{return,list}]),
io:format("decompdir: ~p~n",[L1]),
decompile(L1).
decompile(Beam = [H|_]) when is_integer(H) ->
io:format("decompile: ~p~n",[Beam]),
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam ++ ".beam",[abstract_code]),
{ok,File} = file:open(Beam ++ ".erl",[write]),
io:fwrite(File,"~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]),
file:close(File);
decompile([H|T]) ->
io:format("decompile: ~p~n",[[H|T]]),
decompile(removebeam(H)),
decompile(T);
decompile([]) ->
ok.
shuffle(P) ->
Max = length(P)*10000,
{_,R}= lists:unzip(lists:keysort(1,[{random:uniform(Max),X} || X <- P])),
R.
%%
%% Local Functions
%%
removebeam(L) ->
removebeam1(lists:reverse(L)).
removebeam1([$m,$a,$e,$b,$.|T]) ->
lists:reverse(T);
removebeam1(L) ->
lists:reverse(L).
您在 lager.erl 文件中看不到它,因为它位于 lager.erl 顶部的 lager.hrl 文件中。Erlang 允许您使用 -include("filename.hrl") 指令包含文件。按照惯例,包含文件以 hrl 扩展名结尾,但它实际上可以是任何东西。
https://github.com/basho/lager/blob/master/include/lager.hrl