Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger: add withLevel template #849

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/logger.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import std/logging
import "."/cli

let consoleLogger = newConsoleLogger(levelThreshold = lvlNotice,
fmtStr = "")

func toLevel(verbosity: Verbosity): Level =
case verbosity
of verQuiet: lvlNone
of verNormal: lvlNotice
of verDetailed: lvlInfo

proc setLevel(verbosity: Verbosity) =
consoleLogger.levelThreshold = toLevel(verbosity)

proc setupLogging*(verbosity: Verbosity) =
let consoleLogger = newConsoleLogger(levelThreshold = toLevel(verbosity),
fmtStr = "")
addHandler(consoleLogger)
setLevel(verbosity)

template withLevel*(verbosity: Verbosity, body: untyped): untyped =
let currentLevel = consoleLogger.levelThreshold
consoleLogger.levelThreshold = toLevel(verbosity)
body
consoleLogger.levelThreshold = currentLevel

template logNormal*(msgs: varargs[string]) =
notice(msgs)
Expand Down