From a8c9365c59814ca7ce8c159704c3f05a280777b6 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Fri, 21 Jun 2013 11:34:47 -0400 Subject: [PATCH] PR14927: lay groundwork for colors Created a new command-line option '--color' which is capable of taking in an argument as well. Added a new member 'color_errors' to the systemtap_session class which gets set upon treating the new switch's logic. Also created an entry in the man page. --- cmdline.cxx | 2 ++ cmdline.h | 1 + man/stap.1 | 6 ++++++ session.cxx | 14 ++++++++++++++ session.h | 1 + 5 files changed, 24 insertions(+) diff --git a/cmdline.cxx b/cmdline.cxx index 8fe12f1ef..7392dea89 100644 --- a/cmdline.cxx +++ b/cmdline.cxx @@ -56,5 +56,7 @@ struct option stap_long_options[] = { { "dyninst", 0, NULL, LONG_OPT_RUNTIME_DYNINST }, { "benchmark-sdt-loops", 1, NULL, LONG_OPT_BENCHMARK_SDT_LOOPS }, { "benchmark-sdt-threads", 1, NULL, LONG_OPT_BENCHMARK_SDT_THREADS }, + { "color", 2, NULL, LONG_OPT_COLOR_ERRS }, + { "colour", 2, NULL, LONG_OPT_COLOR_ERRS }, { NULL, 0, NULL, 0 } }; diff --git a/cmdline.h b/cmdline.h index c551e7f5e..e729ac33b 100644 --- a/cmdline.h +++ b/cmdline.h @@ -55,6 +55,7 @@ enum { LONG_OPT_RUNTIME_DYNINST, LONG_OPT_BENCHMARK_SDT_LOOPS, LONG_OPT_BENCHMARK_SDT_THREADS, + LONG_OPT_COLOR_ERRS, }; // NB: when adding new options, consider very carefully whether they diff --git a/man/stap.1 b/man/stap.1 index 806ed45b9..1e96983d1 100644 --- a/man/stap.1 +++ b/man/stap.1 @@ -293,6 +293,12 @@ done automatically after successful runs, but this option will trigger the cleanup manually and then exit. See the CACHING section for more details about cache limits. +.TP +\fB\-\-color\fR[=\fIWHEN\fR], \fB\-\-colour\fR[=\fIWHEN\fR] +This option enables colored error messages, which is turned off by default. +WHEN can be either 'never', 'always', or 'auto' (i.e. enable only if at a +terminal). Defaults to 'auto' if not specified. + .TP .BI \-\-disable\-cache This option disables all use of the cache directory. No files will be either diff --git a/session.cxx b/session.cxx index 9364e1071..aa7439157 100644 --- a/session.cxx +++ b/session.cxx @@ -161,6 +161,7 @@ systemtap_session::systemtap_session (): sysroot = ""; update_release_sysroot = false; suppress_time_limits = false; + color_errors = false; // PR12443: put compiled-in / -I paths in front, to be preferred during // tapset duplicate-file elimination @@ -334,6 +335,7 @@ systemtap_session::systemtap_session (const systemtap_session& other, update_release_sysroot = other.update_release_sysroot; sysenv = other.sysenv; suppress_time_limits = other.suppress_time_limits; + color_errors = other.color_errors; include_path = other.include_path; runtime_path = other.runtime_path; @@ -1274,6 +1276,18 @@ systemtap_session::parse_cmdline (int argc, char * const argv []) benchmark_sdt_threads = strtoul(optarg, NULL, 10); break; + case LONG_OPT_COLOR_ERRS: + if (optarg && strcmp(optarg, "auto") + && strcmp(optarg, "never") + && strcmp(optarg, "always")) { + cerr << _F("Invalid argument '%s' for --color.", optarg) << endl; + return 1; + } + // --color without arg is equivalent to auto + color_errors = ((optarg && !strcmp(optarg, "always")) + || ((!optarg || !strcmp(optarg, "auto")) && isatty(STDERR_FILENO))); + break; + case '?': // Invalid/unrecognized option given or argument required, but // not given. In both cases getopt_long() will have printed the diff --git a/session.h b/session.h index 952e938e7..e742b0240 100644 --- a/session.h +++ b/session.h @@ -213,6 +213,7 @@ public: int download_dbinfo; bool suppress_handler_errors; bool suppress_time_limits; + bool color_errors; enum { kernel_runtime, dyninst_runtime } runtime_mode; bool runtime_usermode_p() const { return runtime_mode == dyninst_runtime; } -- 2.43.5