# Macro to print line number and time of execution (@time + line number)?

**URL:** https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919
**Category:** General Usage
**Tags:** question
**Created:** [July 18, 2017, 9:56am UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919 "2017-07-18T09:56:07Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 9:56am UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/1 "2017-07-18T09:56:07Z")

</div>

`@time` prints useful information about the time it took to evaluate a command. Is there a way to also print on the same line the line number where `@time` is located in a source file?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 18, 2017, 10:13am UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/2 "2017-07-18T10:13:38Z")

</div>

[https://docs.julialang.org/en/latest/stdlib/file/#Base.@\_\_LINE\_\_](https://docs.julialang.org/en/latest/stdlib/file/#Base.@ __LINE__ )

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 10:27am UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/3 "2017-07-18T10:27:59Z")

</div>

Thanks. I knew that. I meant if there is a way to _combine_ the output of `@time` with the line number, in a single printed line.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 18, 2017, 10:36am UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/4 "2017-07-18T10:36:35Z")

</div>

by writing a macro that prints `@ __LINE__ ` and then runs `@time`?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 10:43am UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/5 "2017-07-18T10:43:04Z")

</div>

I don’t know how to write macros.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 18, 2017, 11:12am UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/6 "2017-07-18T11:12:18Z")

</div>

This one should be a simple macro, something like

```julia
macro timeonline(expr)
    quote
        println("was executed on line $(@ __LINE__ )")
        @time $(esc(expr))
    end
end

```

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 12:01pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/7 "2017-07-18T12:01:29Z")

</div>

Thank you! Just one more thing. Is there a way to print both the time and line number _on the same printed line_? Something like:

> line 123: 3.552151 seconds (4.32 M allocations: 185.869 MiB, 25.28% gc time)

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [July 18, 2017, 12:07pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/8 "2017-07-18T12:07:10Z")

</div>

Have you tried `print` instead of `println` ?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 12:15pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/9 "2017-07-18T12:15:00Z")

</div>

Yes, that works. Thanks.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 5:34pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/10 "2017-07-18T17:34:05Z")

</div>

After some more testing, I realized that this is actually printing the line number of the macro, not the line number where the macro is called. Can we fix this?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 18, 2017, 5:47pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/11 "2017-07-18T17:47:19Z")

</div>

Sorry, I overlooked that. That is not very easy; the only trick I know it is [here](https://github.com/timholy/DebuggingUtilities.jl/blob/master/src/DebuggingUtilities.jl#L117).

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 18, 2017, 5:49pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/12 "2017-07-18T17:49:35Z")

</div>

` __source__ ` on 0.7/master

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 5:57pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/13 "2017-07-18T17:57:56Z")

</div>

Where is it defined? Can I use it in 0.6?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 6:12pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/14 "2017-07-18T18:12:33Z")

</div>

I don’t fully understand this, but can one force the `@ __LINE__ ` to expand at the call-site instead of within the macro?

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [July 19, 2017, 12:17pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/15 "2017-07-19T12:17:54Z")

</div>

See also [DebuggingUtilities.jl](https://github.com/timholy/DebuggingUtilities.jl)

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [July 19, 2017, 4:10pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/16 "2017-07-19T16:10:10Z")

</div>

With help from the Gitter community, we came up with a workaround (back for v0.4) for our debugging and logging macros that has worked well (overhead of using backtrace would have been too large).  
The first argument to our macros is an anonymous function, and by looking inside it (it’s never even compiled or executed), we are able to extract the line and file number for where the macro is called (we also pick out a status code from the anonymous function, for convenience sake)  
For example:  
`@error(()->ERR_INVALID_INPUT, "logged error message")`  
I was hoping that v0.6 would have some way of directly getting the line number / file name directly in the macro, but this has done the job for us, and is not too ugly).

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [May 29, 2019, 7:57pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/17 "2019-05-29T19:57:08Z")

</div>

Sorry to revive this. I tried to do this (now on Julia 1.1):

```julia
macro ltime(expr)
    quote
        print("line ", __source__.line, ": ")
        @time $(esc(expr))
    end
end

```

but when I call this macro I get

> UndefVarError: ` __source__ ` not defined.

What is the correct way to use ` __source__ ` here?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [May 29, 2019, 8:35pm UTC](https://discourse.julialang.org/t/macro-to-print-line-number-and-time-of-execution-time-line-number/4919/18 "2019-05-29T20:35:43Z")

</div>

Okay I figured that the following works fine:

```julia
macro ltime(expr)
    quote
        print("line ", $( __source__.line), ": ")
        @time $(esc(expr))
    end
end

```

Based on @Tamas_Papp answer (thanks!).
