All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [RFC PATCH 1/1] test.sh: colorize the output
@ 2017-01-05  8:57 Petr Vorel
  2017-01-05  9:24 ` Cyril Hrubis
  0 siblings, 1 reply; 3+ messages in thread
From: Petr Vorel @ 2017-01-05  8:57 UTC (permalink / raw)
  To: ltp

Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
---
Hi, I know you're busy with release and this is just a toy for interactive
running. But it you find it useful for upstream, I'll try to polish it and
implement TODO.

TODO:
* Create environment variable or getopt switch for enabling/disabling
  colors (instead of macro USE_ANSCI_COLOR), default off. Work in
  similar way like ls (--color[=WHEN], WHEN can be 'always' (default if
  omitted), 'auto', or 'never').
* DRY: Keep default definition only on one place => generate with make?
* Allow user to define colors (overwrite with environment variables).
---
 include/ansi_colors.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/tst_res.c         | 33 ++++++++++++++++++++++++++++++---
 lib/tst_test.c        | 29 ++++++++++++++++++++++++-----
 testcases/lib/test.sh | 25 ++++++++++++++++++++++++-
 4 files changed, 129 insertions(+), 9 deletions(-)
 create mode 100644 include/ansi_colors.h

diff --git a/include/ansi_colors.h b/include/ansi_colors.h
new file mode 100644
index 000000000..3db86a677
--- /dev/null
+++ b/include/ansi_colors.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2017 Petr Vorel <petr.vorel@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * NOTE: these colors should match colors defined in tst_flag2color() in
+ * testcases/lib/test.sh
+ */
+#define ANSI_COLOR_BLUE	"\e[1;34m"
+#define ANSI_COLOR_GREEN	"\e[1;32m"
+#define ANSI_COLOR_RED		"\e[1;31m"
+#define ANSI_COLOR_YELLOW	"\e[1;33m"
+#define ANSI_COLOR_RESET	"\E[00m"
+
+static inline char* ttype2color(int ttype) {
+	switch (TTYPE_RESULT(ttype)) {
+	case TPASS:
+		return ANSI_COLOR_GREEN;
+	break;
+	case TFAIL:
+		return ANSI_COLOR_RED;
+	break;
+	case TBROK:
+		return ANSI_COLOR_YELLOW;
+	break;
+	case TCONF:
+		return ANSI_COLOR_YELLOW;
+	break;
+	case TWARN:
+		return ANSI_COLOR_YELLOW;
+	break;
+	case TINFO:
+		return ANSI_COLOR_GREEN;
+	break;
+	default:
+		return "";
+	}
+}
diff --git a/lib/tst_res.c b/lib/tst_res.c
index 261dec0fb..501a3f64d 100644
--- a/lib/tst_res.c
+++ b/lib/tst_res.c
@@ -50,6 +50,7 @@
 #include "test.h"
 #include "usctest.h"
 #include "ltp_priv.h"
+#include "ansi_colors.h"
 
 long TEST_RETURN;
 int TEST_ERRNO;
@@ -63,6 +64,8 @@ int TEST_ERRNO;
 #define TRUE         1
 #define FALSE        0
 
+#define USE_ANSCI_COLOR
+
 /*
  * EXPAND_VAR_ARGS - Expand the variable portion (arg_fmt) of a result
  *                   message into the specified string.
@@ -254,7 +257,7 @@ static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg)
 	const char *type;
 	int ttype_result = TTYPE_RESULT(ttype);
 	char message[USERMESG];
-	size_t size;
+	size_t size = 0;
 
 	/*
 	 * Save the test result type by ORing ttype into the current exit value
@@ -280,11 +283,23 @@ static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg)
 	 * Build the result line and print it.
 	 */
 	type = strttype(ttype);
+
+#ifdef USE_ANSCI_COLOR
+	char *color = ttype2color(ttype);
+
+	size += snprintf(message + size, sizeof(message) - size, color);
+
+	if (size >= sizeof(message)) {
+		printf("%s: %i: line too long\n", __func__, __LINE__);
+		abort();
+	}
+#endif
+
 	if (T_mode == VERBOSE) {
-		size = snprintf(message, sizeof(message),
+		size += snprintf(message + size, sizeof(message) - size,
 				"%-8s %4d  %s  :  %s", tcid, tnum, type, tmesg);
 	} else {
-		size = snprintf(message, sizeof(message),
+		size += snprintf(message + size, sizeof(message) - size,
 				"%-8s %4d       %s  :  %s",
 				tcid, tnum, type, tmesg);
 	}
@@ -305,6 +320,8 @@ static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg)
 		abort();
 	}
 
+
+
 	if (ttype & TTERRNO) {
 		size += snprintf(message + size, sizeof(message) - size,
 				 ": TEST_ERRNO=%s(%i): %s",
@@ -324,6 +341,16 @@ static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg)
 				 strerror(TEST_RETURN));
 	}
 
+#ifdef USE_ANSCI_COLOR
+	if (size >= sizeof(message)) {
+		printf("%s: %i: line too long\n", __func__, __LINE__);
+		abort();
+	}
+
+	size += snprintf(message + size, sizeof(message) - size,
+			 ANSI_COLOR_RESET);
+#endif
+
 	if (size + 1 >= sizeof(message)) {
 		printf("%s: %i: line too long\n", __func__, __LINE__);
 		abort();
diff --git a/lib/tst_test.c b/lib/tst_test.c
index c48d71877..17e686c9b 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -29,6 +29,7 @@
 #include "tst_test.h"
 #include "tst_device.h"
 #include "lapi/futex.h"
+#include "ansi_colors.h"
 
 #include "old_resource.h"
 #include "old_device.h"
@@ -58,6 +59,8 @@ extern unsigned int tst_max_futexes;
 
 #define IPC_ENV_VAR "LTP_IPC_PATH"
 
+#define USE_ANSCI_COLOR
+
 static char ipc_path[1024];
 const char *tst_ipc_path = ipc_path;
 char *const tst_ipc_envp[] = {ipc_path, NULL};
@@ -194,20 +197,36 @@ static void print_result(const char *file, const int lineno, int ttype,
 	if (ttype & TTERRNO)
 		str_errno = tst_strerrno(TEST_ERRNO);
 
-	ret = snprintf(str, size, "%s:%i: %s: ", file, lineno, res);
+#ifdef USE_ANSCI_COLOR
+	char *color = ttype2color(ttype);
 
+	ret = snprintf(str, size, "%s", color);
+	str += ret;
+	size -= ret;
+#endif
+
+	ret = snprintf(str, size, "%s:%i: %s: ", file, lineno, res);
 	str += ret;
 	size -= ret;
 
 	ret = vsnprintf(str, size, fmt, va);
+	str += ret;
+	size -= ret;
+
+	if (str_errno) {
+		ret = snprintf(str, size, ": %s", str_errno);
+		str += ret;
+		size -= ret;
+	}
 
+#ifdef USE_ANSCI_COLOR
+	ret = snprintf(str, size, ANSI_COLOR_RESET);
 	str += ret;
 	size -= ret;
+#endif
+
+	snprintf(str, size, "\n");
 
-	if (str_errno)
-		snprintf(str, size, ": %s\n", str_errno);
-	else
-		snprintf(str, size, "\n");
 
 	fputs(buf, stderr);
 }
diff --git a/testcases/lib/test.sh b/testcases/lib/test.sh
index 76b706267..535ea8724 100644
--- a/testcases/lib/test.sh
+++ b/testcases/lib/test.sh
@@ -39,15 +39,38 @@ tst_flag2mask()
 	esac
 }
 
+tst_flag2color()
+{
+	# NOTE: these colors should match colors defined in include/ansi_colors.h
+	local blue='\e[1;34m'
+	local green='\e[1;32m'
+	local red='\e[1;31m'
+	local yellow='\e[1;33m'
+
+	case "$1" in
+	TPASS) printf $green;;
+	TFAIL) printf $red;;
+	TBROK) printf $yellow;;
+	TWARN) printf $yellow;;
+	TINFO) printf $blue;;
+	TCONF) printf $yellow;;
+	*) tst_brkm TBROK "Invalid resm type '$1'";;
+	esac
+}
+
 tst_resm()
 {
-	tst_flag2mask "$1"
+	local ttype="$1"
+	tst_flag2mask "$ttype"
 	local mask=$?
 	LTP_RET_VAL=$((LTP_RET_VAL|mask))
 
 	local ret=$1
 	shift
+
+	tst_flag2color "$ttype"
 	echo "$TCID $TST_COUNT $ret : $@"
+	printf '\E[00m'
 
 	case "$ret" in
 	TPASS|TFAIL)
-- 
2.11.0


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

* [LTP] [RFC PATCH 1/1] test.sh: colorize the output
  2017-01-05  8:57 [LTP] [RFC PATCH 1/1] test.sh: colorize the output Petr Vorel
@ 2017-01-05  9:24 ` Cyril Hrubis
  2017-01-05 12:41   ` Petr Vorel
  0 siblings, 1 reply; 3+ messages in thread
From: Cyril Hrubis @ 2017-01-05  9:24 UTC (permalink / raw)
  To: ltp

Hi!
> Hi, I know you're busy with release and this is just a toy for interactive
> running. But it you find it useful for upstream, I'll try to polish it and
> implement TODO.

I guess that we can add support for colors.

> TODO:
> * Create environment variable or getopt switch for enabling/disabling
>   colors (instead of macro USE_ANSCI_COLOR), default off. Work in
>   similar way like ls (--color[=WHEN], WHEN can be 'always' (default if
>   omitted), 'auto', or 'never').

Adding more default options for testcases does not seem to be good idea
to me, environment variable sounds much better.

I would expect colorized output enabled by default, but the color output
would be produced only if we are writing to actual terminal. Utils such
as grep use stat() on stdout to check if it's char device and isatty()
as well. And we should also have some env variable, something as
LTP_COLORIZE_OUTPUT to override the decision.

> * DRY: Keep default definition only on one place => generate with make?
> * Allow user to define colors (overwrite with environment variables).

User defined colors does not sound that useful to me.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [RFC PATCH 1/1] test.sh: colorize the output
  2017-01-05  9:24 ` Cyril Hrubis
@ 2017-01-05 12:41   ` Petr Vorel
  0 siblings, 0 replies; 3+ messages in thread
From: Petr Vorel @ 2017-01-05 12:41 UTC (permalink / raw)
  To: ltp

Hi Cyril,

thanks for your comments!

> I guess that we can add support for colors.
Thanks! It might take some time as it's my private activity it has lower priority.

> Adding more default options for testcases does not seem to be good idea
> to me, environment variable sounds much better.
Right, I also agree.

> I would expect colorized output enabled by default, but the color output
> would be produced only if we are writing to actual terminal. Utils such
> as grep use stat() on stdout to check if it's char device and isatty()
> as well.
Oh yes, I meant this behaviour. Looking into grep and ls (coreutils) sources, they both
use similar approach - using stat()/fstat() and isatty().

> And we should also have some env variable, something as
> LTP_COLORIZE_OUTPUT to override the decision.
Good point. This comes instead of grep/ls --color= switch.

> > * Allow user to define colors (overwrite with environment variables).
> User defined colors does not sound that useful to me.
I was for that as not sure as everyone uses different terminal settings, has different
opinions about colors. And both ls and grep allow it. Although it has lowest priority
(detecting with isatty() is much more important).


Kind regards,
Petr

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

end of thread, other threads:[~2017-01-05 12:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-05  8:57 [LTP] [RFC PATCH 1/1] test.sh: colorize the output Petr Vorel
2017-01-05  9:24 ` Cyril Hrubis
2017-01-05 12:41   ` Petr Vorel

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.