@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