Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sysdig): implement NO_COLOR standard #2107

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions userspace/sysdig/sysdig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ static std::atomic<bool> g_plugin_input = false;
std::vector<sinsp_chisel*> g_chisels;
#endif

enum color_term_out {
COLOR = 0,
NO_COLOR,
FORCE_COLOR
};

static void usage();

//
Expand Down Expand Up @@ -290,6 +296,8 @@ static void usage()
" -M <num_seconds> Stop collecting after <num_seconds> reached.\n"
" -n <num>, --numevents=<num>\n"
" Stop capturing after <num> events\n"
" --color <true|force|false> \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"
Expand Down Expand Up @@ -961,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{""};
Expand Down Expand Up @@ -1037,6 +1054,7 @@ sysdig_init_res sysdig_init(int argc, char **argv)
sinsp_opener opener;
std::unique_ptr<filter_check_list> filter_list;
std::shared_ptr<sinsp_filter_factory> filter_factory;
color_term_out color_flag = COLOR;

// These variables are for the cycle_writer engine
int duration_seconds = 0;
Expand Down Expand Up @@ -1110,22 +1128,10 @@ 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' },
{"color", required_argument, 0, 0 },
{0, 0, 0, 0}
};

#ifndef _WIN32
if (isatty(fileno(stdout)))
{
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";
}

try
{
inspector.reset(new sinsp());
Expand Down Expand Up @@ -1640,6 +1646,28 @@ sysdig_init_res sysdig_init(int argc, char **argv)
plugins.print_plugin_info(inspector.get(), filter_list.get(), name);
return sysdig_init_res(EXIT_SUCCESS);
}

else if (optname == "color")
{
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;
// getopt_long : '?' for an ambiguous match or an extraneous parameter
Expand All @@ -1651,6 +1679,19 @@ sysdig_init_res sysdig_init(int argc, char **argv)
}
}

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
{
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";
}

// given the CLI options, we finish loading and initializing plugins.
// if no plugin has been specified as input with -I, we try to
// reload the config file (if present), and set the input plugin
Expand Down
Loading