all repos — min @ 84576c80948b1beb5860954a1023ac5fdb12ee9c

A small but practical concatenative programming language.

Fixes.
h3rald h3rald@h3rald.com
Tue, 15 Aug 2023 13:00:28 +0000
commit

84576c80948b1beb5860954a1023ac5fdb12ee9c

parent

fa29a21dd2fddd4fd2ffd34012adc7aa5522a435

1 files changed, 73 insertions(+), 0 deletions(-)

jump to
A minpkg/core/niftylogger.nim

@@ -0,0 +1,73 @@

+import + logging, + strutils, + terminal, + std/exitprocs + +if isatty(stdin): + addExitProc(resetAttributes) + +type + NiftyLogger* = ref object of Logger + +proc logPrefix*(level: Level): tuple[msg: string, color: ForegroundColor] = + case level: + of lvlDebug: + return ("---", fgMagenta) + of lvlInfo: + return ("(i)", fgCyan) + of lvlNotice: + return (" ", fgWhite) + of lvlWarn: + return ("(!)", fgYellow) + of lvlError: + return ("(!)", fgRed) + of lvlFatal: + return ("(x)", fgRed) + else: + return (" ", fgWhite) + +method log*(logger: NiftyLogger; level: Level; args: varargs[string, `$`]) = + var f = stdout + if level >= getLogFilter() and level >= logger.levelThreshold: + if level >= lvlWarn: + f = stderr + let ln = substituteLog(logger.fmtStr, level, args) + let prefix = level.logPrefix() + f.setForegroundColor(prefix.color) + f.write(prefix.msg) + f.write(ln) + resetAttributes() + f.write("\n") + if level in {lvlError, lvlFatal}: flushFile(f) + +proc newNiftyLogger*(levelThreshold = lvlAll; fmtStr = " "): NiftyLogger = + new result + result.fmtStr = fmtStr + result.levelThreshold = levelThreshold + +proc getLogLevel*(): string = + return LevelNames[getLogFilter()].toLowerAscii + +proc setLogLevel*(val: var string): string {.discardable.} = + var lvl: Level + case val: + of "debug": + lvl = lvlDebug + of "info": + lvl = lvlInfo + of "notice": + lvl = lvlNotice + of "warn": + lvl = lvlWarn + of "error": + lvl = lvlError + of "fatal": + lvl = lvlFatal + of "none": + lvl = lvlNone + else: + val = "warn" + lvl = lvlWarn + setLogFilter(lvl) + return val