[erlang-questions] Why no ?FUNCTION macro

G Bulmer gbulmer@REDACTED
Fri Sep 21 18:02:31 CEST 2007


Adam

I'm just a newbie, so here's my solution:
-module(funnames).
-compile(export_all).

% -define(FUN, helper(process_info(self(),current_function))).
-define(FUN, case process_info(self(), current_function) of {_,  
{_,F,_}} -> F end).

toyfun() ->
     io:format("starting a function blah, blah, ...~n"),
     io:format("replacing ?FUN, and getting ~p~n", [?FUN]),
     io:format("finishing the function phew!~n").

% just a helper function while I was playing with formatting - it  
isn't used in this version of the code
helper({ current_function, {Mname, Fname, Arrity}}) ->
     atom_to_list(Mname) ++ ":" ++ atom_to_list(Fname) ++ "/" ++  
integer_to_list(Arrity).

I can't think of a neater way to extract the function name from the  
return value of process_info than the case ... end, but
hopefully, someone will make it even better.
Garry


> Mats and Garry,
>
> Actually that was exactly what I was looking for, I only had it in  
> mind like this (felt kind off natural):
>
> error_logger:error_report([{module, ?MODULE}, {function, ? 
> FUNCTION}, {error, Error}]).
>
> However, what you propose works well enough for my purposes!
>
> Thanks,
> Adam
>
> On 9/21/07, mats cronqvist < mats.cronqvist@REDACTED> wrote:On  
> Thu, 2007-09-20 at 15:35 +0100, G Bulmer wrote:
> [...]
> > As I said, this is only curiosity, but if the need is a run-time
> > name, maybe an ingenious function to dig through the stack trace
> > returned by erlang:get_stacktrace() would be workable.
>
>   in that case, i think something like this is a lot simpler;
>
> -define(LOG(T),error_logger:info_report([process_info(self 
> (),current_function)|T])).
>
>   e.g.
> go() -> ?LOG([{xxx,yyy}]).
>
>   will print
>
> =INFO REPORT==== 21-Sep-2007::11:08:38 ===
>     current_function: {foo,go,0}
>     xxx: yyy
>
>   mats
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>

Garry Bulmer

email: gbulmer@REDACTED
Home: +44 (0)24 7667 9497
Mobile: +44 (0)7726 880058






More information about the erlang-questions mailing list