From c9f7be757f09f252f0d5d73be07b048df27a89ad Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Mon, 14 Sep 2015 18:56:06 +0200 Subject: [PATCH] sar: Initial color support Add color support for restart messages and comments displayed by sar. Signed-off-by: Sebastien GODARD --- common.c | 27 +++++++++++++++++++++++++++ common.h | 10 ++++++++-- sar.c | 14 ++++++++++---- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/common.c b/common.c index 969a216..818fa9d 100644 --- 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); +} diff --git a/common.h b/common.h index cc01720..f184fd1 100644 --- 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 acbd045..1b45024 100644 --- 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 */ -- 2.40.0