]> sourceware.org Git - systemtap.git/commitdiff
PR14927: add parsing of SYSTEMTAP_COLORS
authorJonathan Lebon <jlebon@redhat.com>
Tue, 2 Jul 2013 20:29:51 +0000 (16:29 -0400)
committerJonathan Lebon <jlebon@redhat.com>
Thu, 4 Jul 2013 19:36:23 +0000 (15:36 -0400)
Add the parse_stap_color() function to parse out the color sequence of
wanted string types from SYSTEMTAP_COLORS.

session.cxx
staprun/common.c
staprun/staprun.h

index a2c63f11114a88e7e35f4801bc0a1a45a97d2863..f9bd385ede3f3ac11b64781ea5681e5f179a5db5 100644 (file)
@@ -2115,9 +2115,6 @@ type. The env var SYSTEMTAP_COLORS must be in the following format:
 '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'
-This algorithm is loosely based on parse_grep_colors() in grep's main.c,
-written by Mike Haertel and others (for the complete list, see
-<http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>)
 */
 std::string
 systemtap_session::parse_stap_color(std::string type)
index 929f0819aa503fb6bad4e3099764dabc91b788a5..5aa73730249f6fab8e735ab2c5a0ea5d87299e9f 100644 (file)
@@ -602,3 +602,57 @@ void switch_syslog(const char *name)
        use_syslog = 1;
        color_errors = 0;
 }
+
+void print_color(const char *type)
+{
+       if (!color_errors)
+               return;
+
+       if (type == NULL) // Reset
+               eprintf("\033[m\033[K");
+       else {
+               char *seq = parse_stap_color(type);
+               if (seq != NULL) {
+                       eprintf("\033[");
+                       eprintf(seq);
+                       eprintf("m\033[K");
+                       free(seq);
+               }
+       }
+}
+
+/* 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'
+*/
+char *parse_stap_color(const char *type)
+{
+       const char *key, *col, *eq;
+       int n = strlen(type);
+       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 NULL; /* invalid syntax: no = in range */
+               if (!(key < eq && eq < col-1))
+                       return NULL; /* invalid syntax: key or val empty */
+               if (strspn(eq+1, "0123456789;") < (size_t)(col-eq-1))
+                       return NULL; /* invalid syntax: invalid char in val */
+               if (eq-key == n && !strncmp(key, type, n))
+                       return strndup(eq+1, col-eq-1);
+               if (!done) key = col+1; /* advance to next key */
+       }
+
+       return NULL; /* key not found */
+}
index b46c2b0a12d1ebc4715fa844c3fd1bdc3ad3dd65..836c0558cb2d1781046ef1cf5d4717f17494c84e 100644 (file)
 
 extern void eprintf(const char *fmt, ...);
 extern void switch_syslog(const char *name);
+extern void print_color(const char *type);
+extern char *parse_stap_color(const char *type);
 
 #define dbug(level, args...) do {if (verbose>=level) {eprintf("%s:%s:%d ",__name__,__FUNCTION__, __LINE__); eprintf(args);}} while (0)
 
 extern char *__name__;
 
 /* print to stderr */
-#define err(args...) eprintf(args)
+#define err(args...) do {      \
+               print_color("error");   \
+               eprintf(_("ERROR:"));   \
+               print_color(NULL); \
+               eprintf(" ");   \
+               eprintf(args);  \
+       } while (0)
+
+#define warn(args...) do {     \
+               print_color("warning"); \
+               eprintf(_("WARNING:")); \
+               print_color(NULL); \
+               eprintf(" ");   \
+               eprintf(args);  \
+       } while (0)
 
 /* better perror() */
 #define perr(args...) do {                                     \
                int _errno = errno;                             \
-               eprintf("ERROR: ");                             \
-               eprintf(args);                                  \
+               err(args);                                      \
                eprintf(": %s\n", strerror(_errno));            \
        } while (0)
 
This page took 0.036386 seconds and 5 git commands to generate.