From b2ce0ef9d5c65ecad7d2fffdce70b7c73a7b987c Mon Sep 17 00:00:00 2001 From: Nicholas Brown Date: Wed, 18 Dec 2019 13:24:22 +0000 Subject: [PATCH] Add a logging attribute to the ipfix config element This allows the logging level to set as part of configuration, instead of having to use the `-l` CLI option. This mean it can also be changed dynamically when the config file is re-read for changes. --- configs/simple.xml | 2 +- src/common/msg.cc | 31 +++++++++++++++++++++++ src/common/msg.h | 1 + src/modules/ConfigManager.cpp | 13 ++++++++++ src/tests/vermonttest/ConfigTester.cpp | 2 +- src/vermont.cc | 35 ++++++++------------------ 6 files changed, 58 insertions(+), 26 deletions(-) diff --git a/configs/simple.xml b/configs/simple.xml index fefcda2..a2c7051 100644 --- a/configs/simple.xml +++ b/configs/simple.xml @@ -1,4 +1,4 @@ - + en0 ip diff --git a/src/common/msg.cc b/src/common/msg.cc index 1488da3..ada74de 100644 --- a/src/common/msg.cc +++ b/src/common/msg.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,36 @@ extern "C" { } } + /** + * @brief parse a string and return a logging bitmask + * + * @param arg string represent logging level + * @return bitmask of logging levels up to arg + * @return -1 if logging level is not recognised + */ + int + parse_log_level (const char *arg) + { + if (!strcmp("debug", arg)) { + return LOG_UPTO(LOG_DEBUG); + } else if (!strcmp("info", arg)) { + return LOG_UPTO(LOG_INFO); + } else if (!strcmp("notice", arg)) { + return LOG_UPTO(LOG_NOTICE); + } else if (!strcmp("warning", arg)) { + return LOG_UPTO(LOG_WARNING); + } else if (!strcmp("err", arg)) { + return LOG_UPTO(LOG_ERR); + } else if (!strcmp("crit", arg)) { + return LOG_UPTO(LOG_CRIT); + } else if (!strcmp("alert", arg)) { + return LOG_UPTO(LOG_ALERT); + } else if (!strcmp("emerg", arg)) { + return LOG_UPTO(LOG_EMERG); + } + return -1; + } + /** * initializes logging system * must be called at program startup! diff --git a/src/common/msg.h b/src/common/msg.h index 302b627..e2a908d 100644 --- a/src/common/msg.h +++ b/src/common/msg.h @@ -45,6 +45,7 @@ typedef void (*LOGFUNCTION)(void *); void msg_init(void); void msg_shutdown(void); void msg2(const int, const char*, const char*, const char*, const int, const char *, ...); +int parse_log_level (const char *arg); void msg_setlevel(int); int msg_getlevel(); void msg_setquiet(bool); diff --git a/src/modules/ConfigManager.cpp b/src/modules/ConfigManager.cpp index 88b5c66..faa0ced 100644 --- a/src/modules/ConfigManager.cpp +++ b/src/modules/ConfigManager.cpp @@ -152,6 +152,19 @@ void ConfigManager::parseConfig(std::string fileName) " This is not a valid configuration file!"); } + XMLAttribute* logging_attribute = root->getAttribute("logging"); + if (logging_attribute) { + int log_bitask = parse_log_level(logging_attribute->getValue().c_str()); + if (log_bitask == -1) { + msg(LOG_CRIT, "ignoring unknown log level '%s'", + logging_attribute->getValue().c_str()); + } else { + msg(LOG_NOTICE, "setting log level '%s'", + logging_attribute->getValue().c_str()); + msg_setlevel(log_bitask); + } + } + /* process each root element node and add a new node (with its config * attached to the node) to the graph */ diff --git a/src/tests/vermonttest/ConfigTester.cpp b/src/tests/vermonttest/ConfigTester.cpp index 5319b8e..a2b69b0 100644 --- a/src/tests/vermonttest/ConfigTester.cpp +++ b/src/tests/vermonttest/ConfigTester.cpp @@ -43,7 +43,7 @@ Test::TestResult ConfigTester::execTest() void ConfigTester::testConfig(const std::string& configFile) { - std::string vermontCommand = "../../../vermont -ddddd -f test_configs/" + configFile; + std::string vermontCommand = "../../../vermont -l debug -f test_configs/" + configFile; std::string generatedOutput = "gen_output/" + configFile; std::string expectedOutput = "exp_output/" + configFile; diff --git a/src/vermont.cc b/src/vermont.cc index e716600..c16f7d9 100644 --- a/src/vermont.cc +++ b/src/vermont.cc @@ -127,28 +127,6 @@ daemonise (const char *pid_file, uid_t uid, gid_t gid) } } -static int -parse_log_level (const char *arg) -{ - int mask = msg_getlevel(); - - if (!strcmp("debug", arg)) { - mask = LOG_UPTO(LOG_DEBUG); - } else if (!strcmp("info", arg)) { - mask = LOG_UPTO(LOG_INFO); - } else if (!strcmp("notice", arg)) { - mask = LOG_UPTO(LOG_NOTICE); - } else if (!strcmp("warning", arg)) { - mask = LOG_UPTO(LOG_WARNING); - } else if (!strcmp("error", arg)) { - mask = LOG_UPTO(LOG_ERR); - } else if (!strcmp("critical", arg)) { - mask = LOG_UPTO(LOG_CRIT); - } - - return mask; -} - static void usage (int status) { @@ -161,6 +139,8 @@ usage (int status) " -d NOTICE\n" " -dd INFO\n" " -ddd DEBUG\n" + " 'logging' attribute in the configuration\n" + " file takes precedence\n" " -l, --log-level LEVEL Log level.\n" " In increasing order:\n\n" " debug\n" @@ -170,6 +150,8 @@ usage (int status) " error\n" " critical\n\n" " Default: warning\n" + " 'logging' attribute in the configuration\n" + " file takes precedence\n" " -q, --quiet Do not write output to console (implied by -b)\n" " -b, --daemon Run in daemon mode (implies -q)\n" " -p, --pid-file FILE Set process id filename (use with -b)\n" @@ -187,7 +169,7 @@ usage (int status) static int parse_args (int argc, char **argv, struct parameters *params) { - int opt, ret, option_index; + int opt, ret, option_index, log_bitask; struct passwd *pw; struct group *gr; @@ -259,7 +241,12 @@ parse_args (int argc, char **argv, struct parameters *params) break; case 'l': - msg_setlevel(parse_log_level(optarg)); + log_bitask = parse_log_level(optarg); + if (log_bitask == -1) { + msg(LOG_CRIT, "ignoring unknown log level '%s'", optarg); + } else { + msg_setlevel(log_bitask); + } break; case 'q':