From: Alberto Leiva Popper Date: Mon, 18 Feb 2019 19:27:16 +0000 (-0600) Subject: Add option to color output X-Git-Tag: v0.0.2~85 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47955dcdce2283fac4594a217ed8d8d61ceb6671;p=thirdparty%2FFORT-validator.git Add option to color output --- diff --git a/src/config.c b/src/config.c index 5b59a29c..ebad48ec 100644 --- a/src/config.c +++ b/src/config.c @@ -17,19 +17,24 @@ (opt->availability & type)) struct rpki_config { - /* tal file path*/ + /** TAL file name/location. */ char const *tal; - /* Local repository path */ + /** Path of our local clone of the repository */ char const *local_repository; - /* Disable rsync downloads */ + /** Disable rsync downloads? */ bool disable_rsync; - /* Shuffle uris in tal */ + /** + * Shuffle uris in tal? + * (https://tools.ietf.org/html/rfc7730#section-3, last paragraphs) + */ bool shuffle_uris; - /* + /** * rfc6487#section-7.2, last paragraph. * Prevents arbitrarily long paths and loops. */ unsigned int maximum_certificate_depth; + /** Print ANSI color codes? */ + bool color_output; }; @@ -129,6 +134,13 @@ static const struct option_field global_fields[] = { .type = >_bool, .offset = offsetof(struct rpki_config, disable_rsync), .doc = "Enable or disable rsync downloads.", + }, { + .id = 'c', + .name = "color-output", + .type = >_bool, + .offset = offsetof(struct rpki_config, color_output), + .doc = "Print ANSI color codes?", + .availability = AVAILABILITY_GETOPT, }, { 0 }, }; @@ -567,6 +579,12 @@ config_get_max_cert_depth(void) return rpki_config.maximum_certificate_depth; } +bool +config_get_color_output(void) +{ + return rpki_config.color_output; +} + void free_rpki_config(void) { diff --git a/src/config.h b/src/config.h index 71fccefd..a0f06a7d 100644 --- a/src/config.h +++ b/src/config.h @@ -59,6 +59,7 @@ struct option_field { * If zero, signals the end of the array. * If alphanumeric, it's the short option name character. * Otherwise it's just a non-printable identifier. + * Must be unique across all option fields. * Mandatory. */ int id; @@ -112,6 +113,7 @@ char const *config_get_local_repository(void); bool config_get_enable_rsync(void); bool config_get_shuffle_uris(void); unsigned int config_get_max_cert_depth(void); +bool config_get_color_output(void); void free_rpki_config(void); #endif /* SRC_CONFIG_H_ */ diff --git a/src/log.c b/src/log.c index 317aefd1..4c2954c1 100644 --- a/src/log.c +++ b/src/log.c @@ -3,17 +3,28 @@ #include #include +#include "config.h" #include "thread_var.h" +#define COLOR_DEBUG "\x1B[36m" /* Cyan */ +#define COLOR_INFO "\x1B[37m" /* Gray */ +#define COLOR_WARNING "\x1B[33m" /* Yellow */ +#define COLOR_ERROR "\x1B[31m" /* Red */ +#define COLOR_CRITICAL "\x1B[35m" /* Pink */ +#define COLOR_RESET "\x1B[0m" /* Reset */ + #define STDOUT stdout #define STDERR stderr static unsigned int indent; static void -pr_indent(FILE *stream) +pr_prefix(FILE *stream, char const *color, char const *level) { unsigned int i; + if (config_get_color_output()) + fprintf(stream, "%s", color); + fprintf(stream, "%s: ", level); for (i = 0; i < indent; i++) fprintf(stream, " "); } @@ -49,8 +60,13 @@ pr_indent_rm(void) void pr_debug_prefix(void) { - fprintf(STDOUT, "DBG: "); - pr_indent(STDOUT); + pr_prefix(STDOUT, COLOR_DEBUG, "DBG"); +} + +void +pr_debug_suffix(void) +{ + fprintf(STDOUT, "%s\n", COLOR_RESET); } void @@ -63,7 +79,7 @@ pr_debug(const char *format, ...) va_start(args, format); vfprintf(STDOUT, format, args); va_end(args); - fprintf(STDOUT, "\n"); + pr_debug_suffix(); } void @@ -76,7 +92,7 @@ pr_debug_add(const char *format, ...) va_start(args, format); vfprintf(STDOUT, format, args); va_end(args); - fprintf(STDOUT, "\n"); + pr_debug_suffix(); pr_indent_add(); } @@ -93,33 +109,32 @@ pr_debug_rm(const char *format, ...) va_start(args, format); vfprintf(STDOUT, format, args); va_end(args); - fprintf(STDOUT, "\n"); + pr_debug_suffix(); } #endif -static void -pr_prefix(char const *level) -{ - fprintf(STDERR, "%s: ", level); - pr_indent(STDERR); -} +#define PR_PREFIX(stream, color, level, args) do { \ + pr_prefix(stream, color, level); \ + pr_file_name(stream); \ + \ + va_start(args, format); \ + vfprintf(stream, format, args); \ + va_end(args); \ +} while (0) -#define PR_PREFIX(level, args) do { \ - pr_prefix(level); \ - pr_file_name(STDERR); \ - \ - va_start(args, format); \ - vfprintf(STDERR, format, args); \ - va_end(args); \ +#define PR_SUFFIX(stream) do { \ + if (config_get_color_output()) \ + fprintf(stream, "%s", COLOR_RESET); \ + fprintf(stream, "\n"); \ } while (0) void pr_info(const char *format, ...) { va_list args; - PR_PREFIX("INF", args); - fprintf(STDOUT, "\n"); + PR_PREFIX(STDOUT, COLOR_INFO, "INF", args); + PR_SUFFIX(STDOUT); } /** @@ -130,8 +145,8 @@ int pr_warn(const char *format, ...) { va_list args; - PR_PREFIX("WRN", args); - fprintf(STDERR, "\n"); + PR_PREFIX(STDERR, COLOR_WARNING, "WRN", args); + PR_SUFFIX(STDERR); return 0; } @@ -142,8 +157,8 @@ int pr_err(const char *format, ...) { va_list args; - PR_PREFIX("ERR", args); - fprintf(STDERR, "\n"); + PR_PREFIX(STDERR, COLOR_ERROR, "ERR", args); + PR_SUFFIX(STDERR); return -EINVAL; } @@ -166,7 +181,7 @@ pr_errno(int error, const char *format, ...) { va_list args; - PR_PREFIX("ERR", args); + PR_PREFIX(STDERR, COLOR_ERROR, "ERR", args); if (error) { fprintf(STDERR, ": %s", strerror(error)); @@ -179,7 +194,7 @@ pr_errno(int error, const char *format, ...) error = -EINVAL; } - fprintf(STDERR, "\n"); + PR_SUFFIX(STDERR); return error; } @@ -202,7 +217,7 @@ crypto_err(const char *format, ...) va_list args; int error; - PR_PREFIX("ERR", args); + PR_PREFIX(STDERR, COLOR_ERROR, "ERR", args); fprintf(STDERR, ": "); error = ERR_GET_REASON(ERR_peek_last_error()); @@ -221,7 +236,7 @@ crypto_err(const char *format, ...) error = -EINVAL; } - fprintf(STDERR, "\n"); + PR_SUFFIX(STDERR); return error; } @@ -237,14 +252,14 @@ pr_crit(const char *format, ...) { va_list args; - pr_prefix("CRT"); + pr_prefix(STDERR, COLOR_CRITICAL, "CRT"); pr_file_name(STDERR); fprintf(STDERR, "Programming error: "); va_start(args, format); vfprintf(STDERR, format, args); va_end(args); - fprintf(STDERR, "\n"); + PR_SUFFIX(STDERR); return -EINVAL; } diff --git a/src/log.h b/src/log.h index 90b1b0e3..549ba816 100644 --- a/src/log.h +++ b/src/log.h @@ -30,13 +30,13 @@ void pr_debug(const char *, ...) CHECK_FORMAT(1, 2); void pr_debug_add(const char *, ...) CHECK_FORMAT(1, 2); void pr_debug_rm(const char *, ...) CHECK_FORMAT(1, 2); void pr_debug_prefix(void); +void pr_debug_suffix(void); #else #define pr_debug(...) #define pr_debug_add(...) #define pr_debug_rm(...) -#define pr_debug_prefix #endif