all repos — litestore @ 25abe450a10ddeb023c5883278d0223306ca6fb2

A minimalist nosql document store.

src/litestorepkg/lib/logger.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
import 
  strutils,
  times

import
  types

proc currentTime*(plain = false): string =
  if plain:
    return getTime().utc.format("yyyy-MM-dd' @ 'HH:mm:ss")
  else:
    return getTime().utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'")

proc msg(logger: Logger, kind, message: string, params: varargs[string, `$`]) =
  let s = format(message, params)
  if kind == "WARNING":
    stderr.writeLine(currentTime(true) & " " & kind & ": " & s)
  else:
    echo currentTime(true), " ", kind, ": ", s

proc error*(logger: Logger, message: string, params: varargs[string, `$`]) = 
  if logger.level <= lvError:
    logger.msg("  ERROR", message, params)

proc warn*(logger: Logger, message: string, params: varargs[string, `$`]) = 
  if logger.level <= lvWarn:
    logger.msg("WARNING", message, params)

proc info*(logger: Logger, message: string, params: varargs[string, `$`]) = 
  if logger.level <= lvInfo:
    logger.msg("   INFO", message, params)

proc  debug*(logger: Logger, message: string, params: varargs[string, `$`]) = 
  if logger.level <= lvDebug:
    logger.msg("  DEBUG", message, params)

var LOG* {.threadvar.}: Logger

LOG.level = lvWarn