Calling a macro inside of a macro

Hello

I am finding myself debugging at large Julia codebase and for some reason I still debug like a 2nd year undergraduate. For that reason I would like to make a macro that looks some thing like this:

macro pp(input)
    :(println("File: ", @__FILE__ , "Line: ", @__LINE__, " ", <variable name of input>, " ", <actual value of input>))
end

The idea is that I can compare variables across multiple files quickly and find their location, but I cannot get it to work probably as the @__FILE __ and @__LINE __ macros refer to the file and line of the macro definition. (A kind of related question: how to get the variable name of the input of the macro?) A minimal working example could be:

macro pp(input)
    :(println("Line: ", @__LINE__, "Name: ",  " <name of input>  " , $input))
end

a = 1; @pp a

Which returns:

Line: 2 Name: <name of input>  1

Does anyone know a good approach for these kinds of macros, I find my self trying every kind of combination of esc(), $ and : to get it to work.

Kind regards

See Module/file/line for log message generated by a macro and Use @__LINE__ etc in another macro definition (although that is not specifically about “calling macro from another macro”, but it does solve your problem).

2 Likes

What a Champ!

I got:

macro pp(input)
    file = basename(String(__source__.file))
    line = __source__.line
    quote
        println("File: ", $file, "      Line: ", $line, "      Name: ", $(Meta.quot(input)) , "      Value: ", $esc(input))
    end
end

Which gives:

a = 1; @pp a

File: print_macro.jl      Line: 9      Name: a      Value: 1