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.
master
Nicholas Brown 2019-12-18 13:24:22 +00:00
parent 973ec2fb9e
commit b2ce0ef9d5
6 changed files with 58 additions and 26 deletions

View File

@ -1,4 +1,4 @@
<ipfixConfig>
<ipfixConfig logging="info">
<observer id="1">
<interface>en0</interface>
<pcap_filter>ip</pcap_filter>

View File

@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <pthread.h>
#include <sys/syslog.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
@ -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!

View File

@ -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);

View File

@ -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
*/

View File

@ -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;

View File

@ -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':