Nim allows getting a line number from a template (via compile-time information). So you can make a log template to do that:
import logging, strutils
var logger = newConsoleLogger(fmtStr = "[$time][$levelid]")
addHandler(logger)
template log*(lvl: Level, data: string): untyped =
let pos {.compiletime.} = instantiationInfo()
const
addition =
when defined(release):
"[$1] " % [pos.filename]
else:
"[$1:$2] " % [pos.filename, $pos.line]
logger.log(lvl, addition & data)
template log*(data: string): untyped = log(lvlInfo, data)
log("hi world")
If compiled in non-release mode, it will contain the line number & filename. If compiled in release mode - only the filename, but this can be easily changed.Gotta say, that is pretty slick.
I love how easy it is in Nim to get info such as line numbers and stack traces, checking if variables are defined or exist and stuff like that.
I'll definitely be using this. Maybe there's room for it in the logging.nim module?