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

Thomas Lindgren thomasl_erlang@REDACTED
Sun Aug 17 12:01:32 CEST 2008




--- On Sat, 8/16/08, Edwin Fine <erlang-questions_efine@REDACTED> wrote:


> 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...?

The basic problem is that epp works on the token level, not the syntax tree level, so when you find ?FUNCTION there is no information about what function you're in. (And figuring it out seems like a thankless job.) 

One possible hack: expand ?FUNCTION into something that can be recognized and replaced by a later pass. For instance:

1. In epp, replace ?FUNCTION by the syntax tree or tokens of a call to a made-up function, "erlang:current_function()", or some other unique well-known marker.

2. In sys_pre_expand, walk the syntax tree for each function M:F/N, and replace all occurrences of "erlang:current_function()" with {M, F, N}. (As part of expanding away records, etc.)

Occurrences of ?FUNCTION outside of functions will have to be handled too, e.g. by an error.

Best,
Thomas



      



More information about the erlang-questions mailing list