linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kunit: tool: Do not colorize output when redirected
@ 2022-02-24  5:53 Kees Cook
  2022-02-24  6:43 ` David Gow
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Kees Cook @ 2022-02-24  5:53 UTC (permalink / raw)
  To: Brendan Higgins
  Cc: Kees Cook, linux-kselftest, kunit-dev, David Gow, linux-kernel,
	linux-hardening

Filling log files with color codes makes diffs and other comparisons
difficult. Only emit vt100 codes when the stdout is a TTY.

Cc: Brendan Higgins <brendanhiggins@google.com>
Cc: linux-kselftest@vger.kernel.org
Cc: kunit-dev@googlegroups.com
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 tools/testing/kunit/kunit_parser.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tools/testing/kunit/kunit_parser.py b/tools/testing/kunit/kunit_parser.py
index 05ff334761dd..807ed2bd6832 100644
--- a/tools/testing/kunit/kunit_parser.py
+++ b/tools/testing/kunit/kunit_parser.py
@@ -11,6 +11,7 @@
 
 from __future__ import annotations
 import re
+import sys
 
 import datetime
 from enum import Enum, auto
@@ -503,14 +504,20 @@ RESET = '\033[0;0m'
 
 def red(text: str) -> str:
 	"""Returns inputted string with red color code."""
+	if not sys.stdout.isatty():
+		return text
 	return '\033[1;31m' + text + RESET
 
 def yellow(text: str) -> str:
 	"""Returns inputted string with yellow color code."""
+	if not sys.stdout.isatty():
+		return text
 	return '\033[1;33m' + text + RESET
 
 def green(text: str) -> str:
 	"""Returns inputted string with green color code."""
+	if not sys.stdout.isatty():
+		return text
 	return '\033[1;32m' + text + RESET
 
 ANSI_LEN = len(red(''))
-- 
2.30.2


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

* Re: [PATCH] kunit: tool: Do not colorize output when redirected
  2022-02-24  5:53 [PATCH] kunit: tool: Do not colorize output when redirected Kees Cook
@ 2022-02-24  6:43 ` David Gow
  2022-02-24  6:46   ` Kees Cook
  2022-02-24 18:28 ` Daniel Latypov
  2022-03-23 23:30 ` Brendan Higgins
  2 siblings, 1 reply; 5+ messages in thread
From: David Gow @ 2022-02-24  6:43 UTC (permalink / raw)
  To: Kees Cook
  Cc: Brendan Higgins, open list:KERNEL SELFTEST FRAMEWORK,
	KUnit Development, Linux Kernel Mailing List, linux-hardening

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

On Thu, Feb 24, 2022 at 1:53 PM Kees Cook <keescook@chromium.org> wrote:
>
> Filling log files with color codes makes diffs and other comparisons
> difficult. Only emit vt100 codes when the stdout is a TTY.
>
> Cc: Brendan Higgins <brendanhiggins@google.com>
> Cc: linux-kselftest@vger.kernel.org
> Cc: kunit-dev@googlegroups.com
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---

Ah, you can tell a tool has "made it" when people are redirecting its output!

This works fine here.

Reviewed-by: David Gow <davidgow@google.com>

Thanks,
-- David

>  tools/testing/kunit/kunit_parser.py | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/tools/testing/kunit/kunit_parser.py b/tools/testing/kunit/kunit_parser.py
> index 05ff334761dd..807ed2bd6832 100644
> --- a/tools/testing/kunit/kunit_parser.py
> +++ b/tools/testing/kunit/kunit_parser.py
> @@ -11,6 +11,7 @@
>
>  from __future__ import annotations
>  import re
> +import sys
>
>  import datetime
>  from enum import Enum, auto
> @@ -503,14 +504,20 @@ RESET = '\033[0;0m'
>
>  def red(text: str) -> str:
>         """Returns inputted string with red color code."""
> +       if not sys.stdout.isatty():
> +               return text
>         return '\033[1;31m' + text + RESET
>
>  def yellow(text: str) -> str:
>         """Returns inputted string with yellow color code."""
> +       if not sys.stdout.isatty():
> +               return text
>         return '\033[1;33m' + text + RESET
>
>  def green(text: str) -> str:
>         """Returns inputted string with green color code."""
> +       if not sys.stdout.isatty():
> +               return text
>         return '\033[1;32m' + text + RESET
>
>  ANSI_LEN = len(red(''))
> --
> 2.30.2
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220224055350.1854078-1-keescook%40chromium.org.

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4003 bytes --]

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

* Re: [PATCH] kunit: tool: Do not colorize output when redirected
  2022-02-24  6:43 ` David Gow
@ 2022-02-24  6:46   ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2022-02-24  6:46 UTC (permalink / raw)
  To: David Gow
  Cc: Brendan Higgins, open list:KERNEL SELFTEST FRAMEWORK,
	KUnit Development, Linux Kernel Mailing List, linux-hardening

On Thu, Feb 24, 2022 at 02:43:53PM +0800, David Gow wrote:
> On Thu, Feb 24, 2022 at 1:53 PM Kees Cook <keescook@chromium.org> wrote:
> >
> > Filling log files with color codes makes diffs and other comparisons
> > difficult. Only emit vt100 codes when the stdout is a TTY.
> >
> > Cc: Brendan Higgins <brendanhiggins@google.com>
> > Cc: linux-kselftest@vger.kernel.org
> > Cc: kunit-dev@googlegroups.com
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> 
> Ah, you can tell a tool has "made it" when people are redirecting its output!

Heheh. Indeed! I have some more patches coming where I put a diff of
output in the commit log. ;)

> This works fine here.
> 
> Reviewed-by: David Gow <davidgow@google.com>

Thanks!

-Kees

> 
> Thanks,
> -- David
> 
> >  tools/testing/kunit/kunit_parser.py | 7 +++++++
> >  1 file changed, 7 insertions(+)
> >
> > diff --git a/tools/testing/kunit/kunit_parser.py b/tools/testing/kunit/kunit_parser.py
> > index 05ff334761dd..807ed2bd6832 100644
> > --- a/tools/testing/kunit/kunit_parser.py
> > +++ b/tools/testing/kunit/kunit_parser.py
> > @@ -11,6 +11,7 @@
> >
> >  from __future__ import annotations
> >  import re
> > +import sys
> >
> >  import datetime
> >  from enum import Enum, auto
> > @@ -503,14 +504,20 @@ RESET = '\033[0;0m'
> >
> >  def red(text: str) -> str:
> >         """Returns inputted string with red color code."""
> > +       if not sys.stdout.isatty():
> > +               return text
> >         return '\033[1;31m' + text + RESET
> >
> >  def yellow(text: str) -> str:
> >         """Returns inputted string with yellow color code."""
> > +       if not sys.stdout.isatty():
> > +               return text
> >         return '\033[1;33m' + text + RESET
> >
> >  def green(text: str) -> str:
> >         """Returns inputted string with green color code."""
> > +       if not sys.stdout.isatty():
> > +               return text
> >         return '\033[1;32m' + text + RESET
> >
> >  ANSI_LEN = len(red(''))
> > --
> > 2.30.2
> >
> > --
> > You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220224055350.1854078-1-keescook%40chromium.org.



-- 
Kees Cook

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

* Re: [PATCH] kunit: tool: Do not colorize output when redirected
  2022-02-24  5:53 [PATCH] kunit: tool: Do not colorize output when redirected Kees Cook
  2022-02-24  6:43 ` David Gow
@ 2022-02-24 18:28 ` Daniel Latypov
  2022-03-23 23:30 ` Brendan Higgins
  2 siblings, 0 replies; 5+ messages in thread
From: Daniel Latypov @ 2022-02-24 18:28 UTC (permalink / raw)
  To: Kees Cook
  Cc: Brendan Higgins, linux-kselftest, kunit-dev, David Gow,
	linux-kernel, linux-hardening

On Wed, Feb 23, 2022 at 9:53 PM Kees Cook <keescook@chromium.org> wrote:
>
> Filling log files with color codes makes diffs and other comparisons
> difficult. Only emit vt100 codes when the stdout is a TTY.
>
> Cc: Brendan Higgins <brendanhiggins@google.com>
> Cc: linux-kselftest@vger.kernel.org
> Cc: kunit-dev@googlegroups.com
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  tools/testing/kunit/kunit_parser.py | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/tools/testing/kunit/kunit_parser.py b/tools/testing/kunit/kunit_parser.py
> index 05ff334761dd..807ed2bd6832 100644
> --- a/tools/testing/kunit/kunit_parser.py
> +++ b/tools/testing/kunit/kunit_parser.py
> @@ -11,6 +11,7 @@
>
>  from __future__ import annotations
>  import re
> +import sys
>
>  import datetime
>  from enum import Enum, auto
> @@ -503,14 +504,20 @@ RESET = '\033[0;0m'
>
>  def red(text: str) -> str:
>         """Returns inputted string with red color code."""
> +       if not sys.stdout.isatty():
> +               return text

I'd been thinking about doing this for a while.
I'd just gotten used to removing them via sed, or ending pipes with less -R.
I totally agree it's for the best that colors be turned off by default
when piping.

My hesitation was whether we'd want to introduce a flag like grep et al have.
I.e. --color=auto/always/etc.

Pros:
* consistency with grep, maybe less surprise
* allows wrappers around kunit.py to redirect output and still show color
   * this is very niche and I know it. But I happen to own such a wrapper :)

Cons:
* an extra flag that's very niche
  * I'm likely the only person to have ever used `kunit.py run | ... | less -R`
* the logic to check for coloring becomes more complicated, need to add a helper

I'm fine if we just stick with this simpler approach or do it later.
My aforementioned wrapper only redirects stdout when necessary anyways.


>         return '\033[1;31m' + text + RESET
>
>  def yellow(text: str) -> str:
>         """Returns inputted string with yellow color code."""
> +       if not sys.stdout.isatty():
> +               return text
>         return '\033[1;33m' + text + RESET
>
>  def green(text: str) -> str:
>         """Returns inputted string with green color code."""
> +       if not sys.stdout.isatty():
> +               return text
>         return '\033[1;32m' + text + RESET
>
>  ANSI_LEN = len(red(''))
> --
> 2.30.2
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220224055350.1854078-1-keescook%40chromium.org.

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

* Re: [PATCH] kunit: tool: Do not colorize output when redirected
  2022-02-24  5:53 [PATCH] kunit: tool: Do not colorize output when redirected Kees Cook
  2022-02-24  6:43 ` David Gow
  2022-02-24 18:28 ` Daniel Latypov
@ 2022-03-23 23:30 ` Brendan Higgins
  2 siblings, 0 replies; 5+ messages in thread
From: Brendan Higgins @ 2022-03-23 23:30 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kselftest, kunit-dev, David Gow, linux-kernel, linux-hardening

On Thu, Feb 24, 2022 at 12:53 AM Kees Cook <keescook@chromium.org> wrote:
>
> Filling log files with color codes makes diffs and other comparisons
> difficult. Only emit vt100 codes when the stdout is a TTY.
>
> Cc: Brendan Higgins <brendanhiggins@google.com>
> Cc: linux-kselftest@vger.kernel.org
> Cc: kunit-dev@googlegroups.com
> Signed-off-by: Kees Cook <keescook@chromium.org>

Reviewed-by: Brendan Higgins <brendanhiggins@google.com>

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

end of thread, other threads:[~2022-03-23 23:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-24  5:53 [PATCH] kunit: tool: Do not colorize output when redirected Kees Cook
2022-02-24  6:43 ` David Gow
2022-02-24  6:46   ` Kees Cook
2022-02-24 18:28 ` Daniel Latypov
2022-03-23 23:30 ` Brendan Higgins

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).