# How to use @\_\_FILE\_\_ and @\_\_LINE\_\_ correctly in my own macro?

**URL:** https://discourse.julialang.org/t/how-to-use-file-and-line-correctly-in-my-own-macro/64256
**Category:** General Usage
**Tags:** question
**Created:** [July 8, 2021, 8:25am UTC](https://discourse.julialang.org/t/how-to-use-file-and-line-correctly-in-my-own-macro/64256 "2021-07-08T08:25:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [July 8, 2021, 8:25am UTC](https://discourse.julialang.org/t/how-to-use-file-and-line-correctly-in-my-own-macro/64256/1 "2021-07-08T08:25:05Z")

</div>

Hey all 🙂

I’m writing my own logging package. I want to send strings to two places.

```julia
function log(s::String, file, line)
    to_log = "$(Dates.now(Dates.UTC)) in $file:$line\n$s\n"
    TrinityApi.information(
        to_log,
        "DebugLogs.@log"
    )
    println(to_log)
end

```

And here is my macro.

```julia
macro log(s::String) 
    file = esc(@ __FILE__ )
    line = esc(@ __LINE__ )
    quote
        if IS_LOGGING_ON[]
            log($s, $file, $line)
        end
    end
end

```

**I want to show the line and file in which the macro is called. How would I do that?**

_These are all macros I’m defining (for completeness):_

```julia
for f in (
    :log,
    :log_str
)
    @eval macro $f(s::String) 
        file = esc(@ __FILE__ )
        line = esc(@ __LINE__ )
        quote
            if IS_LOGGING_ON[]
                log($s, $file, $line)
            end
        end
    end
end

macro log(s::Symbol...)
    quote
        if IS_LOGGING_ON[]
            log(
                join(string.($(esc(s))), " "),
                @ __FILE__ , @ __LINE__
            )
        end
    end
end

macro log(ex::Expr)
    s = string(ex)
    quote
        $(esc(ex))
        if IS_LOGGING_ON[]
            log($s, @ __FILE__ , @ __LINE__ )
        end
    end
end

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [July 8, 2021, 8:28am UTC](https://discourse.julialang.org/t/how-to-use-file-and-line-correctly-in-my-own-macro/64256/2 "2021-07-08T08:28:51Z")

</div>

See [Module/file/line for log message generated by a macro](https://discourse.julialang.org/t/module-file-line-for-log-message-generated-by-a-macro/63835)

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [July 8, 2021, 8:31am UTC](https://discourse.julialang.org/t/how-to-use-file-and-line-correctly-in-my-own-macro/64256/3 "2021-07-08T08:31:13Z")

</div>

> [@Impressium](#):
>
> I’m writing my own logging package. I want to send strings to two places.

Are you aware of the [LoggingExtras.jl](https://github.com/JuliaLogging/LoggingExtras.jl) package? In particular, you can use a [`TeeLogger`](https://github.com/JuliaLogging/LoggingExtras.jl#teelogger-demux) for sending to multiple places, and a [`TransformerLogger`](https://github.com/JuliaLogging/LoggingExtras.jl#transformerlogger-transformer) to modify the log message.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [July 8, 2021, 8:33am UTC](https://discourse.julialang.org/t/how-to-use-file-and-line-correctly-in-my-own-macro/64256/4 "2021-07-08T08:33:44Z")

</div>

```julia
module DebugLogs
import TrinityApi
import Dates
export @log, @log_str
const IS_LOGGING_ON = Ref(true)

function log(s::String, file, line)
    to_log = "$(Dates.now(Dates.UTC)) in $file:$line\n$s\n"
    TrinityApi.information(
        to_log,
        "DebugLogs.@log"
    )
    println(to_log)
end

for f in (
    :log,
    :log_str
)
    @eval macro $f(s::String) 
        file = String( __source__.file)
        line = string( __source__.line)
        quote
            if IS_LOGGING_ON[]
                log($s, $file, $line)
            end
        end
    end
end

macro log(s::Symbol...)
    file = String( __source__.file)
    line = string( __source__.line)
    quote
        if IS_LOGGING_ON[]
            log(
                join(string.($(esc(s))), " "),
                $file, $line
            )
        end
    end
end

macro log(ex::Expr)
    s = string(ex)
    file = String( __source__.file)
    line = string( __source__.line)
    quote
        $(esc(ex))
        if IS_LOGGING_ON[]
            log($s, $file, $line)
        end
    end
end

end # module

```

That works for me 🙂

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [July 8, 2021, 8:36am UTC](https://discourse.julialang.org/t/how-to-use-file-and-line-correctly-in-my-own-macro/64256/5 "2021-07-08T08:36:00Z")

</div>

That will be a project for the future me 🙂  
Thank you!
