Sepehr Samini
Sepehr Samini

Reputation: 1045

Erlang function does not exist in module?

In lager.elr (the main module of https://github.com/basho/lager) there is no function with name "debug" but I have an application that call debug function from lager module like: lager:debug(Str, Args)

I am beginner in Erlang but I know when we call a function from a module lile "mymodule:myfunction" there should be a function with name "myfunction" in file mymodule.erl but in this case when I search in lager.erl for function "debug" I can't find it.

Upvotes: 5

Views: 1830

Answers (3)

Pascal
Pascal

Reputation: 14042

"I GIVE CRAP ANSWERS" gave a good explanation of this strange behaviour. I post here a code that should show you what was the code generated in the beam file:

In the shell:

utility:decompile([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).

Upvotes: 1

I GIVE CRAP ANSWERS
I GIVE CRAP ANSWERS

Reputation: 18849

The reason you don't see a mention of lager:debug/2 is because lager uses a parse transform. So when compiling the code, it is fed through lagers parse transform and the call to lager:debug/2 is substituted for another call to another module function.

If you compile your code with the correct parse transform option for lager, then the code would work.

Upvotes: 6

Jeremy Wall
Jeremy Wall

Reputation: 25237

You don't see it in the lager.erl file because it's in the lager.hrl file that's included at the top of lager.erl. Erlang allows you to include a file with the -include("filename.hrl") directive. As a convention the include files end in an hrl extension but it could really be anything.

https://github.com/basho/lager/blob/master/include/lager.hrl

Upvotes: 0

Related Questions