Just for sharing. Sometimes you dont know where an echo cometh from (a bottomless pit maybe :-) )
Below code can help with that.
Maybe somebody has a macro that extracts the location-info automatically?
Untill now i havent done macros because i am too busy programming..
# logging with file- and proc-location
var
talkbo: bool = false
locst = "my_module_"
template talk(messagest: string) =
#[
- replacement for echo that is only evaluated when talkbo = true
- you can set talkbo in the function to take precedence over the module-value
- you can append the proc-name to the module-name via procst
]#
if talkbo:
echo locst, ": ", messagest
proc letsTalk() =
talkbo = true
locst &= "letsTalk"
talk("talking..")
letsTalk()
# should return:
# my_module_letsTalk: talking..
You could do something like this :
template trace*(words: varargs[typed, `$`]) =
echo getStackTrace()
echo "=> ", words
proc main() =
trace "Hello, world !"
This will only work if you have the stacktrace in your build (see https://nim-lang.org/docs/nimc.html#additional-features for more info - otherwise -d:debug does the trick)
@clonk
That looks promising! I saw it works. A little pruning would be nice like:
module_proc_linenr: message
But I am gone look into that. Thanks.
The following template works for non-release-compilation, but no special flags are needed apparently.
You can add a flag-variable wispbo (in my suffix-lingo) or customize the style. Also i dont know if the basename-split-off is cross-platform? Maybe use Path and offical approach instead..
Thanks for tips!
template wisp*(wordsq: varargs[string, `$`]) =
# works only for non-release-compilation; thats ok
var
filepathst, filenamest, modulest, procnamest: string
pathsq: seq[string]
let tob = getStackTraceEntries() # a proc from the system-module
if tob.len > 0: # needed for release-compilation when it is empty?
filepathst = $tob[tob.len - 1].filename
pathsq = filepathst.split("/")
filenamest = pathsq[pathsq.len - 1]
modulest = filenamest[0..filenamest.len - 5]
procnamest = $tob[tob.len - 1].procname
echo "==> ", modulest, "_", procnamest, " echos: ", wordsq