Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@__LINE__ (c.f @__FILE__) #8066

Closed
chtlp opened this issue Aug 20, 2014 · 10 comments
Closed

@__LINE__ (c.f @__FILE__) #8066

chtlp opened this issue Aug 20, 2014 · 10 comments
Labels
status:help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@chtlp
Copy link

chtlp commented Aug 20, 2014

Hi, I want to get the file and line number (in the same spirit to __file__ and __file__ macro in C) for logging so I can quickly determine where the message was logged. show_backtrace() has this information, but is there a way of getting the fields without parsing the string, so I can use this efficiently for logging?

@tknopp
Copy link
Contributor

tknopp commented Aug 20, 2014

@chtlp Its @__FILE__

Would be great if you could use the julia-user mailing list for these sort of questions. Thanks!

@tknopp tknopp closed this as completed Aug 20, 2014
@pao
Copy link
Member

pao commented Aug 20, 2014

Maling list link: https://groups.google.com/forum/#!forum/julia-users

@chtlp
Copy link
Author

chtlp commented Aug 20, 2014

Thanks! I will ask in the group in the future. Is there a macro for the line number as well? @__LINE__ doesn't seem to work.

@StefanKarpinski
Copy link
Sponsor Member

Unfortunately, @__LINE__ is much harder to implement. We could convert this into a request for it.

@StefanKarpinski StefanKarpinski changed the title how to get __file__ and __line__ for logging? @__LINE__ (c.f @__FILE__) Aug 20, 2014
@ihnorton ihnorton added the status:help wanted Indicates that a maintainer wants help on an issue or pull request label Oct 24, 2014
@IainNZ
Copy link
Member

IainNZ commented Oct 30, 2014

I'm working through FactCheck.jl and it has the following code to get the line:

function getline()
    bt = backtrace()
    issecond = false
    for frame in bt
        lookup = ccall(:jl_lookup_code_address, Any, (Ptr{Void}, Int32), frame, 0)
        if lookup != ()
            if issecond
                return lookup[3]
            else
                issecond = true
            end
        end
    end
end

I thought I'd throw this in here in case someone needs a dirty hack in the mean time...

@mlubin
Copy link
Member

mlubin commented May 13, 2015

Bump. In JuMP we do tons of code generation, and without this macro it's very hard to give useful backtraces and error messages.
Ref jump-dev/JuMP.jl#441

@ihnorton
Copy link
Member

ihnorton commented Aug 4, 2015

Another possible hack:

macro __LINE__()
    return :(unsafe_load(Ptr{Int}(cglobal(:jl_lineno))))
end

@vtjnash
Copy link
Sponsor Member

vtjnash commented Aug 5, 2015

Another possible hack:

that hack assumes macro-expansion is occurring during parsing, which it often doesn't

@simonster
Copy link
Member

@vtjnash doesn't @__FILE__ also assume that?

@vtjnash
Copy link
Sponsor Member

vtjnash commented Aug 5, 2015

no, since @__FILE__ only works from Base.include

but also, yes, since @__FILE__ only works from Base.include

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

10 participants