[PATCH libinput 2/4] test: add color to litest verbose output

Peter Hutterer peter.hutterer at who-t.net
Tue Mar 28 10:07:50 UTC 2017


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/libinput-util.h |  4 ++++
 test/litest.c       | 29 +++++++++++++++++++++++++----
 tools/shared.c      |  3 ---
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/libinput-util.h b/src/libinput-util.h
index d86ff12..73af689 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -53,6 +53,10 @@
 /* The HW DPI rate we normalize to before calculating pointer acceleration */
 #define DEFAULT_MOUSE_DPI 1000
 
+#define ANSI_HIGHLIGHT "\x1B[0;1;39m"
+#define ANSI_RED "\x1B[0;31m"
+#define ANSI_NORMAL "\x1B[0m"
+
 #define CASE_RETURN_STRING(a) case a: return #a
 
 /*
diff --git a/test/litest.c b/test/litest.c
index f9797b7..d91a524 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -785,19 +785,40 @@ litest_log_handler(struct libinput *libinput,
 		   const char *format,
 		   va_list args)
 {
+	static int is_tty = -1;
 	const char *priority = NULL;
+	const char *color;
+
+	if (is_tty == -1)
+		is_tty = isatty(STDERR_FILENO);
 
 	switch(pri) {
-	case LIBINPUT_LOG_PRIORITY_INFO: priority =  "info "; break;
-	case LIBINPUT_LOG_PRIORITY_ERROR: priority = "error"; break;
-	case LIBINPUT_LOG_PRIORITY_DEBUG: priority = "debug"; break;
+	case LIBINPUT_LOG_PRIORITY_INFO:
+		priority =  "info ";
+		color = ANSI_HIGHLIGHT;
+		break;
+	case LIBINPUT_LOG_PRIORITY_ERROR:
+		priority = "error";
+		color = ANSI_RED;
+		break;
+	case LIBINPUT_LOG_PRIORITY_DEBUG:
+		priority = "debug";
+		color = ANSI_NORMAL;
+		break;
 	default:
 		  abort();
 	}
 
-	fprintf(stderr, "litest %s ", priority);
+	if (!is_tty)
+		color = "";
+
+	fprintf(stderr, "%slitest %s ", color, priority);
+
 	vfprintf(stderr, format, args);
 
+	if (is_tty)
+		fprintf(stderr, ANSI_NORMAL);
+
 	if (strstr(format, "client bug: ") ||
 	    strstr(format, "libinput bug: "))
 		litest_abort_msg("libinput bug triggered, aborting.\n");
diff --git a/tools/shared.c b/tools/shared.c
index c4f19f8..830e0cb 100644
--- a/tools/shared.c
+++ b/tools/shared.c
@@ -73,9 +73,6 @@ log_handler(struct libinput *li,
 	    const char *format,
 	    va_list args)
 {
-#define ANSI_HIGHLIGHT "\x1B[0;1;39m"
-#define ANSI_RED "\x1B[0;31m"
-#define ANSI_NORMAL "\x1B[0m"
 	static int is_tty = -1;
 
 	if (is_tty == -1)
-- 
2.9.3



More information about the wayland-devel mailing list