From bfbd88380666a08f13484ce7561c47cff7fb1c5a Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Tue, 9 Jul 2013 10:06:38 -0400 Subject: [PATCH] PR14927: stapdyn: add colorization for stapwarn() and staperror() The colorize() and parse_stap_color() functions are the same as in session.cxx. --- stapdyn/dynutil.cxx | 60 +++++++++++++++++++++++++++++++++++++++++++-- stapdyn/dynutil.h | 6 +++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/stapdyn/dynutil.cxx b/stapdyn/dynutil.cxx index 0dcc557e1..54d1c5846 100644 --- a/stapdyn/dynutil.cxx +++ b/stapdyn/dynutil.cxx @@ -215,17 +215,73 @@ stapwarn(void) { if (stapdyn_suppress_warnings) return nullstream; - return clog << program_invocation_short_name << ": WARNING: "; + return clog << program_invocation_short_name << ": " + << colorize("WARNING:", "warning") << " "; } // Return a stream for error messages. ostream& staperror(void) { - return clog << program_invocation_short_name << ": ERROR: "; + return clog << program_invocation_short_name << ": " + << colorize("ERROR:", "error") << " "; } // Whether to color error and warning messages bool color_errors; // Initialized in main() +// Adds coloring to strings +std::string +colorize(std::string str, std::string type) +{ + if (str.empty() || !color_errors) + return str; + else { + // Check if this type is defined in SYSTEMTAP_COLORS + std::string color = parse_stap_color(type); + if (!color.empty()) // no need to pollute terminal if not necessary + return "\033[" + color + "m\033[K" + str + "\033[m\033[K"; + else + return str; + } +} + +/* Parse SYSTEMTAP_COLORS and returns the SGR parameter(s) for the given +type. The env var SYSTEMTAP_COLORS must be in the following format: +'key1=val1:key2=val2:' etc... where valid keys are 'error', 'warning', +'source', 'caret', 'token' and valid values constitute SGR parameter(s). +For example, the default setting would be: +'error=01;31:warning=00;33:source=00;34:caret=01:token=01' +*/ +std::string +parse_stap_color(std::string type) +{ + const char *key, *col, *eq; + int n = type.size(); + int done = 0; + + key = getenv("SYSTEMTAP_COLORS"); + if (key == NULL || *key == '\0') + key = "error=01;31:warning=00;33:source=00;34:caret=01:token=01"; + + while (!done) { + if (!(col = strchr(key, ':'))) { + col = strchr(key, '\0'); + done = 1; + } + if (!((eq = strchr(key, '=')) && eq < col)) + return ""; /* invalid syntax: no = in range */ + if (!(key < eq && eq < col-1)) + return ""; /* invalid syntax: key or val empty */ + if (strspn(eq+1, "0123456789;") < (size_t)(col-eq-1)) + return ""; /* invalid syntax: invalid char in val */ + if (eq-key == n && type.compare(0, n, key, n) == 0) + return string(eq+1, col-eq-1); + if (!done) key = col+1; /* advance to next key */ + } + + // Could not find the key + return ""; +} + /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ diff --git a/stapdyn/dynutil.h b/stapdyn/dynutil.h index 9cace1b1e..f9c2cc5af 100644 --- a/stapdyn/dynutil.h +++ b/stapdyn/dynutil.h @@ -64,6 +64,12 @@ std::ostream& staperror(void); // Whether to color error and warning messages extern bool color_errors; +// Adds coloring to strings +std::string colorize(std::string str, std::string type); + +// Parse SYSTEMTAP_COLORS +std::string parse_stap_color(std::string type); + #endif // DYNUTIL_H /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ -- 2.43.5