all repos — min @ 72c2fd8462452e4991fe46149a305b665485f2b3

A small but practical concatenative programming language.

Added new StyledConsoleLogger.
h3rald h3rald@h3rald.com
Wed, 09 Nov 2016 16:19:59 +0100
commit

72c2fd8462452e4991fe46149a305b665485f2b3

parent

a67d67c39ccfdcb125ccaa97e750c20de733ac11

3 files changed, 50 insertions(+), 6 deletions(-)

jump to
M core/interpreter.nimcore/interpreter.nim

@@ -26,7 +26,7 @@ s = s & $item & " "

return s proc debug*(i: In, value: MinValue) = - debug(i.dump & $value) + debug("{" & i.dump & $value & "}") proc debug*(i: In, value: string) = debug(value)

@@ -81,15 +81,15 @@ return "$1($2,$3) [$4]: $5" % [sym.filename, $sym.line, $sym.column, sym.symVal, message]

proc formatTrace(sym: MinValue): string = if sym.filename.isNil or sym.filename == "": - return " - <native> in symbol: $1" % [sym.symVal] + return " <native> in symbol: $1" % [sym.symVal] else: - return " - $1($2,$3) in symbol: $4" % [sym.filename, $sym.line, $sym.column, sym.symVal] + return " $1($2,$3) in symbol: $4" % [sym.filename, $sym.line, $sym.column, sym.symVal] proc stackTrace(i: In) = var trace = i.trace trace.reverse() for sym in trace: - info sym.formatTrace + notice sym.formatTrace proc error(i: In, message: string) = error(i.currSym.formatError(message))

@@ -122,6 +122,7 @@ i.apply(op, scope)

proc push*(i: In, val: MinValue) = if val.kind == minSymbol: + i.debug(val) i.trace.add val if not i.evaluating: i.currSym = val
M core/utils.nimcore/utils.nim

@@ -1,7 +1,8 @@

import strutils, critbits, - logging + logging, + terminal import parser, value,

@@ -9,6 +10,48 @@ scope,

interpreter # Library methods + +system.addQuitProc(resetAttributes) + +type + StyledConsoleLogger* = ref object of Logger + +proc logPrefix*(level: Level): tuple[msg: string, color: ForegroundColor] = + case level: + of lvlDebug: + return ("---", fgCyan) + of lvlInfo: + return ("(i) ", fgCyan) + of lvlNotice: + return ("---", fgMagenta) + of lvlWarn: + return ("(!)", fgYellow) + of lvlError: + return ("(!)", fgRed) + of lvlFatal: + return ("(x)", fgRed) + else: + return ("", fgWhite) + +method log*(logger: StyledConsoleLogger; 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) + #resetAttributes() + f.write(ln) + f.write("\n") + resetAttributes() + if level in {lvlError, lvlFatal}: flushFile(f) + +proc newStyledConsoleLogger*(levelThreshold = lvlAll; fmtStr = " "): StyledConsoleLogger = + new result + result.fmtStr = fmtStr + result.levelThreshold = levelThreshold proc logLevel*(val: var string): string {.discardable.} = var lvl: Level
M minim.nimminim.nim

@@ -38,7 +38,7 @@ min_lang

const PRELUDE* = "prelude.min".slurp.strip -newConsoleLogger().addHandler() +newStyledConsoleLogger().addHandler() newRollingFileLogger(MINIMLOG, fmtStr = verboseFmtStr).addHandler() proc getExecs(): seq[string] =