You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2020/09/20 23:15:29 UTC

[incubator-nlpcraft] branch master updated: Improved logging.

This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new fbf52c2  Improved logging.
fbf52c2 is described below

commit fbf52c21f047180f6e59cb216cdd82a27506b362
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Sun Sep 20 16:15:17 2020 -0700

    Improved logging.
---
 .../apache/nlpcraft/common/ansi/NCAnsiColor.scala  |  8 ++-
 .../org/apache/nlpcraft/common/util/NCUtils.scala  | 68 +++++++++++++++++++++-
 2 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
index 2b2c81b..cb8c287 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
@@ -17,12 +17,13 @@
 
 package org.apache.nlpcraft.common.ansi
 
+import com.typesafe.scalalogging.LazyLogging
 import org.apache.nlpcraft.common._
 
 /**
  * Scala 2.13 shim for `scala.io.AnsiColor`.
  */
-trait NCAnsiColor {
+trait NCAnsiColor extends LazyLogging {
     private final val BLACK = "\u001b[30m"
     private final val RED = "\u001b[31m"
     private final val GREEN = "\u001b[32m"
@@ -76,4 +77,7 @@ trait NCAnsiColor {
     def ansiInvisible: String = if (isEnabled) INVISIBLE else ""
 }
 
-object NCAnsiColor extends NCAnsiColor { }
+object NCAnsiColor extends NCAnsiColor {
+    if (isEnabled)
+        logger.info(s"${U.bgRainbow("ANSI coloring")} ${U.fgRainbow("is enabled")}. Use '-D${ansiCyanFg}NLPCRAFT_ANSI_COLOR_DISABLED${ansiReset}=true' to disable it.")
+}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index 7182c54..83ec8a6 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -74,6 +74,26 @@ object NCUtils extends LazyLogging {
 
     private val idGen = new NCIdGenerator(NCBlowfishHasher.salt(), 8)
 
+    private lazy val ANSI_FG_COLORS = Seq(
+        ansiRedFg,
+        ansiGreenFg,
+        ansiBlueFg,
+        ansiYellowFg,
+        ansiWhiteFg,
+        ansiBlackFg,
+        ansiCyanFg
+    )
+    private lazy val ANSI_BG_COLORS = Seq(
+        ansiRedBg,
+        ansiGreenBg,
+        ansiBlueBg,
+        ansiYellowBg,
+        ansiWhiteBg,
+        ansiBlackBg,
+        ansiCyanBg
+    )
+    private lazy val ANSI_COLORS = for (fg ← ANSI_FG_COLORS; bg ← ANSI_BG_COLORS) yield s"$fg$bg"
+
     // Various decimal formats.
     private final val DEC_FMT0 = mkDecimalFormat("#0")
     private final val DEC_FMT1 = mkDecimalFormat("#0.0")
@@ -1410,6 +1430,52 @@ object NCUtils extends LazyLogging {
         raw"$ansiBold$ansiRedFg/_/ |_/_/ .___/$ansiRedFg\____/_/   \__,_/_/  \__/      $ansiReset$NL" +
         raw"$ansiBold$ansiRedFg       /_/                                              $ansiReset$NL"
 
+    /**
+     *
+     * @param s
+     * @return
+     */
+    def fgRainbow(s: String, addOn: String = ""): String = rainbowImpl(s, ANSI_FG_COLORS, addOn)
+
+    /**
+     *
+     * @param s
+     * @return
+     */
+    def bgRainbow(s: String, addOn: String = ""): String = rainbowImpl(s, ANSI_BG_COLORS, addOn)
+
+    /**
+     *
+     * @param s
+     * @return
+     */
+    def rainbow(s: String, addOn: String = ""): String = randomRainbowImpl(s, ANSI_COLORS, addOn)
+
+    /**
+     *
+     * @param s
+     * @param colors
+     * @param addOn
+     * @return
+     */
+    private def randomRainbowImpl(s: String, colors: Seq[String], addOn: String): String =
+        s.zipWithIndex.foldLeft(new StringBuilder())((buf, zip) ⇒ {
+            buf ++= s"${colors(RND.nextInt(colors.size))}$addOn${zip._1}"
+        })
+        .toString + ansiReset
+
+    /**
+     *
+     * @param s
+     * @param colors
+     * @param addOn
+     * @return
+     */
+    private def rainbowImpl(s: String, colors: Seq[String], addOn: String): String =
+        s.zipWithIndex.foldLeft(new StringBuilder())((buf, zip) ⇒ {
+            buf ++= s"${colors(zip._2 % colors.size)}$addOn${zip._1}"
+        })
+        .toString + ansiReset
 
     /**
      * ANSI color JSON string.
@@ -1459,7 +1525,7 @@ object NCUtils extends LazyLogging {
                 val thNum = new AtomicInteger(1)
 
                 override def newThread(r: Runnable): Thread =
-                    new Thread(r, s"pool-$namePrefix-thread-${thNum.getAndIncrement()}");
+                    new Thread(r, s"pool-$namePrefix-thread-${thNum.getAndIncrement()}")
             },
             new RejectedExecutionHandler() {
                 // Ignore rejections.