<div>Turns out the record object is shared between handlers.  This was not obvious to me at first glance.</div><br><div class="gmail_quote">Hopefully this is the final patch.  :)  This is against the bitbake repo instead of poky.  Please let me know if this is ok.<div>
<br></div><div>Thanks for your patience.</div><div><br></div><div>Seth</div><div><br></div><div>
--------------------------------------------------------------------------------------</div><div><br></div><div><div>Colorize knotty interactive console output</div><div><br></div><div>Add bold color output to log level name and standard color output to log msg</div>

<div>when bitbake is run from an iteractive console.  Color output is only enabled</div><div>if the terminal supports color.</div><div><br></div><div>Used Jason Wessel&#39;s recommendation for transparency on verbose, note and plain.</div>

<div><br></div><div>Signed-off-by: Seth Bollinger &lt;<a href="mailto:seth.boll@gmail.com" target="_blank">seth.boll@gmail.com</a>&gt;</div><div>---</div><div> lib/bb/msg.py       |   43 +++++++++++++++++++++++++++++++++++++++++++</div>
<div>
 lib/bb/ui/knotty.py |    6 +++++-</div><div> 2 files changed, 48 insertions(+), 1 deletions(-)</div><div><br></div><div>diff --git a/lib/bb/msg.py b/lib/bb/msg.py</div><div>index 9b39325..007c95a 100644</div><div>--- a/lib/bb/msg.py</div>

<div>+++ b/lib/bb/msg.py</div><div>@@ -23,6 +23,7 @@ Message handling infrastructure for bitbake</div><div> # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.</div><div> </div><div> import sys</div><div>+import copy</div>

<div> import logging</div><div> import collections</div><div> from itertools import groupby</div><div>@@ -55,6 +56,25 @@ class BBLogFormatter(logging.Formatter):</div><div>         CRITICAL: &#39;ERROR&#39;,</div><div>     }</div>

<div> </div><div>+    color_enabled = False</div><div>+    BASECOLOR, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(29,38)</div><div>+</div><div>+    COLORS = {</div><div>+        DEBUG3  : CYAN,</div><div>

+        DEBUG2  : CYAN,</div><div>+        DEBUG   : CYAN,</div><div>+        VERBOSE : BASECOLOR,</div><div>+        NOTE    : BASECOLOR,</div><div>+        PLAIN   : BASECOLOR,</div><div>+        WARNING : YELLOW,</div>

<div>+        ERROR   : RED,</div><div>+        CRITICAL: RED,</div><div>+    }</div><div>+</div><div>+    BLD = &#39;\033[1;%dm&#39;</div><div>+    STD = &#39;\033[%dm&#39;</div><div>+    RST = &#39;\033[0m&#39;</div><div>

+</div><div>     def getLevelName(self, levelno):</div><div>         try:</div><div>             return self.levelnames[levelno]</div><div>@@ -67,6 +87,8 @@ class BBLogFormatter(logging.Formatter):</div><div>         if record.levelno == self.PLAIN:</div>

<div>             msg = record.getMessage()</div><div>         else:</div><div>+            if self.color_enabled:</div><div>+                record = self.colorize(record)</div><div>             msg = logging.Formatter.format(self, record)</div>

<div> </div><div>         if hasattr(record, &#39;bb_exc_info&#39;):</div><div>@@ -75,6 +97,27 @@ class BBLogFormatter(logging.Formatter):</div><div>             msg += &#39;\n&#39; + &#39;&#39;.join(formatted)</div><div>

         return msg</div><div> </div><div>+    def colorize(self, record):</div><div>+        color = self.COLORS[record.levelno]</div><div>+        if self.color_enabled and color is not None:</div><div>+            record = copy.copy(record)</div>

<div>+            record.levelname = &quot;&quot;.join([self.BLD % color, record.levelname, self.RST])</div><div>+            record.msg = &quot;&quot;.join([self.STD % color, record.msg, self.RST])</div><div>+        return record</div>

<div>+</div><div>+    def enable_color(self):</div><div>+        import curses</div><div>+        try:</div><div>+            win = None</div><div>+            win = curses.initscr()</div><div>+            if curses.has_colors():</div>

<div>+                self.color_enabled = True</div><div>+        except:</div><div>+            pass</div><div>+        finally:</div><div>+            if win is not None:</div><div>+                curses.endwin()</div>

<div>+</div><div> class BBLogFilter(object):</div><div>     def __init__(self, handler, level, debug_domains):</div><div>         self.stdlevel = level</div><div>diff --git a/lib/bb/ui/knotty.py b/lib/bb/ui/knotty.py</div>

<div>index 77ec730..a63d8b1 100644</div><div>--- a/lib/bb/ui/knotty.py</div><div>+++ b/lib/bb/ui/knotty.py</div><div>@@ -238,12 +238,16 @@ def main(server, eventHandler, tf = TerminalFilter):</div><div>     helper = uihelper.BBUIHelper()</div>

<div> </div><div>     console = logging.StreamHandler(sys.stdout)</div><div>-    format = bb.msg.BBLogFormatter(&quot;%(levelname)s: %(message)s&quot;)</div><div>+    format_str = &quot;%(levelname)s: %(message)s&quot;</div>

<div>+    format = bb.msg.BBLogFormatter(format_str)</div><div>+    if interactive:</div><div>+        format.enable_color()</div><div>     bb.msg.addDefaultlogFilter(console)</div><div>     console.setFormatter(format)</div>

<div>     logger.addHandler(console)</div><div>     if consolelogfile:</div><div>         bb.utils.mkdirhier(os.path.dirname(consolelogfile))</div><div>+        format = bb.msg.BBLogFormatter(format_str)</div><div>         consolelog = logging.FileHandler(consolelogfile)</div>

<div>         bb.msg.addDefaultlogFilter(consolelog)</div><div>         consolelog.setFormatter(format)</div><div>-- </div><div>1.7.2.5</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Nov 14, 2012 at 5:26 PM, Richard Purdie <span dir="ltr">&lt;<a href="mailto:richard.purdie@linuxfoundation.org" target="_blank">richard.purdie@linuxfoundation.org</a>&gt;</span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


This works much better. I&#39;ve realised the escape sequences are ending up<br>
in logfiles like the cooker log though so some further tweaking is<br>
needed...<br>
<br>
Also, your patches are coming through linewrapped so I have to fix them<br>
manually before applying.<br>
<br>
Cheers,<br>
<br>
Richard<br></blockquote></div></div>
</div><br>