diff --git a/cliutils.nim b/cliutils.nim index 479241e..2141b39 100644 --- a/cliutils.nim +++ b/cliutils.nim @@ -1,13 +1,15 @@ -import nre, terminal, strtabs, tables, unicode +import std/[nre, terminal, strtabs, tables, unicode] import strutils except toUpper, toLower import ./cliutils/config import ./cliutils/daemonize import ./cliutils/procutil +import ./cliutils/queue_logger export config export daemonize export procutil +export queue_logger type TermColor = ForegroundColor or BackgroundColor diff --git a/cliutils.nimble b/cliutils.nimble index 42b31a3..8c5d206 100644 --- a/cliutils.nimble +++ b/cliutils.nimble @@ -1,6 +1,6 @@ # Package -version = "0.8.2" +version = "0.9.0" author = "Jonathan Bernard" description = "Helper functions for writing command line interfaces." license = "MIT" diff --git a/cliutils/queue_logger.nim b/cliutils/queue_logger.nim new file mode 100644 index 0000000..2d98cf0 --- /dev/null +++ b/cliutils/queue_logger.nim @@ -0,0 +1,25 @@ +import std/[logging] + +type + QueuedLogMessage* = tuple[lvl: Level, msg: string] + + QueueLogger* = ref object of Logger + queue: seq[QueuedLogMessage] + +proc newQueueLogger*( + levelThreshold = lvlAll, + fmtStr = logging.defaultFmtStr): QueueLogger = + + new result + result.fmtStr = fmtStr + result.levelThreshold = levelThreshold + result.queue = newSeq[QueuedLogMessage]() + +method log*(logger: QueueLogger, level: Level, args: varargs[string, `$`]) = + if level >= logger.levelThreshold: + logger.queue.add((level, substituteLog(logger.fmtStr, level, args))) + +proc clearQueue*(logger: QueueLogger) = + logger.queue = newSeq[QueuedLogMessage]() + +func messages*(logger: QueueLogger): seq[QueuedLogMessage] = logger.queue