# How to print function name and source file/line number

**URL:** https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486
**Category:** General Usage
**Tags:** question, debug, metaprogramming
**Created:** [July 22, 2020, 12:12pm UTC](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486 "2020-07-22T12:12:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 22, 2020, 12:12pm UTC](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486/1 "2020-07-22T12:12:43Z")

</div>

Hello, is it possible to print the current function name and the REPL/Juno/VSCode clickable file name/function name, e.g.

```julia
function foo()
    @codeLocation
    ...
end

```

Resulting in:

```julia
Running function foo on /path/to/myfile.jl:123

```

I am aware of ` __source__.file` and ` __source__.line`, but I coudn’t get them working in the way I described and the function @shown in [DebuggingUtilities](https://github.com/timholy/DebuggingUtilities.jl) seems to go a bit too far showing the whole stack.

EDIT:

I did manage to get the file name/ line but not yet the function name. Also I need to call the macro followed with an empty expression, and that’ts ugly:

```julia
macro codeLocation(expr)
    return quote
        print("Running function at ",$("$( __source__.file)"),":",$("$( __source__.line)"))
    end
end

function foo()
    @codeLocation " "
end

foo()

```

Resulting in :

```julia
Running function at /home/lobianco/.julia/dev/BetaML/src/temp.jl:14

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [July 22, 2020, 12:36pm UTC](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486/2 "2020-07-22T12:36:14Z")

</div>

> [@sylvaticus](#):
>
> Also I need to call the macro followed with an empty expression, and that’tsugly:

Just don’t require any argument, no?

```julia
macro codeLocation()

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 22, 2020, 12:41pm UTC](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486/3 "2020-07-22T12:41:33Z")

</div>

ahh… sure… thanks… I am now left with retrieving the function name… I did found ` __module__ ` for the modulo name, but not yet the function name…

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 22, 2020, 1:21pm UTC](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486/4 "2020-07-22T13:21:21Z")

</div>

done it 🙂

```julia
macro codeLocation()
    return quote
        st = stacktrace(backtrace())
        myf = ""
        for frm in st
            funcname = frm.func
            if frm.func != :backtrace && frm.func!= Symbol("macro expansion")
                myf = frm.func
                break
            end
        end
        println("Running function ", $("$( __module__ )"),".$(myf) at ",$("$( __source__.file)"),":",$("$( __source__.line)"))
    end
end

function foo()
    @codeLocation
end

```

(taking code from DebuggingUtils)

```julia
julia> foo()
Running function Main.foo at /home/lobianco/.julia/dev/BetaML/src/temp.jl:18

```
