From 9c360a00da523896d6e47d955afa47630923d46b Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Tue, 1 Mar 2022 13:36:01 +0100 Subject: [PATCH] sadf: Add basic colorization to sadf's output Colorization concerns only comments displayed in CSV and RAW formats. It also concerns some hints displayed in RAW format in debug mode. Signed-off-by: Sebastien GODARD --- common.c | 1 + common.h | 1 + raw_stats.c | 18 +++++++++--------- sa_common.c | 5 ++++- sadf.c | 30 +++++++++++++++++++++--------- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/common.c b/common.c index 2ef8039..5a915e6 100644 --- a/common.c +++ b/common.c @@ -1510,6 +1510,7 @@ void cprintf_s(int type, char *format, char *string) else if (type == IS_ZERO) { printf("%s", sc_zero_int_stat); } + /* IS_RESTART and IS_DEBUG are the same value */ else if (type == IS_RESTART) { printf("%s", sc_sa_restart); } diff --git a/common.h b/common.h index a72c3a7..9aed879 100644 --- a/common.h +++ b/common.h @@ -212,6 +212,7 @@ extern char persistent_name_type[MAX_FILE_LEN]; #define IS_INT 0 #define IS_STR 1 #define IS_RESTART 2 +#define IS_DEBUG IS_RESTART #define IS_COMMENT 3 #define IS_ZERO 4 diff --git a/raw_stats.c b/raw_stats.c index 34a4ca6..a9dfb99 100644 --- a/raw_stats.c +++ b/raw_stats.c @@ -114,7 +114,7 @@ void pval(unsigned long long valp, unsigned long long valc) if (DISPLAY_DEBUG_MODE(flags)) { if (valc < valp) { /* Field's value has decreased */ - printf(" [DEC]"); + cprintf_s(IS_DEBUG, "%s", " [DEC]"); } } printf("; %llu; %llu;", valp, valc); @@ -168,12 +168,12 @@ __print_funct_t raw_print_cpu_stats(struct activity *a, char *timestr, int curr) scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal + scc->cpu_hardirq + scc->cpu_softirq) == 0) { /* CPU is offline */ - printf(" [OFF]"); + cprintf_s(IS_DEBUG, "%s", " [OFF]"); } else { if (!get_per_cpu_interval(scc, scp)) { /* CPU is tickless */ - printf(" [TLS]"); + cprintf_s(IS_DEBUG, "%s", " [TLS]"); } } } @@ -530,7 +530,7 @@ __print_funct_t raw_print_serial_stats(struct activity *a, char *timestr, int cu printf("%s; %s", timestr, pfield(a->hdr_line, FIRST)); if (!found && DISPLAY_DEBUG_MODE(flags)) { - printf(" [NEW]"); + cprintf_s(IS_DEBUG, "%s", " [NEW]"); } printf("; %u;", ssc->line); if (!found) { @@ -596,7 +596,7 @@ __print_funct_t raw_print_disk_stats(struct activity *a, char *timestr, int curr /* This is a newly registered interface. Previous stats are zero */ sdp = &sdpzero; if (DISPLAY_DEBUG_MODE(flags)) { - printf(" [%s]", j == -1 ? "NEW" : "BCK"); + cprintf_s(IS_DEBUG, "%s", j == -1 ? " [NEW]" : " [BCK]"); } } else { @@ -661,7 +661,7 @@ __print_funct_t raw_print_net_dev_stats(struct activity *a, char *timestr, int c /* This is a newly registered interface. Previous stats are zero */ sndp = &sndzero; if (DISPLAY_DEBUG_MODE(flags)) { - printf(" [%s]", j == -1 ? "NEW" : "BCK"); + cprintf_s(IS_DEBUG, "%s", j == -1 ? " [NEW]" : " [BCK]"); } } else { @@ -721,7 +721,7 @@ __print_funct_t raw_print_net_edev_stats(struct activity *a, char *timestr, int /* This is a newly registered interface. Previous stats are zero */ snedp = &snedzero; if (DISPLAY_DEBUG_MODE(flags)) { - printf(" [%s]", j == -1 ? "NEW" : "BCK"); + cprintf_s(IS_DEBUG, "%s", j == -1 ? " [NEW]" : " [BCK]"); } } else { @@ -1622,7 +1622,7 @@ __print_funct_t raw_print_fchost_stats(struct activity *a, char *timestr, int cu /* This is a newly registered host. Previous stats are zero */ sfcp = &sfczero; if (DISPLAY_DEBUG_MODE(flags)) { - printf(" [NEW]"); + cprintf_s(IS_DEBUG, "%s", " [NEW]"); } } @@ -1690,7 +1690,7 @@ __print_funct_t raw_print_softnet_stats(struct activity *a, char *timestr, int c if (ssnc->processed + ssnc->dropped + ssnc->time_squeeze + ssnc->received_rps + ssnc->flow_limit == 0) { /* CPU is considered offline */ - printf(" [OFF]"); + cprintf_s(IS_DEBUG, "%s", " [OFF]"); } } printf("; %d;", i - 1); diff --git a/sa_common.c b/sa_common.c index f965a37..3c858de 100644 --- a/sa_common.c +++ b/sa_common.c @@ -1498,10 +1498,13 @@ int read_record_hdr(int ifd, void *buffer, struct record_header *record_hdr, /* Raw output in debug mode */ if (DISPLAY_DEBUG_MODE(flags) && (ofmt->id == F_RAW_OUTPUT)) { - printf("# uptime_cs; %llu; ust_time; %llu; extra_next; %u; record_type; %d; HH:MM:SS; %02d:%02d:%02d\n", + char out[128]; + + sprintf(out, "# uptime_cs; %llu; ust_time; %llu; extra_next; %u; record_type; %d; HH:MM:SS; %02d:%02d:%02d\n", record_hdr->uptime_cs, record_hdr->ust_time, record_hdr->extra_next, record_hdr->record_type, record_hdr->hour, record_hdr->minute, record_hdr->second); + cprintf_s(IS_COMMENT, "%s", out); } /* Sanity checks */ diff --git a/sadf.c b/sadf.c index 0cc1395..990d6ce 100644 --- a/sadf.c +++ b/sadf.c @@ -335,8 +335,9 @@ void list_fields(unsigned int act_id) unsigned int msk; char *hl; char hline[HEADER_LINE_LEN] = ""; + char out[256]; - printf("# hostname;interval;timestamp"); + cprintf_s(IS_COMMENT, "%s", "# hostname;interval;timestamp"); for (i = 0; i < NR_ACT; i++) { @@ -345,9 +346,10 @@ void list_fields(unsigned int act_id) if (IS_SELECTED(act[i]->options) && (act[i]->nr_ini > 0)) { if (!HAS_MULTIPLE_OUTPUTS(act[i]->options)) { - printf(";%s", act[i]->hdr_line); + sprintf(out, ";%s", act[i]->hdr_line); + cprintf_s(IS_COMMENT, "%s", out); if ((act[i]->nr_ini > 1) && DISPLAY_HORIZONTALLY(flags)) { - printf("[...]"); + cprintf_s(IS_COMMENT, "%s", "[...]"); } } else { @@ -361,20 +363,23 @@ void list_fields(unsigned int act_id) if ((act[i]->opt_flags & 0xff00) & (msk << 8)) { /* Display whole header line */ *(hl + j) = ';'; - printf(";%s", hl); + sprintf(out, ";%s", hl); + cprintf_s(IS_COMMENT, "%s", out); } else { /* Display only the first part of the header line */ *(hl + j) = '\0'; - printf(";%s", hl); + sprintf(out, ";%s", hl); + cprintf_s(IS_COMMENT, "%s", out); } *(hl + j) = '&'; } else { - printf(";%s", hl); + sprintf(out, ";%s", hl); + cprintf_s(IS_COMMENT, "%s", out); } if ((act[i]->nr_ini > 1) && DISPLAY_HORIZONTALLY(flags)) { - printf("[...]"); + cprintf_s(IS_COMMENT, "%s", "[...]"); } } } @@ -752,8 +757,12 @@ int generic_write_stats(int curr, int use_tm_start, int use_tm_end, int reset, else if (format == F_RAW_OUTPUT) { /* Raw output */ if (DISPLAY_DEBUG_MODE(flags)) { - printf("# name; %s; nr_curr; %d; nr_alloc; %d; nr_ini; %d\n", act[i]->name, - act[i]->nr[curr], act[i]->nr_allocated, act[i]->nr_ini); + char out[128]; + + sprintf(out, "# name; %s; nr_curr; %d; nr_alloc; %d; nr_ini; %d\n", + act[i]->name, act[i]->nr[curr], act[i]->nr_allocated, + act[i]->nr_ini); + cprintf_s(IS_COMMENT, "%s", out); } if (IS_SELECTED(act[i]->options) && (act[i]->nr[curr] > 0)) { @@ -1845,6 +1854,9 @@ int main(int argc, char **argv) } } + /* Init color strings */ + init_colors(); + if (USE_OPTION_A(flags)) { /* Set -P ALL -I ALL if needed */ set_bitmaps(act, &flags); -- 2.40.0