I’m trying to trace the filename and line of a function call to use it later in the program.
Using stacktrace()
in code below, but my goal is revealing line 6 number, and so far I got to knowing line 3.
function trace()
# do something
stacktrace() # this is line 3
end
a = trace() # this is line 6
for x in a
println(x)
end
#trace() at trace.jl:3
#top-level scope at none:0
Can’t you use @__LINE__
and @__FILE__
?
3 Likes
Thanks for the clue! I probably can use @__FILE__
and @__LINE__
if I pass them explicitly into trace function:
struct Location
file:: String
line:: Integer
end
function calledfrom(file, line)
Location(file, line)
end
x = calledfrom(@__FILE__, @__LINE__)
Originally, I wanted a calledfrom()
to work without arguments. Do you think it is possible?
Here is what I was looking for:
macro macro_caller_lineno()
return __source__.line
end
a = @macro_caller_lineno # this will be 5
Described in this commit in Julia codebase.