1 /*
2  * Copyright (c) 2017 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * Functions to print errors and traces
9  */
10 
11 #include <stdlib.h> /* for exit */
12 #include <stdio.h>  /* for printfs */
13 #include <stdarg.h> /* for va args */
14 #include <unistd.h>
15 #include "soc.h"
16 #include "posix_board_if.h"
17 #include "cmdline.h"
18 
posix_vprint_error_and_exit(const char * format,va_list vargs)19 void posix_vprint_error_and_exit(const char *format, va_list vargs)
20 {
21 	vfprintf(stderr, format, vargs);
22 	posix_exit(1);
23 }
24 
posix_vprint_warning(const char * format,va_list vargs)25 void posix_vprint_warning(const char *format, va_list vargs)
26 {
27 	vfprintf(stderr, format, vargs);
28 }
29 
posix_vprint_trace(const char * format,va_list vargs)30 void posix_vprint_trace(const char *format, va_list vargs)
31 {
32 	vfprintf(stdout, format, vargs);
33 }
34 
posix_print_error_and_exit(const char * format,...)35 void posix_print_error_and_exit(const char *format, ...)
36 {
37 	va_list variable_args;
38 
39 	va_start(variable_args, format);
40 	posix_vprint_error_and_exit(format, variable_args);
41 	va_end(variable_args);
42 }
43 
posix_print_warning(const char * format,...)44 void posix_print_warning(const char *format, ...)
45 {
46 	va_list variable_args;
47 
48 	va_start(variable_args, format);
49 	vfprintf(stderr, format, variable_args);
50 	va_end(variable_args);
51 }
52 
posix_print_trace(const char * format,...)53 void posix_print_trace(const char *format, ...)
54 {
55 	va_list variable_args;
56 
57 	va_start(variable_args, format);
58 	vfprintf(stdout, format, variable_args);
59 	va_end(variable_args);
60 }
61 
62 /**
63  * Are stdout and stderr connected to a tty
64  * 0  = no
65  * 1  = yes
66  * -1 = we do not know yet
67  * Indexed 0:stdout, 1:stderr
68  */
69 static int is_a_tty[2] = {-1, -1};
70 
trace_disable_color(char * argv,int offset)71 void trace_disable_color(char *argv, int offset)
72 {
73 	is_a_tty[0] = 0;
74 	is_a_tty[1] = 0;
75 }
76 
trace_enable_color(char * argv,int offset)77 void trace_enable_color(char *argv, int offset)
78 {
79 	is_a_tty[0] = -1;
80 	is_a_tty[1] = -1;
81 
82 }
83 
trace_force_color(char * argv,int offset)84 void trace_force_color(char *argv, int offset)
85 {
86 	is_a_tty[0] = 1;
87 	is_a_tty[1] = 1;
88 }
89 
posix_trace_over_tty(int file_number)90 int posix_trace_over_tty(int file_number)
91 {
92 	return is_a_tty[file_number];
93 }
94 
decide_about_color(void)95 static void decide_about_color(void)
96 {
97 	if (is_a_tty[0] == -1) {
98 		is_a_tty[0] = isatty(STDOUT_FILENO);
99 	}
100 	if (is_a_tty[1] == -1) {
101 		is_a_tty[1] = isatty(STDERR_FILENO);
102 	}
103 }
104 
105 NATIVE_TASK(decide_about_color, PRE_BOOT_2, 0);
106 
native_add_tracing_options(void)107 void native_add_tracing_options(void)
108 {
109 	static struct args_struct_t trace_options[] = {
110 		/*
111 		 * Fields:
112 		 * manual, mandatory, switch,
113 		 * option_name, var_name ,type,
114 		 * destination, callback,
115 		 * description
116 		 */
117 		{ false, false, true,
118 		"color", "color", 'b',
119 		NULL, trace_enable_color,
120 		"(default) Enable color in traces if printing to console"},
121 		{ false, false, true,
122 		"no-color", "no-color", 'b',
123 		NULL, trace_disable_color,
124 		"Disable color in traces even if printing to console"},
125 		{ false, false, true,
126 		"force-color", "force-color", 'b',
127 		NULL, trace_force_color,
128 		"Enable color in traces even if printing to files/pipes"},
129 		ARG_TABLE_ENDMARKER};
130 
131 	native_add_command_line_opts(trace_options);
132 }
133