Add cmdline option parsing support

Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
This commit is contained in:
Pau Espin 2018-12-12 19:37:02 +01:00
parent 2768f95086
commit ae25399f75
1 changed files with 95 additions and 4 deletions

View File

@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include "config.h"
#include "osysmon.h"
@ -69,8 +70,6 @@ static struct vty_app_info vty_info = {
.is_config_node = osysmon_is_config_node,
};
static const char *config_file = "osmo-sysmon.cfg";
struct osysmon_state *g_oss;
@ -110,6 +109,88 @@ static void signal_handler(int signal)
}
}
static void print_usage()
{
printf("Usage: osmo-sysmon\n");
}
static void print_help()
{
printf(" -h --help This text.\n");
printf(" -c --config-file filename The config file to use.\n");
printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
printf(" -D --daemonize Fork the process into a background daemon.\n");
printf(" -s --disable-color Do not print ANSI colors in the log\n");
printf(" -T --timestamp Prefix every log line with a timestamp.\n");
printf(" -e --log-level number Set a global loglevel.\n");
printf(" -V --version Print the version of OsmoHLR.\n");
}
static struct {
const char *config_file;
bool daemonize;
} cmdline_opts = {
.config_file = "osmo-sysmon.cfg",
.daemonize = false,
};
static void handle_options(int argc, char **argv)
{
while (1) {
int option_index = 0, c;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"config-file", 1, 0, 'c'},
{"debug", 1, 0, 'd'},
{"daemonize", 0, 0, 'D'},
{"disable-color", 0, 0, 's'},
{"log-level", 1, 0, 'e'},
{"timestamp", 0, 0, 'T'},
{"version", 0, 0, 'V' },
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "hc:d:Dse:TV",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'h':
print_usage();
print_help();
exit(0);
case 'c':
cmdline_opts.config_file = optarg;
break;
case 'd':
log_parse_category_mask(osmo_stderr_target, optarg);
break;
case 'D':
cmdline_opts.daemonize = 1;
break;
case 's':
log_set_use_color(osmo_stderr_target, 0);
break;
case 'e':
log_set_log_level(osmo_stderr_target, atoi(optarg));
break;
case 'T':
log_set_print_timestamp(osmo_stderr_target, 1);
break;
case 'V':
print_version(1);
exit(0);
break;
default:
/* catch unknown options *as well as* missing arguments. */
fprintf(stderr, "Error in command line options. Exiting.\n");
exit(-1);
break;
}
}
}
int main(int argc, char **argv)
{
int rc;
@ -122,14 +203,16 @@ int main(int argc, char **argv)
INIT_LLIST_HEAD(&g_oss->files);
vty_init(&vty_info);
handle_options(argc, argv);
osysmon_sysinfo_init();
osysmon_ctrl_init();
osysmon_rtnl_init();
osysmon_file_init();
rc = vty_read_config_file(config_file, NULL);
rc = vty_read_config_file(cmdline_opts.config_file, NULL);
if (rc < 0) {
fprintf(stderr, "Failed to parse the config file %s\n", config_file);
fprintf(stderr, "Failed to parse the config file %s\n",
cmdline_opts.config_file);
exit(2);
}
@ -137,6 +220,14 @@ int main(int argc, char **argv)
signal(SIGUSR2, &signal_handler);
osmo_init_ignore_signals();
if (cmdline_opts.daemonize) {
rc = osmo_daemonize();
if (rc < 0) {
perror("Error during daemonize");
exit(1);
}
}
while (1) {
struct value_node *root = value_node_add(g_oss, NULL, "root", NULL);
osysmon_sysinfo_poll(root);