[v4,4/5] eal: move logging initialization earlier

Message ID 20230626184223.155390-5-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series Logging related patchs |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger June 26, 2023, 6:42 p.m. UTC
  The log stream should be setup before any messages.
This ensures that any startup problems are captured on the
syslog output, not only shown on the stderr.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c | 40 +++++++++++++++++++--------------------
 lib/eal/linux/eal.c   | 44 +++++++++++++++++++++----------------------
 2 files changed, 40 insertions(+), 44 deletions(-)
  

Patch

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 70087837da18..d3aac3d628a8 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -364,7 +364,7 @@  eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
+/* Parse the arguments for --log-level and --syslog */
 static void
 eal_log_level_parse(int argc, char **argv)
 {
@@ -386,20 +386,18 @@  eal_log_level_parse(int argc, char **argv)
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 				  eal_long_options, &option_index)) != EOF) {
 
-		int ret;
-
-		/* getopt is not happy, stop right now */
-		if (opt == '?')
-			break;
-
-		ret = (opt == OPT_LOG_LEVEL_NUM) ?
-		    eal_parse_common_option(opt, optarg, internal_conf) : 0;
-
-		/* common parser is not happy */
-		if (ret < 0)
+		switch (opt) {
+		case OPT_SYSLOG_NUM:		/* fallthrough */
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				goto error;
 			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto error;
+		}
 	}
-
+error:
 	/* restore getopt lib */
 	optind = old_optind;
 	optopt = old_optopt;
@@ -437,8 +435,8 @@  eal_parse_args(int argc, char **argv)
 			goto out;
 		}
 
-		/* eal_log_level_parse() already handled this option */
-		if (opt == OPT_LOG_LEVEL_NUM)
+		/* eal_log_level_parse() already handled these */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -615,6 +613,12 @@  rte_eal_init(int argc, char **argv)
 	/* set log level as early as possible */
 	eal_log_level_parse(argc, argv);
 
+	if (eal_log_init(getprogname(), internal_conf->syslog_facility) < 0) {
+		rte_eal_init_alert("Cannot init logging.");
+		rte_errno = ENOMEM;
+		return -1;
+	}
+
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
@@ -762,12 +766,6 @@  rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(getprogname(), internal_conf->syslog_facility) < 0) {
-		rte_eal_init_alert("Cannot init logging.");
-		rte_errno = ENOMEM;
-		return -1;
-	}
-
 	/* in secondary processes, memory init may allocate additional fbarrays
 	 * not present in primary processes, so to avoid any potential issues,
 	 * initialize memzones first.
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 60bb130aea15..51c4ec75d57b 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,7 +546,7 @@  eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
+/* Parse the arguments for --log-level and --syslog */
 static void
 eal_log_level_parse(int argc, char **argv)
 {
@@ -566,20 +566,18 @@  eal_log_level_parse(int argc, char **argv)
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 				  eal_long_options, &option_index)) != EOF) {
 
-		int ret;
-
-		/* getopt is not happy, stop right now */
-		if (opt == '?')
-			break;
-
-		ret = (opt == OPT_LOG_LEVEL_NUM) ?
-			eal_parse_common_option(opt, optarg, internal_conf) : 0;
-
-		/* common parser is not happy */
-		if (ret < 0)
+		switch (opt) {
+		case OPT_SYSLOG_NUM:		/* fallthrough */
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				goto error;
 			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto error;
+		}
 	}
-
+error:
 	/* restore getopt lib */
 	optind = old_optind;
 	optopt = old_optopt;
@@ -651,8 +649,8 @@  eal_parse_args(int argc, char **argv)
 			goto out;
 		}
 
-		/* eal_log_level_parse() already handled this option */
-		if (opt == OPT_LOG_LEVEL_NUM)
+		/* eal_log_level_parse() already handled these */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -997,6 +995,14 @@  rte_eal_init(int argc, char **argv)
 	/* set log level as early as possible */
 	eal_log_level_parse(argc, argv);
 
+	if (eal_log_init(program_invocation_short_name,
+			 internal_conf->syslog_facility) < 0) {
+		rte_eal_init_alert("Cannot init logging.");
+		rte_errno = ENOMEM;
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
+		return -1;
+	}
+
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
@@ -1167,14 +1173,6 @@  rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(program_invocation_short_name,
-			 internal_conf->syslog_facility) < 0) {
-		rte_eal_init_alert("Cannot init logging.");
-		rte_errno = ENOMEM;
-		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");