From 767f0cafeaf7cf62ac7eacd87120b5a495d8e5ae Mon Sep 17 00:00:00 2001 From: Roberto Mariani Date: Wed, 23 Dec 2009 09:15:36 +0100 Subject: [PATCH] Improve granularity of errors with a "caution" flag. - Added caution flag to struct SkSmartAttributeParsedData. This permits to have also a caution flag for minimal or supposed problems, without boring with an excessive warning. - Implements use of caution flag for coloring the different errors --- atasmart.c | 65 ++++++++++++++++++++++++++++++++++++--------------- atasmart.h | 2 +- vala/atasmart.vapi | 1 + 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/atasmart.c b/atasmart.c index ead52b3..b8cb85b 100644 --- a/atasmart.c +++ b/atasmart.c @@ -1266,10 +1266,8 @@ static void verify_sectors(SkDisk *d, SkSmartAttributeParsedData *a) { a->pretty_value = SK_SMART_ATTRIBUTE_UNIT_UNKNOWN; d->attribute_verification_bad = TRUE; } else { - if ((!strcmp(a->name, "reallocated-sector-count") || - !strcmp(a->name, "current-pending-sector")) && - a->pretty_value > 0) - a->warn = TRUE; + if (a->pretty_value > 0) + a->caution = TRUE; } } @@ -1847,6 +1845,7 @@ int sk_disk_smart_parse_attributes(SkDisk *d, SkSmartAttributeParseCallback cb, a.flags = ((uint16_t) p[2] << 8) | p[1]; a.prefailure = !!(p[1] & 1); a.online = !!(p[1] & 2); + a.caution = FALSE; memcpy(a.raw, p+5, 6); @@ -2243,14 +2242,38 @@ static char *print_value(char *s, size_t len, uint64_t pretty_value, SkSmartAttr return s; } -#define HIGHLIGHT "\x1B[1m" -#define ENDHIGHLIGHT "\x1B[0m" +typedef enum SkColors { + SK_COLOR_BRIGHT_RED, + SK_COLOR_RED, + SK_COLOR_YELLOW, + SK_COLOR_END, + _SK_COLOR_MAX +} SkColors; + +static const char* highlight(SkColors color) { + + /* %STRINGPOOLSTART% */ + const char * const map[] = { + [SK_COLOR_BRIGHT_RED] = "\x1B[1;31m", + [SK_COLOR_RED] = "\x1B[0;31m", + [SK_COLOR_YELLOW] = "\x1B[0;33m", + [SK_COLOR_END] = "\x1B[0m" + }; + /* %STRINGPOOLSTOP% */ + + if (!isatty(STDOUT_FILENO)) + return ""; + + if (color >= _SK_COLOR_MAX) + return ""; + + return _P(map[color]); +} static void disk_dump_attributes(SkDisk *d, const SkSmartAttributeParsedData *a, void* userdata) { char name[32]; char pretty[32]; char tt[32], tw[32], tc[32]; - SkBool highlight; snprintf(tt, sizeof(tt), "%3u", a->threshold); tt[sizeof(tt)-1] = 0; @@ -2259,10 +2282,14 @@ static void disk_dump_attributes(SkDisk *d, const SkSmartAttributeParsedData *a, snprintf(tc, sizeof(tc), "%3u", a->current_value); tc[sizeof(tc)-1] = 0; - highlight = a->warn && isatty(1); - - if (highlight) - fprintf(stderr, HIGHLIGHT); + if (a->warn) { + if (a->good_now_valid && !a->good_now) + printf("%s", highlight(SK_COLOR_BRIGHT_RED)); + else if (a->good_in_the_past_valid && !a->good_in_the_past) + printf("%s", highlight(SK_COLOR_RED)); + } else if (a->caution) { + printf("%s", highlight(SK_COLOR_YELLOW)); + } printf("%3u %-27s %-3s %-3s %-3s %-11s 0x%02x%02x%02x%02x%02x%02x %-7s %-7s %-4s %-4s\n", a->id, @@ -2277,8 +2304,8 @@ static void disk_dump_attributes(SkDisk *d, const SkSmartAttributeParsedData *a, a->good_now_valid ? yes_no(a->good_now) : "n/a", a->good_in_the_past_valid ? yes_no(a->good_in_the_past) : "n/a"); - if (highlight) - fprintf(stderr, ENDHIGHLIGHT); + if (a->warn || a->caution) + printf("%s", highlight(SK_COLOR_END)); } int sk_disk_dump(SkDisk *d) { @@ -2343,9 +2370,9 @@ int sk_disk_dump(SkDisk *d) { ret = sk_disk_smart_status(d, &good); printf("%sSMART Disk Health Good: %s%s\n", - ret >= 0 && !good ? HIGHLIGHT : "", + ret >= 0 && !good ? highlight(SK_COLOR_BRIGHT_RED) : "", ret >= 0 ? yes_no(good) : strerror(errno), - ret >= 0 && !good ? ENDHIGHLIGHT : ""); + ret >= 0 && !good ? highlight(SK_COLOR_END) : ""); if ((ret = sk_disk_smart_read_data(d)) < 0) return ret; @@ -2379,9 +2406,9 @@ int sk_disk_dump(SkDisk *d) { printf("Bad Sectors: %s\n", strerror(errno)); else printf("%sBad Sectors: %s%s\n", - value > 0 ? HIGHLIGHT : "", + value > 0 ? highlight(SK_COLOR_YELLOW) : "", print_value(pretty, sizeof(pretty), value, SK_SMART_ATTRIBUTE_UNIT_SECTORS), - value > 0 ? ENDHIGHLIGHT : ""); + value > 0 ? highlight(SK_COLOR_END) : ""); if (sk_disk_smart_get_power_on(d, &power_on) < 0) { printf("Powered On: %s\n", strerror(errno)); @@ -2410,9 +2437,9 @@ int sk_disk_dump(SkDisk *d) { printf("Overall Status: %s\n", strerror(errno)); else printf("%sOverall Status: %s%s\n", - overall != SK_SMART_OVERALL_GOOD ? HIGHLIGHT : "", + overall != SK_SMART_OVERALL_GOOD ? highlight(SK_COLOR_RED) : "", sk_smart_overall_to_string(overall), - overall != SK_SMART_OVERALL_GOOD ? ENDHIGHLIGHT : ""); + overall != SK_SMART_OVERALL_GOOD ? highlight(SK_COLOR_END) : ""); printf("%3s %-27s %5s %5s %5s %-11s %-14s %-7s %-7s %-4s %-4s\n", "ID#", diff --git a/atasmart.h b/atasmart.h index aa4e339..1c41cd9 100644 --- a/atasmart.h +++ b/atasmart.h @@ -160,7 +160,7 @@ typedef struct SkSmartAttributeParsedData { SkBool good_now:1, good_now_valid:1; SkBool good_in_the_past:1, good_in_the_past_valid:1; SkBool current_value_valid:1, worst_value_valid:1; - SkBool warn:1; + SkBool warn:1, caution:1; uint8_t current_value, worst_value; uint64_t pretty_value; uint8_t raw[6]; diff --git a/vala/atasmart.vapi b/vala/atasmart.vapi index 05b8c37..b46db4e 100644 --- a/vala/atasmart.vapi +++ b/vala/atasmart.vapi @@ -107,6 +107,7 @@ namespace AtaSmart { public bool current_value_valid; public bool worst_value_valid; public bool warn; + public bool caution; public uint8 current_value; public uint8 worst_value; public uint64 pretty_value; -- 1.6.5