]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Add option to color output
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 18 Feb 2019 19:27:16 +0000 (13:27 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 18 Feb 2019 19:30:26 +0000 (13:30 -0600)
src/config.c
src/config.h
src/log.c
src/log.h

index 5b59a29c06d744241ce04fff011d2768b30fb632..ebad48ec6aaae7c32740f74a05f1e3c05ed01ca8 100644 (file)
                            (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 = &gt_bool,
                .offset = offsetof(struct rpki_config, disable_rsync),
                .doc = "Enable or disable rsync downloads.",
+       }, {
+               .id = 'c',
+               .name = "color-output",
+               .type = &gt_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)
 {
index 71fccefd3d311918f64ba2c630a5103a1ca4ec30..a0f06a7d939300c86d36f910b1835a4e9acbdf50 100644 (file)
@@ -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_ */
index 317aefd18f5436762c8a63b945f3292d0612051f..4c2954c1b1e786b7a97cfd49629577c95fd2bb0a 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -3,17 +3,28 @@
 #include <openssl/bio.h>
 #include <openssl/err.h>
 
+#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;
 }
index 90b1b0e3a80f6d743f7c7f53095233f8dff64993..549ba816a25337c3012a965403f975e9c09fa867 100644 (file)
--- 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