[erlang-questions] How to get the line number of current executable code?

Ulf Wiger ulf@REDACTED
Sun Aug 17 11:19:44 CEST 2008


I should perhaps bring special notation to the smiley at the
end of my message. (:

Whether or not the solution is cool is surely a matter of
taste - but I believe that the one who first came up with it
was Mats Cronqvist, the champion of obfuscated Erlang
code. (:

I'm not sure about *very* expensive. The exception mechanism
isn't that heavyweight. At least in the past, there were issues
with HiPE, in that exceptions in native code would generate an
empty stack trace. I vaguely recall that being fixed now.

I agree that a compile-time constant would be much preferred.

BR,
Ulf W

2008/8/16 Edwin Fine <erlang-questions_efine@REDACTED>:
> Ulf,
>
> On second thoughts, although that IS very cool, it would likely be very
> expensive because it generates and catches an exception. This would matter
> if it is used to do a lot of logging (which is what I would use it for).
> What I really would like is a compile-time constant (pre-processor macro). I
> suppose one could hack epp...?
>
> Ed
>
> On Fri, Aug 15, 2008 at 5:06 PM, Ulf Wiger <ulf@REDACTED> wrote:
>>
>> Why sure, you just do this:
>>
>> -module(m).
>> -export([f/0]).
>>
>> -define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).
>>
>> f() ->
>>  {current_function, ?FUNCTION}.
>>
>>
>>
>> Eshell V5.5.4  (abort with ^G)
>> 1> c(m).
>> {ok,m}
>> 2> m:f().
>> {current_function,{m,f,0}}
>> 3>
>>
>> BR,
>> Ulf W  ;-)
>>
>> 2008/8/15 Edwin Fine <erlang-questions_efine@REDACTED>:
>> > Something I've wished for in numerous languages over the years is a
>> > macro
>> > that expands to the current function, something like ?MODULE. I don't
>> > suppose there is one lurking somewhere in Erlang...?
>> >
>> > 2008/8/15 Matt Williamson <dawsdesign@REDACTED>
>> >>
>> >> You should trust the macro. It must use a similar method to Python's
>> >> because they are both compiled to bytecode and thus there wouldn't
>> >> *really*
>> >> be line numbers in either one.
>> >>
>> >> 2008/8/15 devdoer bird <devdoer2@REDACTED>
>> >>>
>> >>> Thanks.
>> >>>
>> >>> How can I do this without macro? I know python supply some tools to
>> >>> determine the line number in run time,like inspect module.
>> >>>
>> >>>
>> >>> 2008/8/16, Anders Nygren <anders.nygren@REDACTED>:
>> >>>>
>> >>>> 2008/8/15 devdoer bird <devdoer2@REDACTED>:
>> >>>> > HI:
>> >>>> >
>> >>>> > I want to implement a function like "get_current_lineno()/0" to get
>> >>>> > the
>> >>>> > current line number of the calling point?
>> >>>> > Eg.
>> >>>> > .....
>> >>>> > ....
>> >>>> > io:format("current line is ~w\n",[get_current_lineno()])
>> >>>> > .....
>> >>>> >
>> >>>> > the above code will print the line number of the calling point in
>> >>>> > the
>> >>>> > source
>> >>>> > file.
>> >>>> >
>> >>>> > How can I do this  in erlang?
>> >>>>
>> >>>> There is a predefined macro ?LINE that does that
>> >>>> so
>> >>>> io:format("current line is ~w\n",[?LINE])
>> >>>>
>> >>>> /Anders
>> >>>>
>> >>>> > _______________________________________________
>> >>>> > erlang-questions mailing list
>> >>>> > erlang-questions@REDACTED
>> >>>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>> >>>> >
>> >>>
>> >>>
>> >>> _______________________________________________
>> >>> erlang-questions mailing list
>> >>> erlang-questions@REDACTED
>> >>> http://www.erlang.org/mailman/listinfo/erlang-questions
>> >>
>> >>
>> >> _______________________________________________
>> >> erlang-questions mailing list
>> >> erlang-questions@REDACTED
>> >> http://www.erlang.org/mailman/listinfo/erlang-questions
>> >
>> >
>> >
>> > --
>> > For every expert there is an equal and opposite expert - Arthur C.
>> > Clarke
>> >
>> > _______________________________________________
>> > erlang-questions mailing list
>> > erlang-questions@REDACTED
>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>> >
>>
>
>
>
> --
> For every expert there is an equal and opposite expert - Arthur C. Clarke
>



More information about the erlang-questions mailing list