I am trying to write a macro for debug print in the Nim language.
Currently this macro adds filename
andline
to the output by instantiationInfo()
.
import macros
macro debugPrint(msg: untyped): typed =
result = quote do:
let pos = instantiationInfo()
echo pos.filename, ":", pos.line, ": ", `msg`
proc hello() =
debugPrint "foo bar"
hello()
currently output:
debug_print.nim:9: foo bar
I would like to add the name of the procedure (or iterator) of the place where the macro was called.
desired output:
debug_print.nim:9(proc hello): foo bar
How can I get the name of procedure (or iterator) in Nim, like __func__
in C?
proc bar_baz()
I got:bar_baz_9c8JPzPvtM9azO6OB23bjc3Q_3
(NOTE the trailing _3 when multiple functions are are in a module, and the leading_
if function name contains underscore) – Triable