'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)
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 */
+}
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)