]> granicus.if.org Git - sysstat/commitdiff
sar: Initial color support
authorSebastien GODARD <sysstat@users.noreply.github.com>
Mon, 14 Sep 2015 16:56:06 +0000 (18:56 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Wed, 16 Sep 2015 05:57:46 +0000 (07:57 +0200)
Add color support for restart messages and comments displayed by sar.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
common.c
common.h
sar.c

index 969a21600fe9224d28566c00bd96acbef0859b28..818fa9de64b293863a4c5a3762e8a2202d151b4e 100644 (file)
--- a/common.c
+++ b/common.c
@@ -56,6 +56,8 @@ char sc_percent_low[] = C_BOLD_BLUE;
 char sc_zero_int_stat[] = C_LIGHT_YELLOW;
 char sc_int_stat[] = C_BOLD_YELLOW;
 char sc_item_name[] = C_LIGHT_GREEN;
+char sc_sa_restart[] = C_LIGHT_RED;
+char sc_sa_comment[] = C_LIGHT_CYAN;
 char sc_normal[] = C_NORMAL;
 
 /* Type of persistent device names used in sar and iostat */
@@ -947,6 +949,8 @@ void init_colors(void)
                strcpy(sc_zero_int_stat, "");
                strcpy(sc_int_stat, "");
                strcpy(sc_item_name, "");
+               strcpy(sc_sa_comment, "");
+               strcpy(sc_sa_restart, "");
                strcpy(sc_normal, "");
        }
 }
@@ -1072,3 +1076,26 @@ void cprintf_in(int type, char *format, char *item_string, int item_int)
        }
        printf("%s", sc_normal);
 }
+
+/*
+ ***************************************************************************
+ * Print a string using selected color.
+ *
+ * IN:
+ * @type       Type of string to display.
+ * @format     Output format.
+ * @string     String to display.
+ ***************************************************************************
+*/
+void cprintf_s(int type, char *format, char *string)
+{
+       if (type == IS_RESTART) {
+               printf("%s", sc_sa_restart);
+       }
+       else {
+               /* IS_COMMENT */
+               printf("%s", sc_sa_comment);
+       }
+       printf(format, string);
+       printf("%s", sc_normal);
+}
index cc01720add4ff43678546934403edbbee744c301..f184fd133e23f3ddf0b0819a1929ed0cc0a9b48c 100644 (file)
--- a/common.h
+++ b/common.h
@@ -156,18 +156,22 @@ extern char persistent_name_type[MAX_FILE_LEN];
  ***************************************************************************
  */
 
+#define C_LIGHT_RED    "\e[31;22m"
 #define C_BOLD_RED     "\e[31;1m"
 #define C_LIGHT_GREEN  "\e[32;22m"
 #define C_LIGHT_YELLOW "\e[33;22m"
 #define C_BOLD_YELLOW  "\e[33;1m"
 #define C_BOLD_BLUE    "\e[34;1m"
+#define C_LIGHT_CYAN   "\e[36;22m"
 #define C_NORMAL       "\e[0m"
 
 #define PERCENT_LIMIT_HIGH     75.0
 #define PERCENT_LIMIT_LOW      50.0
 
-#define IS_INT 0
-#define IS_STR 1
+#define IS_INT         0
+#define IS_STR         1
+#define IS_RESTART     2
+#define IS_COMMENT     3
 
 /*
  ***************************************************************************
@@ -202,6 +206,8 @@ extern void
        cprintf_in(int, char *, char *, int);
 extern void
        cprintf_pc(int, int, int, ...);
+extern void
+       cprintf_s(int, char *, char *);
 extern void
        cprintf_ull(int, int, ...);
 extern char *
diff --git a/sar.c b/sar.c
index acbd0457fbc8797c29a1685d29ff4ff4fd6263a1..1b450246a2bce98597fd397be3344e17d14d10d5 100644 (file)
--- a/sar.c
+++ b/sar.c
@@ -661,7 +661,7 @@ int sa_read(void *buffer, int size)
 int sar_print_special(int curr, int use_tm_start, int use_tm_end, int rtype,
                      int ifd, char *file, struct file_magic *file_magic)
 {
-       char cur_time[26];
+       char cur_time[26], restart[64];
        int dp = 1;
        unsigned int new_cpu_nr;
 
@@ -680,8 +680,10 @@ int sar_print_special(int curr, int use_tm_start, int use_tm_end, int rtype,
                                                     file_hdr.sa_vol_act_nr);
 
                if (dp) {
-                       printf("\n%-11s       LINUX RESTART\t(%d CPU)\n",
-                              cur_time, new_cpu_nr > 1 ? new_cpu_nr - 1 : 1);
+                       printf("\n%-11s", cur_time);
+                       sprintf(restart, "  LINUX RESTART\t(%d CPU)\n",
+                               new_cpu_nr > 1 ? new_cpu_nr - 1 : 1);
+                       cprintf_s(IS_RESTART, "%s", restart);
                        return 1;
                }
        }
@@ -692,7 +694,8 @@ int sar_print_special(int curr, int use_tm_start, int use_tm_end, int rtype,
                replace_nonprintable_char(ifd, file_comment);
 
                if (dp && DISPLAY_COMMENT(flags)) {
-                       printf("%-11s  COM %s\n", cur_time, file_comment);
+                       printf("%-11s", cur_time);
+                       cprintf_s(IS_COMMENT, "  COM %s\n", file_comment);
                        return 1;
                }
        }
@@ -1197,6 +1200,9 @@ int main(int argc, char **argv)
        init_nls();
 #endif
 
+       /* Init color strings */
+       init_colors();
+
        tm_start.use = tm_end.use = FALSE;
 
        /* Allocate and init activity bitmaps */