All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] bitbake: Colorize knotty interactive console output
@ 2012-11-11 15:17 Seth Bollinger
  2012-11-11 22:54 ` Chris Larson
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Seth Bollinger @ 2012-11-11 15:17 UTC (permalink / raw)
  To: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 3426 bytes --]

Add bold color output to log level name and standard color output to log msg
when bitbake is run from an iteractive console.  Color output is only
enabled
if the terminal supports color.

Signed-off-by: Seth Bollinger <seth.boll@gmail.com>
---
 bitbake/lib/bb/msg.py       |   40 ++++++++++++++++++++++++++++++++++++++++
 bitbake/lib/bb/ui/knotty.py |    6 +++++-
 2 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
index 9b39325..68b2ad7 100644
--- a/bitbake/lib/bb/msg.py
+++ b/bitbake/lib/bb/msg.py
@@ -55,6 +55,25 @@ class BBLogFormatter(logging.Formatter):
         CRITICAL: 'ERROR',
     }

+    color_enabled = False
+    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30,38)
+
+    COLORS = {
+        DEBUG3  : CYAN,
+        DEBUG2  : CYAN,
+        DEBUG   : CYAN,
+        VERBOSE : WHITE,
+        NOTE    : WHITE,
+        PLAIN   : WHITE,
+        WARNING : YELLOW,
+        ERROR   : RED,
+        CRITICAL: RED,
+    }
+
+    BLD = '\033[1;%dm'
+    STD = '\033[%dm'
+    RST = '\033[0m'
+
     def getLevelName(self, levelno):
         try:
             return self.levelnames[levelno]
@@ -67,6 +86,8 @@ class BBLogFormatter(logging.Formatter):
         if record.levelno == self.PLAIN:
             msg = record.getMessage()
         else:
+            if self.color_enabled:
+                self.colorize(record)
             msg = logging.Formatter.format(self, record)

         if hasattr(record, 'bb_exc_info'):
@@ -75,6 +96,25 @@ class BBLogFormatter(logging.Formatter):
             msg += '\n' + ''.join(formatted)
         return msg

+    def colorize(self, record):
+        color = self.COLORS[record.levelno]
+        if self.color_enabled and color is not None:
+            record.levelname = "".join([self.BLD % color,
record.levelname, self.RST])
+            record.msg = "".join([self.STD % color, record.msg, self.RST])
+
+    def enable_color(self):
+        import curses
+        try:
+            win = None
+            win = curses.initscr()
+            if curses.has_colors():
+                self.color_enabled = True
+        except:
+            pass
+        finally:
+            if win is not None:
+                curses.endwin()
+
 class BBLogFilter(object):
     def __init__(self, handler, level, debug_domains):
         self.stdlevel = level
diff --git a/bitbake/lib/bb/ui/knotty.py b/bitbake/lib/bb/ui/knotty.py
index b99a121..d9aa973 100644
--- a/bitbake/lib/bb/ui/knotty.py
+++ b/bitbake/lib/bb/ui/knotty.py
@@ -238,12 +238,16 @@ def main(server, eventHandler, tf = TerminalFilter):
     helper = uihelper.BBUIHelper()

     console = logging.StreamHandler(sys.stdout)
-    format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
+    format_str = "%(levelname)s: %(message)s"
+    format = bb.msg.BBLogFormatter(format_str)
+    if interactive:
+        format.enable_color()
     bb.msg.addDefaultlogFilter(console)
     console.setFormatter(format)
     logger.addHandler(console)
     if consolelogfile:
         bb.utils.mkdirhier(os.path.dirname(consolelogfile))
+        format = bb.msg.BBLogFormatter(format_str)
         consolelog = logging.FileHandler(consolelogfile)
         bb.msg.addDefaultlogFilter(consolelog)
         consolelog.setFormatter(format)
-- 
1.7.2.5

[-- Attachment #2: Type: text/html, Size: 8365 bytes --]

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-11 15:17 [PATCH v2] bitbake: Colorize knotty interactive console output Seth Bollinger
@ 2012-11-11 22:54 ` Chris Larson
  2012-11-12  2:02   ` Seth Bollinger
  2012-11-13 14:23 ` Richard Purdie
  2012-11-22  3:36 ` Robert Yang
  2 siblings, 1 reply; 15+ messages in thread
From: Chris Larson @ 2012-11-11 22:54 UTC (permalink / raw)
  To: Seth Bollinger; +Cc: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 667 bytes --]

On Sun, Nov 11, 2012 at 8:17 AM, Seth Bollinger <seth.boll@gmail.com> wrote:

> @@ -67,6 +86,8 @@ class BBLogFormatter(logging.Formatter):
>          if record.levelno == self.PLAIN:
>              msg = record.getMessage()
>          else:
> +            if self.color_enabled:
> +                self.colorize(record)
>


I'm not seeing your definition of color_enabled in the constructor.
Wouldn't this attribute not exist when it's first created, before
enable_color() is called? Have you tested this with a non-interactive
terminal? I'd think you'd want a self.color_enabled = False in the
constructor for the formatter.
-- 
Christopher Larson

[-- Attachment #2: Type: text/html, Size: 1282 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-11 22:54 ` Chris Larson
@ 2012-11-12  2:02   ` Seth Bollinger
  2012-11-12  2:24     ` Chris Larson
  0 siblings, 1 reply; 15+ messages in thread
From: Seth Bollinger @ 2012-11-12  2:02 UTC (permalink / raw)
  To: Chris Larson; +Cc: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 1284 bytes --]

color_enabled is initialized on line 58 of msg.py.  I did this in the same
way as other class variables are initialized.

    levelnames = {
        DEBUG3   : 'DEBUG',
        DEBUG2   : 'DEBUG',
        DEBUG   : 'DEBUG',
        VERBOSE: 'NOTE',
        NOTE    : 'NOTE',
        PLAIN  : '',
        WARNING : 'WARNING',
        ERROR   : 'ERROR',
        CRITICAL: 'ERROR',
    }

    color_enabled = False
    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30,38)


On Sun, Nov 11, 2012 at 4:54 PM, Chris Larson <clarson@kergoth.com> wrote:

>
>
> On Sun, Nov 11, 2012 at 8:17 AM, Seth Bollinger <seth.boll@gmail.com>wrote:
>
>> @@ -67,6 +86,8 @@ class BBLogFormatter(logging.Formatter):
>>          if record.levelno == self.PLAIN:
>>              msg = record.getMessage()
>>          else:
>> +            if self.color_enabled:
>> +                self.colorize(record)
>>
>
>
> I'm not seeing your definition of color_enabled in the constructor.
> Wouldn't this attribute not exist when it's first created, before
> enable_color() is called? Have you tested this with a non-interactive
> terminal? I'd think you'd want a self.color_enabled = False in the
> constructor for the formatter.
> --
> Christopher Larson
>

[-- Attachment #2: Type: text/html, Size: 2402 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-12  2:02   ` Seth Bollinger
@ 2012-11-12  2:24     ` Chris Larson
  0 siblings, 0 replies; 15+ messages in thread
From: Chris Larson @ 2012-11-12  2:24 UTC (permalink / raw)
  To: Seth Bollinger; +Cc: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 699 bytes --]

On Sun, Nov 11, 2012 at 7:02 PM, Seth Bollinger <seth.boll@gmail.com> wrote:

> color_enabled is initialized on line 58 of msg.py.  I did this in the same
> way as other class variables are initialized.
>
>     levelnames = {
>         DEBUG3   : 'DEBUG',
>         DEBUG2   : 'DEBUG',
>         DEBUG   : 'DEBUG',
>         VERBOSE: 'NOTE',
>         NOTE    : 'NOTE',
>         PLAIN  : '',
>         WARNING : 'WARNING',
>         ERROR   : 'ERROR',
>         CRITICAL: 'ERROR',
>     }
>
>     color_enabled = False
>


Ah! I missed that, thanks for clarifying. Other than that question, it
looks pretty decent to me. We'll see if Richard agrees.
-- 
Christopher Larson

[-- Attachment #2: Type: text/html, Size: 1278 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-11 15:17 [PATCH v2] bitbake: Colorize knotty interactive console output Seth Bollinger
  2012-11-11 22:54 ` Chris Larson
@ 2012-11-13 14:23 ` Richard Purdie
  2012-11-13 14:45   ` Seth Bollinger
  2012-11-22  3:36 ` Robert Yang
  2 siblings, 1 reply; 15+ messages in thread
From: Richard Purdie @ 2012-11-13 14:23 UTC (permalink / raw)
  To: Seth Bollinger; +Cc: bitbake-devel

On Sun, 2012-11-11 at 09:17 -0600, Seth Bollinger wrote:
> Add bold color output to log level name and standard color output to
> log msg
> when bitbake is run from an iteractive console.  Color output is only
> enabled
> if the terminal supports color.
>
> Signed-off-by: Seth Bollinger <seth.boll@gmail.com>
> ---
>  bitbake/lib/bb/msg.py       |   40
> ++++++++++++++++++++++++++++++++++++++++
>  bitbake/lib/bb/ui/knotty.py |    6 +++++-
>  2 files changed, 45 insertions(+), 1 deletions(-)

I love the idea. I tried testing it and realised that it doesn't work in
my black text on a white background terminals though, the result of the
patch was unreadable :(

Cheers,

Richard





^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-13 14:23 ` Richard Purdie
@ 2012-11-13 14:45   ` Seth Bollinger
  2012-11-13 15:22     ` Richard Purdie
  0 siblings, 1 reply; 15+ messages in thread
From: Seth Bollinger @ 2012-11-13 14:45 UTC (permalink / raw)
  To: Richard Purdie; +Cc: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 1258 bytes --]

I'm not sure how best to resolve this.  Would you like me to add some code
to try and detect background color?  This seems like it might be more
complex than it's worth...

Or would it be better to have a selectable light background color set?  If
you think light background is more common (I dislike staring at large
blocks of white :)) it could default to the light color set.

Thanks!

Seth


On Tue, Nov 13, 2012 at 8:23 AM, Richard Purdie <
richard.purdie@linuxfoundation.org> wrote:

> On Sun, 2012-11-11 at 09:17 -0600, Seth Bollinger wrote:
> > Add bold color output to log level name and standard color output to
> > log msg
> > when bitbake is run from an iteractive console.  Color output is only
> > enabled
> > if the terminal supports color.
> >
> > Signed-off-by: Seth Bollinger <seth.boll@gmail.com>
> > ---
> >  bitbake/lib/bb/msg.py       |   40
> > ++++++++++++++++++++++++++++++++++++++++
> >  bitbake/lib/bb/ui/knotty.py |    6 +++++-
> >  2 files changed, 45 insertions(+), 1 deletions(-)
>
> I love the idea. I tried testing it and realised that it doesn't work in
> my black text on a white background terminals though, the result of the
> patch was unreadable :(
>
> Cheers,
>
> Richard
>
>
>

[-- Attachment #2: Type: text/html, Size: 1751 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-13 14:45   ` Seth Bollinger
@ 2012-11-13 15:22     ` Richard Purdie
  2012-11-13 22:36       ` Jason Wessel
  0 siblings, 1 reply; 15+ messages in thread
From: Richard Purdie @ 2012-11-13 15:22 UTC (permalink / raw)
  To: Seth Bollinger; +Cc: bitbake-devel

On Tue, 2012-11-13 at 08:45 -0600, Seth Bollinger wrote:
> I'm not sure how best to resolve this.  Would you like me to add some
> code to try and detect background color?  This seems like it might be
> more complex than it's worth...
>
> Or would it be better to have a selectable light background color
> set?  If you think light background is more common (I dislike staring
> at large blocks of white :)) it could default to the light color set.

I have not looked into this and am not an expert on terminals and colour
control of them. I don't know how we can solve this but the current
situation where it prints white on white on my terminal is clearly
unacceptable. This has to be a problem others have run into and solved
before though...

Cheers,

Richard







^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-13 15:22     ` Richard Purdie
@ 2012-11-13 22:36       ` Jason Wessel
  2012-11-14 13:18         ` Seth Bollinger
  0 siblings, 1 reply; 15+ messages in thread
From: Jason Wessel @ 2012-11-13 22:36 UTC (permalink / raw)
  To: Richard Purdie; +Cc: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 1831 bytes --]

On 11/13/2012 09:22 AM, Richard Purdie wrote:
> On Tue, 2012-11-13 at 08:45 -0600, Seth Bollinger wrote:
>> I'm not sure how best to resolve this.  Would you like me to add some
>> code to try and detect background color?  This seems like it might be
>> more complex than it's worth...
>>
>> Or would it be better to have a selectable light background color
>> set?  If you think light background is more common (I dislike staring
>> at large blocks of white :)) it could default to the light color set.
> I have not looked into this and am not an expert on terminals and colour
> control of them. I don't know how we can solve this but the current
> situation where it prints white on white on my terminal is clearly
> unacceptable. This has to be a problem others have run into and solved
> before though...


If you are planning to colorize things, my recommendation would be to take the "middle ground" and use the transparency instead of using white which should work in general on any modern terminal.

Change the patch a bit like this:
+    color_enabled = False
+    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30,38)

    BASECOLOR, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(29,38)

+
+    COLORS = {
+        DEBUG3  : CYAN,
+        DEBUG2  : CYAN,
+        DEBUG   : CYAN,
+        VERBOSE : BASECOLOR,
+        NOTE    : BASECOLOR,
+        PLAIN   : BASECOLOR,
+        WARNING : YELLOW,
+        ERROR   : RED,
+        CRITICAL: RED,
+    }


Most of the time terminals are either "dark" or "light", and default color is always set to -1 in the curses interface.  For example, the attached patch will allow the patch to work on my "lawngreen" default xterms as well as more typical defaults for gnome-terminal or rxvt.

Cheers,
Jason.

[-- Attachment #2: colors.diff --]
[-- Type: application/mbox, Size: 705 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-13 22:36       ` Jason Wessel
@ 2012-11-14 13:18         ` Seth Bollinger
  2012-11-14 13:22           ` Richard Purdie
  0 siblings, 1 reply; 15+ messages in thread
From: Seth Bollinger @ 2012-11-14 13:18 UTC (permalink / raw)
  To: Jason Wessel; +Cc: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 2246 bytes --]

Thanks!  I've tested this and it works on my dark color scheme (it's better
actually :)).  I also tested on white and it looks fine.  If Richard is ok
with this solution, I'll make the changes and submit another patch.

Seth


On Tue, Nov 13, 2012 at 4:36 PM, Jason Wessel <jason.wessel@windriver.com>wrote:

> On 11/13/2012 09:22 AM, Richard Purdie wrote:
> > On Tue, 2012-11-13 at 08:45 -0600, Seth Bollinger wrote:
> >> I'm not sure how best to resolve this.  Would you like me to add some
> >> code to try and detect background color?  This seems like it might be
> >> more complex than it's worth...
> >>
> >> Or would it be better to have a selectable light background color
> >> set?  If you think light background is more common (I dislike staring
> >> at large blocks of white :)) it could default to the light color set.
> > I have not looked into this and am not an expert on terminals and colour
> > control of them. I don't know how we can solve this but the current
> > situation where it prints white on white on my terminal is clearly
> > unacceptable. This has to be a problem others have run into and solved
> > before though...
>
>
> If you are planning to colorize things, my recommendation would be to take
> the "middle ground" and use the transparency instead of using white which
> should work in general on any modern terminal.
>
> Change the patch a bit like this:
> +    color_enabled = False
> +    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30,38)
>
>     BASECOLOR, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE =
> range(29,38)
>
> +
> +    COLORS = {
> +        DEBUG3  : CYAN,
> +        DEBUG2  : CYAN,
> +        DEBUG   : CYAN,
> +        VERBOSE : BASECOLOR,
> +        NOTE    : BASECOLOR,
> +        PLAIN   : BASECOLOR,
> +        WARNING : YELLOW,
> +        ERROR   : RED,
> +        CRITICAL: RED,
> +    }
>
>
> Most of the time terminals are either "dark" or "light", and default color
> is always set to -1 in the curses interface.  For example, the attached
> patch will allow the patch to work on my "lawngreen" default xterms as well
> as more typical defaults for gnome-terminal or rxvt.
>
> Cheers,
> Jason.
>

[-- Attachment #2: Type: text/html, Size: 2917 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-14 13:18         ` Seth Bollinger
@ 2012-11-14 13:22           ` Richard Purdie
  2012-11-14 14:43             ` Seth Bollinger
  0 siblings, 1 reply; 15+ messages in thread
From: Richard Purdie @ 2012-11-14 13:22 UTC (permalink / raw)
  To: Seth Bollinger; +Cc: bitbake-devel

On Wed, 2012-11-14 at 07:18 -0600, Seth Bollinger wrote:
> Thanks!  I've tested this and it works on my dark color scheme (it's
> better actually :)).  I also tested on white and it looks fine.  If
> Richard is ok with this solution, I'll make the changes and submit
> another patch.
> 
It sounds promising, I would like to try the patch...

Cheers,

Richard






^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-14 13:22           ` Richard Purdie
@ 2012-11-14 14:43             ` Seth Bollinger
  2012-11-14 23:26               ` Richard Purdie
  0 siblings, 1 reply; 15+ messages in thread
From: Seth Bollinger @ 2012-11-14 14:43 UTC (permalink / raw)
  To: Richard Purdie; +Cc: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 4038 bytes --]

Add bold color output to log level name and standard color output to log msg
when bitbake is run from an iteractive console.  Color output is only
enabled
if the terminal supports color.

Used Jason Wessel's recommendation for transparency on verbose, note and
plain.

Signed-off-by: Seth Bollinger <seth.boll@gmail.com>
---
 bitbake/lib/bb/msg.py       |   40 ++++++++++++++++++++++++++++++++++++++++
 bitbake/lib/bb/ui/knotty.py |    6 +++++-
 2 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
index 9b39325..68b2ad7 100644
--- a/bitbake/lib/bb/msg.py
+++ b/bitbake/lib/bb/msg.py
@@ -55,6 +55,25 @@ class BBLogFormatter(logging.Formatter):
         CRITICAL: 'ERROR',
     }

+    color_enabled = False
+    BASECOLOR, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE =
range(29,38)
+
+    COLORS = {
+        DEBUG3  : CYAN,
+        DEBUG2  : CYAN,
+        DEBUG   : CYAN,
+        VERBOSE : BASECOLOR,
+        NOTE    : BASECOLOR,
+        PLAIN   : BASECOLOR,
+        WARNING : YELLOW,
+        ERROR   : RED,
+        CRITICAL: RED,
+    }
+
+    BLD = '\033[1;%dm'
+    STD = '\033[%dm'
+    RST = '\033[0m'
+
     def getLevelName(self, levelno):
         try:
             return self.levelnames[levelno]
@@ -67,6 +86,8 @@ class BBLogFormatter(logging.Formatter):
         if record.levelno == self.PLAIN:
             msg = record.getMessage()
         else:
+            if self.color_enabled:
+                self.colorize(record)
             msg = logging.Formatter.format(self, record)

         if hasattr(record, 'bb_exc_info'):
@@ -75,6 +96,25 @@ class BBLogFormatter(logging.Formatter):
             msg += '\n' + ''.join(formatted)
         return msg

+    def colorize(self, record):
+        color = self.COLORS[record.levelno]
+        if self.color_enabled and color is not None:
+            record.levelname = "".join([self.BLD % color,
record.levelname, self.RST])
+            record.msg = "".join([self.STD % color, record.msg, self.RST])
+
+    def enable_color(self):
+        import curses
+        try:
+            win = None
+            win = curses.initscr()
+            if curses.has_colors():
+                self.color_enabled = True
+        except:
+            pass
+        finally:
+            if win is not None:
+                curses.endwin()
+
 class BBLogFilter(object):
     def __init__(self, handler, level, debug_domains):
         self.stdlevel = level
diff --git a/bitbake/lib/bb/ui/knotty.py b/bitbake/lib/bb/ui/knotty.py
index b99a121..d9aa973 100644
--- a/bitbake/lib/bb/ui/knotty.py
+++ b/bitbake/lib/bb/ui/knotty.py
@@ -238,12 +238,16 @@ def main(server, eventHandler, tf = TerminalFilter):
     helper = uihelper.BBUIHelper()

     console = logging.StreamHandler(sys.stdout)
-    format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
+    format_str = "%(levelname)s: %(message)s"
+    format = bb.msg.BBLogFormatter(format_str)
+    if interactive:
+        format.enable_color()
     bb.msg.addDefaultlogFilter(console)
     console.setFormatter(format)
     logger.addHandler(console)
     if consolelogfile:
         bb.utils.mkdirhier(os.path.dirname(consolelogfile))
+        format = bb.msg.BBLogFormatter(format_str)
         consolelog = logging.FileHandler(consolelogfile)
         bb.msg.addDefaultlogFilter(consolelog)
         consolelog.setFormatter(format)
-- 
1.7.2.5



On Wed, Nov 14, 2012 at 7:22 AM, Richard Purdie <
richard.purdie@linuxfoundation.org> wrote:

> On Wed, 2012-11-14 at 07:18 -0600, Seth Bollinger wrote:
> > Thanks!  I've tested this and it works on my dark color scheme (it's
> > better actually :)).  I also tested on white and it looks fine.  If
> > Richard is ok with this solution, I'll make the changes and submit
> > another patch.
> >
> It sounds promising, I would like to try the patch...
>
> Cheers,
>
> Richard
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 5500 bytes --]

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-14 14:43             ` Seth Bollinger
@ 2012-11-14 23:26               ` Richard Purdie
       [not found]                 ` <CA+JN8xN-vkLxtd+J4BZoQ39xs3Yujh-J=CQ8q+T1k0r-t-bpzA@mail.gmail.com>
  0 siblings, 1 reply; 15+ messages in thread
From: Richard Purdie @ 2012-11-14 23:26 UTC (permalink / raw)
  To: Seth Bollinger; +Cc: bitbake-devel

On Wed, 2012-11-14 at 08:43 -0600, Seth Bollinger wrote:
> Add bold color output to log level name and standard color output to
> log msg
> when bitbake is run from an iteractive console.  Color output is only
> enabled
> if the terminal supports color.
>
> Used Jason Wessel's recommendation for transparency on verbose, note
> and plain.
> 
This works much better. I've realised the escape sequences are ending up
in logfiles like the cooker log though so some further tweaking is
needed...

Also, your patches are coming through linewrapped so I have to fix them
manually before applying.

Cheers,

Richard


> Signed-off-by: Seth Bollinger <seth.boll@gmail.com>
> ---
>  bitbake/lib/bb/msg.py       |   40
> ++++++++++++++++++++++++++++++++++++++++
>  bitbake/lib/bb/ui/knotty.py |    6 +++++-
>  2 files changed, 45 insertions(+), 1 deletions(-)
> 
> 
> diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
> index 9b39325..68b2ad7 100644
> --- a/bitbake/lib/bb/msg.py
> +++ b/bitbake/lib/bb/msg.py
> @@ -55,6 +55,25 @@ class BBLogFormatter(logging.Formatter):
>          CRITICAL: 'ERROR',
>      }
>  
> +    color_enabled = False
> +    BASECOLOR, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
> = range(29,38)
> +
> +    COLORS = {
> +        DEBUG3  : CYAN,
> +        DEBUG2  : CYAN,
> +        DEBUG   : CYAN,
> +        VERBOSE : BASECOLOR,
> +        NOTE    : BASECOLOR,
> +        PLAIN   : BASECOLOR,
> +        WARNING : YELLOW,
> +        ERROR   : RED,
> +        CRITICAL: RED,
> +    }
> +
> +    BLD = '\033[1;%dm'
> +    STD = '\033[%dm'
> +    RST = '\033[0m'
> +
>      def getLevelName(self, levelno):
>          try:
>              return self.levelnames[levelno]
> @@ -67,6 +86,8 @@ class BBLogFormatter(logging.Formatter):
>          if record.levelno == self.PLAIN:
>              msg = record.getMessage()
>          else:
> +            if self.color_enabled:
> +                self.colorize(record)
>              msg = logging.Formatter.format(self, record)
>  
>          if hasattr(record, 'bb_exc_info'):
> @@ -75,6 +96,25 @@ class BBLogFormatter(logging.Formatter):
>              msg += '\n' + ''.join(formatted)
>          return msg
>  
> +    def colorize(self, record):
> +        color = self.COLORS[record.levelno]
> +        if self.color_enabled and color is not None:
> +            record.levelname = "".join([self.BLD % color,
> record.levelname, self.RST])
> +            record.msg = "".join([self.STD % color, record.msg,
> self.RST])
> +
> +    def enable_color(self):
> +        import curses
> +        try:
> +            win = None
> +            win = curses.initscr()
> +            if curses.has_colors():
> +                self.color_enabled = True
> +        except:
> +            pass
> +        finally:
> +            if win is not None:
> +                curses.endwin()
> +
>  class BBLogFilter(object):
>      def __init__(self, handler, level, debug_domains):
>          self.stdlevel = level
> diff --git a/bitbake/lib/bb/ui/knotty.py b/bitbake/lib/bb/ui/knotty.py
> index b99a121..d9aa973 100644
> --- a/bitbake/lib/bb/ui/knotty.py
> +++ b/bitbake/lib/bb/ui/knotty.py
> @@ -238,12 +238,16 @@ def main(server, eventHandler, tf =
> TerminalFilter):
>      helper = uihelper.BBUIHelper()
>  
>      console = logging.StreamHandler(sys.stdout)
> -    format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
> +    format_str = "%(levelname)s: %(message)s"
> +    format = bb.msg.BBLogFormatter(format_str)
> +    if interactive:
> +        format.enable_color()
>      bb.msg.addDefaultlogFilter(console)
>      console.setFormatter(format)
>      logger.addHandler(console)
>      if consolelogfile:
>          bb.utils.mkdirhier(os.path.dirname(consolelogfile))
> +        format = bb.msg.BBLogFormatter(format_str)
>          consolelog = logging.FileHandler(consolelogfile)
>          bb.msg.addDefaultlogFilter(consolelog)
>          consolelog.setFormatter(format)
> -- 
> 1.7.2.5
> 
> 
> 
> 
> On Wed, Nov 14, 2012 at 7:22 AM, Richard Purdie
> <richard.purdie@linuxfoundation.org> wrote:
>         On Wed, 2012-11-14 at 07:18 -0600, Seth Bollinger wrote:
>         > Thanks!  I've tested this and it works on my dark color
>         scheme (it's
>         > better actually :)).  I also tested on white and it looks
>         fine.  If
>         > Richard is ok with this solution, I'll make the changes and
>         submit
>         > another patch.
>         >
>         It sounds promising, I would like to try the patch...
>         
>         Cheers,
>         
>         Richard
>         
>         
>         
> 
> 





^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2] bitbake: Colorize knotty interactive console output
       [not found]                 ` <CA+JN8xN-vkLxtd+J4BZoQ39xs3Yujh-J=CQ8q+T1k0r-t-bpzA@mail.gmail.com>
@ 2012-11-16 14:45                   ` Seth Bollinger
  0 siblings, 0 replies; 15+ messages in thread
From: Seth Bollinger @ 2012-11-16 14:45 UTC (permalink / raw)
  To: bitbake-devel

[-- Attachment #1: Type: text/plain, Size: 4563 bytes --]

Turns out the record object is shared between handlers.  This was not
obvious to me at first glance.

Hopefully this is the final patch.  :)  This is against the bitbake repo
instead of poky.  Please let me know if this is ok.

Thanks for your patience.

Seth

--------------------------------------------------------------------------------------

Colorize knotty interactive console output

Add bold color output to log level name and standard color output to log msg
when bitbake is run from an iteractive console.  Color output is only
enabled
if the terminal supports color.

Used Jason Wessel's recommendation for transparency on verbose, note and
plain.

Signed-off-by: Seth Bollinger <seth.boll@gmail.com>
---
 lib/bb/msg.py       |   43 +++++++++++++++++++++++++++++++++++++++++++
 lib/bb/ui/knotty.py |    6 +++++-
 2 files changed, 48 insertions(+), 1 deletions(-)

diff --git a/lib/bb/msg.py b/lib/bb/msg.py
index 9b39325..007c95a 100644
--- a/lib/bb/msg.py
+++ b/lib/bb/msg.py
@@ -23,6 +23,7 @@ Message handling infrastructure for bitbake
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 import sys
+import copy
 import logging
 import collections
 from itertools import groupby
@@ -55,6 +56,25 @@ class BBLogFormatter(logging.Formatter):
         CRITICAL: 'ERROR',
     }

+    color_enabled = False
+    BASECOLOR, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE =
range(29,38)
+
+    COLORS = {
+        DEBUG3  : CYAN,
+        DEBUG2  : CYAN,
+        DEBUG   : CYAN,
+        VERBOSE : BASECOLOR,
+        NOTE    : BASECOLOR,
+        PLAIN   : BASECOLOR,
+        WARNING : YELLOW,
+        ERROR   : RED,
+        CRITICAL: RED,
+    }
+
+    BLD = '\033[1;%dm'
+    STD = '\033[%dm'
+    RST = '\033[0m'
+
     def getLevelName(self, levelno):
         try:
             return self.levelnames[levelno]
@@ -67,6 +87,8 @@ class BBLogFormatter(logging.Formatter):
         if record.levelno == self.PLAIN:
             msg = record.getMessage()
         else:
+            if self.color_enabled:
+                record = self.colorize(record)
             msg = logging.Formatter.format(self, record)

         if hasattr(record, 'bb_exc_info'):
@@ -75,6 +97,27 @@ class BBLogFormatter(logging.Formatter):
             msg += '\n' + ''.join(formatted)
         return msg

+    def colorize(self, record):
+        color = self.COLORS[record.levelno]
+        if self.color_enabled and color is not None:
+            record = copy.copy(record)
+            record.levelname = "".join([self.BLD % color,
record.levelname, self.RST])
+            record.msg = "".join([self.STD % color, record.msg, self.RST])
+        return record
+
+    def enable_color(self):
+        import curses
+        try:
+            win = None
+            win = curses.initscr()
+            if curses.has_colors():
+                self.color_enabled = True
+        except:
+            pass
+        finally:
+            if win is not None:
+                curses.endwin()
+
 class BBLogFilter(object):
     def __init__(self, handler, level, debug_domains):
         self.stdlevel = level
diff --git a/lib/bb/ui/knotty.py b/lib/bb/ui/knotty.py
index 77ec730..a63d8b1 100644
--- a/lib/bb/ui/knotty.py
+++ b/lib/bb/ui/knotty.py
@@ -238,12 +238,16 @@ def main(server, eventHandler, tf = TerminalFilter):
     helper = uihelper.BBUIHelper()

     console = logging.StreamHandler(sys.stdout)
-    format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
+    format_str = "%(levelname)s: %(message)s"
+    format = bb.msg.BBLogFormatter(format_str)
+    if interactive:
+        format.enable_color()
     bb.msg.addDefaultlogFilter(console)
     console.setFormatter(format)
     logger.addHandler(console)
     if consolelogfile:
         bb.utils.mkdirhier(os.path.dirname(consolelogfile))
+        format = bb.msg.BBLogFormatter(format_str)
         consolelog = logging.FileHandler(consolelogfile)
         bb.msg.addDefaultlogFilter(consolelog)
         consolelog.setFormatter(format)
-- 
1.7.2.5


On Wed, Nov 14, 2012 at 5:26 PM, Richard Purdie <
richard.purdie@linuxfoundation.org> wrote:
>
> This works much better. I've realised the escape sequences are ending up
> in logfiles like the cooker log though so some further tweaking is
> needed...
>
> Also, your patches are coming through linewrapped so I have to fix them
> manually before applying.
>
> Cheers,
>
> Richard
>

[-- Attachment #2: Type: text/html, Size: 6287 bytes --]

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-11 15:17 [PATCH v2] bitbake: Colorize knotty interactive console output Seth Bollinger
  2012-11-11 22:54 ` Chris Larson
  2012-11-13 14:23 ` Richard Purdie
@ 2012-11-22  3:36 ` Robert Yang
  2012-11-25 21:37   ` Richard Purdie
  2 siblings, 1 reply; 15+ messages in thread
From: Robert Yang @ 2012-11-22  3:36 UTC (permalink / raw)
  To: Seth Bollinger; +Cc: bitbake-devel


I'm afraid that this patch doesn't work well, it will output chaotic message
after the "Ctrl-Z" and "fg":

On Fedora 17 64 bit
$ screen
$ bitbake xxx
$ C-Z
$ fg

then it will output like this:

(164 of 9052): 
                                                                          0: 
linux-libc-headers-3.4.3-r0 do_fetch (pid 10632) 
           Currently 4 running tasks (164 of 9052):
          0: linux-libc-headers-3.4.3-r0 do_fetch (pid 1Currently 4 running 
tasks (165 of 9052): 
 
0: linux-libc-headers-3.4.3-r0 do_fetch (pid 1Currently 5 running tasks (165 of 
9052): 
                                                                  0: 
linux-libc-headers-3.4.3-r0 do_fetch (pid 10632)
      Currently 4 running tasks (165 of 9052): 
 
                           0: linux-libc-headers-3.4.3-r0 do_fetch (pid 
1Currently 4 running tasks (166 of 9052): 
 
                      0: linux-libc-headers-3.4.3-r0 do_fetch (pid 1Currently 5 
running tasks (166 of 9052):
 
0: linux-libc-headers-3.4.3-r0 do_fetch (pid 10632) 
                    Currently 4 running tasks (166 of 9052): 
 
                                         0: linux-libc-headers-3.4.3-r0 do_fetch 
(pid 1Currently 4 running tasks (167 of 9052): 
 
                           0: linux-libc-headers-3.4.3-r0 do_fetch (pid 
1Currently 5 running tasks (167 of 9052):


Revert this patch would be OK.

// Robert

On 11/11/2012 11:17 PM, Seth Bollinger wrote:
> Add bold color output to log level name and standard color output to log msg
> when bitbake is run from an iteractive console.  Color output is only
> enabled
> if the terminal supports color.
>
> Signed-off-by: Seth Bollinger <seth.boll@gmail.com>
> ---
>   bitbake/lib/bb/msg.py       |   40 ++++++++++++++++++++++++++++++++++++++++
>   bitbake/lib/bb/ui/knotty.py |    6 +++++-
>   2 files changed, 45 insertions(+), 1 deletions(-)
>
> diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
> index 9b39325..68b2ad7 100644
> --- a/bitbake/lib/bb/msg.py
> +++ b/bitbake/lib/bb/msg.py
> @@ -55,6 +55,25 @@ class BBLogFormatter(logging.Formatter):
>           CRITICAL: 'ERROR',
>       }
>
> +    color_enabled = False
> +    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30,38)
> +
> +    COLORS = {
> +        DEBUG3  : CYAN,
> +        DEBUG2  : CYAN,
> +        DEBUG   : CYAN,
> +        VERBOSE : WHITE,
> +        NOTE    : WHITE,
> +        PLAIN   : WHITE,
> +        WARNING : YELLOW,
> +        ERROR   : RED,
> +        CRITICAL: RED,
> +    }
> +
> +    BLD = '\033[1;%dm'
> +    STD = '\033[%dm'
> +    RST = '\033[0m'
> +
>       def getLevelName(self, levelno):
>           try:
>               return self.levelnames[levelno]
> @@ -67,6 +86,8 @@ class BBLogFormatter(logging.Formatter):
>           if record.levelno == self.PLAIN:
>               msg = record.getMessage()
>           else:
> +            if self.color_enabled:
> +                self.colorize(record)
>               msg = logging.Formatter.format(self, record)
>
>           if hasattr(record, 'bb_exc_info'):
> @@ -75,6 +96,25 @@ class BBLogFormatter(logging.Formatter):
>               msg += '\n' + ''.join(formatted)
>           return msg
>
> +    def colorize(self, record):
> +        color = self.COLORS[record.levelno]
> +        if self.color_enabled and color is not None:
> +            record.levelname = "".join([self.BLD % color,
> record.levelname, self.RST])
> +            record.msg = "".join([self.STD % color, record.msg, self.RST])
> +
> +    def enable_color(self):
> +        import curses
> +        try:
> +            win = None
> +            win = curses.initscr()
> +            if curses.has_colors():
> +                self.color_enabled = True
> +        except:
> +            pass
> +        finally:
> +            if win is not None:
> +                curses.endwin()
> +
>   class BBLogFilter(object):
>       def __init__(self, handler, level, debug_domains):
>           self.stdlevel = level
> diff --git a/bitbake/lib/bb/ui/knotty.py b/bitbake/lib/bb/ui/knotty.py
> index b99a121..d9aa973 100644
> --- a/bitbake/lib/bb/ui/knotty.py
> +++ b/bitbake/lib/bb/ui/knotty.py
> @@ -238,12 +238,16 @@ def main(server, eventHandler, tf = TerminalFilter):
>       helper = uihelper.BBUIHelper()
>
>       console = logging.StreamHandler(sys.stdout)
> -    format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
> +    format_str = "%(levelname)s: %(message)s"
> +    format = bb.msg.BBLogFormatter(format_str)
> +    if interactive:
> +        format.enable_color()
>       bb.msg.addDefaultlogFilter(console)
>       console.setFormatter(format)
>       logger.addHandler(console)
>       if consolelogfile:
>           bb.utils.mkdirhier(os.path.dirname(consolelogfile))
> +        format = bb.msg.BBLogFormatter(format_str)
>           consolelog = logging.FileHandler(consolelogfile)
>           bb.msg.addDefaultlogFilter(consolelog)
>           consolelog.setFormatter(format)
>
>
>
> _______________________________________________
> bitbake-devel mailing list
> bitbake-devel@lists.openembedded.org
> http://lists.linuxtogo.org/cgi-bin/mailman/listinfo/bitbake-devel
>



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] bitbake: Colorize knotty interactive console output
  2012-11-22  3:36 ` Robert Yang
@ 2012-11-25 21:37   ` Richard Purdie
  0 siblings, 0 replies; 15+ messages in thread
From: Richard Purdie @ 2012-11-25 21:37 UTC (permalink / raw)
  To: Robert Yang; +Cc: bitbake-devel

On Thu, 2012-11-22 at 11:36 +0800, Robert Yang wrote:
> I'm afraid that this patch doesn't work well, it will output chaotic message
> after the "Ctrl-Z" and "fg":

I could reproduce this and have pushed a fix for it.

Cheers,

Richard




^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2012-11-26  9:36 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-11 15:17 [PATCH v2] bitbake: Colorize knotty interactive console output Seth Bollinger
2012-11-11 22:54 ` Chris Larson
2012-11-12  2:02   ` Seth Bollinger
2012-11-12  2:24     ` Chris Larson
2012-11-13 14:23 ` Richard Purdie
2012-11-13 14:45   ` Seth Bollinger
2012-11-13 15:22     ` Richard Purdie
2012-11-13 22:36       ` Jason Wessel
2012-11-14 13:18         ` Seth Bollinger
2012-11-14 13:22           ` Richard Purdie
2012-11-14 14:43             ` Seth Bollinger
2012-11-14 23:26               ` Richard Purdie
     [not found]                 ` <CA+JN8xN-vkLxtd+J4BZoQ39xs3Yujh-J=CQ8q+T1k0r-t-bpzA@mail.gmail.com>
2012-11-16 14:45                   ` Seth Bollinger
2012-11-22  3:36 ` Robert Yang
2012-11-25 21:37   ` Richard Purdie

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.