Skip to contentSkip to conversation

kunit: tool: Do not colorize output when redirected

PATCH In review Kees Cook 4 participants 5 messages Feb 24, 2022 – Mar 23, 2022

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 kunit: tool: Do not colorize output when redirected

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

---
 tools/testing/kunit/kunit_parser.py | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/kunit/kunit_parser.py +7 -0
index 05ff334761dd..807ed2bd6832 100644
@@ -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(''))
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Kees Cook

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

---

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

This works fine here.

Thanks,
-- David

Quoted 43 lines
 tools/testing/kunit/kunit_parser.py | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/kunit/kunit_parser.py +7 -1
index 05ff334761dd..807ed2bd6832 100644
@@ -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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220224055350.1854078-1-keescook%40chromium.org.

author
#2 lore raw
Quoted 5 lines · David Gow
Quoted 4 lines · Kees Cook

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

Quoted 7 lines
Quoted 5 lines

---

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. ;)

Quoted 3 lines

This works fine here.

Thanks!

-Kees

Quoted 47 lines

Thanks,
-- David

Quoted 43 lines
 tools/testing/kunit/kunit_parser.py | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/kunit/kunit_parser.py +7 -1
index 05ff334761dd..807ed2bd6832 100644
@@ -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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220224055350.1854078-1-keescook%40chromium.org.

Signature · 1 line
Kees Cook
#3 lore raw
In reply to Kees Cook
Quoted 25 lines · Kees Cook

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

---
 tools/testing/kunit/kunit_parser.py | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/kunit/kunit_parser.py +1 -0
index 05ff334761dd..807ed2bd6832 100644
@@ -11,6 +11,7 @@
 
 from __future__ import annotations
 import re
+import sys
 
 import datetime
 from enum import Enum, auto

tools/testing/kunit/kunit_parser.py RESET = '\033[0;0m' · line 508

Quoted 5 lines
tools/testing/kunit/kunit_parser.py +2 -0
@@ -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.

Quoted 22 lines
tools/testing/kunit/kunit_parser.py +4 -1
        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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220224055350.1854078-1-keescook%40chromium.org.