Skip to content

Commit

Permalink
fix(sysdig): address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Roberto Scolaro <roberto.scolaro21@gmail.com>
  • Loading branch information
therealbobo committed Jun 7, 2024
1 parent acf525a commit 3418d72
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 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,7 +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"
" --no-color Avoid using colors on terminal output.\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 @@ -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{""};
Expand Down Expand Up @@ -1038,7 +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;
bool no_color_flag = false;
color_term_out color_flag = COLOR;

// These variables are for the cycle_writer engine
int duration_seconds = 0;
Expand Down Expand Up @@ -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}
};

Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down

0 comments on commit 3418d72

Please sign in to comment.