Skip to content

Instantly share code, notes, and snippets.

@Yardanico Yardanico/simple_log.nim
Last active Feb 3, 2019

Embed
What would you like to do?
import strutils, times, terminal, macros
export strutils.format
type
LogLevel* = enum
lvlDebug = "DEBUG"
lvlInfo = "INFO"
lvlNotice = "NOTICE"
lvlWarn = "WARN"
lvlError = "ERROR"
lvlFatal = "FATAL"
# Logging level, can be changed
var logLevel* = lvlInfo
proc getColor(lvl: LogLevel): ForegroundColor =
## Gets color for the specified logging level
case lvl
of lvlDebug: fgWhite
of lvlInfo: fgGreen
of lvlNotice: fgCyan
of lvlWarn: fgYellow
of lvlError, lvlFatal: fgRed
proc log*(lvl: LogLevel, line: string) =
## Logs message with specified log level to the stdout
# Only print messages with specified log level or higher
if lvl < logLevel: return
setForegroundColor(stdout, getColor(lvl))
stdout.write("$1 | $2 | $3\n" % [$now(), $lvl, line])
resetAttributes()
macro getFormatted(data: varargs[untyped]): untyped =
## Returns a strutils.format call for code like
## getFormatted("Test", a = 1, b = "hello")
result = newTree(nnkCall)
result.add(newIdentNode("format"))
var fmtString = ""
var i = 1
# List of arguments to a `format` call
var tmp = newSeq[NimNode]()
for arg in data:
# Literal string
if arg.kind == nnkStrLit:
fmtString &= arg.strVal
# a = b
elif arg.kind == nnkExprEqExpr:
fmtString &= ", " & arg[0].strVal & " = $" & $i
tmp.add(arg[1])
inc(i)
result.add(newLit(fmtString))
# Add nodes which will be used as arguments
for node in tmp:
result.add(node)
template genTemplate(name: untyped, lvl: untyped): untyped =
template name*(data: varargs[untyped]): untyped =
log(lvl, getFormatted(data))
# Generate templates for all log levels
genTemplate(logDebug, lvlDebug)
genTemplate(logInfo, lvlInfo)
genTemplate(logNotice, lvlNotice)
genTemplate(logWarn, lvlWarn)
genTemplate(logError, lvlError)
genTemplate(logFatal, lvlFatal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.