make patterns runtime only

This commit is contained in:
Dmitriy Ryajov 2019-09-08 11:37:52 -06:00
parent 93a9fd203a
commit 116051208a
1 changed files with 35 additions and 28 deletions

View File

@ -7,64 +7,71 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
## Small debug module that's enabled through a ``-d:debugout`` ## Small debug module that's only runs in non release builds.
## flag. It's inspired by the Nodejs ``debug`` module that ## It's inspired by the Nodejs ``debug`` module which allows printing
## allows printing colorized output based on patterns. This ## colorized output based on matched patterns. This module is powered
## module is powered by the standard ``nre`` module and as such ## by the standard ``nre`` module and as such it supports the same set
## it supports the same set of regexp expressions. ## of regexp expressions.
## ##
## To enable debug output, pass the ``debugout`` flag during build ## The output is controled through two environment variables ``DEBUG``
## time with ``-d`` flag. In addition, the debugout flag takes a comma ## and ``DEBUG_COLOR``. By default, it will print everything to stderr
## separated list of patters that will narow the debug output to ## but if only some output is of interested, ``DEBUG`` accepts a coma
## only those matched by the patterns. By default however, all ## separated list of regexp expressions to match the output against.
## debug statements are outputed to the stderr. ## Each match gets assigned a different color to help differentiate
## the output lines.
##
## Colorization is done with 256 ANSI colors, and each run gets a random
## color, if more than one match is specified, each one gets a random color
## to disable this behavior use ``DEBUG_COLOR`` with a valid ANSI color code.
## This will also disable individual colorization for each matched line.
##
when defined(debugout) and not defined(release): when not defined(release):
import random, import random,
tables, tables,
nre, nre,
strutils, strutils,
times, times,
terminal, terminal,
sequtils sequtils,
os
const debugout {.strdefine.}: string = ".*"
type type
Match = object Match = object
pattern: Regex pattern: Regex
color: string color: string
if isTrueColorSupported(): # if isTrueColorSupported():
system.addQuitProc(resetAttributes) # system.addQuitProc(resetAttributes)
enableTrueColors() # enableTrueColors()
var context {.threadvar.} : OrderedTableRef[string, Match] var context {.threadvar.} : OrderedTableRef[string, Match]
proc initDebugCtx() = proc initDebugCtx() =
let debugout = getEnv("DEBUG", ".*")
let debugColor = getEnv("DEBUG_COLOR")
let isDebugColor = debugColor.len > 0
context = newOrderedTable[string, Match]() context = newOrderedTable[string, Match]()
var patrns = @[".*"] var patrns: seq[string] = @[]
if debugout != "true": patrns = debugout.split(re"[,\s]").filterIt(it.len > 0)
patrns = debugout.split(re"[,\s]").filterIt(it.len > 0)
randomize() randomize()
for p in patrns: for p in patrns:
context[p] = Match(pattern: re(p), color: $rand(0..272)) # 256 ansi colors context[p] = Match(pattern: re(p), color: if isDebugColor: debugColor else: $rand(0..272)) # 256 ansi colors
proc doDebug(data: string): void {.gcsafe.} = proc doDebug(data: string, line: string): void {.gcsafe.} =
if isNil(context): if isNil(context):
initDebugCtx() initDebugCtx()
for m in context.values: for m in context.values:
if data.match(m.pattern).isSome: if data.contains(m.pattern):
stderr.writeLine("\u001b[38;5;" & m.color & "m " & alignLeft(data, 4) & "\e[0m") stderr.writeLine("\u001b[38;5;" & m.color & "m " & alignLeft(line & data, 4) & "\e[0m")
return return
template debug*(data: string) = template debug*(data: string) =
let module = instantiationInfo() let module = instantiationInfo()
let line = "$# $#:$# - $#" % let line = "$# $#:$# - " %
[now().format("yyyy-MM-dd HH:mm:ss:fffffffff"), [now().format("yyyy-MM-dd HH:mm:ss:fffffffff"),
module.filename[0..^5], module.filename[0..^5],
$module.line, $module.line]
data] doDebug(data, line)
doDebug(line)
else: else:
template debug*(data: string) = discard template debug*(data: string) = discard