Change in osmo-sysmon[master]: Add cmdline option parsing support

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Thu Dec 13 14:33:22 UTC 2018


Pau Espin Pedrol has submitted this change and it was merged. ( https://gerrit.osmocom.org/12285 )

Change subject: Add cmdline option parsing support
......................................................................

Add cmdline option parsing support

Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
---
M src/osysmon_main.c
1 file changed, 95 insertions(+), 4 deletions(-)

Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved

Objections:
  Max: I would prefer this is not merged as is



diff --git a/src/osysmon_main.c b/src/osysmon_main.c
index 5983212..486ee8f 100644
--- a/src/osysmon_main.c
+++ b/src/osysmon_main.c
@@ -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 @@
 	.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 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 @@
 	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 @@
 	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);

-- 
To view, visit https://gerrit.osmocom.org/12285
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I742974bd1440b09b49d26703c13361dd1c41008b
Gerrit-Change-Number: 12285
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181213/1134c926/attachment.htm>


More information about the gerrit-log mailing list