native_posix: Add control of traces color
For native_posix:
Added command line options to control if traces should have
or not colors.
+
Detect if we are connected to a tty or not, and if we are
set the defaults for that option appropriately
Signed-off-by: Alberto Escolar Piedras <alpi@oticon.com>
diff --git a/arch/posix/include/posix_trace.h b/arch/posix/include/posix_trace.h
index f7d47bc..ae160b0 100644
--- a/arch/posix/include/posix_trace.h
+++ b/arch/posix/include/posix_trace.h
@@ -13,6 +13,19 @@
void posix_print_error_and_exit(const char *format, ...);
void posix_print_warning(const char *format, ...);
void posix_print_trace(const char *format, ...);
+/**
+ * Return 1 if traces to <output> will go to a tty.
+ * When printing to a terminal we may use ASCII escapes for color or other
+ * niceties.
+ * But when redirecting to files, or piping to other commands, those should be
+ * disabled by default.
+ *
+ * Where the <output> should be set to 0 to query about posix_print_trace output
+ * (typically STDOUT)
+ * and 1 to query about the warning and error output (posix_print_error/warning)
+ * outputs (typically STDERR)
+ */
+int posix_trace_over_tty(int output);
#ifdef __cplusplus
}
diff --git a/boards/posix/native_posix/cmdline.c b/boards/posix/native_posix/cmdline.c
index fdfcf71..754080d 100644
--- a/boards/posix/native_posix/cmdline.c
+++ b/boards/posix/native_posix/cmdline.c
@@ -14,6 +14,7 @@
#include "cmdline.h"
#include "toolchain.h"
#include "posix_trace.h"
+#include "native_tracing.h"
static int s_argc, test_argc;
static char **s_argv, **test_argv;
@@ -89,7 +90,6 @@
(void *)NULL, NULL,
"Any argument that follows will be ignored by the top level, "
"and made available for possible tests"},
-
ARG_TABLE_ENDMARKER};
native_add_command_line_opts(testargs_options);
@@ -104,6 +104,7 @@
{
int i;
+ native_add_tracing_options();
native_add_testargs_option();
s_argv = argv;
diff --git a/boards/posix/native_posix/native_tracing.h b/boards/posix/native_posix/native_tracing.h
new file mode 100644
index 0000000..6c84c78
--- /dev/null
+++ b/boards/posix/native_posix/native_tracing.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2018 Oticon A/S
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#ifndef NATIVE_TRACING_H
+#define NATIVE_TRACING_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void native_add_tracing_options(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* NATIVE_TRACING_H */
diff --git a/boards/posix/native_posix/tracing.c b/boards/posix/native_posix/tracing.c
index 6e0edfa..73ef2c2 100644
--- a/boards/posix/native_posix/tracing.c
+++ b/boards/posix/native_posix/tracing.c
@@ -11,9 +11,10 @@
#include <stdlib.h> /* for exit */
#include <stdio.h> /* for printfs */
#include <stdarg.h> /* for va args */
-
+#include <unistd.h>
+#include "soc.h"
#include "posix_board_if.h"
-
+#include "cmdline.h"
void posix_print_error_and_exit(const char *format, ...)
{
@@ -42,3 +43,75 @@
vfprintf(stdout, format, variable_args);
va_end(variable_args);
}
+
+/**
+ * Are stdout and stderr connectd to a tty
+ * 0 = no
+ * 1 = yes
+ * -1 = we do not know yet
+ * Indexed 0:stdout, 1:stderr
+ */
+static int is_a_tty[2] = {-1, -1};
+
+void trace_disable_color(char *argv, int offset)
+{
+ is_a_tty[0] = 0;
+ is_a_tty[1] = 0;
+}
+
+void trace_enable_color(char *argv, int offset)
+{
+ is_a_tty[0] = -1;
+ is_a_tty[1] = -1;
+
+}
+
+void trace_force_color(char *argv, int offset)
+{
+ is_a_tty[0] = 1;
+ is_a_tty[1] = 1;
+}
+
+int posix_trace_over_tty(int file_number)
+{
+ return is_a_tty[file_number];
+}
+
+static void decide_about_color(void)
+{
+ if (is_a_tty[0] == -1) {
+ is_a_tty[0] = isatty(STDOUT_FILENO);
+ }
+ if (is_a_tty[1] == -1) {
+ is_a_tty[1] = isatty(STDERR_FILENO);
+ }
+}
+
+NATIVE_TASK(decide_about_color, PRE_BOOT_2, 0);
+
+void native_add_tracing_options(void)
+{
+ static struct args_struct_t trace_options[] = {
+ /*
+ * Fields:
+ * manual, mandatory, switch,
+ * option_name, var_name ,type,
+ * destination, callback,
+ * description
+ */
+ { false, false, true,
+ "color", "color", 'b',
+ NULL, trace_enable_color,
+ "(default) Enable color in traces if printing to console"},
+ { false, false, true,
+ "no-color", "no-color", 'b',
+ NULL, trace_disable_color,
+ "Disable color in traces even if printing to console"},
+ { false, false, true,
+ "force-color", "force-color", 'b',
+ NULL, trace_force_color,
+ "Enable color in traces even if printing to files/pipes"},
+ ARG_TABLE_ENDMARKER};
+
+ native_add_command_line_opts(trace_options);
+}