From 579f09478738d44e2218c73f4865a97ba2251abd Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Tue, 2 Jul 2013 11:04:36 -0400 Subject: [PATCH] PR14927: add -C switch for color in staprun/io Also make stap send the -C switch if itself it was called with --color. As opposed to the --color switch of stap, the WHEN argument was made mandatory here so that it's consistent with the others, and it doesn't conflict with the MODULE argument (and users don't have to resort to using e.g. '-Cauto'). A few formatting fixes in the staprun manual page. --- buildrun.cxx | 6 ++++++ staprun/common.c | 19 +++++++++++++++++-- staprun/mainloop.c | 3 ++- staprun/staprun.8 | 14 +++++++++++--- staprun/staprun.h | 1 + 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/buildrun.cxx b/buildrun.cxx index 4250f75ad..578b5ff24 100644 --- a/buildrun.cxx +++ b/buildrun.cxx @@ -749,6 +749,12 @@ make_run_command (systemtap_session& s, const string& remotedir, staprun_cmd.push_back(s.size_option); } + if (s.color_errors) + { + staprun_cmd.push_back("-C"); + staprun_cmd.push_back("auto"); + } + staprun_cmd.push_back((remotedir.empty() ? s.tmpdir : remotedir) + "/" + s.module_filename()); diff --git a/staprun/common.c b/staprun/common.c index 2baf42980..3a23029bb 100644 --- a/staprun/common.c +++ b/staprun/common.c @@ -39,6 +39,7 @@ int fnum_max; int remote_id; const char *remote_uri; int relay_basedir_fd; +int color_errors; /* module variables */ char *modname = NULL; @@ -134,8 +135,9 @@ void parse_args(int argc, char **argv) remote_id = -1; remote_uri = NULL; relay_basedir_fd = -1; + color_errors = 0; - while ((c = getopt(argc, argv, "ALu::vb:t:dc:o:x:S:DwRr:VT:" + while ((c = getopt(argc, argv, "ALu::vb:t:dc:o:x:S:DwRr:VT:C:" #ifdef HAVE_OPENAT "F:" #endif @@ -227,6 +229,16 @@ void parse_args(int argc, char **argv) usage(argv[0]); } break; + case 'C': + if (strcmp(optarg, "auto") + && strcmp(optarg, "never") + && strcmp(optarg, "always")) { + err(_("Invalid option '%s' for -C."), optarg); + usage(argv[0]); + } + color_errors = (!strcmp(optarg, "always") + || (!strcmp(optarg, "auto") && isatty(STDERR_FILENO))); + break; default: usage(argv[0]); } @@ -299,7 +311,7 @@ void parse_args(int argc, char **argv) void usage(char *prog) { - err(_("\n%s [-v] [-w] [-V] [-u] [-c cmd ] [-x pid] [-u user] [-A|-L|-d]\n" + err(_("\n%s [-v] [-w] [-V] [-u] [-c cmd ] [-x pid] [-u user] [-A|-L|-d] [-C WHEN]\n" "\t[-b bufsize] [-R] [-r N:URI] [-o FILE [-D] [-S size[,N]]] MODULE [module-options]\n"), prog); err(_("-v Increase verbosity.\n" "-V Print version number and exit.\n" @@ -318,6 +330,8 @@ void usage(char *prog) " That value will be per-cpu in bulk mode.\n" "-L Load module and start probes, then detach.\n" "-A Attach to loaded systemtap module.\n" + "-C WHEN Enable colored errors. WHEN must be either 'auto',\n" + " 'never', or 'always'.\n" "-d Delete a module. Only detached or unused modules\n" " the user has permission to access will be deleted. Use \"*\"\n" " (quoted) to delete all unused modules.\n" @@ -580,4 +594,5 @@ void switch_syslog(const char *name) { openlog(name, LOG_PID, LOG_DAEMON); use_syslog = 1; + color_errors = 0; } diff --git a/staprun/mainloop.c b/staprun/mainloop.c index e68efb307..b42711898 100644 --- a/staprun/mainloop.c +++ b/staprun/mainloop.c @@ -502,9 +502,10 @@ void cleanup_and_exit(int detach, int rc) if (pid == 0) { /* child process */ /* Run the command. */ char *cmd; - int rc = asprintf(&cmd, "%s %s %s -d '%s'", staprun, + int rc = asprintf(&cmd, "%s %s %s -d -C %s '%s'", staprun, (verbose >= 1) ? "-v" : "", (verbose >= 2) ? "-v" : "", + color_errors ? "auto" : "never", modname); if (rc >= 1) { execlp("sh", "sh", "-c", cmd, NULL); diff --git a/staprun/staprun.8 b/staprun/staprun.8 index c3ad53650..8e56924c7 100644 --- a/staprun/staprun.8 +++ b/staprun/staprun.8 @@ -82,6 +82,14 @@ option. .B \-A Attach to loaded systemtap module. .TP +.B \-C WHEN +Enable colored error messages, which is turned off by default. +WHEN must be either "never", "always", or "auto" (i.e. enable only if at a +terminal). Colors can be modified using the SYSTEMTAP_COLORS environment +variable. See the +.IR stap (1) +manual page for more information on syntax and behaviour. +.TP .B \-d Delete a module. Only detached or unused modules the user has permission to access will be deleted. Use "*" @@ -93,7 +101,7 @@ Run staprun in background as a daemon and show it's pid. .B \-R Rename the module to a unique name before inserting it. .TP -.B \-r " N:URI" +.B \-r N:URI Pass the given number and URI data to the tapset functions remote_id() and remote_uri(). .TP @@ -186,11 +194,11 @@ To reattach to a kernel module, the option would be used. .SH FILE SWITCHING BY SIGNAL -After the +After .I staprun launched the .I stapio -, users can command it to switch output file to next file when it +program, users can command it to switch output file to next file when it outputs to file(s) (running staprun with .B \-o option) by sending a diff --git a/staprun/staprun.h b/staprun/staprun.h index 0dfe4a1e5..9ac245786 100644 --- a/staprun/staprun.h +++ b/staprun/staprun.h @@ -236,6 +236,7 @@ extern int fnum_max; extern int remote_id; extern const char *remote_uri; extern int relay_basedir_fd; +extern int color_errors; /* getopt variables */ extern char *optarg; -- 2.43.5