]> granicus.if.org Git - sysstat/commitdiff
sadf: Add basic colorization to sadf's output
authorSebastien GODARD <sysstat@users.noreply.github.com>
Tue, 1 Mar 2022 12:36:01 +0000 (13:36 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Tue, 1 Mar 2022 12:36:01 +0000 (13:36 +0100)
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 <sysstat@users.noreply.github.com>
common.c
common.h
raw_stats.c
sa_common.c
sadf.c

index 2ef80398804c194ff6ab9464a429a372acefaac6..5a915e649d7f755716e6f5f798d3469f945e9157 100644 (file)
--- 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);
        }
index a72c3a7cbe690e8eb433fe1a2299f1271833cace..9aed879e39e8a659ef0ec8bdc710520930e952bf 100644 (file)
--- 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
 
index 34a4ca684a923f9c7b9b89bbc3c07fbd453913a9..a9dfb99717d197a6d621eb8e0a4f39111d378dbf 100644 (file)
@@ -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);
index f965a37ae346d7230f7c1c8d266394d69daecd51..3c858de3b5a4d896dde4def366e2e2a74ff68a1b 100644 (file)
@@ -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 0cc1395f10ba063a1def65535ad1fc451fe6193b..990d6cea3c36910883758ea7320b9c551d4a7d32 100644 (file)
--- 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);