@LinasBa Thank you so much, now Iβm closer to the goal:
using Logging, LoggingExtras
macro fatal(exs...)
Base.CoreLogging.logmsg_code((Base.CoreLogging.@_sourceinfo)..., :Info, exs...)
end
@fatal "My message"
This was much more complicated than I thought, but this seems to work correctly with all escaping and location information
function maybe_make_kwargs(ex)
if ex isa Expr && ex.head === :(=) ||
ex isa Expr && ex.head === :...
return ex
else
Expr(:(=), ex, esc(ex))
end
end
macro fatal(exs...)
location = LineNumberNode(__source__.line, __source__.file)
level = Base.CoreLogging.LogLevel(1001)
message = esc(exs[1])
kwargs = map(maybe_make_kwargs, exs[2:end])
Expr(:macrocall, Base.CoreLogging.var"@logmsg", location, level, message, kwargs...)
end
Testing:
julia> x = 123;
julia> @fatal "hello" x y = 456
β LogLevel(1001): hello
β x = 123
β y = 456
β @ Main REPL[4]:1
julia> function f()
x = 678
@fatal "hello from f" x y = 321
end;
julia> f()
β LogLevel(1001): hello from f
β x = 678
β y = 321
β @ Main REPL[5]:3
julia> module A
import ..Main: @fatal
a = 111
g() = @fatal "hello from module A $a" a y = 444
end;
julia> A.g()
β LogLevel(1001): hello from module A 111
β a = 111
β y = 444
β @ Main.A REPL[7]:4
Thank you very much. It is working. I would like to add small question. Is it possible to change macro name in console output? LogLevel(1001) β Fatal
Current:
β LogLevel(1001): hello from f
β x = 678
β y = 321
β @ Main REPL[5]:3
Desired:
β Fatal: hello from f
β x = 678
β y = 321
β @ Main REPL[5]:3