# Tracing and saving line number of a function call

**URL:** https://discourse.julialang.org/t/tracing-and-saving-line-number-of-a-function-call/27488
**Category:** General Usage
**Created:** [August 13, 2019, 5:43am UTC](https://discourse.julialang.org/t/tracing-and-saving-line-number-of-a-function-call/27488 "2019-08-13T05:43:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![epogrebnyak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/epogrebnyak/32/6287_2.png) [@epogrebnyak](https://discourse.julialang.org/u/epogrebnyak)
#### Post date: [August 13, 2019, 5:43am UTC](https://discourse.julialang.org/t/tracing-and-saving-line-number-of-a-function-call/27488/1 "2019-08-13T05:43:53Z")

</div>

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.

```julia
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

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 13, 2019, 6:49am UTC](https://discourse.julialang.org/t/tracing-and-saving-line-number-of-a-function-call/27488/2 "2019-08-13T06:49:50Z")

</div>

Can’t you use `@ __LINE__ ` and `@ __FILE__ `?

---

<div class="post-metadata">

### Author: ![epogrebnyak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/epogrebnyak/32/6287_2.png) [@epogrebnyak](https://discourse.julialang.org/u/epogrebnyak)
#### Post date: [August 13, 2019, 8:35am UTC](https://discourse.julialang.org/t/tracing-and-saving-line-number-of-a-function-call/27488/3 "2019-08-13T08:35:13Z")

</div>

Thanks for the clue! I probably can use `@ __FILE__ ` and `@ __LINE__ ` if I pass them explicitly into trace function:

```julia
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?

---

<div class="post-metadata">

### Author: ![epogrebnyak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/epogrebnyak/32/6287_2.png) [@epogrebnyak](https://discourse.julialang.org/u/epogrebnyak)
#### Post date: [August 13, 2019, 11:58am UTC](https://discourse.julialang.org/t/tracing-and-saving-line-number-of-a-function-call/27488/4 "2019-08-13T11:58:01Z")

</div>

Here is what I was looking for:

```julia
macro macro_caller_lineno()
    return __source__.line
end

a = @macro_caller_lineno # this will be 5

```

Described in this [commit](https://github.com/JuliaLang/julia/pull/21746/commits/6b05e37815448e351c34b35bb76909f22cae6980) in Julia codebase.
