From 116051208abe79133a856fbea26206a3c38302b9 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 8 Sep 2019 11:37:52 -0600 Subject: [PATCH] make patterns runtime only --- libp2p/helpers/debug.nim | 63 ++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/libp2p/helpers/debug.nim b/libp2p/helpers/debug.nim index 72aff52f7..06239b061 100644 --- a/libp2p/helpers/debug.nim +++ b/libp2p/helpers/debug.nim @@ -7,64 +7,71 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -## Small debug module that's enabled through a ``-d:debugout`` -## flag. It's inspired by the Nodejs ``debug`` module that -## allows printing colorized output based on patterns. This -## module is powered by the standard ``nre`` module and as such -## it supports the same set of regexp expressions. +## Small debug module that's only runs in non release builds. +## It's inspired by the Nodejs ``debug`` module which allows printing +## colorized output based on matched patterns. This module is powered +## by the standard ``nre`` module and as such it supports the same set +## of regexp expressions. ## -## To enable debug output, pass the ``debugout`` flag during build -## time with ``-d`` flag. In addition, the debugout flag takes a comma -## separated list of patters that will narow the debug output to -## only those matched by the patterns. By default however, all -## debug statements are outputed to the stderr. +## The output is controled through two environment variables ``DEBUG`` +## and ``DEBUG_COLOR``. By default, it will print everything to stderr +## but if only some output is of interested, ``DEBUG`` accepts a coma +## separated list of regexp expressions to match the output against. +## 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, tables, nre, strutils, times, terminal, - sequtils - - const debugout {.strdefine.}: string = ".*" + sequtils, + os type Match = object pattern: Regex color: string - if isTrueColorSupported(): - system.addQuitProc(resetAttributes) - enableTrueColors() + # if isTrueColorSupported(): + # system.addQuitProc(resetAttributes) + # enableTrueColors() var context {.threadvar.} : OrderedTableRef[string, Match] proc initDebugCtx() = + let debugout = getEnv("DEBUG", ".*") + let debugColor = getEnv("DEBUG_COLOR") + let isDebugColor = debugColor.len > 0 context = newOrderedTable[string, Match]() - var patrns = @[".*"] - if debugout != "true": - patrns = debugout.split(re"[,\s]").filterIt(it.len > 0) + var patrns: seq[string] = @[] + patrns = debugout.split(re"[,\s]").filterIt(it.len > 0) randomize() 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): initDebugCtx() for m in context.values: - if data.match(m.pattern).isSome: - stderr.writeLine("\u001b[38;5;" & m.color & "m " & alignLeft(data, 4) & "\e[0m") + if data.contains(m.pattern): + stderr.writeLine("\u001b[38;5;" & m.color & "m " & alignLeft(line & data, 4) & "\e[0m") return template debug*(data: string) = let module = instantiationInfo() - let line = "$# $#:$# - $#" % + let line = "$# $#:$# - " % [now().format("yyyy-MM-dd HH:mm:ss:fffffffff"), module.filename[0..^5], - $module.line, - data] - doDebug(line) + $module.line] + doDebug(data, line) else: template debug*(data: string) = discard \ No newline at end of file