]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Add log output and level configuration properties (still unfunctional)
authorpcarana <pc.moreno2099@gmail.com>
Mon, 14 Oct 2019 15:13:16 +0000 (10:13 -0500)
committerpcarana <pc.moreno2099@gmail.com>
Thu, 7 Nov 2019 16:41:53 +0000 (10:41 -0600)
src/Makefile.am
src/config.c
src/config.h
src/config/log_conf.c [new file with mode: 0644]
src/config/log_conf.h [new file with mode: 0644]

index 78b75eaca2486e090474496019643168b0ea2733..55c48204ff855f444356beb4e831595921a1cc8c 100644 (file)
@@ -42,6 +42,7 @@ fort_SOURCES += asn1/signed_data.h asn1/signed_data.c
 
 fort_SOURCES += config/boolean.c config/boolean.h
 fort_SOURCES += config/filename_format.h config/filename_format.c
+fort_SOURCES += config/log_conf.h config/log_conf.c
 fort_SOURCES += config/mode.c config/mode.h
 fort_SOURCES += config/incidences.h config/incidences.c
 fort_SOURCES += config/str.c config/str.h
index 4f22f7dff67fdf1d1dc68a94d93d773594fdfd93..5ef71f81863daa94f70c34e528e2900af7538c27 100644 (file)
@@ -81,6 +81,10 @@ struct rpki_config {
                bool color;
                /** Format in which file names will be printed. */
                enum filename_format filename_format;
+               /* Log level */
+               enum log_level level;
+               /* Log output */
+               enum log_output output;
        } log;
 
        struct {
@@ -331,11 +335,23 @@ static const struct option_field options[] = {
                .type = &gt_filename_format,
                .offset = offsetof(struct rpki_config, log.filename_format),
                .doc = "File name variant to print during debug/error messages",
+       }, {
+               .id = 4001,
+               .name = "log.level",
+               .type = &gt_log_level,
+               .offset = offsetof(struct rpki_config, log.level),
+               .doc = "Log level to print message of equal or higher importance",
+       }, {
+               .id = 4002,
+               .name = "log.output",
+               .type = &gt_log_output,
+               .offset = offsetof(struct rpki_config, log.output),
+               .doc = "Output where log messages will be printed",
        },
 
        /* Incidences */
        {
-               .id = 4001,
+               .id = 7001,
                .name = "incidences",
                .type = &gt_incidences,
                .doc = "Override actions on validation errors",
@@ -556,6 +572,8 @@ set_default_values(void)
 
        rpki_config.log.color = false;
        rpki_config.log.filename_format = FNF_GLOBAL;
+       rpki_config.log.level = LOG_LEVEL_WARNING;
+       rpki_config.log.output = CONSOLE;
 
        rpki_config.output.roa = NULL;
        rpki_config.output.bgpsec = NULL;
@@ -809,6 +827,18 @@ config_get_filename_format(void)
        return rpki_config.log.filename_format;
 }
 
+enum log_level
+config_get_log_level(void)
+{
+       return rpki_config.log.level;
+}
+
+enum log_output
+config_get_log_output(void)
+{
+       return rpki_config.log.output;
+}
+
 char *
 config_get_rsync_program(void)
 {
index c3d86062489517e58a8dc19c48821e0266d68db5..a7e3ab642c2f4c1266cbd0a413d40106888569b7 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdint.h>
 
 #include "config/filename_format.h"
+#include "config/log_conf.h"
 #include "config/mode.h"
 #include "config/sync_strategy.h"
 #include "config/string_array.h"
@@ -32,6 +33,8 @@ unsigned int config_get_max_cert_depth(void);
 enum mode config_get_mode(void);
 bool config_get_color_output(void);
 enum filename_format config_get_filename_format(void);
+enum log_level config_get_log_level(void);
+enum log_output config_get_log_output(void);
 char *config_get_rsync_program(void);
 struct string_array const *config_get_rsync_args(bool);
 char const *config_get_output_roa(void);
diff --git a/src/config/log_conf.c b/src/config/log_conf.c
new file mode 100644 (file)
index 0000000..ac9d11f
--- /dev/null
@@ -0,0 +1,133 @@
+#include "config/log_conf.h"
+
+#include <getopt.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "log.h"
+#include "config/str.h"
+
+#define LOG_LEVEL_VALUE_ERROR "error"
+#define LOG_LEVEL_VALUE_WARNING "warning"
+#define LOG_LEVEL_VALUE_INFO "info"
+#define LOG_LEVEL_VALUE_DEBUG "debug"
+
+#define LOG_OUTPUT_VALUE_SYSLOG "syslog"
+#define LOG_OUTPUT_VALUE_CONSOLE "console"
+
+#define DEREFERENCE(type, void_value) (*((enum log_##type *) void_value))
+
+static void
+print_log_level(struct option_field const *field, void *value)
+{
+       char const *str = "<unknown>";
+
+       switch (DEREFERENCE(level, value)) {
+       case LOG_LEVEL_ERROR:
+               str = LOG_LEVEL_VALUE_ERROR;
+               break;
+       case LOG_LEVEL_WARNING:
+               str = LOG_LEVEL_VALUE_WARNING;
+               break;
+       case LOG_LEVEL_INFO:
+               str = LOG_LEVEL_VALUE_INFO;
+               break;
+       case LOG_LEVEL_DEBUG:
+               str = LOG_LEVEL_VALUE_DEBUG;
+               break;
+       }
+
+       pr_info("%s: %s", field->name, str);
+}
+
+static void
+print_log_output(struct option_field const *field, void *value)
+{
+       char const *str = "<unknown>";
+
+       switch (DEREFERENCE(output, value)) {
+       case SYSLOG:
+               str = LOG_OUTPUT_VALUE_SYSLOG;
+               break;
+       case CONSOLE:
+               str = LOG_OUTPUT_VALUE_CONSOLE;
+               break;
+       }
+
+       pr_info("%s: %s", field->name, str);
+}
+
+static int
+parse_argv_log_level(struct option_field const *field, char const *str,
+    void *result)
+{
+       if (strcmp(str, LOG_LEVEL_VALUE_ERROR) == 0)
+               DEREFERENCE(level, result) = LOG_LEVEL_ERROR;
+       else if (strcmp(str, LOG_LEVEL_VALUE_WARNING) == 0)
+               DEREFERENCE(level, result) = LOG_LEVEL_WARNING;
+       else if (strcmp(str, LOG_LEVEL_VALUE_INFO) == 0)
+               DEREFERENCE(level, result) = LOG_LEVEL_INFO;
+       else if (strcmp(str, LOG_LEVEL_VALUE_DEBUG) == 0)
+               DEREFERENCE(level, result) = LOG_LEVEL_DEBUG;
+       else
+               return pr_err("Unknown log level: '%s'", str);
+
+       return 0;
+}
+
+static int
+parse_argv_log_output(struct option_field const *field, char const *str,
+    void *result)
+{
+       if (strcmp(str, LOG_OUTPUT_VALUE_SYSLOG) == 0)
+               DEREFERENCE(output, result) = SYSLOG;
+       else if (strcmp(str, LOG_OUTPUT_VALUE_CONSOLE) == 0)
+               DEREFERENCE(output, result) = CONSOLE;
+       else
+               return pr_err("Unknown log output: '%s'", str);
+
+       return 0;
+}
+
+static int
+parse_json_log_level(struct option_field const *opt, json_t *json,
+    void *result)
+{
+       char const *string;
+       int error;
+
+       error = parse_json_string(json, opt->name, &string);
+       return error ? error : parse_argv_log_level(opt, string, result);
+}
+
+static int
+parse_json_log_output(struct option_field const *opt, json_t *json,
+    void *result)
+{
+       char const *string;
+       int error;
+
+       error = parse_json_string(json, opt->name, &string);
+       return error ? error : parse_argv_log_output(opt, string, result);
+}
+
+const struct global_type gt_log_level = {
+       .has_arg = required_argument,
+       .size = sizeof(enum log_level),
+       .print = print_log_level,
+       .parse.argv = parse_argv_log_level,
+       .parse.json = parse_json_log_level,
+       .arg_doc = LOG_LEVEL_VALUE_ERROR
+           "|" LOG_LEVEL_VALUE_WARNING
+           "|" LOG_LEVEL_VALUE_INFO
+           "|" LOG_LEVEL_VALUE_DEBUG,
+};
+
+const struct global_type gt_log_output = {
+       .has_arg = required_argument,
+       .size = sizeof(enum log_output),
+       .print = print_log_output,
+       .parse.argv = parse_argv_log_output,
+       .parse.json = parse_json_log_output,
+       .arg_doc = LOG_OUTPUT_VALUE_SYSLOG "|" LOG_OUTPUT_VALUE_CONSOLE,
+};
diff --git a/src/config/log_conf.h b/src/config/log_conf.h
new file mode 100644 (file)
index 0000000..e2222db
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef SRC_CONFIG_LOG_CONF_H_
+#define SRC_CONFIG_LOG_CONF_H_
+
+#include "config/types.h"
+
+enum log_level {
+       LOG_LEVEL_ERROR,
+       LOG_LEVEL_WARNING,
+       LOG_LEVEL_INFO,
+       LOG_LEVEL_DEBUG
+};
+
+enum log_output {
+       SYSLOG,
+       CONSOLE
+};
+
+extern const struct global_type gt_log_level;
+extern const struct global_type gt_log_output;
+
+#endif /* SRC_CONFIG_LOG_CONF_H_ */