diff --git a/userspace/sysdig/sysdig.cpp b/userspace/sysdig/sysdig.cpp index 28a4096dbc..151a70459e 100644 --- a/userspace/sysdig/sysdig.cpp +++ b/userspace/sysdig/sysdig.cpp @@ -72,6 +72,12 @@ static std::atomic g_plugin_input = false; std::vector g_chisels; #endif +enum color_term_out { + COLOR = 0, + NO_COLOR, + FORCE_COLOR +}; + static void usage(); // @@ -290,7 +296,8 @@ static void usage() " -M Stop collecting after reached.\n" " -n , --numevents=\n" " Stop capturing after events\n" -" --no-color Avoid using colors on terminal output.\n" +" --color \n" +" Set colors settings on terminal output.\n" " --page-faults Capture user/kernel major/minor page faults\n" " --plugin-config-file\n" " Load the plugin configuration from a Falco-compatible yaml file.\n" @@ -962,6 +969,15 @@ void disable_tty_echo() { } #endif +static inline bool stdout_supports_color() +{ +#ifdef _WIN32 + return false; +#else + return isatty(fileno(stdout)); +#endif +} + std::string escape_output_format(const std::string& s) { std::stringstream ss{""}; @@ -1038,7 +1054,7 @@ sysdig_init_res sysdig_init(int argc, char **argv) sinsp_opener opener; std::unique_ptr filter_list; std::shared_ptr filter_factory; - bool no_color_flag = false; + color_term_out color_flag = COLOR; // These variables are for the cycle_writer engine int duration_seconds = 0; @@ -1112,7 +1128,7 @@ sysdig_init_res sysdig_init(int argc, char **argv) {"print-hex", no_argument, 0, 'x'}, {"print-hex-ascii", no_argument, 0, 'X'}, {"compress", no_argument, 0, 'z' }, - {"no-color", no_argument, 0, 0 }, + {"color", required_argument, 0, 0 }, {0, 0, 0, 0} }; @@ -1631,9 +1647,26 @@ sysdig_init_res sysdig_init(int argc, char **argv) return sysdig_init_res(EXIT_SUCCESS); } - else if (optname == "no-color") + else if (optname == "color") { - no_color_flag = true; + auto color_state = std::string(optarg); + if (color_state == "true") + { + color_flag = COLOR; + } + else if (color_state == "force") + { + color_flag = FORCE_COLOR; + } + else if (color_state == "false") + { + color_flag = NO_COLOR; + } + else + { + fprintf(stderr, "invalid color mode for flag --color\n"); + return sysdig_init_res(EXIT_FAILURE); + } } } break; @@ -1646,16 +1679,14 @@ sysdig_init_res sysdig_init(int argc, char **argv) } } -#ifndef _WIN32 - char* no_color = getenv("NO_COLOR"); - if (isatty(fileno(stdout)) && - (no_color != nullptr && strcmp(no_color, "1") != 0) && !no_color_flag) + char* no_color_env = getenv("NO_COLOR"); + if ((color_flag == FORCE_COLOR) || + (stdout_supports_color() && (no_color_env != nullptr && no_color_env[0] != '\0') && color_flag == COLOR)) { output_format = R"(*%evt.num %evt.outputtime %evt.cpu \e[01;32m%proc.name\e[00m (\e[01;36m%proc.pid\e[00m.%thread.tid) %evt.dir \e[01;34m%evt.type\e[00m %evt.info)"; output_format_plugin = R"(*%evt.num %evt.datetime.s [\e[01;32m%evt.pluginname\e[00m] %evt.plugininfo)"; } else -#endif { output_format = "*%evt.num %evt.outputtime %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.info"; output_format_plugin = "*%evt.num %evt.datetime.s [%evt.pluginname] %evt.plugininfo";