DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
@ 2020-08-14 17:34 Stephen Hemminger
  2020-08-14 18:39 ` Dmitry Kozlyuk
                   ` (20 more replies)
  0 siblings, 21 replies; 277+ messages in thread
From: Stephen Hemminger @ 2020-08-14 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

Example:
[       0.000040] EAL: Probing VFIO support...

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
 lib/librte_eal/common/eal_common_options.c    |  5 +++
 lib/librte_eal/common/eal_internal_cfg.h      |  1 +
 lib/librte_eal/common/eal_options.h           |  2 +
 lib/librte_eal/linux/eal_log.c                | 41 +++++++++++++++++--
 5 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index bd3977cb3d91..9336046c63a9 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -118,3 +118,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/lib/librte_eal/common/eal_common_options.c b/lib/librte_eal/common/eal_common_options.c
index a5426e12346a..240610770ee1 100644
--- a/lib/librte_eal/common/eal_common_options.c
+++ b/lib/librte_eal/common/eal_common_options.c
@@ -76,6 +76,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1626,6 +1627,9 @@ eal_parse_common_option(int opt, const char *optarg,
 		}
 		break;
 	}
+	case OPT_LOG_TIMESTAMP_NUM:
+		conf->log_timestamp = 1;
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -1945,6 +1949,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<int>   Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<int>\n"
diff --git a/lib/librte_eal/common/eal_internal_cfg.h b/lib/librte_eal/common/eal_internal_cfg.h
index 13f93388a781..75c476870c57 100644
--- a/lib/librte_eal/common/eal_internal_cfg.h
+++ b/lib/librte_eal/common/eal_internal_cfg.h
@@ -70,6 +70,7 @@ struct internal_config {
 	 * per-node) non-legacy mode only.
 	 */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
+	volatile uint8_t log_timestamp;   /**< add timestamp to console output */
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/librte_eal/common/eal_options.h b/lib/librte_eal/common/eal_options.h
index 89769d48b487..7c86f2e19109 100644
--- a/lib/librte_eal/common/eal_options.h
+++ b/lib/librte_eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/librte_eal/linux/eal_log.c b/lib/librte_eal/linux/eal_log.c
index 43c8460bfb07..cbd0924ec847 100644
--- a/lib/librte_eal/linux/eal_log.c
+++ b/lib/librte_eal/linux/eal_log.c
@@ -5,9 +5,12 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <unistd.h>
+#include <time.h>
 #include <sys/types.h>
 #include <syslog.h>
 #include <sys/queue.h>
+#include <sys/uio.h>
 
 #include <rte_memory.h>
 #include <rte_eal.h>
@@ -18,6 +21,9 @@
 #include <rte_log.h>
 
 #include "eal_private.h"
+#include "eal_internal_cfg.h"
+
+static struct timespec log_start_time;
 
 /*
  * default log function
@@ -25,11 +31,38 @@
 static ssize_t
 console_log_write(__rte_unused void *c, const char *buf, size_t size)
 {
+	const struct internal_config *internal_conf
+		= eal_get_internal_configuration();
 	ssize_t ret;
+	char tbuf[64];
+	struct iovec iov[2];
+
+	if (internal_conf->log_timestamp) {
+		struct timespec ts;
+
+		/* format up monotonic timestamp */
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= log_start_time.tv_sec;
+		ts.tv_nsec -= log_start_time.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
 
-	/* write on stdout */
-	ret = fwrite(buf, 1, size, stdout);
-	fflush(stdout);
+		iov[0].iov_base = tbuf;
+		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
+					   ts.tv_sec, ts.tv_nsec / 1000u);
+
+		/* extra cast is workaround to remove const quallifier */
+		iov[1].iov_base = (void *)(uintptr_t)buf;
+		iov[1].iov_len = size;
+
+		fflush(stdout);
+		ret = writev(STDOUT_FILENO, iov, 2);
+	} else {
+		/* write on stdout */
+		ret = fwrite(buf, 1, size, stdout);
+	}
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
@@ -50,6 +83,8 @@ rte_eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	clock_gettime(CLOCK_MONOTONIC, &log_start_time);
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
-- 
2.27.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
@ 2020-08-14 18:39 ` Dmitry Kozlyuk
  2020-08-14 18:45   ` Stephen Hemminger
  2023-03-06 18:18 ` [PATCH v2 0/2] Add option to timestamp console log Stephen Hemminger
                   ` (19 subsequent siblings)
  20 siblings, 1 reply; 277+ messages in thread
From: Dmitry Kozlyuk @ 2020-08-14 18:39 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Fri, 14 Aug 2020 10:34:41 -0700, Stephen Hemminger wrote:
> When debugging driver or startup issues, it is useful to have
> a timestamp on each message printed. The messages in syslog
> already have a timestamp, but often syslog is not available
> during testing. The timestamp format is chosen to look
> like the default Linux dmesg timestamp.
> 
> Example:
> [       0.000040] EAL: Probing VFIO support...
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
>  lib/librte_eal/common/eal_common_options.c    |  5 +++
>  lib/librte_eal/common/eal_internal_cfg.h      |  1 +
>  lib/librte_eal/common/eal_options.h           |  2 +
>  lib/librte_eal/linux/eal_log.c                | 41 +++++++++++++++++--
>  5 files changed, 51 insertions(+), 3 deletions(-)
[snip]

> @@ -1945,6 +1949,7 @@ eal_common_usage(void)
>  	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
>  #ifndef RTE_EXEC_ENV_WINDOWS
>  	       "  --"OPT_SYSLOG"            Set syslog facility\n"
> +	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
>  #endif

Seems like FreeBSD doesn't support this option as well.

It is clear that iovec approach saves a syscall on each write, but is it worth
implementing log timestamps on each platform instead of prepending it in
common code (with a second fwrite call)? For fast event stream with precise
timestamps, there is tracing framework.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-08-14 18:39 ` Dmitry Kozlyuk
@ 2020-08-14 18:45   ` Stephen Hemminger
  2020-08-14 19:09     ` Dmitry Kozlyuk
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2020-08-14 18:45 UTC (permalink / raw)
  To: Dmitry Kozlyuk; +Cc: dev

On Fri, 14 Aug 2020 21:39:29 +0300
Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:

> On Fri, 14 Aug 2020 10:34:41 -0700, Stephen Hemminger wrote:
> > When debugging driver or startup issues, it is useful to have
> > a timestamp on each message printed. The messages in syslog
> > already have a timestamp, but often syslog is not available
> > during testing. The timestamp format is chosen to look
> > like the default Linux dmesg timestamp.
> > 
> > Example:
> > [       0.000040] EAL: Probing VFIO support...
> > 
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> > ---
> >  doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
> >  lib/librte_eal/common/eal_common_options.c    |  5 +++
> >  lib/librte_eal/common/eal_internal_cfg.h      |  1 +
> >  lib/librte_eal/common/eal_options.h           |  2 +
> >  lib/librte_eal/linux/eal_log.c                | 41 +++++++++++++++++--
> >  5 files changed, 51 insertions(+), 3 deletions(-)  
> [snip]
> 
> > @@ -1945,6 +1949,7 @@ eal_common_usage(void)
> >  	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
> >  #ifndef RTE_EXEC_ENV_WINDOWS
> >  	       "  --"OPT_SYSLOG"            Set syslog facility\n"
> > +	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
> >  #endif  
> 
> Seems like FreeBSD doesn't support this option as well.
> 
> It is clear that iovec approach saves a syscall on each write, but is it worth
> implementing log timestamps on each platform instead of prepending it in
> common code (with a second fwrite call)? For fast event stream with precise
> timestamps, there is tracing framework.

FreeBsd doesn't go through the same log code.

Iovec is to keep timestamp and message as an atomic message.
There is no good way to use fwrite to get anything atomic.
Fwrite is unnecessary here anyway saving a copy and avoid issues with library
getting corrupted is good.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-08-14 18:45   ` Stephen Hemminger
@ 2020-08-14 19:09     ` Dmitry Kozlyuk
  2020-08-14 19:20       ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Dmitry Kozlyuk @ 2020-08-14 19:09 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Fri, 14 Aug 2020 11:45:49 -0700, Stephen Hemminger wrote:
> On Fri, 14 Aug 2020 21:39:29 +0300
> Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:
[...]
> FreeBsd doesn't go through the same log code.

Then --log-timestamp shouldn't appear on FreeBSD if it's unsupported.

> Iovec is to keep timestamp and message as an atomic message.
> There is no good way to use fwrite to get anything atomic.
> Fwrite is unnecessary here anyway saving a copy and avoid issues with library
> getting corrupted is good.

Got it, thanks.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-08-14 19:09     ` Dmitry Kozlyuk
@ 2020-08-14 19:20       ` Stephen Hemminger
  2020-08-17 10:37         ` Bruce Richardson
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2020-08-14 19:20 UTC (permalink / raw)
  To: Dmitry Kozlyuk; +Cc: dev

On Fri, 14 Aug 2020 22:09:11 +0300
Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:

> On Fri, 14 Aug 2020 11:45:49 -0700, Stephen Hemminger wrote:
> > On Fri, 14 Aug 2020 21:39:29 +0300
> > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:  
> [...]
> > FreeBsd doesn't go through the same log code.  
> 
> Then --log-timestamp shouldn't appear on FreeBSD if it's unsupported.

Was using same ifdef that is already there. There are several options already
(like syslog facility) which do nothing on FreeBSD anyway.

> 
> > Iovec is to keep timestamp and message as an atomic message.
> > There is no good way to use fwrite to get anything atomic.
> > Fwrite is unnecessary here anyway saving a copy and avoid issues with library
> > getting corrupted is good.  
> 
> Got it, thanks.


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-08-14 19:20       ` Stephen Hemminger
@ 2020-08-17 10:37         ` Bruce Richardson
  2020-08-17 15:11           ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Bruce Richardson @ 2020-08-17 10:37 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: Dmitry Kozlyuk, dev

On Fri, Aug 14, 2020 at 12:20:28PM -0700, Stephen Hemminger wrote:
> On Fri, 14 Aug 2020 22:09:11 +0300
> Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:
> 
> > On Fri, 14 Aug 2020 11:45:49 -0700, Stephen Hemminger wrote:
> > > On Fri, 14 Aug 2020 21:39:29 +0300
> > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:  
> > [...]
> > > FreeBsd doesn't go through the same log code.  
> > 
> > Then --log-timestamp shouldn't appear on FreeBSD if it's unsupported.
> 
> Was using same ifdef that is already there. There are several options already
> (like syslog facility) which do nothing on FreeBSD anyway.
> 
While that is correct, I think that we really should support this on
FreeBSD also, since I don't think there is any technical reason we can't do
so, right? Even a sub-optimal solution where the timestamp write and log
write are non-atomic would be better than nothing.

/Bruce

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-08-17 10:37         ` Bruce Richardson
@ 2020-08-17 15:11           ` Stephen Hemminger
  2020-10-19 14:11             ` Thomas Monjalon
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2020-08-17 15:11 UTC (permalink / raw)
  To: Bruce Richardson; +Cc: Dmitry Kozlyuk, dev

On Mon, 17 Aug 2020 11:37:40 +0100
Bruce Richardson <bruce.richardson@intel.com> wrote:

> On Fri, Aug 14, 2020 at 12:20:28PM -0700, Stephen Hemminger wrote:
> > On Fri, 14 Aug 2020 22:09:11 +0300
> > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:
> >   
> > > On Fri, 14 Aug 2020 11:45:49 -0700, Stephen Hemminger wrote:  
> > > > On Fri, 14 Aug 2020 21:39:29 +0300
> > > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:    
> > > [...]  
> > > > FreeBsd doesn't go through the same log code.    
> > > 
> > > Then --log-timestamp shouldn't appear on FreeBSD if it's unsupported.  
> > 
> > Was using same ifdef that is already there. There are several options already
> > (like syslog facility) which do nothing on FreeBSD anyway.
> >   
> While that is correct, I think that we really should support this on
> FreeBSD also, since I don't think there is any technical reason we can't do
> so, right? Even a sub-optimal solution where the timestamp write and log
> write are non-atomic would be better than nothing.
> 
> /Bruce

FreeBSD now just uses default log stream (stdout).
Linux has log stream to syslog and stdout.

There is nothing Linux specific in the old or new code.
The Linux code could just be moved to common, and both could use it.
That is a separate patch, and someone with expertise and build setup for FreeBSD needs to do it.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-08-17 15:11           ` Stephen Hemminger
@ 2020-10-19 14:11             ` Thomas Monjalon
  2020-10-19 15:25               ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Thomas Monjalon @ 2020-10-19 14:11 UTC (permalink / raw)
  To: Bruce Richardson, Dmitry Kozlyuk, Stephen Hemminger
  Cc: dev, david.marchand, olivier.matz

There was no ack or conclusion for this patch.

17/08/2020 17:11, Stephen Hemminger:
> On Mon, 17 Aug 2020 11:37:40 +0100
> Bruce Richardson <bruce.richardson@intel.com> wrote:
> 
> > On Fri, Aug 14, 2020 at 12:20:28PM -0700, Stephen Hemminger wrote:
> > > On Fri, 14 Aug 2020 22:09:11 +0300
> > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:
> > >   
> > > > On Fri, 14 Aug 2020 11:45:49 -0700, Stephen Hemminger wrote:  
> > > > > On Fri, 14 Aug 2020 21:39:29 +0300
> > > > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:    
> > > > [...]  
> > > > > FreeBsd doesn't go through the same log code.    
> > > > 
> > > > Then --log-timestamp shouldn't appear on FreeBSD if it's unsupported.  
> > > 
> > > Was using same ifdef that is already there. There are several options already
> > > (like syslog facility) which do nothing on FreeBSD anyway.
> > >   
> > While that is correct, I think that we really should support this on
> > FreeBSD also, since I don't think there is any technical reason we can't do
> > so, right? Even a sub-optimal solution where the timestamp write and log
> > write are non-atomic would be better than nothing.
> > 
> > /Bruce
> 
> FreeBSD now just uses default log stream (stdout).
> Linux has log stream to syslog and stdout.
> 
> There is nothing Linux specific in the old or new code.
> The Linux code could just be moved to common, and both could use it.
> That is a separate patch, and someone with expertise and build setup for FreeBSD needs to do it.
> 






^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-10-19 14:11             ` Thomas Monjalon
@ 2020-10-19 15:25               ` Stephen Hemminger
  2024-03-21 17:22                 ` Thomas Monjalon
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2020-10-19 15:25 UTC (permalink / raw)
  To: Thomas Monjalon
  Cc: Bruce Richardson, Dmitry Kozlyuk, dev, david.marchand, olivier.matz

On Mon, 19 Oct 2020 16:11:19 +0200
Thomas Monjalon <thomas@monjalon.net> wrote:

> There was no ack or conclusion for this patch.
> 
> 17/08/2020 17:11, Stephen Hemminger:
> > On Mon, 17 Aug 2020 11:37:40 +0100
> > Bruce Richardson <bruce.richardson@intel.com> wrote:
> >   
> > > On Fri, Aug 14, 2020 at 12:20:28PM -0700, Stephen Hemminger wrote:  
> > > > On Fri, 14 Aug 2020 22:09:11 +0300
> > > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:
> > > >     
> > > > > On Fri, 14 Aug 2020 11:45:49 -0700, Stephen Hemminger wrote:    
> > > > > > On Fri, 14 Aug 2020 21:39:29 +0300
> > > > > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:      
> > > > > [...]    
> > > > > > FreeBsd doesn't go through the same log code.      
> > > > > 
> > > > > Then --log-timestamp shouldn't appear on FreeBSD if it's unsupported.    
> > > > 
> > > > Was using same ifdef that is already there. There are several options already
> > > > (like syslog facility) which do nothing on FreeBSD anyway.
> > > >     
> > > While that is correct, I think that we really should support this on
> > > FreeBSD also, since I don't think there is any technical reason we can't do
> > > so, right? Even a sub-optimal solution where the timestamp write and log
> > > write are non-atomic would be better than nothing.
> > > 
> > > /Bruce  
> > 
> > FreeBSD now just uses default log stream (stdout).
> > Linux has log stream to syslog and stdout.
> > 
> > There is nothing Linux specific in the old or new code.
> > The Linux code could just be moved to common, and both could use it.
> > That is a separate patch, and someone with expertise and build setup for FreeBSD needs to do it.
> >   

Lets fix FreeBSD to use common code for logging, then timestamp option can be added?

^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v2 0/2] Add option to timestamp console log
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
  2020-08-14 18:39 ` Dmitry Kozlyuk
@ 2023-03-06 18:18 ` Stephen Hemminger
  2023-03-06 18:18   ` [PATCH v2 1/2] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
                     ` (2 more replies)
  2023-03-06 19:28 ` [PATCH v3 " Stephen Hemminger
                   ` (18 subsequent siblings)
  20 siblings, 3 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-06 18:18 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This is a reprise of earlier patch to add timestamp to console
messages.

Example:
# dpdk-testpmd -l 1-4 -n 4 --vdev net_null0 --log-timestamp -- -i
EAL: Detected CPU lcores: 16
EAL: Detected NUMA nodes: 1
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
Interactive-mode selected
[       0.191407] testpmd: create a new mbuf pool <mb_pool_0>: n=171456, size=2176, socket=0
[       0.191510] testpmd: preferred mempool ops selected: ring_mp_mc


v2 
   - rebase to current DPDK
   - make linux log code common to freebsd

Stephen Hemminger (2):
  eal: unify logging code for FreeBsd and Linux
  eal: add option to put timestamp on console output

 .../freebsd_gsg/freebsd_eal_parameters.rst    | 32 ++++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +
 lib/eal/common/eal_common_options.c           |  5 +
 lib/eal/common/eal_internal_cfg.h             |  1 +
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/freebsd/eal.c                         |  6 ++
 lib/eal/linux/eal_log.c                       | 61 ------------
 lib/eal/linux/meson.build                     |  1 -
 lib/eal/unix/eal_log.c                        | 97 +++++++++++++++++++
 lib/eal/unix/meson.build                      |  1 +
 10 files changed, 149 insertions(+), 62 deletions(-)
 delete mode 100644 lib/eal/linux/eal_log.c
 create mode 100644 lib/eal/unix/eal_log.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v2 1/2] eal: unify logging code for FreeBsd and Linux
  2023-03-06 18:18 ` [PATCH v2 0/2] Add option to timestamp console log Stephen Hemminger
@ 2023-03-06 18:18   ` Stephen Hemminger
  2023-03-06 18:18   ` [PATCH v2 2/2] eal: add option to put timestamp on console output Stephen Hemminger
  2023-03-07  9:09   ` [PATCH v2 0/2] Add option to timestamp console log Bruce Richardson
  2 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-06 18:18 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

FreeBSD logging code was not using syslog and did not have
the same options as Linux. Move the log writing code to common
tree.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c             | 6 ++++++
 lib/eal/linux/meson.build         | 1 -
 lib/eal/{linux => unix}/eal_log.c | 0
 lib/eal/unix/meson.build          | 1 +
 4 files changed, 7 insertions(+), 1 deletion(-)
 rename lib/eal/{linux => unix}/eal_log.c (100%)

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 7db4239c5187..15326ccc321d 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -744,6 +744,12 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
+	if (eal_log_init(logid, 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/meson.build b/lib/eal/linux/meson.build
index 3cccfa36c0a4..1b913acc0681 100644
--- a/lib/eal/linux/meson.build
+++ b/lib/eal/linux/meson.build
@@ -11,7 +11,6 @@ sources += files(
         'eal_hugepage_info.c',
         'eal_interrupts.c',
         'eal_lcore.c',
-        'eal_log.c',
         'eal_memalloc.c',
         'eal_memory.c',
         'eal_thread.c',
diff --git a/lib/eal/linux/eal_log.c b/lib/eal/unix/eal_log.c
similarity index 100%
rename from lib/eal/linux/eal_log.c
rename to lib/eal/unix/eal_log.c
diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
index cc7d67dd321d..37d07594df29 100644
--- a/lib/eal/unix/meson.build
+++ b/lib/eal/unix/meson.build
@@ -6,6 +6,7 @@ sources += files(
         'eal_file.c',
         'eal_filesystem.c',
         'eal_firmware.c',
+        'eal_log.c',
         'eal_unix_memory.c',
         'eal_unix_thread.c',
         'eal_unix_timer.c',
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v2 2/2] eal: add option to put timestamp on console output
  2023-03-06 18:18 ` [PATCH v2 0/2] Add option to timestamp console log Stephen Hemminger
  2023-03-06 18:18   ` [PATCH v2 1/2] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
@ 2023-03-06 18:18   ` Stephen Hemminger
  2023-03-07  9:09   ` [PATCH v2 0/2] Add option to timestamp console log Bruce Richardson
  2 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-06 18:18 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format was chosen to be the
same as the default Linux dmesg timestamp.

The messages are written using writev() which avoids unnecessary
buffering and ensures that if multiple threads are logging at
the same time that timestamp and message buffer don't get
interleaved.

Example:
[       0.000040] EAL: Probing VFIO support...

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    | 32 ++++++++++++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
 lib/eal/common/eal_common_options.c           |  5 +++
 lib/eal/common/eal_internal_cfg.h             |  1 +
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/unix/eal_log.c                        | 42 +++++++++++++++++--
 6 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index fba467a2ce92..99cff10e963c 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -18,3 +18,35 @@ FreeBSD-specific EAL parameters
 -------------------------------
 
 There are currently no FreeBSD-specific EAL command-line parameters available.
+
+Other options
+~~~~~~~~~~~~~
+
+*   ``--syslog <syslog facility>``
+
+    Set syslog facility. Valid syslog facilities are::
+
+        auth
+        cron
+        daemon
+        ftp
+        kern
+        lpr
+        mail
+        news
+        syslog
+        user
+        uucp
+        local0
+        local1
+        local2
+        local3
+        local4
+        local5
+        local6
+        local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..719ca6851625 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -135,3 +135,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 03059336987d..2d3d8e82f7f3 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -76,6 +76,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1833,6 +1834,9 @@ eal_parse_common_option(int opt, const char *optarg,
 		}
 		break;
 	}
+	case OPT_LOG_TIMESTAMP_NUM:
+		conf->log_timestamp = 1;
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -2194,6 +2198,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index 167ec501fa79..33144c3619dd 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -85,6 +85,7 @@ struct internal_config {
 	 * per-node) non-legacy mode only.
 	 */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
+	volatile uint8_t log_timestamp;   /**< add timestamp to console output */
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..cc9723868e3c 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index d44416fd6570..d8a900efedea 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -4,11 +4,17 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
 #include "eal_log.h"
+#include "eal_private.h"
+
+static struct timespec log_start_time;
 
 /*
  * default log function
@@ -16,11 +22,39 @@
 static ssize_t
 console_log_write(__rte_unused void *c, const char *buf, size_t size)
 {
+	const struct internal_config *internal_conf =
+		eal_get_internal_configuration();
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
+	/* add optional timestamp for stderr */
+	if (internal_conf->log_timestamp) {
+		struct iovec iov[2];
+		struct timespec ts;
+		char tbuf[64];
+
+		/* format up monotonic timestamp */
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= log_start_time.tv_sec;
+		ts.tv_nsec -= log_start_time.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		/* use writev to put timestamp and buf in same operation */
+		iov[0].iov_base = tbuf;
+		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
+					   ts.tv_sec, ts.tv_nsec / 1000u);
+
+		/* casts are to unconstify the buf */
+		iov[1].iov_base = (void *)(uintptr_t)buf;
+		iov[1].iov_len = size;
+		ret = writev(STDERR_FILENO, iov, 2);
+	} else {
+		/* write on stderr */
+		ret = fwrite(buf, 1, size, stderr);
+		fflush(stderr);
+	}
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
@@ -49,6 +83,8 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	clock_gettime(CLOCK_MONOTONIC, &log_start_time);
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v3 0/2] Add option to timestamp console log
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
  2020-08-14 18:39 ` Dmitry Kozlyuk
  2023-03-06 18:18 ` [PATCH v2 0/2] Add option to timestamp console log Stephen Hemminger
@ 2023-03-06 19:28 ` Stephen Hemminger
  2023-03-06 19:28   ` [PATCH v3 1/2] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
                     ` (2 more replies)
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
                   ` (17 subsequent siblings)
  20 siblings, 3 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-06 19:28 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This is a reprise of earlier patch to add timestamp to console
messages.

Example:
# dpdk-testpmd -l 1-4 -n 4 --vdev net_null0 --log-timestamp -- -i
EAL: Detected CPU lcores: 16
EAL: Detected NUMA nodes: 1
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
Interactive-mode selected
[       0.191407] testpmd: create a new mbuf pool <mb_pool_0>: n=171456, size=2176, socket=0
[       0.191510] testpmd: preferred mempool ops selected: ring_mp_mc


v3 - fix log init on FreeBSD

v2 
   - rebase to current DPDK
   - make linux log code common to freebsd


Stephen Hemminger (2):
  eal: unify logging code for FreeBsd and Linux
  eal: add option to put timestamp on console output

 .../freebsd_gsg/freebsd_eal_parameters.rst    | 32 ++++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +
 lib/eal/common/eal_common_options.c           |  5 +
 lib/eal/common/eal_internal_cfg.h             |  1 +
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/freebsd/eal.c                         |  7 ++
 lib/eal/linux/eal_log.c                       | 61 ------------
 lib/eal/linux/meson.build                     |  1 -
 lib/eal/unix/eal_log.c                        | 97 +++++++++++++++++++
 lib/eal/unix/meson.build                      |  1 +
 10 files changed, 150 insertions(+), 62 deletions(-)
 delete mode 100644 lib/eal/linux/eal_log.c
 create mode 100644 lib/eal/unix/eal_log.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v3 1/2] eal: unify logging code for FreeBsd and Linux
  2023-03-06 19:28 ` [PATCH v3 " Stephen Hemminger
@ 2023-03-06 19:28   ` Stephen Hemminger
  2023-03-06 19:28   ` [PATCH v3 2/2] eal: add option to put timestamp on console output Stephen Hemminger
  2023-03-07  7:33   ` [PATCH v3 0/2] Add option to timestamp console log Morten Brørup
  2 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-06 19:28 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

FreeBSD logging code was not using syslog and did not have
the same options as Linux. Move the log writing code to common
tree.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c             | 7 +++++++
 lib/eal/linux/meson.build         | 1 -
 lib/eal/{linux => unix}/eal_log.c | 0
 lib/eal/unix/meson.build          | 1 +
 4 files changed, 8 insertions(+), 1 deletion(-)
 rename lib/eal/{linux => unix}/eal_log.c (100%)

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 7db4239c5187..3a53f9e13887 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -51,6 +51,7 @@
 #include "eal_hugepages.h"
 #include "eal_options.h"
 #include "eal_memcfg.h"
+#include "eal_log.h"
 #include "eal_trace.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
@@ -744,6 +745,12 @@ 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/meson.build b/lib/eal/linux/meson.build
index 3cccfa36c0a4..1b913acc0681 100644
--- a/lib/eal/linux/meson.build
+++ b/lib/eal/linux/meson.build
@@ -11,7 +11,6 @@ sources += files(
         'eal_hugepage_info.c',
         'eal_interrupts.c',
         'eal_lcore.c',
-        'eal_log.c',
         'eal_memalloc.c',
         'eal_memory.c',
         'eal_thread.c',
diff --git a/lib/eal/linux/eal_log.c b/lib/eal/unix/eal_log.c
similarity index 100%
rename from lib/eal/linux/eal_log.c
rename to lib/eal/unix/eal_log.c
diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
index cc7d67dd321d..37d07594df29 100644
--- a/lib/eal/unix/meson.build
+++ b/lib/eal/unix/meson.build
@@ -6,6 +6,7 @@ sources += files(
         'eal_file.c',
         'eal_filesystem.c',
         'eal_firmware.c',
+        'eal_log.c',
         'eal_unix_memory.c',
         'eal_unix_thread.c',
         'eal_unix_timer.c',
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-06 19:28 ` [PATCH v3 " Stephen Hemminger
  2023-03-06 19:28   ` [PATCH v3 1/2] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
@ 2023-03-06 19:28   ` Stephen Hemminger
  2023-03-07  9:35     ` fengchengwen
  2023-03-07  7:33   ` [PATCH v3 0/2] Add option to timestamp console log Morten Brørup
  2 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-06 19:28 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

Example:
[       0.000040] EAL: Probing VFIO support...

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    | 32 ++++++++++++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
 lib/eal/common/eal_common_options.c           |  5 +++
 lib/eal/common/eal_internal_cfg.h             |  1 +
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/unix/eal_log.c                        | 42 +++++++++++++++++--
 6 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index fba467a2ce92..99cff10e963c 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -18,3 +18,35 @@ FreeBSD-specific EAL parameters
 -------------------------------
 
 There are currently no FreeBSD-specific EAL command-line parameters available.
+
+Other options
+~~~~~~~~~~~~~
+
+*   ``--syslog <syslog facility>``
+
+    Set syslog facility. Valid syslog facilities are::
+
+        auth
+        cron
+        daemon
+        ftp
+        kern
+        lpr
+        mail
+        news
+        syslog
+        user
+        uucp
+        local0
+        local1
+        local2
+        local3
+        local4
+        local5
+        local6
+        local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..719ca6851625 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -135,3 +135,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 03059336987d..2d3d8e82f7f3 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -76,6 +76,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1833,6 +1834,9 @@ eal_parse_common_option(int opt, const char *optarg,
 		}
 		break;
 	}
+	case OPT_LOG_TIMESTAMP_NUM:
+		conf->log_timestamp = 1;
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -2194,6 +2198,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index 167ec501fa79..33144c3619dd 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -85,6 +85,7 @@ struct internal_config {
 	 * per-node) non-legacy mode only.
 	 */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
+	volatile uint8_t log_timestamp;   /**< add timestamp to console output */
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..cc9723868e3c 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index d44416fd6570..d8a900efedea 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -4,11 +4,17 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
 #include "eal_log.h"
+#include "eal_private.h"
+
+static struct timespec log_start_time;
 
 /*
  * default log function
@@ -16,11 +22,39 @@
 static ssize_t
 console_log_write(__rte_unused void *c, const char *buf, size_t size)
 {
+	const struct internal_config *internal_conf =
+		eal_get_internal_configuration();
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
+	/* add optional timestamp for stderr */
+	if (internal_conf->log_timestamp) {
+		struct iovec iov[2];
+		struct timespec ts;
+		char tbuf[64];
+
+		/* format up monotonic timestamp */
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= log_start_time.tv_sec;
+		ts.tv_nsec -= log_start_time.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		/* use writev to put timestamp and buf in same operation */
+		iov[0].iov_base = tbuf;
+		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
+					   ts.tv_sec, ts.tv_nsec / 1000u);
+
+		/* casts are to unconstify the buf */
+		iov[1].iov_base = (void *)(uintptr_t)buf;
+		iov[1].iov_len = size;
+		ret = writev(STDERR_FILENO, iov, 2);
+	} else {
+		/* write on stderr */
+		ret = fwrite(buf, 1, size, stderr);
+		fflush(stderr);
+	}
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
@@ -49,6 +83,8 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	clock_gettime(CLOCK_MONOTONIC, &log_start_time);
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v3 0/2] Add option to timestamp console log
  2023-03-06 19:28 ` [PATCH v3 " Stephen Hemminger
  2023-03-06 19:28   ` [PATCH v3 1/2] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
  2023-03-06 19:28   ` [PATCH v3 2/2] eal: add option to put timestamp on console output Stephen Hemminger
@ 2023-03-07  7:33   ` Morten Brørup
  2023-03-07  9:12     ` Bruce Richardson
  2023-03-07 16:04     ` Stephen Hemminger
  2 siblings, 2 replies; 277+ messages in thread
From: Morten Brørup @ 2023-03-07  7:33 UTC (permalink / raw)
  To: Stephen Hemminger, dev

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Monday, 6 March 2023 20.28
> 
> This is a reprise of earlier patch to add timestamp to console
> messages.
> 
> Example:
> # dpdk-testpmd -l 1-4 -n 4 --vdev net_null0 --log-timestamp -- -i
> EAL: Detected CPU lcores: 16
> EAL: Detected NUMA nodes: 1
> EAL: Detected static linkage of DPDK
> EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
> EAL: Selected IOVA mode 'VA'
> Interactive-mode selected
> [       0.191407] testpmd: create a new mbuf pool <mb_pool_0>: n=171456,
> size=2176, socket=0
> [       0.191510] testpmd: preferred mempool ops selected: ring_mp_mc

This is useful.

Here's some feature creep: Since the timestamp output is configurable, there is no need to settle on one specific timestamp format. The option could allow a choice between MONOTONIC and REALTIME, with an option for REALTIME to include the date in ISO 8601 format (YYYY-MM-DD). And REALTIME could be UTC or local time. You could consider taking a format string for strftime(), with the extension that %f expands to 6 digit microseconds like in Python [1].

[1]: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

With or without feature creep,

Series-acked-by: Morten Brørup <mb@smartsharesystems.com>


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v2 0/2] Add option to timestamp console log
  2023-03-06 18:18 ` [PATCH v2 0/2] Add option to timestamp console log Stephen Hemminger
  2023-03-06 18:18   ` [PATCH v2 1/2] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
  2023-03-06 18:18   ` [PATCH v2 2/2] eal: add option to put timestamp on console output Stephen Hemminger
@ 2023-03-07  9:09   ` Bruce Richardson
  2 siblings, 0 replies; 277+ messages in thread
From: Bruce Richardson @ 2023-03-07  9:09 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Mon, Mar 06, 2023 at 10:18:14AM -0800, Stephen Hemminger wrote:
> This is a reprise of earlier patch to add timestamp to console
> messages.
> 
> Example:
> # dpdk-testpmd -l 1-4 -n 4 --vdev net_null0 --log-timestamp -- -i
> EAL: Detected CPU lcores: 16
> EAL: Detected NUMA nodes: 1
> EAL: Detected static linkage of DPDK
> EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
> EAL: Selected IOVA mode 'VA'
> Interactive-mode selected
> [       0.191407] testpmd: create a new mbuf pool <mb_pool_0>: n=171456, size=2176, socket=0
> [       0.191510] testpmd: preferred mempool ops selected: ring_mp_mc
> 
> 
> v2 
>    - rebase to current DPDK
>    - make linux log code common to freebsd
> 
> Stephen Hemminger (2):
>   eal: unify logging code for FreeBsd and Linux
>   eal: add option to put timestamp on console output
>
Series-acked-by: Bruce Richardson <bruce.richardson@intel.com> 

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 0/2] Add option to timestamp console log
  2023-03-07  7:33   ` [PATCH v3 0/2] Add option to timestamp console log Morten Brørup
@ 2023-03-07  9:12     ` Bruce Richardson
  2023-03-07 16:04     ` Stephen Hemminger
  1 sibling, 0 replies; 277+ messages in thread
From: Bruce Richardson @ 2023-03-07  9:12 UTC (permalink / raw)
  To: Morten Brørup; +Cc: Stephen Hemminger, dev

On Tue, Mar 07, 2023 at 08:33:17AM +0100, Morten Brørup wrote:
> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Monday, 6 March 2023 20.28
> > 
> > This is a reprise of earlier patch to add timestamp to console
> > messages.
> > 
> > Example:
> > # dpdk-testpmd -l 1-4 -n 4 --vdev net_null0 --log-timestamp -- -i
> > EAL: Detected CPU lcores: 16
> > EAL: Detected NUMA nodes: 1
> > EAL: Detected static linkage of DPDK
> > EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
> > EAL: Selected IOVA mode 'VA'
> > Interactive-mode selected
> > [       0.191407] testpmd: create a new mbuf pool <mb_pool_0>: n=171456,
> > size=2176, socket=0
> > [       0.191510] testpmd: preferred mempool ops selected: ring_mp_mc
> 
> This is useful.
> 
> Here's some feature creep: Since the timestamp output is configurable, there is no need to settle on one specific timestamp format. The option could allow a choice between MONOTONIC and REALTIME, with an option for REALTIME to include the date in ISO 8601 format (YYYY-MM-DD). And REALTIME could be UTC or local time. You could consider taking a format string for strftime(), with the extension that %f expands to 6 digit microseconds like in Python [1].
> 
> [1]: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
> 
> With or without feature creep,
> 
> Series-acked-by: Morten Brørup <mb@smartsharesystems.com>
>

Missed the fact there was already a v3 when I acked v2, so repeating here.

Series-acked-by: Bruce Richardson <bruce.richardson@intel.com>

I'd rather not have the feature creep on it though - at least not yet!

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-06 19:28   ` [PATCH v3 2/2] eal: add option to put timestamp on console output Stephen Hemminger
@ 2023-03-07  9:35     ` fengchengwen
  2023-03-07 16:05       ` Stephen Hemminger
  2023-03-07 16:06       ` Stephen Hemminger
  0 siblings, 2 replies; 277+ messages in thread
From: fengchengwen @ 2023-03-07  9:35 UTC (permalink / raw)
  To: Stephen Hemminger, dev; +Cc: Bruce Richardson

On 2023/3/7 3:28, Stephen Hemminger wrote:
> When debugging driver or startup issues, it is useful to have
> a timestamp on each message printed. The messages in syslog
> already have a timestamp, but often syslog is not available
> during testing. The timestamp format is chosen to look
> like the default Linux dmesg timestamp.
> 
> Example:
> [       0.000040] EAL: Probing VFIO support...
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  .../freebsd_gsg/freebsd_eal_parameters.rst    | 32 ++++++++++++++
>  doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
>  lib/eal/common/eal_common_options.c           |  5 +++
>  lib/eal/common/eal_internal_cfg.h             |  1 +
>  lib/eal/common/eal_options.h                  |  2 +
>  lib/eal/unix/eal_log.c                        | 42 +++++++++++++++++--
>  6 files changed, 84 insertions(+), 3 deletions(-)
> 
> diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
> index fba467a2ce92..99cff10e963c 100644
> --- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
> +++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
> @@ -18,3 +18,35 @@ FreeBSD-specific EAL parameters
>  -------------------------------
>  
>  There are currently no FreeBSD-specific EAL command-line parameters available.
> +
> +Other options
> +~~~~~~~~~~~~~
> +
> +*   ``--syslog <syslog facility>``
> +
> +    Set syslog facility. Valid syslog facilities are::
> +
> +        auth
> +        cron
> +        daemon
> +        ftp
> +        kern
> +        lpr
> +        mail
> +        news
> +        syslog
> +        user
> +        uucp
> +        local0
> +        local1
> +        local2
> +        local3
> +        local4
> +        local5
> +        local6
> +        local7

This should add to commit 1/2 [PATCH v3 1/2] eal: unify logging code for FreeBsd and Linux

> +
> +*   ``--log-timestamp``
> +
> +    Add a timestamp of seconds and microseconds to each log message
> +    written to standard output.
> diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
> index ea8f38139119..719ca6851625 100644
> --- a/doc/guides/linux_gsg/linux_eal_parameters.rst
> +++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
> @@ -135,3 +135,8 @@ Other options
>          local5
>          local6
>          local7
> +
> +*   ``--log-timestamp``
> +
> +    Add a timestamp of seconds and microseconds to each log message
> +    written to standard output.
> diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
> index 03059336987d..2d3d8e82f7f3 100644
> --- a/lib/eal/common/eal_common_options.c
> +++ b/lib/eal/common/eal_common_options.c
> @@ -76,6 +76,7 @@ eal_long_options[] = {
>  	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
>  	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
>  	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
> +	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
>  	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
>  	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
>  	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
> @@ -1833,6 +1834,9 @@ eal_parse_common_option(int opt, const char *optarg,
>  		}
>  		break;
>  	}
> +	case OPT_LOG_TIMESTAMP_NUM:
> +		conf->log_timestamp = 1;
> +		break;
>  
>  #ifndef RTE_EXEC_ENV_WINDOWS
>  	case OPT_TRACE_NUM: {
> @@ -2194,6 +2198,7 @@ eal_common_usage(void)
>  	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
>  #ifndef RTE_EXEC_ENV_WINDOWS
>  	       "  --"OPT_SYSLOG"            Set syslog facility\n"
> +	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
>  #endif
>  	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
>  	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
> diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
> index 167ec501fa79..33144c3619dd 100644
> --- a/lib/eal/common/eal_internal_cfg.h
> +++ b/lib/eal/common/eal_internal_cfg.h
> @@ -85,6 +85,7 @@ struct internal_config {
>  	 * per-node) non-legacy mode only.
>  	 */
>  	volatile int syslog_facility;	  /**< facility passed to openlog() */
> +	volatile uint8_t log_timestamp;   /**< add timestamp to console output */
>  	/** default interrupt mode for VFIO */
>  	volatile enum rte_intr_mode vfio_intr_mode;
>  	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
> diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
> index 3cc9cb641284..cc9723868e3c 100644
> --- a/lib/eal/common/eal_options.h
> +++ b/lib/eal/common/eal_options.h
> @@ -35,6 +35,8 @@ enum {
>  	OPT_LCORES_NUM,
>  #define OPT_LOG_LEVEL         "log-level"
>  	OPT_LOG_LEVEL_NUM,
> +#define OPT_LOG_TIMESTAMP     "log-timestamp"
> +	OPT_LOG_TIMESTAMP_NUM,
>  #define OPT_TRACE             "trace"
>  	OPT_TRACE_NUM,
>  #define OPT_TRACE_DIR         "trace-dir"
> diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
> index d44416fd6570..d8a900efedea 100644
> --- a/lib/eal/unix/eal_log.c
> +++ b/lib/eal/unix/eal_log.c
> @@ -4,11 +4,17 @@
>  
>  #include <stdio.h>
>  #include <sys/types.h>
> +#include <sys/uio.h>
>  #include <syslog.h>
> +#include <time.h>
> +#include <unistd.h>
>  
>  #include <rte_log.h>
>  
>  #include "eal_log.h"
> +#include "eal_private.h"
> +
> +static struct timespec log_start_time;

This is process's private, how about support multi-process ? so that all process has the same base?

>  
>  /*
>   * default log function
> @@ -16,11 +22,39 @@
>  static ssize_t
>  console_log_write(__rte_unused void *c, const char *buf, size_t size)
>  {
> +	const struct internal_config *internal_conf =
> +		eal_get_internal_configuration();
>  	ssize_t ret;
>  
> -	/* write on stderr */
> -	ret = fwrite(buf, 1, size, stderr);
> -	fflush(stderr);
> +	/* add optional timestamp for stderr */
> +	if (internal_conf->log_timestamp) {
> +		struct iovec iov[2];
> +		struct timespec ts;
> +		char tbuf[64];
> +
> +		/* format up monotonic timestamp */
> +		clock_gettime(CLOCK_MONOTONIC, &ts);
> +		ts.tv_sec -= log_start_time.tv_sec;
> +		ts.tv_nsec -= log_start_time.tv_nsec;
> +		if (ts.tv_nsec < 0) {
> +			--ts.tv_sec;
> +			ts.tv_nsec += 1000000000ul;
> +		}
> +
> +		/* use writev to put timestamp and buf in same operation */
> +		iov[0].iov_base = tbuf;
> +		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
> +					   ts.tv_sec, ts.tv_nsec / 1000u);
> +
> +		/* casts are to unconstify the buf */
> +		iov[1].iov_base = (void *)(uintptr_t)buf;
> +		iov[1].iov_len = size;
> +		ret = writev(STDERR_FILENO, iov, 2);
> +	} else {
> +		/* write on stderr */
> +		ret = fwrite(buf, 1, size, stderr);
> +		fflush(stderr);
> +	}
>  
>  	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
>  	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);

The syslog will add timestamp, but the syslog backend will re-write timestamp, so
in the last, you can't find the real-timestamp of this log print. sometimes it requires
to get real log time.
PS: we found it in our test environment because RR schedule hang too long (similar question
also found: https://bugzilla.redhat.com/show_bug.cgi?id=1855447).

So suggest add timestamp in syslog string also, and don't convert to monotonic and just
print as normal format (just like syslog).

> @@ -49,6 +83,8 @@ eal_log_init(const char *id, int facility)
>  {
>  	FILE *log_stream;
>  
> +	clock_gettime(CLOCK_MONOTONIC, &log_start_time);
> +
>  	log_stream = fopencookie(NULL, "w+", console_log_func);
>  	if (log_stream == NULL)
>  		return -1;
> 

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 0/2] Add option to timestamp console log
  2023-03-07  7:33   ` [PATCH v3 0/2] Add option to timestamp console log Morten Brørup
  2023-03-07  9:12     ` Bruce Richardson
@ 2023-03-07 16:04     ` Stephen Hemminger
  1 sibling, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-07 16:04 UTC (permalink / raw)
  To: Morten Brørup; +Cc: dev

On Tue, 7 Mar 2023 08:33:17 +0100
Morten Brørup <mb@smartsharesystems.com> wrote:

> Here's some feature creep: Since the timestamp output is configurable, there is no need to settle on one specific timestamp format. The option could allow a choice between MONOTONIC and REALTIME, with an option for REALTIME to include the date in ISO 8601 format (YYYY-MM-DD). And REALTIME could be UTC or local time. You could consider taking a format string for strftime(), with the extension that %f expands to 6 digit microseconds like in Python [1].

Does that have compiler static checking issues if you allow user to set formats?

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-07  9:35     ` fengchengwen
@ 2023-03-07 16:05       ` Stephen Hemminger
  2023-03-07 16:06       ` Stephen Hemminger
  1 sibling, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-07 16:05 UTC (permalink / raw)
  To: fengchengwen; +Cc: dev, Bruce Richardson

On Tue, 7 Mar 2023 17:35:32 +0800
fengchengwen <fengchengwen@huawei.com> wrote:

> On 2023/3/7 3:28, Stephen Hemminger wrote:
> > When debugging driver or startup issues, it is useful to have
> > a timestamp on each message printed. The messages in syslog
> > already have a timestamp, but often syslog is not available
> > during testing. The timestamp format is chosen to look
> > like the default Linux dmesg timestamp.
> > 
> > Example:
> > [       0.000040] EAL: Probing VFIO support...
> > 
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> > ---
> >  .../freebsd_gsg/freebsd_eal_parameters.rst    | 32 ++++++++++++++
> >  doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
> >  lib/eal/common/eal_common_options.c           |  5 +++
> >  lib/eal/common/eal_internal_cfg.h             |  1 +
> >  lib/eal/common/eal_options.h                  |  2 +
> >  lib/eal/unix/eal_log.c                        | 42 +++++++++++++++++--
> >  6 files changed, 84 insertions(+), 3 deletions(-)
> > 
> > diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
> > index fba467a2ce92..99cff10e963c 100644
> > --- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
> > +++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
> > @@ -18,3 +18,35 @@ FreeBSD-specific EAL parameters
> >  -------------------------------
> >  
> >  There are currently no FreeBSD-specific EAL command-line parameters available.
> > +
> > +Other options
> > +~~~~~~~~~~~~~
> > +
> > +*   ``--syslog <syslog facility>``
> > +
> > +    Set syslog facility. Valid syslog facilities are::
> > +
> > +        auth
> > +        cron
> > +        daemon
> > +        ftp
> > +        kern
> > +        lpr
> > +        mail
> > +        news
> > +        syslog
> > +        user
> > +        uucp
> > +        local0
> > +        local1
> > +        local2
> > +        local3
> > +        local4
> > +        local5
> > +        local6
> > +        local7  
> 
> This should add to commit 1/2 [PATCH v3 1/2] eal: unify logging code for FreeBsd and Linux
> 
> > +
> > +*   ``--log-timestamp``
> > +
> > +    Add a timestamp of seconds and microseconds to each log message
> > +    written to standard output.
> > diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
> > index ea8f38139119..719ca6851625 100644
> > --- a/doc/guides/linux_gsg/linux_eal_parameters.rst
> > +++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
> > @@ -135,3 +135,8 @@ Other options
> >          local5
> >          local6
> >          local7
> > +
> > +*   ``--log-timestamp``
> > +
> > +    Add a timestamp of seconds and microseconds to each log message
> > +    written to standard output.
> > diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
> > index 03059336987d..2d3d8e82f7f3 100644
> > --- a/lib/eal/common/eal_common_options.c
> > +++ b/lib/eal/common/eal_common_options.c
> > @@ -76,6 +76,7 @@ eal_long_options[] = {
> >  	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
> >  	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
> >  	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
> > +	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
> >  	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
> >  	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
> >  	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
> > @@ -1833,6 +1834,9 @@ eal_parse_common_option(int opt, const char *optarg,
> >  		}
> >  		break;
> >  	}
> > +	case OPT_LOG_TIMESTAMP_NUM:
> > +		conf->log_timestamp = 1;
> > +		break;
> >  
> >  #ifndef RTE_EXEC_ENV_WINDOWS
> >  	case OPT_TRACE_NUM: {
> > @@ -2194,6 +2198,7 @@ eal_common_usage(void)
> >  	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
> >  #ifndef RTE_EXEC_ENV_WINDOWS
> >  	       "  --"OPT_SYSLOG"            Set syslog facility\n"
> > +	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
> >  #endif
> >  	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
> >  	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
> > diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
> > index 167ec501fa79..33144c3619dd 100644
> > --- a/lib/eal/common/eal_internal_cfg.h
> > +++ b/lib/eal/common/eal_internal_cfg.h
> > @@ -85,6 +85,7 @@ struct internal_config {
> >  	 * per-node) non-legacy mode only.
> >  	 */
> >  	volatile int syslog_facility;	  /**< facility passed to openlog() */
> > +	volatile uint8_t log_timestamp;   /**< add timestamp to console output */
> >  	/** default interrupt mode for VFIO */
> >  	volatile enum rte_intr_mode vfio_intr_mode;
> >  	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
> > diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
> > index 3cc9cb641284..cc9723868e3c 100644
> > --- a/lib/eal/common/eal_options.h
> > +++ b/lib/eal/common/eal_options.h
> > @@ -35,6 +35,8 @@ enum {
> >  	OPT_LCORES_NUM,
> >  #define OPT_LOG_LEVEL         "log-level"
> >  	OPT_LOG_LEVEL_NUM,
> > +#define OPT_LOG_TIMESTAMP     "log-timestamp"
> > +	OPT_LOG_TIMESTAMP_NUM,
> >  #define OPT_TRACE             "trace"
> >  	OPT_TRACE_NUM,
> >  #define OPT_TRACE_DIR         "trace-dir"
> > diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
> > index d44416fd6570..d8a900efedea 100644
> > --- a/lib/eal/unix/eal_log.c
> > +++ b/lib/eal/unix/eal_log.c
> > @@ -4,11 +4,17 @@
> >  
> >  #include <stdio.h>
> >  #include <sys/types.h>
> > +#include <sys/uio.h>
> >  #include <syslog.h>
> > +#include <time.h>
> > +#include <unistd.h>
> >  
> >  #include <rte_log.h>
> >  
> >  #include "eal_log.h"
> > +#include "eal_private.h"
> > +
> > +static struct timespec log_start_time;  
> 
> This is process's private, how about support multi-process ? so that all process has the same base?


If using primary/secondary, then the secondary's output would have time zero
of when secondary started.  Not sure if that is good or bad?

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-07  9:35     ` fengchengwen
  2023-03-07 16:05       ` Stephen Hemminger
@ 2023-03-07 16:06       ` Stephen Hemminger
  2023-03-08  0:36         ` fengchengwen
  1 sibling, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-07 16:06 UTC (permalink / raw)
  To: fengchengwen; +Cc: dev, Bruce Richardson

On Tue, 7 Mar 2023 17:35:32 +0800
fengchengwen <fengchengwen@huawei.com> wrote:

> The syslog will add timestamp, but the syslog backend will re-write timestamp, so
> in the last, you can't find the real-timestamp of this log print. sometimes it requires
> to get real log time.
> PS: we found it in our test environment because RR schedule hang too long (similar question
> also found: https://bugzilla.redhat.com/show_bug.cgi?id=1855447).
> 
> So suggest add timestamp in syslog string also, and don't convert to monotonic and just
> print as normal format (just like syslog).


Are you using systemd?

Never, never configure a DPDK application with real-time process priority.
Polling model and RT don't mix.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-07 16:06       ` Stephen Hemminger
@ 2023-03-08  0:36         ` fengchengwen
  2023-03-08  2:03           ` Stephen Hemminger
  2023-03-08  2:51           ` Stephen Hemminger
  0 siblings, 2 replies; 277+ messages in thread
From: fengchengwen @ 2023-03-08  0:36 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev, Bruce Richardson



On 2023/3/8 0:06, Stephen Hemminger wrote:
> On Tue, 7 Mar 2023 17:35:32 +0800
> fengchengwen <fengchengwen@huawei.com> wrote:
> 
>> The syslog will add timestamp, but the syslog backend will re-write timestamp, so
>> in the last, you can't find the real-timestamp of this log print. sometimes it requires
>> to get real log time.
>> PS: we found it in our test environment because RR schedule hang too long (similar question
>> also found: https://bugzilla.redhat.com/show_bug.cgi?id=1855447).
>>
>> So suggest add timestamp in syslog string also, and don't convert to monotonic and just
>> print as normal format (just like syslog).
> 
> 
> Are you using systemd?

Yes

> 
> Never, never configure a DPDK application with real-time process priority.
> Polling model and RT don't mix.

Maybe we should document them ?

> .
> 

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-08  0:36         ` fengchengwen
@ 2023-03-08  2:03           ` Stephen Hemminger
  2023-03-09  0:55             ` fengchengwen
  2023-03-08  2:51           ` Stephen Hemminger
  1 sibling, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-08  2:03 UTC (permalink / raw)
  To: fengchengwen; +Cc: dev, Bruce Richardson

On Wed, 8 Mar 2023 08:36:48 +0800
fengchengwen <fengchengwen@huawei.com> wrote:

> On 2023/3/8 0:06, Stephen Hemminger wrote:
> > On Tue, 7 Mar 2023 17:35:32 +0800
> > fengchengwen <fengchengwen@huawei.com> wrote:
> >   
> >> The syslog will add timestamp, but the syslog backend will re-write timestamp, so
> >> in the last, you can't find the real-timestamp of this log print. sometimes it requires
> >> to get real log time.
> >> PS: we found it in our test environment because RR schedule hang too long (similar question
> >> also found: https://bugzilla.redhat.com/show_bug.cgi?id=1855447).
> >>
> >> So suggest add timestamp in syslog string also, and don't convert to monotonic and just
> >> print as normal format (just like syslog).  
> > 
> > 
> > Are you using systemd?  
> 
> Yes

There is redhat bug about this:
https://bugzilla.redhat.com/show_bug.cgi?id=991678

> > Never, never configure a DPDK application with real-time process priority.
> > Polling model and RT don't mix.  
> 
> Maybe we should document them ?

Part of previous discussion here:
https://mails.dpdk.org/archives/dev/2021-April/203778.html

In my experience running DPDK on isolated threads (cgroup or scheduler isolation)
combined with remapping interrupts gives best response without lockup.

I.e don't depend on scheduler to do the right thing. Instead ensure that
each thread runs on dedicated CPU.




^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-08  0:36         ` fengchengwen
  2023-03-08  2:03           ` Stephen Hemminger
@ 2023-03-08  2:51           ` Stephen Hemminger
  1 sibling, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-03-08  2:51 UTC (permalink / raw)
  To: fengchengwen; +Cc: dev, Bruce Richardson

On Wed, 8 Mar 2023 08:36:48 +0800
fengchengwen <fengchengwen@huawei.com> wrote:

> > Never, never configure a DPDK application with real-time process priority.
> > Polling model and RT don't mix.  
> 
> Maybe we should document them ?


LOL - ChatGPT is amusing if you ask it. The answer is pro grammatically
correct, but will not work. The caveat at the end is much bigger than it
understands.

Question:

Is Dataplane Development Kit (DPDK) compatible with Linux Real Time thread priority like SCH_FIFO? 
Yes, Dataplane Development Kit (DPDK) is compatible with Linux Real-Time thread priority scheduling policies, such as SCHED_FIFO.

Answer:

DPDK supports setting the thread priority using the standard Linux pthread APIs, including pthread_setschedparam() and pthread_attr_setschedpolicy(). This allows the application to configure the thread scheduling policy and priority for DPDK worker threads.

To set the real-time priority of DPDK threads, you can use the pthread_setschedparam() function to set the scheduling policy to SCHED_FIFO or SCHED_RR and the priority value to a value between 1 and 99. Here is an example of how to set the real-time priority of a DPDK thread:

> pthread_t tid;
> struct sched_param sched_param;
> pthread_attr_t attr;
> 
> pthread_attr_init(&attr);
> pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
> 
> sched_param.sched_priority = 50;
> pthread_attr_setschedparam(&attr, &sched_param);
> pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
> 
> pthread_create(&tid, &attr, thread_func, arg);


In this example, the SCHED_FIFO scheduling policy is used, and the priority value is set to 50. You can adjust the priority value according to your needs.

Note that using real-time scheduling policies like SCHED_FIFO or SCHED_RR requires careful consideration of system resources and may impact system performance if not used properly. It is recommended to consult the Linux documentation and to test the system thoroughly before deploying any application using real-time scheduling policies.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v3 2/2] eal: add option to put timestamp on console output
  2023-03-08  2:03           ` Stephen Hemminger
@ 2023-03-09  0:55             ` fengchengwen
  0 siblings, 0 replies; 277+ messages in thread
From: fengchengwen @ 2023-03-09  0:55 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev, Bruce Richardson

On 2023/3/8 10:03, Stephen Hemminger wrote:
> On Wed, 8 Mar 2023 08:36:48 +0800
> fengchengwen <fengchengwen@huawei.com> wrote:
> 
>> On 2023/3/8 0:06, Stephen Hemminger wrote:
>>> On Tue, 7 Mar 2023 17:35:32 +0800
>>> fengchengwen <fengchengwen@huawei.com> wrote:
>>>   
>>>> The syslog will add timestamp, but the syslog backend will re-write timestamp, so
>>>> in the last, you can't find the real-timestamp of this log print. sometimes it requires
>>>> to get real log time.
>>>> PS: we found it in our test environment because RR schedule hang too long (similar question
>>>> also found: https://bugzilla.redhat.com/show_bug.cgi?id=1855447).
>>>>
>>>> So suggest add timestamp in syslog string also, and don't convert to monotonic and just
>>>> print as normal format (just like syslog).  
>>>
>>>
>>> Are you using systemd?  
>>
>> Yes
> 
> There is redhat bug about this:
> https://bugzilla.redhat.com/show_bug.cgi?id=991678
> 
>>> Never, never configure a DPDK application with real-time process priority.
>>> Polling model and RT don't mix.  
>>
>> Maybe we should document them ?
> 
> Part of previous discussion here:
> https://mails.dpdk.org/archives/dev/2021-April/203778.html
> 
> In my experience running DPDK on isolated threads (cgroup or scheduler isolation)
> combined with remapping interrupts gives best response without lockup.
> 
> I.e don't depend on scheduler to do the right thing. Instead ensure that
> each thread runs on dedicated CPU.

Got it, thanks.

> 
> 
> 
> .
> 

^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v4 0/5] Logging related patchs
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (2 preceding siblings ...)
  2023-03-06 19:28 ` [PATCH v3 " Stephen Hemminger
@ 2023-06-26 18:42 ` Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 1/5] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
                     ` (5 more replies)
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
                   ` (16 subsequent siblings)
  20 siblings, 6 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-26 18:42 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This patch set rebases and extends some earlier work on logging.

Stephen Hemminger (5):
  eal: unify logging code for FreeBsd and Linux
  eal: turn off getopt_long error message during eal_log_level
  eal: skip stdio on console logging
  eal: move logging initialization earlier
  eal: add option to put timestamp on console output

 .../freebsd_gsg/freebsd_eal_parameters.rst    | 32 +++++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +
 lib/eal/common/eal_common_options.c           |  6 ++
 lib/eal/common/eal_internal_cfg.h             |  3 +
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/freebsd/eal.c                         | 39 +++++---
 lib/eal/linux/eal.c                           | 48 +++++-----
 lib/eal/linux/eal_log.c                       | 61 ------------
 lib/eal/linux/meson.build                     |  1 -
 lib/eal/unix/eal_log.c                        | 93 +++++++++++++++++++
 lib/eal/unix/meson.build                      |  1 +
 lib/eal/windows/eal.c                         |  3 +
 12 files changed, 194 insertions(+), 100 deletions(-)
 delete mode 100644 lib/eal/linux/eal_log.c
 create mode 100644 lib/eal/unix/eal_log.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v4 1/5] eal: unify logging code for FreeBsd and Linux
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
@ 2023-06-26 18:42   ` Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 2/5] eal: turn off getopt_long error message during eal_log_level Stephen Hemminger
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-26 18:42 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

FreeBSD logging code was not using syslog and did not have
the same options as Linux. Move the log writing code to common
source tree.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    | 27 +++++++++++++++++++
 lib/eal/freebsd/eal.c                         |  7 +++++
 lib/eal/linux/meson.build                     |  1 -
 lib/eal/{linux => unix}/eal_log.c             |  0
 lib/eal/unix/meson.build                      |  1 +
 5 files changed, 35 insertions(+), 1 deletion(-)
 rename lib/eal/{linux => unix}/eal_log.c (100%)

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index fba467a2ce92..9270d9fa3bfc 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -18,3 +18,30 @@ FreeBSD-specific EAL parameters
 -------------------------------
 
 There are currently no FreeBSD-specific EAL command-line parameters available.
+
+Other options
+~~~~~~~~~~~~~
+
+*   ``--syslog <syslog facility>``
+
+    Set syslog facility. Valid syslog facilities are::
+
+        auth
+        cron
+        daemon
+        ftp
+        kern
+        lpr
+        mail
+        news
+        syslog
+        user
+        uucp
+        local0
+        local1
+        local2
+        local3
+        local4
+        local5
+        local6
+        local7
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 7008303e112a..6df6873e3889 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -52,6 +52,7 @@
 #include "eal_hugepages.h"
 #include "eal_options.h"
 #include "eal_memcfg.h"
+#include "eal_log.h"
 #include "eal_trace.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
@@ -759,6 +760,12 @@ 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/meson.build b/lib/eal/linux/meson.build
index 5af456db9edb..e99ebed25692 100644
--- a/lib/eal/linux/meson.build
+++ b/lib/eal/linux/meson.build
@@ -11,7 +11,6 @@ sources += files(
         'eal_hugepage_info.c',
         'eal_interrupts.c',
         'eal_lcore.c',
-        'eal_log.c',
         'eal_memalloc.c',
         'eal_memory.c',
         'eal_thread.c',
diff --git a/lib/eal/linux/eal_log.c b/lib/eal/unix/eal_log.c
similarity index 100%
rename from lib/eal/linux/eal_log.c
rename to lib/eal/unix/eal_log.c
diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
index cc7d67dd321d..37d07594df29 100644
--- a/lib/eal/unix/meson.build
+++ b/lib/eal/unix/meson.build
@@ -6,6 +6,7 @@ sources += files(
         'eal_file.c',
         'eal_filesystem.c',
         'eal_firmware.c',
+        'eal_log.c',
         'eal_unix_memory.c',
         'eal_unix_thread.c',
         'eal_unix_timer.c',
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v4 2/5] eal: turn off getopt_long error message during eal_log_level
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 1/5] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
@ 2023-06-26 18:42   ` Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 3/5] eal: skip stdio on console logging Stephen Hemminger
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-26 18:42 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Keith Wiles, Bruce Richardson, Dmitry Kozlyuk,
	Narcisa Ana Maria Vasile, Dmitry Malloy, Pallavi Kadam

If DPDK application is given a bogus option, the error message
would get printed twice. Once during scan for log level and
again during parsing of arguments.

Example:
	# ./build/app/dpdk-testpmd --bogus
	./build/app/dpdk-testpmd: unrecognized option '--bogus'
	EAL: Detected CPU lcores: 16
	EAL: Detected NUMA nodes: 1
	./build/app/dpdk-testpmd: unrecognized option '--bogus'

	Usage: ./build/app/dpdk-testpmd [options]

Fix by suppressing printing error message on first pass.

Signed-off-by: Keith Wiles <keith.wiles@intel.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c | 2 ++
 lib/eal/linux/eal.c   | 2 ++
 lib/eal/windows/eal.c | 3 +++
 3 files changed, 7 insertions(+)

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 6df6873e3889..70087837da18 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -381,6 +381,7 @@ eal_log_level_parse(int argc, char **argv)
 	argvopt = argv;
 	optind = 1;
 	optreset = 1;
+	opterr = 0;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 				  eal_long_options, &option_index)) != EOF) {
@@ -424,6 +425,7 @@ eal_parse_args(int argc, char **argv)
 	argvopt = argv;
 	optind = 1;
 	optreset = 1;
+	opterr = 1;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 				  eal_long_options, &option_index)) != EOF) {
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 145afafde234..60bb130aea15 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -561,6 +561,7 @@ eal_log_level_parse(int argc, char **argv)
 
 	argvopt = argv;
 	optind = 1;
+	opterr = 0;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 				  eal_long_options, &option_index)) != EOF) {
@@ -638,6 +639,7 @@ eal_parse_args(int argc, char **argv)
 
 	argvopt = argv;
 	optind = 1;
+	opterr = 1;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 				  eal_long_options, &option_index)) != EOF) {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 2d7a0e9ab27e..0800a9e5c2d2 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -106,6 +106,8 @@ eal_log_level_parse(int argc, char **argv)
 	struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
+	opterr = 0;
+
 	argvopt = argv;
 
 	eal_reset_internal_config(internal_conf);
@@ -143,6 +145,7 @@ eal_parse_args(int argc, char **argv)
 		eal_get_internal_configuration();
 
 	argvopt = argv;
+	opterr = 1;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 		eal_long_options, &option_index)) != EOF) {
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v4 3/5] eal: skip stdio on console logging
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 1/5] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 2/5] eal: turn off getopt_long error message during eal_log_level Stephen Hemminger
@ 2023-06-26 18:42   ` Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 4/5] eal: move logging initialization earlier Stephen Hemminger
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-26 18:42 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

There is no need to use stdio when logging to console.
Using the write system call directly avoids unnecessary copy
to stdio output buffer.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/unix/eal_log.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index d44416fd6570..baa721021991 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <syslog.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
@@ -19,8 +20,7 @@ console_log_write(__rte_unused void *c, const char *buf, size_t size)
 	ssize_t ret;
 
 	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
+	ret = write(STDERR_FILENO, buf, size);
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v4 4/5] eal: move logging initialization earlier
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
                     ` (2 preceding siblings ...)
  2023-06-26 18:42   ` [PATCH v4 3/5] eal: skip stdio on console logging Stephen Hemminger
@ 2023-06-26 18:42   ` Stephen Hemminger
  2023-06-26 18:42   ` [PATCH v4 5/5] eal: add option to put timestamp on console output Stephen Hemminger
  2023-06-27  7:40   ` [PATCH v4 0/5] Logging related patchs Morten Brørup
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-26 18:42 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

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(-)

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");
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v4 5/5] eal: add option to put timestamp on console output
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
                     ` (3 preceding siblings ...)
  2023-06-26 18:42   ` [PATCH v4 4/5] eal: move logging initialization earlier Stephen Hemminger
@ 2023-06-26 18:42   ` Stephen Hemminger
  2023-06-27  7:40   ` [PATCH v4 0/5] Logging related patchs Morten Brørup
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-26 18:42 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

Example:
$ dpdk-testpmd --log-timestamp -- -i
[       0.007615] EAL: Detected CPU lcores: 16
[       0.007657] EAL: Detected NUMA nodes: 1
[       0.007878] EAL: Detected static linkage of DPDK
[       0.009469] EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
[       0.012175] EAL: Selected IOVA mode 'VA'
[       0.120016] testpmd: No probed ethernet devices
Interactive-mode selected
[       0.155959] testpmd: create a new mbuf pool <mb_pool_0>: n=267456, size=2176, socket=0
[       0.155990] testpmd: preferred mempool ops selected: ring_mp_mc

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    |  5 +++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
 lib/eal/common/eal_common_options.c           |  6 ++++
 lib/eal/common/eal_internal_cfg.h             |  3 ++
 lib/eal/common/eal_options.h                  |  2 ++
 lib/eal/freebsd/eal.c                         |  4 ++-
 lib/eal/linux/eal.c                           |  4 ++-
 lib/eal/unix/eal_log.c                        | 36 +++++++++++++++++--
 8 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index 9270d9fa3bfc..99cff10e963c 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -45,3 +45,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..719ca6851625 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -135,3 +135,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 03059336987d..c6c74cc31e9c 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -76,6 +76,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1835,6 +1836,10 @@ eal_parse_common_option(int opt, const char *optarg,
 	}
 
 #ifndef RTE_EXEC_ENV_WINDOWS
+	case OPT_LOG_TIMESTAMP_NUM:
+		conf->log_timestamp = 1;
+		break;
+
 	case OPT_TRACE_NUM: {
 		if (eal_trace_args_save(optarg) < 0) {
 			RTE_LOG(ERR, EAL, "invalid parameters for --"
@@ -2194,6 +2199,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index 167ec501fa79..1a1a7fdcfa8c 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -84,7 +84,10 @@ struct internal_config {
 	/**< true if storing all pages within single files (per-page-size,
 	 * per-node) non-legacy mode only.
 	 */
+	volatile uint8_t log_timestamp;   /**< add timestamp to console output */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
+	struct timespec log_start_time;	  /**< when logging was started */
+
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..cc9723868e3c 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index d3aac3d628a8..5c7cea8809bd 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -388,6 +388,7 @@ eal_log_level_parse(int argc, char **argv)
 
 		switch (opt) {
 		case OPT_SYSLOG_NUM:		/* fallthrough */
+		case OPT_LOG_TIMESTAMP_NUM:	/* fallthrough */
 		case OPT_LOG_LEVEL_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				goto error;
@@ -436,7 +437,8 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM || opt == OPT_LOG_LEVEL_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 51c4ec75d57b..051f9ad7f5bf 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -568,6 +568,7 @@ eal_log_level_parse(int argc, char **argv)
 
 		switch (opt) {
 		case OPT_SYSLOG_NUM:		/* fallthrough */
+		case OPT_LOG_TIMESTAMP_NUM:	/* fallthrough */
 		case OPT_LOG_LEVEL_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				goto error;
@@ -650,7 +651,8 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM || opt == OPT_LOG_LEVEL_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index baa721021991..f65a22612310 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -4,12 +4,15 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <rte_log.h>
 
 #include "eal_log.h"
+#include "eal_private.h"
 
 /*
  * default log function
@@ -17,10 +20,36 @@
 static ssize_t
 console_log_write(__rte_unused void *c, const char *buf, size_t size)
 {
+	const struct internal_config *internal_conf = eal_get_internal_configuration();
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = write(STDERR_FILENO, buf, size);
+	/* add optional timestamp for stderr */
+	if (internal_conf->log_timestamp) {
+		struct iovec iov[2];
+		struct timespec ts;
+		char tbuf[64];
+
+		/* format up monotonic timestamp */
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= internal_conf->log_start_time.tv_sec;
+		ts.tv_nsec -= internal_conf->log_start_time.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		/* use writev to put timestamp and buf in single operation */
+		iov[0].iov_base = tbuf;
+		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
+					   ts.tv_sec, ts.tv_nsec / 1000u);
+
+		/* casts are to unconstify the buf */
+		iov[1].iov_base = (void *)(uintptr_t)buf;
+		iov[1].iov_len = size;
+		ret = writev(STDERR_FILENO, iov, 2);
+	} else {
+		ret = write(STDERR_FILENO, buf, size);
+	}
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
@@ -47,8 +76,11 @@ static cookie_io_functions_t console_log_func = {
 int
 eal_log_init(const char *id, int facility)
 {
+	struct internal_config *internal_conf = eal_get_internal_configuration();
 	FILE *log_stream;
 
+	clock_gettime(CLOCK_MONOTONIC, &internal_conf->log_start_time);
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v4 0/5] Logging related patchs
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
                     ` (4 preceding siblings ...)
  2023-06-26 18:42   ` [PATCH v4 5/5] eal: add option to put timestamp on console output Stephen Hemminger
@ 2023-06-27  7:40   ` Morten Brørup
  2023-06-27 14:49     ` Stephen Hemminger
  2023-06-27 15:02     ` Bruce Richardson
  5 siblings, 2 replies; 277+ messages in thread
From: Morten Brørup @ 2023-06-27  7:40 UTC (permalink / raw)
  To: Stephen Hemminger, dev

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Monday, 26 June 2023 20.42
> To: dev@dpdk.org
> Cc: Stephen Hemminger
> Subject: [PATCH v4 0/5] Logging related patchs
> 
> This patch set rebases and extends some earlier work on logging.
> 
> Stephen Hemminger (5):
>   eal: unify logging code for FreeBsd and Linux
>   eal: turn off getopt_long error message during eal_log_level
>   eal: skip stdio on console logging
>   eal: move logging initialization earlier
>   eal: add option to put timestamp on console output

Series-acked-by: Morten Brørup <mb@smartsharesystems.com>


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v4 0/5] Logging related patchs
  2023-06-27  7:40   ` [PATCH v4 0/5] Logging related patchs Morten Brørup
@ 2023-06-27 14:49     ` Stephen Hemminger
  2023-06-27 15:04       ` Morten Brørup
  2023-06-27 15:02     ` Bruce Richardson
  1 sibling, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-27 14:49 UTC (permalink / raw)
  To: Morten Brørup; +Cc: dev

On Tue, 27 Jun 2023 09:40:01 +0200
Morten Brørup <mb@smartsharesystems.com> wrote:

> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Monday, 26 June 2023 20.42
> > To: dev@dpdk.org
> > Cc: Stephen Hemminger
> > Subject: [PATCH v4 0/5] Logging related patchs
> > 
> > This patch set rebases and extends some earlier work on logging.
> > 
> > Stephen Hemminger (5):
> >   eal: unify logging code for FreeBsd and Linux
> >   eal: turn off getopt_long error message during eal_log_level
> >   eal: skip stdio on console logging
> >   eal: move logging initialization earlier
> >   eal: add option to put timestamp on console output  
> 
> Series-acked-by: Morten Brørup <mb@smartsharesystems.com>
> 

There are a few bugs, still working them out.
In initial review feedback there was desire for timestamp feature
to be global. I.e secondary process would show time since start
of primary. But this would hard to implement due to chicken/egg
issues during argument parsing during startup; so made a decision
that it wasn't worth doing.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v4 0/5] Logging related patchs
  2023-06-27  7:40   ` [PATCH v4 0/5] Logging related patchs Morten Brørup
  2023-06-27 14:49     ` Stephen Hemminger
@ 2023-06-27 15:02     ` Bruce Richardson
  1 sibling, 0 replies; 277+ messages in thread
From: Bruce Richardson @ 2023-06-27 15:02 UTC (permalink / raw)
  To: Morten Brørup; +Cc: Stephen Hemminger, dev

On Tue, Jun 27, 2023 at 09:40:01AM +0200, Morten Brørup wrote:
> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Monday, 26 June 2023 20.42
> > To: dev@dpdk.org
> > Cc: Stephen Hemminger
> > Subject: [PATCH v4 0/5] Logging related patchs
> > 
> > This patch set rebases and extends some earlier work on logging.
> > 
> > Stephen Hemminger (5):
> >   eal: unify logging code for FreeBsd and Linux
> >   eal: turn off getopt_long error message during eal_log_level
> >   eal: skip stdio on console logging
> >   eal: move logging initialization earlier
> >   eal: add option to put timestamp on console output
> 
> Series-acked-by: Morten Brørup <mb@smartsharesystems.com>
> 
Gave it a quick sanity-test run on FreeBSD and all looks ok to me.

Tested-by: Bruce Richardson <bruce.richardson@intel.com>

^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v4 0/5] Logging related patchs
  2023-06-27 14:49     ` Stephen Hemminger
@ 2023-06-27 15:04       ` Morten Brørup
  0 siblings, 0 replies; 277+ messages in thread
From: Morten Brørup @ 2023-06-27 15:04 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Tuesday, 27 June 2023 16.49
> 
> On Tue, 27 Jun 2023 09:40:01 +0200
> Morten Brørup <mb@smartsharesystems.com> wrote:
> 
> > > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > > Sent: Monday, 26 June 2023 20.42
> > > To: dev@dpdk.org
> > > Cc: Stephen Hemminger
> > > Subject: [PATCH v4 0/5] Logging related patchs
> > >
> > > This patch set rebases and extends some earlier work on logging.
> > >
> > > Stephen Hemminger (5):
> > >   eal: unify logging code for FreeBsd and Linux
> > >   eal: turn off getopt_long error message during eal_log_level
> > >   eal: skip stdio on console logging
> > >   eal: move logging initialization earlier
> > >   eal: add option to put timestamp on console output
> >
> > Series-acked-by: Morten Brørup <mb@smartsharesystems.com>
> >
> 
> There are a few bugs, still working them out.
> In initial review feedback there was desire for timestamp feature
> to be global. I.e secondary process would show time since start
> of primary. But this would hard to implement due to chicken/egg
> issues during argument parsing during startup; so made a decision
> that it wasn't worth doing.

I suppose it could be implemented with a piece of shared memory set by the primary process and read by secondary processes, holding the start time offset from a system global timer (e.g. CLOCK_MONOTONIC) and a tristate variable telling if timestamps are configured - which, if true or false, overrides any timestamp parameter given to the secondary process.

Anyway, secondary process support is far from perfect, so I wouldn't worry too much about getting timestamp parameters from the primary process to the secondary process either. In short: I agree with your decision.


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v5 0/6] Logging related patches
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (3 preceding siblings ...)
  2023-06-26 18:42 ` [PATCH v4 0/5] Logging related patchs Stephen Hemminger
@ 2023-06-28 17:58 ` Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 1/6] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
                     ` (5 more replies)
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
                   ` (15 subsequent siblings)
  20 siblings, 6 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-28 17:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This patch set rebases and extends some earlier work on logging.

Stephen Hemminger (6):
  eal: unify logging code for FreeBsd and Linux
  eal: turn off getopt_long error message during eal_log_level
  eal: fix handling of syslog facility
  eal: skip stdio on console logging
  eal: allow user to set default log stream before init
  eal: add option to put timestamp on console output

v6 
   - make more of the log arg parsing common
   - handle --syslog argument better
   - drop the early logging initialization (need shmem to be setup)
   - base timestamp on start of primary process (in shmem)

 .../freebsd_gsg/freebsd_eal_parameters.rst    |  32 ++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |   5 +
 lib/eal/common/eal_common_log.c               |   6 +
 lib/eal/common/eal_common_options.c           |   8 +-
 lib/eal/common/eal_internal_cfg.h             |   3 +
 lib/eal/common/eal_log.h                      |   6 +
 lib/eal/common/eal_options.h                  |   2 +
 lib/eal/freebsd/eal.c                         |  59 ++-----
 lib/eal/linux/eal.c                           |  52 ++----
 lib/eal/linux/eal_log.c                       |  61 -------
 lib/eal/linux/meson.build                     |   1 -
 lib/eal/unix/eal_log.c                        | 165 ++++++++++++++++++
 lib/eal/unix/meson.build                      |   1 +
 lib/eal/windows/eal.c                         |   3 +
 14 files changed, 255 insertions(+), 149 deletions(-)
 delete mode 100644 lib/eal/linux/eal_log.c
 create mode 100644 lib/eal/unix/eal_log.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v5 1/6] eal: unify logging code for FreeBsd and Linux
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
@ 2023-06-28 17:58   ` Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 2/6] eal: turn off getopt_long error message during eal_log_level Stephen Hemminger
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-28 17:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

FreeBSD logging code was not using syslog and did not have
the same options as Linux. Move the log writing code to common
source tree.

Also fix a number of argument parsing bugs:
 1. If application is given a bogus option, the error message
    would get printed twice. Once during scan for log level and
    again during parsing of arguments.
 2. A bad argument give to --log-level option was given the
    code would keep going.
 3. The syslog facility argument was parsed too late to
    all log to be initialized before other errors.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    |  27 ++++
 lib/eal/common/eal_log.h                      |   5 +
 lib/eal/freebsd/eal.c                         |  55 ++------
 lib/eal/linux/eal.c                           |  48 +------
 lib/eal/linux/eal_log.c                       |  61 ---------
 lib/eal/linux/meson.build                     |   1 -
 lib/eal/unix/eal_log.c                        | 121 ++++++++++++++++++
 lib/eal/unix/meson.build                      |   1 +
 8 files changed, 173 insertions(+), 146 deletions(-)
 delete mode 100644 lib/eal/linux/eal_log.c
 create mode 100644 lib/eal/unix/eal_log.c

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index fba467a2ce92..9270d9fa3bfc 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -18,3 +18,30 @@ FreeBSD-specific EAL parameters
 -------------------------------
 
 There are currently no FreeBSD-specific EAL command-line parameters available.
+
+Other options
+~~~~~~~~~~~~~
+
+*   ``--syslog <syslog facility>``
+
+    Set syslog facility. Valid syslog facilities are::
+
+        auth
+        cron
+        daemon
+        ftp
+        kern
+        lpr
+        mail
+        news
+        syslog
+        user
+        uucp
+        local0
+        local1
+        local2
+        local3
+        local4
+        local5
+        local6
+        local7
diff --git a/lib/eal/common/eal_log.h b/lib/eal/common/eal_log.h
index c784fa604389..31dc489350f6 100644
--- a/lib/eal/common/eal_log.h
+++ b/lib/eal/common/eal_log.h
@@ -13,6 +13,11 @@
  */
 int eal_log_init(const char *id, int facility);
 
+/*
+ * Scan command line args for log settings.
+ */
+int eal_log_level_parse(int argc, char * const argv[]);
+
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
  */
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 7008303e112a..bb9a2b1653d9 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -52,6 +52,7 @@
 #include "eal_hugepages.h"
 #include "eal_options.h"
 #include "eal_memcfg.h"
+#include "eal_log.h"
 #include "eal_trace.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
@@ -363,48 +364,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
@@ -610,7 +569,11 @@ rte_eal_init(int argc, char **argv)
 	eal_save_args(argc, argv);
 
 	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log option.");
+		rte_errno = EINVAL;
+		return -1;
+	}
 
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
@@ -759,6 +722,12 @@ 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 145afafde234..d7f268fc8116 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
@@ -649,7 +610,7 @@ eal_parse_args(int argc, char **argv)
 			goto out;
 		}
 
-		/* eal_log_level_parse() already handled this option */
+		/* eal_log_level_parse() already handled these */
 		if (opt == OPT_LOG_LEVEL_NUM)
 			continue;
 
@@ -993,7 +954,12 @@ rte_eal_init(int argc, char **argv)
 	eal_reset_internal_config(internal_conf);
 
 	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log option.");
+		rte_errno = EINVAL;
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
+		return -1;
+	}
 
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
diff --git a/lib/eal/linux/eal_log.c b/lib/eal/linux/eal_log.c
deleted file mode 100644
index d44416fd6570..000000000000
--- a/lib/eal/linux/eal_log.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <syslog.h>
-
-#include <rte_log.h>
-
-#include "eal_log.h"
-
-/*
- * default log function
- */
-static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
-{
-	ssize_t ret;
-
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
-
-	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
-	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
-
-	return ret;
-}
-
-static int
-console_log_close(__rte_unused void *c)
-{
-	closelog();
-	return 0;
-}
-
-static cookie_io_functions_t console_log_func = {
-	.write = console_log_write,
-	.close = console_log_close,
-};
-
-/*
- * set the log to default function, called during eal init process,
- * once memzones are available.
- */
-int
-eal_log_init(const char *id, int facility)
-{
-	FILE *log_stream;
-
-	log_stream = fopencookie(NULL, "w+", console_log_func);
-	if (log_stream == NULL)
-		return -1;
-
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
-
-	eal_log_set_default(log_stream);
-
-	return 0;
-}
diff --git a/lib/eal/linux/meson.build b/lib/eal/linux/meson.build
index 5af456db9edb..e99ebed25692 100644
--- a/lib/eal/linux/meson.build
+++ b/lib/eal/linux/meson.build
@@ -11,7 +11,6 @@ sources += files(
         'eal_hugepage_info.c',
         'eal_interrupts.c',
         'eal_lcore.c',
-        'eal_log.c',
         'eal_memalloc.c',
         'eal_memory.c',
         'eal_thread.c',
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
new file mode 100644
index 000000000000..f8a5c853f2df
--- /dev/null
+++ b/lib/eal/unix/eal_log.c
@@ -0,0 +1,121 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
+ */
+
+#include <getopt.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <syslog.h>
+
+#include <rte_log.h>
+
+#include "eal_log.h"
+#include "eal_internal_cfg.h"
+#include "eal_options.h"
+#include "eal_private.h"
+
+/*
+ * default log function
+ */
+static ssize_t
+console_log_write(__rte_unused void *c, const char *buf, size_t size)
+{
+	ssize_t ret;
+
+	/* write on stderr */
+	ret = fwrite(buf, 1, size, stderr);
+	fflush(stderr);
+
+	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
+	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
+
+#ifdef RTE_EXEC_ENV_LINUX
+	/* Handle glibc quirk: write function should return the number of bytes
+	 * copied from buf, or 0 on error. (The function must not return a negative value.)
+	 * FreeBSD expects that write function behaves like write(2).
+	 */
+	if (ret < 0)
+		ret = 0;
+#endif
+
+	return ret;
+}
+
+static int
+console_log_close(__rte_unused void *c)
+{
+	closelog();
+	return 0;
+}
+
+static cookie_io_functions_t console_log_func = {
+	.write = console_log_write,
+	.close = console_log_close,
+};
+
+
+/*
+ * Parse the arguments for --log-level and --syslog
+ */
+int
+eal_log_level_parse(int argc, char *const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	char * const old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optrset = optreset;
+#endif
+
+	optind = 1;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = 1;
+#endif
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
+/*
+ * set the log to default function, called during eal init process,
+ * once memzones are available.
+ */
+int
+eal_log_init(const char *id, int facility)
+{
+	FILE *log_stream;
+
+	log_stream = fopencookie(NULL, "w+", console_log_func);
+	if (log_stream == NULL)
+		return -1;
+
+	openlog(id, LOG_NDELAY | LOG_PID, facility);
+
+	eal_log_set_default(log_stream);
+
+	return 0;
+}
diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
index cc7d67dd321d..37d07594df29 100644
--- a/lib/eal/unix/meson.build
+++ b/lib/eal/unix/meson.build
@@ -6,6 +6,7 @@ sources += files(
         'eal_file.c',
         'eal_filesystem.c',
         'eal_firmware.c',
+        'eal_log.c',
         'eal_unix_memory.c',
         'eal_unix_thread.c',
         'eal_unix_timer.c',
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v5 2/6] eal: turn off getopt_long error message during eal_log_level
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 1/6] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
@ 2023-06-28 17:58   ` Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 3/6] eal: fix handling of syslog facility Stephen Hemminger
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-28 17:58 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Keith Wiles, Dmitry Kozlyuk,
	Narcisa Ana Maria Vasile, Dmitry Malloy, Pallavi Kadam

If DPDK application is given a bogus option, the error message
would get printed twice. Once during scan for log level and
again during parsing of arguments.

Example:
	# ./build/app/dpdk-testpmd --bogus
	./build/app/dpdk-testpmd: unrecognized option '--bogus'
	EAL: Detected CPU lcores: 16
	EAL: Detected NUMA nodes: 1
	./build/app/dpdk-testpmd: unrecognized option '--bogus'

	Usage: ./build/app/dpdk-testpmd [options]

Fix by suppressing printing error message on first pass.

Signed-off-by: Keith Wiles <keith.wiles@intel.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/unix/eal_log.c | 3 +++
 lib/eal/windows/eal.c  | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index f8a5c853f2df..3f54284494d7 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -64,12 +64,14 @@ eal_log_level_parse(int argc, char *const argv[])
 	int option_index, opt;
 	const int old_optind = optind;
 	const int old_optopt = optopt;
+	const int old_opterr = opterr;
 	char * const old_optarg = optarg;
 #ifdef RTE_EXEC_ENV_FREEBSD
 	const int old_optrset = optreset;
 #endif
 
 	optind = 1;
+	opterr = 0;
 #ifdef RTE_EXEC_ENV_FREEBSD
 	optreset = 1;
 #endif
@@ -94,6 +96,7 @@ eal_log_level_parse(int argc, char *const argv[])
 	optind = old_optind;
 	optopt = old_optopt;
 	optarg = old_optarg;
+	opterr = old_opterr;
 #ifdef RTE_EXEC_ENV_FREEBSD
 	optreset = old_optreset;
 #endif
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 2d7a0e9ab27e..0800a9e5c2d2 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -106,6 +106,8 @@ eal_log_level_parse(int argc, char **argv)
 	struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
+	opterr = 0;
+
 	argvopt = argv;
 
 	eal_reset_internal_config(internal_conf);
@@ -143,6 +145,7 @@ eal_parse_args(int argc, char **argv)
 		eal_get_internal_configuration();
 
 	argvopt = argv;
+	opterr = 1;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 		eal_long_options, &option_index)) != EOF) {
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v5 3/6] eal: fix handling of syslog facility
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 1/6] eal: unify logging code for FreeBsd and Linux Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 2/6] eal: turn off getopt_long error message during eal_log_level Stephen Hemminger
@ 2023-06-28 17:58   ` Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 4/6] eal: skip stdio on console logging Stephen Hemminger
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-28 17:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

The syslog facility option needs to be parsed before log_init
is called. Also help message whas missing the required argument.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 2 +-
 lib/eal/freebsd/eal.c               | 2 +-
 lib/eal/linux/eal.c                 | 2 +-
 lib/eal/unix/eal_log.c              | 1 +
 4 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 03059336987d..005da4d12001 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -2193,7 +2193,7 @@ eal_common_usage(void)
 	       "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
-	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_SYSLOG"=<facility> Set syslog facility\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bb9a2b1653d9..104507b61514 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -394,7 +394,7 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled this option */
-		if (opt == OPT_LOG_LEVEL_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index d7f268fc8116..e30c44380e30 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -611,7 +611,7 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these */
-		if (opt == OPT_LOG_LEVEL_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 3f54284494d7..93cf04fae043 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -80,6 +80,7 @@ eal_log_level_parse(int argc, char *const argv[])
 				  eal_long_options, &option_index)) != EOF) {
 
 		switch (opt) {
+		case OPT_SYSLOG_NUM:
 		case OPT_LOG_LEVEL_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v5 4/6] eal: skip stdio on console logging
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
                     ` (2 preceding siblings ...)
  2023-06-28 17:58   ` [PATCH v5 3/6] eal: fix handling of syslog facility Stephen Hemminger
@ 2023-06-28 17:58   ` Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 5/6] eal: allow user to set default log stream before init Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 6/6] eal: add option to put timestamp on console output Stephen Hemminger
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-28 17:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

There is no need to use stdio when logging to console.
Using the write system call directly avoids unnecessary copy
to stdio output buffer.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/unix/eal_log.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 93cf04fae043..7921417b050a 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -6,6 +6,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <syslog.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
@@ -23,8 +24,7 @@ console_log_write(__rte_unused void *c, const char *buf, size_t size)
 	ssize_t ret;
 
 	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
+	ret = write(STDERR_FILENO, buf, size);
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v5 5/6] eal: allow user to set default log stream before init
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
                     ` (3 preceding siblings ...)
  2023-06-28 17:58   ` [PATCH v5 4/6] eal: skip stdio on console logging Stephen Hemminger
@ 2023-06-28 17:58   ` Stephen Hemminger
  2023-06-28 17:58   ` [PATCH v5 6/6] eal: add option to put timestamp on console output Stephen Hemminger
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-28 17:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

It is useful for application to be able to set the default log
stream before call rte_eal_init(). This makes all messages go
to the new default.

For example, to skip using syslog; just doing
	rte_log_set_default(stderr);

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_log.c | 6 ++++++
 lib/eal/common/eal_log.h        | 1 +
 lib/eal/unix/eal_log.c          | 4 ++++
 3 files changed, 11 insertions(+)

diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c
index bd7b188ceb4a..9b158c201205 100644
--- a/lib/eal/common/eal_common_log.c
+++ b/lib/eal/common/eal_common_log.c
@@ -535,6 +535,12 @@ eal_log_set_default(FILE *default_log)
 #endif
 }
 
+FILE *
+eal_log_get_default(void)
+{
+	return default_log_stream;
+}
+
 /*
  * Called by eal_cleanup
  */
diff --git a/lib/eal/common/eal_log.h b/lib/eal/common/eal_log.h
index 31dc489350f6..268c2a264382 100644
--- a/lib/eal/common/eal_log.h
+++ b/lib/eal/common/eal_log.h
@@ -22,6 +22,7 @@ int eal_log_level_parse(int argc, char * const argv[]);
  * Determine where log data is written when no call to rte_openlog_stream.
  */
 void eal_log_set_default(FILE *default_log);
+FILE *eal_log_get_default(void);
 
 /*
  * Save a log option for later.
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 7921417b050a..3bfe5853095f 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -113,6 +113,10 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	/* has user has already setup a log stream */
+	if (eal_log_get_default())
+		return 0;
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v5 6/6] eal: add option to put timestamp on console output
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
                     ` (4 preceding siblings ...)
  2023-06-28 17:58   ` [PATCH v5 5/6] eal: allow user to set default log stream before init Stephen Hemminger
@ 2023-06-28 17:58   ` Stephen Hemminger
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-28 17:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson, Anatoly Burakov

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

The first few lines are not timestamped because the flag is stored
in internal configuration which is stored in shared memory
which is not setup up until a little later in startup process.

Example:
$ dpdk-testpmd --log-timestamp -- -i
EAL: Detected CPU lcores: 16
EAL: Detected NUMA nodes: 1
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
[       0.112264] testpmd: No probed ethernet devices
Interactive-mode selected
[       0.184573] testpmd: create a new mbuf pool <mb_pool_0>: n=163456, size=2176, socket=0
[       0.184612] testpmd: preferred mempool ops selected: ring_mp_mc

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    |  5 +++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
 lib/eal/common/eal_common_options.c           |  6 +++
 lib/eal/common/eal_internal_cfg.h             |  3 ++
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/freebsd/eal.c                         |  4 +-
 lib/eal/linux/eal.c                           |  4 +-
 lib/eal/unix/eal_log.c                        | 44 +++++++++++++++++--
 8 files changed, 67 insertions(+), 6 deletions(-)

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index 9270d9fa3bfc..99cff10e963c 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -45,3 +45,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..719ca6851625 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -135,3 +135,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 005da4d12001..67fe6efb4526 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -76,6 +76,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1835,6 +1836,10 @@ eal_parse_common_option(int opt, const char *optarg,
 	}
 
 #ifndef RTE_EXEC_ENV_WINDOWS
+	case OPT_LOG_TIMESTAMP_NUM:
+		conf->log_timestamp = 1;
+		break;
+
 	case OPT_TRACE_NUM: {
 		if (eal_trace_args_save(optarg) < 0) {
 			RTE_LOG(ERR, EAL, "invalid parameters for --"
@@ -2194,6 +2199,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"=<facility> Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index 167ec501fa79..ca502d5dd365 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -84,7 +84,10 @@ struct internal_config {
 	/**< true if storing all pages within single files (per-page-size,
 	 * per-node) non-legacy mode only.
 	 */
+	uint8_t log_timestamp;          /**< add timestamp to console output */
+	struct timespec log_start_time; /**< monotonic clock when started */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
+
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..cc9723868e3c 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 104507b61514..ae3a736e862d 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -394,7 +394,9 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled this option */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index e30c44380e30..6deb694e7242 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -611,7 +611,9 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 3bfe5853095f..172b332b8417 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -5,7 +5,9 @@
 #include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <rte_log.h>
@@ -19,12 +21,38 @@
  * default log function
  */
 static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
+console_log_write(void *c, const char *buf, size_t size)
 {
+	const struct internal_config *internal_conf = c;
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = write(STDERR_FILENO, buf, size);
+	/* add optional timestamp for stderr */
+	if (internal_conf->log_timestamp) {
+		struct iovec iov[2];
+		struct timespec ts;
+		char tbuf[64];
+
+		/* format up monotonic timestamp */
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= internal_conf->log_start_time.tv_sec;
+		ts.tv_nsec -= internal_conf->log_start_time.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		/* use writev to put timestamp and buf in single operation */
+		iov[0].iov_base = tbuf;
+		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
+					   ts.tv_sec, ts.tv_nsec / 1000u);
+
+		/* casts are to unconstify the buf */
+		iov[1].iov_base = (void *)(uintptr_t)buf;
+		iov[1].iov_len = size;
+		ret = writev(STDERR_FILENO, iov, 2);
+	} else {
+		ret = write(STDERR_FILENO, buf, size);
+	}
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
@@ -82,6 +110,7 @@ eal_log_level_parse(int argc, char *const argv[])
 		switch (opt) {
 		case OPT_SYSLOG_NUM:
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_LOG_TIMESTAMP_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -111,13 +140,20 @@ eal_log_level_parse(int argc, char *const argv[])
 int
 eal_log_init(const char *id, int facility)
 {
+	struct internal_config *internal_conf = eal_get_internal_configuration();
 	FILE *log_stream;
 
 	/* has user has already setup a log stream */
 	if (eal_log_get_default())
 		return 0;
 
-	log_stream = fopencookie(NULL, "w+", console_log_func);
+	if (internal_conf->process_type == RTE_PROC_PRIMARY &&
+	    internal_conf->log_timestamp) {
+		if (clock_gettime(CLOCK_MONOTONIC, &internal_conf->log_start_time) < 0)
+			return -1;
+	}
+
+	log_stream = fopencookie(internal_conf, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v6 0/6] Logging related patches
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (4 preceding siblings ...)
  2023-06-28 17:58 ` [PATCH v5 0/6] Logging related patches Stephen Hemminger
@ 2023-06-29 15:58 ` Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 1/6] eal: unify logging code Stephen Hemminger
                     ` (5 more replies)
  2023-07-05 22:48 ` [PATCH v7 0/5] Logging timetamp and related patches Stephen Hemminger
                   ` (14 subsequent siblings)
  20 siblings, 6 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-29 15:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Patchset that includes:
  - unified code for more of log argument handling
  - fix for duplicate option errors
  - timestamp option for logging

v6
  - fix build on freebsd and windows
  - add test for log timestamp

Stephen Hemminger (6):
  eal: unify logging code
  eal: turn off getopt_long error message during eal_log_level
  eal: fix help message for syslog option
  eal: skip stdio on console logging
  eal: allow user to set default log stream before init
  eal: add option to put timestamp on console output

 app/dumpcap/main.c                            |   3 +
 app/pdump/main.c                              |   3 +
 app/proc-info/main.c                          |   3 +
 app/test/test_eal_flags.c                     |   9 ++
 .../freebsd_gsg/freebsd_eal_parameters.rst    |  32 +++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |   5 +
 lib/eal/common/eal_common_log.c               |  55 +++++++++
 lib/eal/common/eal_common_options.c           |   8 +-
 lib/eal/common/eal_internal_cfg.h             |   3 +
 lib/eal/common/eal_log.h                      |   6 +
 lib/eal/common/eal_options.h                  |   2 +
 lib/eal/freebsd/eal.c                         |  55 ++-------
 lib/eal/linux/eal.c                           |  46 +-------
 lib/eal/linux/eal_log.c                       |  61 ----------
 lib/eal/linux/meson.build                     |   1 -
 lib/eal/unix/eal_log.c                        | 111 ++++++++++++++++++
 lib/eal/unix/meson.build                      |   1 +
 lib/eal/windows/eal.c                         |  36 +-----
 18 files changed, 259 insertions(+), 181 deletions(-)
 delete mode 100644 lib/eal/linux/eal_log.c
 create mode 100644 lib/eal/unix/eal_log.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v6 1/6] eal: unify logging code
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
@ 2023-06-29 15:58   ` Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 2/6] eal: turn off getopt_long error message during eal_log_level Stephen Hemminger
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-29 15:58 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Narcisa Ana Maria Vasile, Dmitry Malloy, Pallavi Kadam

FreeBSD logging code was not using syslog and did not have
the same options as Linux. Use a common set of functions for that.

Pre-parsing for log level is common to all OS's.

Now the pre-scan can return an error.
A bad argument give to --log-level option was given the
code would keep going.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

make log_parse common to windows
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    | 27 +++++++++
 lib/eal/common/eal_common_log.c               | 46 ++++++++++++++++
 lib/eal/common/eal_log.h                      |  5 ++
 lib/eal/freebsd/eal.c                         | 55 ++++---------------
 lib/eal/linux/eal.c                           | 46 ++--------------
 lib/eal/linux/meson.build                     |  1 -
 lib/eal/{linux => unix}/eal_log.c             | 11 ++++
 lib/eal/unix/meson.build                      |  1 +
 lib/eal/windows/eal.c                         | 35 ------------
 9 files changed, 108 insertions(+), 119 deletions(-)
 rename lib/eal/{linux => unix}/eal_log.c (76%)

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index fba467a2ce92..9270d9fa3bfc 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -18,3 +18,30 @@ FreeBSD-specific EAL parameters
 -------------------------------
 
 There are currently no FreeBSD-specific EAL command-line parameters available.
+
+Other options
+~~~~~~~~~~~~~
+
+*   ``--syslog <syslog facility>``
+
+    Set syslog facility. Valid syslog facilities are::
+
+        auth
+        cron
+        daemon
+        ftp
+        kern
+        lpr
+        mail
+        news
+        syslog
+        user
+        uucp
+        local0
+        local1
+        local2
+        local3
+        local4
+        local5
+        local6
+        local7
diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c
index bd7b188ceb4a..7e2f010fa371 100644
--- a/lib/eal/common/eal_common_log.c
+++ b/lib/eal/common/eal_common_log.c
@@ -2,6 +2,7 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
+#include <getopt.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdarg.h>
@@ -16,6 +17,8 @@
 #include <rte_per_lcore.h>
 
 #include "eal_log.h"
+#include "eal_internal_cfg.h"
+#include "eal_options.h"
 #include "eal_private.h"
 
 struct rte_log_dynamic_type {
@@ -223,6 +226,49 @@ log_save_level(uint32_t priority, const char *regex, const char *pattern)
 	return -1;
 }
 
+
+/* Parse the all arguments looking for --log-level */
+int
+eal_log_level_parse(int argc, char *const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	char * const old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_log_save_regexp(const char *regex, uint32_t level)
 {
diff --git a/lib/eal/common/eal_log.h b/lib/eal/common/eal_log.h
index c784fa604389..31dc489350f6 100644
--- a/lib/eal/common/eal_log.h
+++ b/lib/eal/common/eal_log.h
@@ -13,6 +13,11 @@
  */
 int eal_log_init(const char *id, int facility);
 
+/*
+ * Scan command line args for log settings.
+ */
+int eal_log_level_parse(int argc, char * const argv[]);
+
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
  */
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 7008303e112a..bb9a2b1653d9 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -52,6 +52,7 @@
 #include "eal_hugepages.h"
 #include "eal_options.h"
 #include "eal_memcfg.h"
+#include "eal_log.h"
 #include "eal_trace.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
@@ -363,48 +364,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
@@ -610,7 +569,11 @@ rte_eal_init(int argc, char **argv)
 	eal_save_args(argc, argv);
 
 	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log option.");
+		rte_errno = EINVAL;
+		return -1;
+	}
 
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
@@ -759,6 +722,12 @@ 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 145afafde234..c9475560d986 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
@@ -993,7 +954,12 @@ rte_eal_init(int argc, char **argv)
 	eal_reset_internal_config(internal_conf);
 
 	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log option.");
+		rte_errno = EINVAL;
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
+		return -1;
+	}
 
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
diff --git a/lib/eal/linux/meson.build b/lib/eal/linux/meson.build
index 5af456db9edb..e99ebed25692 100644
--- a/lib/eal/linux/meson.build
+++ b/lib/eal/linux/meson.build
@@ -11,7 +11,6 @@ sources += files(
         'eal_hugepage_info.c',
         'eal_interrupts.c',
         'eal_lcore.c',
-        'eal_log.c',
         'eal_memalloc.c',
         'eal_memory.c',
         'eal_thread.c',
diff --git a/lib/eal/linux/eal_log.c b/lib/eal/unix/eal_log.c
similarity index 76%
rename from lib/eal/linux/eal_log.c
rename to lib/eal/unix/eal_log.c
index d44416fd6570..85d817c2d31e 100644
--- a/lib/eal/linux/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -9,6 +9,8 @@
 #include <rte_log.h>
 
 #include "eal_log.h"
+#include "eal_internal_cfg.h"
+#include "eal_private.h"
 
 /*
  * default log function
@@ -25,6 +27,15 @@ console_log_write(__rte_unused void *c, const char *buf, size_t size)
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
 
+#ifdef RTE_EXEC_ENV_LINUX
+	/* Handle glibc quirk: write function should return the number of bytes
+	 * copied from buf, or 0 on error. (The function must not return a negative value.)
+	 * FreeBSD expects that write function behaves like write(2).
+	 */
+	if (ret < 0)
+		ret = 0;
+#endif
+
 	return ret;
 }
 
diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
index cc7d67dd321d..37d07594df29 100644
--- a/lib/eal/unix/meson.build
+++ b/lib/eal/unix/meson.build
@@ -6,6 +6,7 @@ sources += files(
         'eal_file.c',
         'eal_filesystem.c',
         'eal_firmware.c',
+        'eal_log.c',
         'eal_unix_memory.c',
         'eal_unix_thread.c',
         'eal_unix_timer.c',
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 2d7a0e9ab27e..240d5b8ad3e4 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v6 2/6] eal: turn off getopt_long error message during eal_log_level
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 1/6] eal: unify logging code Stephen Hemminger
@ 2023-06-29 15:58   ` Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 3/6] eal: fix help message for syslog option Stephen Hemminger
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-29 15:58 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Keith Wiles, Dmitry Kozlyuk,
	Narcisa Ana Maria Vasile, Dmitry Malloy, Pallavi Kadam

If DPDK application is given a bogus option, the error message
would get printed twice. Once during scan for log level and
again during parsing of arguments.

Example:
	# ./build/app/dpdk-testpmd --bogus
	./build/app/dpdk-testpmd: unrecognized option '--bogus'
	EAL: Detected CPU lcores: 16
	EAL: Detected NUMA nodes: 1
	./build/app/dpdk-testpmd: unrecognized option '--bogus'

	Usage: ./build/app/dpdk-testpmd [options]

Fix by suppressing printing error message on first pass.

Signed-off-by: Keith Wiles <keith.wiles@intel.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_log.c | 3 +++
 lib/eal/windows/eal.c           | 1 +
 2 files changed, 4 insertions(+)

diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c
index 7e2f010fa371..ada3ed5ebec9 100644
--- a/lib/eal/common/eal_common_log.c
+++ b/lib/eal/common/eal_common_log.c
@@ -235,6 +235,7 @@ eal_log_level_parse(int argc, char *const argv[])
 	int option_index, opt;
 	const int old_optind = optind;
 	const int old_optopt = optopt;
+	const int old_opterr = opterr;
 	char * const old_optarg = optarg;
 #ifdef RTE_EXEC_ENV_FREEBSD
 	const int old_optreset = optreset;
@@ -242,6 +243,7 @@ eal_log_level_parse(int argc, char *const argv[])
 #endif
 
 	optind = 1;
+	opterr = 0;
 
 	while ((opt = getopt_long(argc, argv, eal_short_options,
 				  eal_long_options, &option_index)) != EOF) {
@@ -263,6 +265,7 @@ eal_log_level_parse(int argc, char *const argv[])
 	optind = old_optind;
 	optopt = old_optopt;
 	optarg = old_optarg;
+	opterr = old_opterr;
 #ifdef RTE_EXEC_ENV_FREEBSD
 	optreset = old_optreset;
 #endif
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 240d5b8ad3e4..c6006c48ddef 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -108,6 +108,7 @@ eal_parse_args(int argc, char **argv)
 		eal_get_internal_configuration();
 
 	argvopt = argv;
+	opterr = 1;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 		eal_long_options, &option_index)) != EOF) {
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v6 3/6] eal: fix help message for syslog option
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 1/6] eal: unify logging code Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 2/6] eal: turn off getopt_long error message during eal_log_level Stephen Hemminger
@ 2023-06-29 15:58   ` Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 4/6] eal: skip stdio on console logging Stephen Hemminger
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-29 15:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The --syslog flag takes facility argument.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 03059336987d..005da4d12001 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -2193,7 +2193,7 @@ eal_common_usage(void)
 	       "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
-	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_SYSLOG"=<facility> Set syslog facility\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v6 4/6] eal: skip stdio on console logging
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
                     ` (2 preceding siblings ...)
  2023-06-29 15:58   ` [PATCH v6 3/6] eal: fix help message for syslog option Stephen Hemminger
@ 2023-06-29 15:58   ` Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 5/6] eal: allow user to set default log stream before init Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 6/6] eal: add option to put timestamp on console output Stephen Hemminger
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-29 15:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

There is no need to use stdio when logging to console.
Using the write system call directly avoids unnecessary copy
to stdio output buffer.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/unix/eal_log.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 85d817c2d31e..151f30eb6071 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <syslog.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
@@ -21,8 +22,7 @@ console_log_write(__rte_unused void *c, const char *buf, size_t size)
 	ssize_t ret;
 
 	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
+	ret = write(STDERR_FILENO, buf, size);
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v6 5/6] eal: allow user to set default log stream before init
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
                     ` (3 preceding siblings ...)
  2023-06-29 15:58   ` [PATCH v6 4/6] eal: skip stdio on console logging Stephen Hemminger
@ 2023-06-29 15:58   ` Stephen Hemminger
  2023-06-29 15:58   ` [PATCH v6 6/6] eal: add option to put timestamp on console output Stephen Hemminger
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-29 15:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Reshma Pattan, Maryam Tahhan

It is useful for application to be able to set the default log
stream before call rte_eal_init(). This makes all messages go
to the new default.

For example, to skip using syslog; just doing
	rte_openlog_stream(stderr);

There is no reason for helper command line applications to clutter
syslog with messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/dumpcap/main.c              | 3 +++
 app/pdump/main.c                | 3 +++
 app/proc-info/main.c            | 3 +++
 lib/eal/common/eal_common_log.c | 6 ++++++
 lib/eal/common/eal_log.h        | 1 +
 lib/eal/unix/eal_log.c          | 4 ++++
 6 files changed, 20 insertions(+)

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index 64294bbfb3e6..bf31eef13cf4 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -639,6 +639,9 @@ static void dpdk_init(void)
 		eal_argv[i++] = strdup(file_prefix);
 	}
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	if (rte_eal_init(eal_argc, eal_argv) < 0)
 		rte_exit(EXIT_FAILURE, "EAL init failed: is primary process running?\n");
 }
diff --git a/app/pdump/main.c b/app/pdump/main.c
index c94606275b28..82c67350e4ab 100644
--- a/app/pdump/main.c
+++ b/app/pdump/main.c
@@ -989,6 +989,9 @@ main(int argc, char **argv)
 
 	argc += 2;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	diag = rte_eal_init(argc, argp);
 	if (diag < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/app/proc-info/main.c b/app/proc-info/main.c
index 53e852a07c14..371b1f382d66 100644
--- a/app/proc-info/main.c
+++ b/app/proc-info/main.c
@@ -1774,6 +1774,9 @@ main(int argc, char **argv)
 
 	argc += 4;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	ret = rte_eal_init(argc, argp);
 	if (ret < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c
index ada3ed5ebec9..827830916eb7 100644
--- a/lib/eal/common/eal_common_log.c
+++ b/lib/eal/common/eal_common_log.c
@@ -584,6 +584,12 @@ eal_log_set_default(FILE *default_log)
 #endif
 }
 
+FILE *
+eal_log_get_default(void)
+{
+	return default_log_stream;
+}
+
 /*
  * Called by eal_cleanup
  */
diff --git a/lib/eal/common/eal_log.h b/lib/eal/common/eal_log.h
index 31dc489350f6..268c2a264382 100644
--- a/lib/eal/common/eal_log.h
+++ b/lib/eal/common/eal_log.h
@@ -22,6 +22,7 @@ int eal_log_level_parse(int argc, char * const argv[]);
  * Determine where log data is written when no call to rte_openlog_stream.
  */
 void eal_log_set_default(FILE *default_log);
+FILE *eal_log_get_default(void);
 
 /*
  * Save a log option for later.
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 151f30eb6071..98753cb52e09 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -60,6 +60,10 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	/* has user has already setup a log stream */
+	if (eal_log_get_default())
+		return 0;
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v6 6/6] eal: add option to put timestamp on console output
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
                     ` (4 preceding siblings ...)
  2023-06-29 15:58   ` [PATCH v6 5/6] eal: allow user to set default log stream before init Stephen Hemminger
@ 2023-06-29 15:58   ` Stephen Hemminger
  5 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-06-29 15:58 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson, Anatoly Burakov

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

The first few lines are not timestamped because the flag is stored
in internal configuration which is stored in shared memory
which is not setup up until a little later in startup process.

Example:
$ dpdk-testpmd --log-timestamp -- -i
EAL: Detected CPU lcores: 16
EAL: Detected NUMA nodes: 1
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
[       0.112264] testpmd: No probed ethernet devices
Interactive-mode selected
[       0.184573] testpmd: create a new mbuf pool <mb_pool_0>: n=163456, size=2176, socket=0
[       0.184612] testpmd: preferred mempool ops selected: ring_mp_mc

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c                     |  9 ++++
 .../freebsd_gsg/freebsd_eal_parameters.rst    |  5 +++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
 lib/eal/common/eal_common_options.c           |  6 +++
 lib/eal/common/eal_internal_cfg.h             |  3 ++
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/unix/eal_log.c                        | 43 +++++++++++++++++--
 7 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index d2b91e20750e..57637728d811 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1055,6 +1055,10 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1162,6 +1166,11 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index 9270d9fa3bfc..99cff10e963c 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -45,3 +45,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..719ca6851625 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -135,3 +135,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 005da4d12001..67fe6efb4526 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -76,6 +76,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1835,6 +1836,10 @@ eal_parse_common_option(int opt, const char *optarg,
 	}
 
 #ifndef RTE_EXEC_ENV_WINDOWS
+	case OPT_LOG_TIMESTAMP_NUM:
+		conf->log_timestamp = 1;
+		break;
+
 	case OPT_TRACE_NUM: {
 		if (eal_trace_args_save(optarg) < 0) {
 			RTE_LOG(ERR, EAL, "invalid parameters for --"
@@ -2194,6 +2199,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"=<facility> Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index 167ec501fa79..ca502d5dd365 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -84,7 +84,10 @@ struct internal_config {
 	/**< true if storing all pages within single files (per-page-size,
 	 * per-node) non-legacy mode only.
 	 */
+	uint8_t log_timestamp;          /**< add timestamp to console output */
+	struct timespec log_start_time; /**< monotonic clock when started */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
+
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..cc9723868e3c 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 98753cb52e09..1ee94a5ee1e6 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -4,7 +4,9 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <rte_log.h>
@@ -17,12 +19,38 @@
  * default log function
  */
 static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
+console_log_write(void *c, const char *buf, size_t size)
 {
+	const struct internal_config *internal_conf = c;
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = write(STDERR_FILENO, buf, size);
+	/* add optional timestamp for stderr */
+	if (internal_conf->log_timestamp) {
+		struct iovec iov[2];
+		struct timespec ts;
+		char tbuf[64];
+
+		/* format up monotonic timestamp */
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= internal_conf->log_start_time.tv_sec;
+		ts.tv_nsec -= internal_conf->log_start_time.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		/* use writev to put timestamp and buf in single operation */
+		iov[0].iov_base = tbuf;
+		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
+					   ts.tv_sec, ts.tv_nsec / 1000u);
+
+		/* casts are to unconstify the buf */
+		iov[1].iov_base = (void *)(uintptr_t)buf;
+		iov[1].iov_len = size;
+		ret = writev(STDERR_FILENO, iov, 2);
+	} else {
+		ret = write(STDERR_FILENO, buf, size);
+	}
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
@@ -58,13 +86,20 @@ static cookie_io_functions_t console_log_func = {
 int
 eal_log_init(const char *id, int facility)
 {
+	struct internal_config *internal_conf = eal_get_internal_configuration();
 	FILE *log_stream;
 
 	/* has user has already setup a log stream */
 	if (eal_log_get_default())
 		return 0;
 
-	log_stream = fopencookie(NULL, "w+", console_log_func);
+	if (internal_conf->process_type == RTE_PROC_PRIMARY &&
+	    internal_conf->log_timestamp) {
+		if (clock_gettime(CLOCK_MONOTONIC, &internal_conf->log_start_time) < 0)
+			return -1;
+	}
+
+	log_stream = fopencookie(internal_conf, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v7 0/5] Logging timetamp and related patches
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (5 preceding siblings ...)
  2023-06-29 15:58 ` [PATCH v6 0/6] Logging related patches Stephen Hemminger
@ 2023-07-05 22:48 ` Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 1/5] windows: make getopt functions have const properties Stephen Hemminger
                     ` (4 more replies)
  2024-03-18 18:30 ` [PATCH v8 0/5] Logging timestamp and related patches Stephen Hemminger
                   ` (13 subsequent siblings)
  20 siblings, 5 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-07-05 22:48 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Patchset that includes:
  - unified code for more of log argument handling
  - fix for duplicate option errors
  - timestamp option for logging

v7
  - consolidate patches
  - fix windows to have same getopt args as Linux and FreeBSD

Stephen Hemminger (5):
  windows: make getopt functions have const properties
  eal: fix help message for syslog option
  eal: unify logging code
  eal: allow user to set default log stream before init
  eal: add option to put timestamp on console output

 app/dumpcap/main.c                            |   3 +
 app/pdump/main.c                              |   3 +
 app/proc-info/main.c                          |   3 +
 app/test/test_eal_flags.c                     |   9 ++
 .../freebsd_gsg/freebsd_eal_parameters.rst    |  32 +++++
 doc/guides/linux_gsg/linux_eal_parameters.rst |   5 +
 lib/eal/common/eal_common_log.c               |  55 +++++++++
 lib/eal/common/eal_common_options.c           |   8 +-
 lib/eal/common/eal_internal_cfg.h             |   3 +
 lib/eal/common/eal_log.h                      |   6 +
 lib/eal/common/eal_options.h                  |   2 +
 lib/eal/freebsd/eal.c                         |  55 ++-------
 lib/eal/linux/eal.c                           |  46 +-------
 lib/eal/linux/eal_log.c                       |  61 ----------
 lib/eal/linux/meson.build                     |   1 -
 lib/eal/unix/eal_log.c                        | 111 ++++++++++++++++++
 lib/eal/unix/meson.build                      |   1 +
 lib/eal/windows/eal.c                         |  36 +-----
 lib/eal/windows/getopt.c                      |  23 ++--
 lib/eal/windows/include/getopt.h              |   8 +-
 20 files changed, 275 insertions(+), 196 deletions(-)
 delete mode 100644 lib/eal/linux/eal_log.c
 create mode 100644 lib/eal/unix/eal_log.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v7 1/5] windows: make getopt functions have const properties
  2023-07-05 22:48 ` [PATCH v7 0/5] Logging timetamp and related patches Stephen Hemminger
@ 2023-07-05 22:48   ` Stephen Hemminger
  2023-07-10 21:10     ` Tyler Retzlaff
  2023-07-05 22:48   ` [PATCH v7 2/5] eal: fix help message for syslog option Stephen Hemminger
                     ` (3 subsequent siblings)
  4 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2023-07-05 22:48 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Dmitry Kozlyuk, Narcisa Ana Maria Vasile,
	Dmitry Malloy, Pallavi Kadam

This aligns getopt, getopt_long, etc to have the same const
attributes as Linux and FreeBSD. The changes are derived from
the FreeBSD version of getopt_long.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/windows/getopt.c         | 23 ++++++++++++-----------
 lib/eal/windows/include/getopt.h |  8 ++++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lib/eal/windows/getopt.c b/lib/eal/windows/getopt.c
index a1f51c6c2318..50ff71b9300d 100644
--- a/lib/eal/windows/getopt.c
+++ b/lib/eal/windows/getopt.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-const char    *optarg;		/* argument associated with option */
+char    *optarg;		/* argument associated with option */
 int	opterr = 1;		/* if error message should be printed */
 int	optind = 1;		/* index into parent argv vector */
 int	optopt = '?';		/* character checked for validity */
@@ -39,9 +39,9 @@ static void pass(const char *a) {(void) a; }
 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
 #define	INORDER		1
 
-#define	EMSG		""
+static char EMSG[] = "";
 
-static const char *place = EMSG; /* option letter processing */
+static char *place = EMSG; /* option letter processing */
 
 /* XXX: set optreset to 1 rather than these two */
 static int nonopt_start = -1; /* first non option argument (for permute) */
@@ -80,7 +80,7 @@ gcd(int a, int b)
  */
 static void
 permute_args(int panonopt_start, int panonopt_end, int opt_end,
-	char **nargv)
+	char * const *nargv)
 {
 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
 	char *swap;
@@ -101,11 +101,12 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
 				pos -= nnonopts;
 			else
 				pos += nopts;
+
 			swap = nargv[pos];
 			/* LINTED const cast */
-			((char **) nargv)[pos] = nargv[cstart];
+			((char **)(uintptr_t)nargv)[pos] = nargv[cstart];
 			/* LINTED const cast */
-			((char **)nargv)[cstart] = swap;
+			((char **)(uintptr_t)nargv)[cstart] = swap;
 		}
 	}
 }
@@ -116,7 +117,7 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
  * Returns -1 if short_too is set and the option does not match long_options.
  */
 static int
-parse_long_options(char **nargv, const char *options,
+parse_long_options(char * const *nargv, const char *options,
 	const struct option *long_options, int *idx, int short_too)
 {
 	const char *current_argv;
@@ -236,7 +237,7 @@ parse_long_options(char **nargv, const char *options,
  *	Parse argc/argv argument vector.  Called by user level routines.
  */
 static int
-getopt_internal(int nargc, char **nargv, const char *options,
+getopt_internal(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx, int flags)
 {
 	char *oli;				/* option letter list index */
@@ -434,7 +435,7 @@ getopt_internal(int nargc, char **nargv, const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt(int nargc, char *nargv[], const char *options)
+getopt(int nargc, char *const nargv[], const char *options)
 {
 	return getopt_internal(nargc, nargv, options, NULL, NULL,
 			       FLAG_PERMUTE);
@@ -445,7 +446,7 @@ getopt(int nargc, char *nargv[], const char *options)
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long(int nargc, char *nargv[], const char *options,
+getopt_long(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
@@ -458,7 +459,7 @@ getopt_long(int nargc, char *nargv[], const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long_only(int nargc, char *nargv[], const char *options,
+getopt_long_only(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
diff --git a/lib/eal/windows/include/getopt.h b/lib/eal/windows/include/getopt.h
index 6f57af454b17..e4cf6873cb0c 100644
--- a/lib/eal/windows/include/getopt.h
+++ b/lib/eal/windows/include/getopt.h
@@ -44,7 +44,7 @@
 
 
 /** argument to current option, or NULL if it has none */
-extern const char *optarg;
+extern char *optarg;
 /** Current position in arg string.  Starts from 1.
  * Setting to 0 resets state.
  */
@@ -80,14 +80,14 @@ struct option {
 };
 
 /** Compat: getopt */
-int getopt(int argc, char *argv[], const char *options);
+int getopt(int argc, char *const argv[], const char *options);
 
 /** Compat: getopt_long */
-int getopt_long(int argc, char *argv[], const char *options,
+int getopt_long(int argc, char *const argv[], const char *options,
 		const struct option *longopts, int *longindex);
 
 /** Compat: getopt_long_only */
-int getopt_long_only(int nargc, char *argv[], const char *options,
+int getopt_long_only(int nargc, char *const argv[], const char *options,
 		     const struct option *long_options, int *idx);
 
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v7 2/5] eal: fix help message for syslog option
  2023-07-05 22:48 ` [PATCH v7 0/5] Logging timetamp and related patches Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 1/5] windows: make getopt functions have const properties Stephen Hemminger
@ 2023-07-05 22:48   ` Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 3/5] eal: unify logging code Stephen Hemminger
                     ` (2 subsequent siblings)
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-07-05 22:48 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The --syslog flag takes facility argument.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 03059336987d..005da4d12001 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -2193,7 +2193,7 @@ eal_common_usage(void)
 	       "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
-	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_SYSLOG"=<facility> Set syslog facility\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v7 3/5] eal: unify logging code
  2023-07-05 22:48 ` [PATCH v7 0/5] Logging timetamp and related patches Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 1/5] windows: make getopt functions have const properties Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 2/5] eal: fix help message for syslog option Stephen Hemminger
@ 2023-07-05 22:48   ` Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 4/5] eal: allow user to set default log stream before init Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 5/5] eal: add option to put timestamp on console output Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-07-05 22:48 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Narcisa Ana Maria Vasile, Dmitry Malloy, Pallavi Kadam

FreeBSD logging code was not using syslog and did not have
the same options as Linux. Use a common set of functions for that.

Pre-parsing for log level is common to all OS's.

Now the pre-scan can return an error.
A bad argument give to --log-level option was given the
code would keep going.

Use opterr to fix duplicate error message on bad option.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    | 27 +++++++++
 lib/eal/common/eal_common_log.c               | 49 +++++++++++++++++
 lib/eal/common/eal_log.h                      |  5 ++
 lib/eal/freebsd/eal.c                         | 55 ++++---------------
 lib/eal/linux/eal.c                           | 46 ++--------------
 lib/eal/linux/meson.build                     |  1 -
 lib/eal/{linux => unix}/eal_log.c             | 11 ++++
 lib/eal/unix/meson.build                      |  1 +
 lib/eal/windows/eal.c                         | 36 +-----------
 9 files changed, 112 insertions(+), 119 deletions(-)
 rename lib/eal/{linux => unix}/eal_log.c (76%)

diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index fba467a2ce92..9270d9fa3bfc 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -18,3 +18,30 @@ FreeBSD-specific EAL parameters
 -------------------------------
 
 There are currently no FreeBSD-specific EAL command-line parameters available.
+
+Other options
+~~~~~~~~~~~~~
+
+*   ``--syslog <syslog facility>``
+
+    Set syslog facility. Valid syslog facilities are::
+
+        auth
+        cron
+        daemon
+        ftp
+        kern
+        lpr
+        mail
+        news
+        syslog
+        user
+        uucp
+        local0
+        local1
+        local2
+        local3
+        local4
+        local5
+        local6
+        local7
diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c
index bd7b188ceb4a..1b2080e8ef04 100644
--- a/lib/eal/common/eal_common_log.c
+++ b/lib/eal/common/eal_common_log.c
@@ -2,6 +2,7 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
+#include <getopt.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdarg.h>
@@ -16,6 +17,8 @@
 #include <rte_per_lcore.h>
 
 #include "eal_log.h"
+#include "eal_internal_cfg.h"
+#include "eal_options.h"
 #include "eal_private.h"
 
 struct rte_log_dynamic_type {
@@ -223,6 +226,52 @@ log_save_level(uint32_t priority, const char *regex, const char *pattern)
 	return -1;
 }
 
+
+/* Parse the all arguments looking for --log-level */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_log_save_regexp(const char *regex, uint32_t level)
 {
diff --git a/lib/eal/common/eal_log.h b/lib/eal/common/eal_log.h
index c784fa604389..31dc489350f6 100644
--- a/lib/eal/common/eal_log.h
+++ b/lib/eal/common/eal_log.h
@@ -13,6 +13,11 @@
  */
 int eal_log_init(const char *id, int facility);
 
+/*
+ * Scan command line args for log settings.
+ */
+int eal_log_level_parse(int argc, char * const argv[]);
+
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
  */
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 7008303e112a..bb9a2b1653d9 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -52,6 +52,7 @@
 #include "eal_hugepages.h"
 #include "eal_options.h"
 #include "eal_memcfg.h"
+#include "eal_log.h"
 #include "eal_trace.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
@@ -363,48 +364,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
@@ -610,7 +569,11 @@ rte_eal_init(int argc, char **argv)
 	eal_save_args(argc, argv);
 
 	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log option.");
+		rte_errno = EINVAL;
+		return -1;
+	}
 
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
@@ -759,6 +722,12 @@ 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 145afafde234..c9475560d986 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
@@ -993,7 +954,12 @@ rte_eal_init(int argc, char **argv)
 	eal_reset_internal_config(internal_conf);
 
 	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log option.");
+		rte_errno = EINVAL;
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
+		return -1;
+	}
 
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
diff --git a/lib/eal/linux/meson.build b/lib/eal/linux/meson.build
index 5af456db9edb..e99ebed25692 100644
--- a/lib/eal/linux/meson.build
+++ b/lib/eal/linux/meson.build
@@ -11,7 +11,6 @@ sources += files(
         'eal_hugepage_info.c',
         'eal_interrupts.c',
         'eal_lcore.c',
-        'eal_log.c',
         'eal_memalloc.c',
         'eal_memory.c',
         'eal_thread.c',
diff --git a/lib/eal/linux/eal_log.c b/lib/eal/unix/eal_log.c
similarity index 76%
rename from lib/eal/linux/eal_log.c
rename to lib/eal/unix/eal_log.c
index d44416fd6570..85d817c2d31e 100644
--- a/lib/eal/linux/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -9,6 +9,8 @@
 #include <rte_log.h>
 
 #include "eal_log.h"
+#include "eal_internal_cfg.h"
+#include "eal_private.h"
 
 /*
  * default log function
@@ -25,6 +27,15 @@ console_log_write(__rte_unused void *c, const char *buf, size_t size)
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
 
+#ifdef RTE_EXEC_ENV_LINUX
+	/* Handle glibc quirk: write function should return the number of bytes
+	 * copied from buf, or 0 on error. (The function must not return a negative value.)
+	 * FreeBSD expects that write function behaves like write(2).
+	 */
+	if (ret < 0)
+		ret = 0;
+#endif
+
 	return ret;
 }
 
diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
index cc7d67dd321d..37d07594df29 100644
--- a/lib/eal/unix/meson.build
+++ b/lib/eal/unix/meson.build
@@ -6,6 +6,7 @@ sources += files(
         'eal_file.c',
         'eal_filesystem.c',
         'eal_firmware.c',
+        'eal_log.c',
         'eal_unix_memory.c',
         'eal_unix_thread.c',
         'eal_unix_timer.c',
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 2d7a0e9ab27e..c6006c48ddef 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
@@ -143,6 +108,7 @@ eal_parse_args(int argc, char **argv)
 		eal_get_internal_configuration();
 
 	argvopt = argv;
+	opterr = 1;
 
 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
 		eal_long_options, &option_index)) != EOF) {
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v7 4/5] eal: allow user to set default log stream before init
  2023-07-05 22:48 ` [PATCH v7 0/5] Logging timetamp and related patches Stephen Hemminger
                     ` (2 preceding siblings ...)
  2023-07-05 22:48   ` [PATCH v7 3/5] eal: unify logging code Stephen Hemminger
@ 2023-07-05 22:48   ` Stephen Hemminger
  2023-07-05 22:48   ` [PATCH v7 5/5] eal: add option to put timestamp on console output Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-07-05 22:48 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Reshma Pattan, Maryam Tahhan

It is useful for application to be able to set the default log
stream before call rte_eal_init(). This makes all messages go
to the new default.

For example, to skip using syslog; just doing
	rte_openlog_stream(stderr);

There is no reason for helper command line applications to clutter
syslog with messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/dumpcap/main.c              | 3 +++
 app/pdump/main.c                | 3 +++
 app/proc-info/main.c            | 3 +++
 lib/eal/common/eal_common_log.c | 6 ++++++
 lib/eal/common/eal_log.h        | 1 +
 lib/eal/unix/eal_log.c          | 4 ++++
 6 files changed, 20 insertions(+)

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index 64294bbfb3e6..bf31eef13cf4 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -639,6 +639,9 @@ static void dpdk_init(void)
 		eal_argv[i++] = strdup(file_prefix);
 	}
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	if (rte_eal_init(eal_argc, eal_argv) < 0)
 		rte_exit(EXIT_FAILURE, "EAL init failed: is primary process running?\n");
 }
diff --git a/app/pdump/main.c b/app/pdump/main.c
index c94606275b28..82c67350e4ab 100644
--- a/app/pdump/main.c
+++ b/app/pdump/main.c
@@ -989,6 +989,9 @@ main(int argc, char **argv)
 
 	argc += 2;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	diag = rte_eal_init(argc, argp);
 	if (diag < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/app/proc-info/main.c b/app/proc-info/main.c
index 53e852a07c14..371b1f382d66 100644
--- a/app/proc-info/main.c
+++ b/app/proc-info/main.c
@@ -1774,6 +1774,9 @@ main(int argc, char **argv)
 
 	argc += 4;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	ret = rte_eal_init(argc, argp);
 	if (ret < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c
index 1b2080e8ef04..1533ee871f47 100644
--- a/lib/eal/common/eal_common_log.c
+++ b/lib/eal/common/eal_common_log.c
@@ -584,6 +584,12 @@ eal_log_set_default(FILE *default_log)
 #endif
 }
 
+FILE *
+eal_log_get_default(void)
+{
+	return default_log_stream;
+}
+
 /*
  * Called by eal_cleanup
  */
diff --git a/lib/eal/common/eal_log.h b/lib/eal/common/eal_log.h
index 31dc489350f6..268c2a264382 100644
--- a/lib/eal/common/eal_log.h
+++ b/lib/eal/common/eal_log.h
@@ -22,6 +22,7 @@ int eal_log_level_parse(int argc, char * const argv[]);
  * Determine where log data is written when no call to rte_openlog_stream.
  */
 void eal_log_set_default(FILE *default_log);
+FILE *eal_log_get_default(void);
 
 /*
  * Save a log option for later.
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index 85d817c2d31e..e66bcc68d07f 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -60,6 +60,10 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	/* has user has already setup a log stream */
+	if (eal_log_get_default())
+		return 0;
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v7 5/5] eal: add option to put timestamp on console output
  2023-07-05 22:48 ` [PATCH v7 0/5] Logging timetamp and related patches Stephen Hemminger
                     ` (3 preceding siblings ...)
  2023-07-05 22:48   ` [PATCH v7 4/5] eal: allow user to set default log stream before init Stephen Hemminger
@ 2023-07-05 22:48   ` Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2023-07-05 22:48 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson, Anatoly Burakov

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

The first few lines are not timestamped because the flag is stored
in internal configuration which is stored in shared memory
which is not setup up until a little later in startup process.

This logging skips the unnecessary step of going through stdio,
which makes it more robust against being called in interrupt
handlers etc.

Example:
$ dpdk-testpmd --log-timestamp -- -i
EAL: Detected CPU lcores: 16
EAL: Detected NUMA nodes: 1
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
[       0.112264] testpmd: No probed ethernet devices
Interactive-mode selected
[       0.184573] testpmd: create a new mbuf pool <mb_pool_0>: n=163456, size=2176, socket=0
[       0.184612] testpmd: preferred mempool ops selected: ring_mp_mc

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c                     |  9 ++++
 .../freebsd_gsg/freebsd_eal_parameters.rst    |  5 +++
 doc/guides/linux_gsg/linux_eal_parameters.rst |  5 +++
 lib/eal/common/eal_common_options.c           |  6 +++
 lib/eal/common/eal_internal_cfg.h             |  3 ++
 lib/eal/common/eal_options.h                  |  2 +
 lib/eal/unix/eal_log.c                        | 45 ++++++++++++++++---
 7 files changed, 70 insertions(+), 5 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index d2b91e20750e..57637728d811 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1055,6 +1055,10 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1162,6 +1166,11 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
index 9270d9fa3bfc..99cff10e963c 100644
--- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
+++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst
@@ -45,3 +45,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..719ca6851625 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -135,3 +135,8 @@ Other options
         local5
         local6
         local7
+
+*   ``--log-timestamp``
+
+    Add a timestamp of seconds and microseconds to each log message
+    written to standard output.
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 005da4d12001..67fe6efb4526 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -76,6 +76,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1835,6 +1836,10 @@ eal_parse_common_option(int opt, const char *optarg,
 	}
 
 #ifndef RTE_EXEC_ENV_WINDOWS
+	case OPT_LOG_TIMESTAMP_NUM:
+		conf->log_timestamp = 1;
+		break;
+
 	case OPT_TRACE_NUM: {
 		if (eal_trace_args_save(optarg) < 0) {
 			RTE_LOG(ERR, EAL, "invalid parameters for --"
@@ -2194,6 +2199,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"=<facility> Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index 167ec501fa79..ca502d5dd365 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -84,7 +84,10 @@ struct internal_config {
 	/**< true if storing all pages within single files (per-page-size,
 	 * per-node) non-legacy mode only.
 	 */
+	uint8_t log_timestamp;          /**< add timestamp to console output */
+	struct timespec log_start_time; /**< monotonic clock when started */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
+
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..cc9723868e3c 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/unix/eal_log.c b/lib/eal/unix/eal_log.c
index e66bcc68d07f..1ee94a5ee1e6 100644
--- a/lib/eal/unix/eal_log.c
+++ b/lib/eal/unix/eal_log.c
@@ -4,7 +4,10 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
@@ -16,13 +19,38 @@
  * default log function
  */
 static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
+console_log_write(void *c, const char *buf, size_t size)
 {
+	const struct internal_config *internal_conf = c;
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
+	/* add optional timestamp for stderr */
+	if (internal_conf->log_timestamp) {
+		struct iovec iov[2];
+		struct timespec ts;
+		char tbuf[64];
+
+		/* format up monotonic timestamp */
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= internal_conf->log_start_time.tv_sec;
+		ts.tv_nsec -= internal_conf->log_start_time.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		/* use writev to put timestamp and buf in single operation */
+		iov[0].iov_base = tbuf;
+		iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
+					   ts.tv_sec, ts.tv_nsec / 1000u);
+
+		/* casts are to unconstify the buf */
+		iov[1].iov_base = (void *)(uintptr_t)buf;
+		iov[1].iov_len = size;
+		ret = writev(STDERR_FILENO, iov, 2);
+	} else {
+		ret = write(STDERR_FILENO, buf, size);
+	}
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
@@ -58,13 +86,20 @@ static cookie_io_functions_t console_log_func = {
 int
 eal_log_init(const char *id, int facility)
 {
+	struct internal_config *internal_conf = eal_get_internal_configuration();
 	FILE *log_stream;
 
 	/* has user has already setup a log stream */
 	if (eal_log_get_default())
 		return 0;
 
-	log_stream = fopencookie(NULL, "w+", console_log_func);
+	if (internal_conf->process_type == RTE_PROC_PRIMARY &&
+	    internal_conf->log_timestamp) {
+		if (clock_gettime(CLOCK_MONOTONIC, &internal_conf->log_start_time) < 0)
+			return -1;
+	}
+
+	log_stream = fopencookie(internal_conf, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v7 1/5] windows: make getopt functions have const properties
  2023-07-05 22:48   ` [PATCH v7 1/5] windows: make getopt functions have const properties Stephen Hemminger
@ 2023-07-10 21:10     ` Tyler Retzlaff
  0 siblings, 0 replies; 277+ messages in thread
From: Tyler Retzlaff @ 2023-07-10 21:10 UTC (permalink / raw)
  To: Stephen Hemminger
  Cc: dev, Dmitry Kozlyuk, Narcisa Ana Maria Vasile, Dmitry Malloy,
	Pallavi Kadam

On Wed, Jul 05, 2023 at 03:48:01PM -0700, Stephen Hemminger wrote:
> This aligns getopt, getopt_long, etc to have the same const
> attributes as Linux and FreeBSD. The changes are derived from
> the FreeBSD version of getopt_long.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---

Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>

^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v8 0/5] Logging timestamp and related patches
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (6 preceding siblings ...)
  2023-07-05 22:48 ` [PATCH v7 0/5] Logging timetamp and related patches Stephen Hemminger
@ 2024-03-18 18:30 ` Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 1/5] log: unify logging code Stephen Hemminger
                     ` (4 more replies)
  2024-03-18 22:02 ` [PATCH v9 0/5] Logging unification and timestamp Stephen Hemminger
                   ` (12 subsequent siblings)
  20 siblings, 5 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 18:30 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Improvements and unification of logging library (for 24.07 release).
This is update to earlier patch set.

v8 - rebase to current code base where logging in in lib/log
     use stdio for log timestamp
     initialization changes (setup log earlier)

Stephen Hemminger (5):
  log: unify logging code
  eal: make eal_log_level_parse common
  eal: allow user to set default log stream before init
  eal: add option to put timestamp on console output
  eal: initialize logging before plugins

 app/dumpcap/main.c                            |  3 +
 app/pdump/main.c                              |  3 +
 app/proc-info/main.c                          |  3 +
 app/test/test_eal_flags.c                     |  9 +++
 doc/guides/linux_gsg/linux_eal_parameters.rst | 27 ---------
 doc/guides/prog_guide/log_lib.rst             | 28 +++++++++-
 lib/eal/common/eal_common_options.c           | 56 ++++++++++++++++++-
 lib/eal/common/eal_options.h                  |  3 +
 lib/eal/freebsd/eal.c                         | 50 +++--------------
 lib/eal/linux/eal.c                           | 55 +++---------------
 lib/eal/unix/eal_unix_log.c                   |  0
 lib/eal/windows/eal.c                         | 35 ------------
 lib/log/log.c                                 |  6 ++
 lib/log/log_freebsd.c                         | 12 ----
 lib/log/log_internal.h                        | 11 ++++
 lib/log/{log_linux.c => log_unix.c}           | 36 +++++++++++-
 lib/log/log_windows.c                         |  6 ++
 lib/log/meson.build                           | 12 ++--
 lib/log/version.map                           |  2 +
 19 files changed, 184 insertions(+), 173 deletions(-)
 create mode 100644 lib/eal/unix/eal_unix_log.c
 delete mode 100644 lib/log/log_freebsd.c
 rename lib/log/{log_linux.c => log_unix.c} (58%)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v8 1/5] log: unify logging code
  2024-03-18 18:30 ` [PATCH v8 0/5] Logging timestamp and related patches Stephen Hemminger
@ 2024-03-18 18:30   ` Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 2/5] eal: make eal_log_level_parse common Stephen Hemminger
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 18:30 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

FreeBSD and Linux logging code can use common code. This also
fixes FreeBSD not using syslog.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/linux_gsg/linux_eal_parameters.rst | 27 -------------------
 doc/guides/prog_guide/log_lib.rst             | 18 +++++++++++--
 lib/eal/freebsd/eal.c                         |  8 ++++++
 lib/log/log_freebsd.c                         | 12 ---------
 lib/log/{log_linux.c => log_unix.c}           |  0
 lib/log/meson.build                           | 12 ++++++---
 6 files changed, 32 insertions(+), 45 deletions(-)
 delete mode 100644 lib/log/log_freebsd.c
 rename lib/log/{log_linux.c => log_unix.c} (100%)

diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..d86f94d8a85d 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -108,30 +108,3 @@ Memory-related options
 *   ``--match-allocations``
 
     Free hugepages back to system exactly as they were originally allocated.
-
-Other options
-~~~~~~~~~~~~~
-
-*   ``--syslog <syslog facility>``
-
-    Set syslog facility. Valid syslog facilities are::
-
-        auth
-        cron
-        daemon
-        ftp
-        kern
-        lpr
-        mail
-        news
-        syslog
-        user
-        uucp
-        local0
-        local1
-        local2
-        local3
-        local4
-        local5
-        local6
-        local7
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index ff9d1b54a2c8..aacb36c36ce0 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -5,8 +5,8 @@ Log Library
 ===========
 
 The DPDK Log library provides the logging functionality for other DPDK libraries and drivers.
-By default, in a Linux application, logs are sent to syslog and also to the console.
-On FreeBSD and Windows applications, logs are sent only to the console.
+By default, in a Linux (or FreeBSD) application, logs are sent to syslog and also to the console.
+In Windows applications, logs are sent only to the console.
 However, the log function can be overridden by the user to use a different logging mechanism.
 
 Log Levels
@@ -29,6 +29,7 @@ will be emitted by the application to the log output.
 That level can be configured either by the application calling the relevant APIs from the logging library,
 or by the user passing the ``--log-level`` parameter to the EAL via the application.
 
+
 Setting Global Log Level
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -59,6 +60,19 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+
+Setting syslog facility
+~~~~~~~~~~~~~~~~~~~~~~~
+
+On Linux and FreeBSD, where syslog is used a ``facility`` argument can be
+used to specify what type of program is logging.
+The default facility is ``daemon`` but it can be overridden
+by the ``--syslog`` EAL parameter. See ``syslog.3`` man page for full values.
+For example::
+
+	/path/to/app --syslog local0
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bab77118e967..004b8fad2db3 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -760,6 +760,14 @@ 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;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		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/log/log_freebsd.c b/lib/log/log_freebsd.c
deleted file mode 100644
index 698d3c542337..000000000000
--- a/lib/log/log_freebsd.c
+++ /dev/null
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2023 Intel Corporation
- */
-
-#include <rte_common.h>
-#include "log_internal.h"
-
-int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
-{
-	return 0;
-}
diff --git a/lib/log/log_linux.c b/lib/log/log_unix.c
similarity index 100%
rename from lib/log/log_linux.c
rename to lib/log/log_unix.c
diff --git a/lib/log/meson.build b/lib/log/meson.build
index 0d4319b36f77..60516a0b2a2d 100644
--- a/lib/log/meson.build
+++ b/lib/log/meson.build
@@ -2,8 +2,12 @@
 # Copyright(c) 2023 Intel Corporation
 
 includes += global_inc
-sources = files(
-        'log.c',
-        'log_' + exec_env + '.c',
-)
+sources = files('log.c')
+
+if is_windows
+    sources += files('log_windows.c')
+else
+    sources += files('log_unix.c')
+endif
+
 headers = files('rte_log.h')
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v8 2/5] eal: make eal_log_level_parse common
  2024-03-18 18:30 ` [PATCH v8 0/5] Logging timestamp and related patches Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 1/5] log: unify logging code Stephen Hemminger
@ 2024-03-18 18:30   ` Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 3/5] eal: allow user to set default log stream before init Stephen Hemminger
                     ` (2 subsequent siblings)
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 18:30 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The code to parse for log-level option should be same on
all OS variants.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 46 +++++++++++++++++++++++++++++
 lib/eal/common/eal_options.h        |  1 +
 lib/eal/freebsd/eal.c               | 42 --------------------------
 lib/eal/linux/eal.c                 | 39 ------------------------
 lib/eal/windows/eal.c               | 35 ----------------------
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index e541f0793964..7310d10dfd78 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -1640,6 +1640,51 @@ eal_parse_huge_unlink(const char *arg, struct hugepage_file_discipline *out)
 	return -1;
 }
 
+/* Parse the all arguments looking for --log-level */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_parse_common_option(int opt, const char *optarg,
 			struct internal_config *conf)
@@ -2173,6 +2218,7 @@ rte_vect_set_max_simd_bitwidth(uint16_t bitwidth)
 	return 0;
 }
 
+
 void
 eal_common_usage(void)
 {
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..f3f2e104f6d7 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -96,6 +96,7 @@ enum {
 extern const char eal_short_options[];
 extern const struct option eal_long_options[];
 
+int eal_log_level_parse(int argc, char * const argv[]);
 int eal_parse_common_option(int opt, const char *argv,
 			    struct internal_config *conf);
 int eal_option_device_parse(void);
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 004b8fad2db3..b4f8d68b0a65 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -363,48 +363,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index fd422f1f6236..bffeb1f34eb9 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 52f0e7462dd8..85171b27687e 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v8 3/5] eal: allow user to set default log stream before init
  2024-03-18 18:30 ` [PATCH v8 0/5] Logging timestamp and related patches Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 1/5] log: unify logging code Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 2/5] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-18 18:30   ` Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 4/5] eal: add option to put timestamp on console output Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 5/5] eal: initialize logging before plugins Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 18:30 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

It is useful for application to be able to set the default log
stream before call rte_eal_init(). This makes all messages go
to the new default.

For example, to skip using syslog; just doing
	rte_openlog_stream(stderr);

There is no reason for helper command line applications to clutter
syslog with messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/dumpcap/main.c     | 3 +++
 app/pdump/main.c       | 3 +++
 app/proc-info/main.c   | 3 +++
 lib/log/log.c          | 6 ++++++
 lib/log/log_internal.h | 2 ++
 lib/log/log_unix.c     | 4 ++++
 lib/log/version.map    | 1 +
 7 files changed, 22 insertions(+)

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index cc0f66b2bc61..27934ca7e688 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -633,6 +633,9 @@ static void dpdk_init(void)
 			rte_panic("No memory\n");
 	}
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	if (rte_eal_init(eal_argc, eal_argv) < 0)
 		rte_exit(EXIT_FAILURE, "EAL init failed: is primary process running?\n");
 }
diff --git a/app/pdump/main.c b/app/pdump/main.c
index a9205e130bb1..7b9ba68b1a14 100644
--- a/app/pdump/main.c
+++ b/app/pdump/main.c
@@ -995,6 +995,9 @@ main(int argc, char **argv)
 
 	argc += 2;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	diag = rte_eal_init(argc, argp);
 	if (diag < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/app/proc-info/main.c b/app/proc-info/main.c
index b672aaefbe99..24ee52c4ac7a 100644
--- a/app/proc-info/main.c
+++ b/app/proc-info/main.c
@@ -2149,6 +2149,9 @@ main(int argc, char **argv)
 
 	argc += 4;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	ret = rte_eal_init(argc, argp);
 	if (ret < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/lib/log/log.c b/lib/log/log.c
index 255f757d94cc..4cc944305057 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -519,6 +519,12 @@ eal_log_set_default(FILE *default_log)
 #endif
 }
 
+FILE *
+eal_log_get_default(void)
+{
+	return default_log_stream;
+}
+
 /*
  * Called by eal_cleanup
  */
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 451629f1c1ba..c77e687e28bc 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -21,6 +21,8 @@ int eal_log_init(const char *id, int facility);
  */
 __rte_internal
 void eal_log_set_default(FILE *default_log);
+__rte_internal
+FILE *eal_log_get_default(void);
 
 /*
  * Save a log option for later.
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index 2dfb0c974b1d..a415bae5774d 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -49,6 +49,10 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	/* skip if user has already setup a log stream */
+	if (eal_log_get_default())
+		return 0;
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
diff --git a/lib/log/version.map b/lib/log/version.map
index 0648f8831aff..6ecc656d1d65 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_get_default;
 	eal_log_init;
 	eal_log_level2str;
 	eal_log_save_pattern;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v8 4/5] eal: add option to put timestamp on console output
  2024-03-18 18:30 ` [PATCH v8 0/5] Logging timestamp and related patches Stephen Hemminger
                     ` (2 preceding siblings ...)
  2024-03-18 18:30   ` [PATCH v8 3/5] eal: allow user to set default log stream before init Stephen Hemminger
@ 2024-03-18 18:30   ` Stephen Hemminger
  2024-03-18 18:30   ` [PATCH v8 5/5] eal: initialize logging before plugins Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 18:30 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

The first few lines are not timestamped because the flag is stored
in internal configuration which is stored in shared memory
which is not setup up until a little later in startup process.

This logging skips the unnecessary step of going through stdio,
which makes it more robust against being called in interrupt
handlers etc.

Example:
$ dpdk-testpmd --log-timestamp -- -i
EAL: Detected CPU lcores: 16
EAL: Detected NUMA nodes: 1
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
[       0.112264] testpmd: No probed ethernet devices
Interactive-mode selected
[       0.184573] testpmd: create a new mbuf pool <mb_pool_0>: n=163456, size=2176, socket=0
[       0.184612] testpmd: preferred mempool ops selected: ring_mp_mc

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  9 ++++++++
 doc/guides/prog_guide/log_lib.rst   | 10 +++++++++
 lib/eal/common/eal_common_options.c | 10 +++++++--
 lib/eal/common/eal_options.h        |  2 ++
 lib/log/log_internal.h              |  9 ++++++++
 lib/log/log_unix.c                  | 32 +++++++++++++++++++++++++++--
 lib/log/log_windows.c               |  6 ++++++
 lib/log/version.map                 |  1 +
 8 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 6cb4b0675730..07a038fb6051 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1055,6 +1055,10 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1162,6 +1166,11 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index aacb36c36ce0..1d6b2e3cea5d 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -73,6 +73,16 @@ For example::
 	/path/to/app --syslog local0
 
 
+Console timestamp
+~~~~~~~~~~~~~~~~~
+
+On Linux and FreeBSD, an optional timestamp can be added before each
+message by adding the ``--log-timestamp`` option.
+For example::
+
+	/path/to/app --log-level=lib.*:debug --log-timestamp
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 7310d10dfd78..9bc95433d27c 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -77,6 +77,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1663,6 +1664,7 @@ eal_log_level_parse(int argc, char * const argv[])
 
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_LOG_TIMESTAMP_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1890,7 +1892,7 @@ eal_parse_common_option(int opt, const char *optarg,
 		break;
 #endif
 
-	case OPT_LOG_LEVEL_NUM: {
+	case OPT_LOG_LEVEL_NUM:
 		if (eal_parse_log_level(optarg) < 0) {
 			EAL_LOG(ERR,
 				"invalid parameters for --"
@@ -1898,7 +1900,10 @@ eal_parse_common_option(int opt, const char *optarg,
 			return -1;
 		}
 		break;
-	}
+
+	case OPT_LOG_TIMESTAMP_NUM:
+		eal_log_enable_timestamp();
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -2261,6 +2266,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index f3f2e104f6d7..e24c9eca53ca 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index c77e687e28bc..1af27fe8f9d3 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -5,8 +5,10 @@
 #ifndef LOG_INTERNAL_H
 #define LOG_INTERNAL_H
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <time.h>
 
 #include <rte_compat.h>
 
@@ -44,4 +46,11 @@ const char *eal_log_level2str(uint32_t level);
 __rte_internal
 void rte_eal_log_cleanup(void);
 
+/*
+ * Add timestamp to console logs
+ */
+__rte_internal
+void eal_log_enable_timestamp(void);
+
+
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index a415bae5774d..71ce366b6825 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -2,24 +2,52 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
 #include "log_internal.h"
 
+static bool timestamp_enabled;
+static struct timespec log_started;
+
+void
+eal_log_enable_timestamp(void)
+{
+	timestamp_enabled = true;
+	clock_gettime(CLOCK_MONOTONIC, &log_started);
+}
+
 /*
  * default log function
  */
 static ssize_t
 console_log_write(__rte_unused void *c, const char *buf, size_t size)
 {
+	struct timespec ts;
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
+	if (timestamp_enabled) {
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= log_started.tv_sec;
+		ts.tv_nsec -= log_started.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		ret = fprintf(stderr, "[%8lu.%06lu] %.*s",
+			      ts.tv_sec, ts.tv_nsec / 1000u,
+			      (int) size, buf);
+	} else {
+		ret = fwrite(buf, 1, size, stderr);
+	}
 	fflush(stderr);
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a6a08895501e..0c471bfb15f6 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -6,6 +6,12 @@
 #include <rte_log.h>
 #include "log_internal.h"
 
+void
+eal_log_enable_timestamp(void)
+{
+	/* not implemented */
+}
+
 /* set the log to default function, called during eal init process. */
 int
 eal_log_init(__rte_unused const char *id, __rte_unused int facility)
diff --git a/lib/log/version.map b/lib/log/version.map
index 6ecc656d1d65..32c555f29e79 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_enable_timestamp;
 	eal_log_get_default;
 	eal_log_init;
 	eal_log_level2str;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v8 5/5] eal: initialize logging before plugins
  2024-03-18 18:30 ` [PATCH v8 0/5] Logging timestamp and related patches Stephen Hemminger
                     ` (3 preceding siblings ...)
  2024-03-18 18:30   ` [PATCH v8 4/5] eal: add option to put timestamp on console output Stephen Hemminger
@ 2024-03-18 18:30   ` Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 18:30 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Want to make sure that as many log messages as possible
get added with the real log stream.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c       | 16 ++++++++--------
 lib/eal/linux/eal.c         | 16 ++++++++--------
 lib/eal/unix/eal_unix_log.c |  0
 3 files changed, 16 insertions(+), 16 deletions(-)
 create mode 100644 lib/eal/unix/eal_unix_log.c

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index b4f8d68b0a65..e4f00f31fbdd 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -592,6 +592,14 @@ rte_eal_init(int argc, char **argv)
 		internal_conf->in_memory = false;
 	}
 
+	if (eal_log_init(program_invocation_short_name,
+			 internal_conf->syslog_facility) < 0) {
+		rte_eal_init_alert("Cannot init logging.");
+		rte_errno = ENOMEM;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		return -1;
+	}
+
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
@@ -718,14 +726,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;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		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 bffeb1f34eb9..e24f24b1b0ce 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -973,6 +973,14 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
+	if (eal_log_init(program_invocation_short_name,
+			 internal_conf->syslog_facility) < 0) {
+		rte_eal_init_alert("Cannot init logging.");
+		rte_errno = ENOMEM;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		return -1;
+	}
+
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
@@ -1107,14 +1115,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;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
diff --git a/lib/eal/unix/eal_unix_log.c b/lib/eal/unix/eal_unix_log.c
new file mode 100644
index 000000000000..e69de29bb2d1
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v9 0/5] Logging unification and timestamp
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (7 preceding siblings ...)
  2024-03-18 18:30 ` [PATCH v8 0/5] Logging timestamp and related patches Stephen Hemminger
@ 2024-03-18 22:02 ` Stephen Hemminger
  2024-03-18 22:02   ` [PATCH v9 1/5] log: unify logging code Stephen Hemminger
                     ` (4 more replies)
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                   ` (11 subsequent siblings)
  20 siblings, 5 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 22:02 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Improvements and unification of logging library (for 24.07 release).
This is update to earlier patch set.

v9 - reorder patches and fix FreeBSD build

v8 - rebase to current code base where logging in in lib/log
     use stdio for log timestamp
     initialization changes (setup log earlier)

Stephen Hemminger (5):
  log: unify logging code
  eal: make eal_log_level_parse common
  eal: initialize logging before plugins
  eal: allow user to set default log stream before init
  eal: add option to put timestamp on console output

 app/dumpcap/main.c                            |  3 +
 app/pdump/main.c                              |  3 +
 app/proc-info/main.c                          |  3 +
 app/test/test_eal_flags.c                     |  9 +++
 doc/guides/linux_gsg/linux_eal_parameters.rst | 27 ---------
 doc/guides/prog_guide/log_lib.rst             | 28 +++++++++-
 lib/eal/common/eal_common_options.c           | 56 ++++++++++++++++++-
 lib/eal/common/eal_options.h                  |  3 +
 lib/eal/freebsd/eal.c                         | 50 +++--------------
 lib/eal/linux/eal.c                           | 55 +++---------------
 lib/eal/unix/eal_unix_log.c                   |  0
 lib/eal/windows/eal.c                         | 35 ------------
 lib/log/log.c                                 |  6 ++
 lib/log/log_freebsd.c                         | 12 ----
 lib/log/log_internal.h                        | 11 ++++
 lib/log/{log_linux.c => log_unix.c}           | 36 +++++++++++-
 lib/log/log_windows.c                         |  6 ++
 lib/log/meson.build                           | 12 ++--
 lib/log/version.map                           |  2 +
 19 files changed, 184 insertions(+), 173 deletions(-)
 create mode 100644 lib/eal/unix/eal_unix_log.c
 delete mode 100644 lib/log/log_freebsd.c
 rename lib/log/{log_linux.c => log_unix.c} (58%)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v9 1/5] log: unify logging code
  2024-03-18 22:02 ` [PATCH v9 0/5] Logging unification and timestamp Stephen Hemminger
@ 2024-03-18 22:02   ` Stephen Hemminger
  2024-03-18 22:02   ` [PATCH v9 2/5] eal: make eal_log_level_parse common Stephen Hemminger
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 22:02 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

FreeBSD and Linux logging code can use common code. This also
fixes FreeBSD not using syslog.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/linux_gsg/linux_eal_parameters.rst | 27 -------------------
 doc/guides/prog_guide/log_lib.rst             | 18 +++++++++++--
 lib/eal/freebsd/eal.c                         |  8 ++++++
 lib/log/log_freebsd.c                         | 12 ---------
 lib/log/{log_linux.c => log_unix.c}           |  0
 lib/log/meson.build                           | 12 ++++++---
 6 files changed, 32 insertions(+), 45 deletions(-)
 delete mode 100644 lib/log/log_freebsd.c
 rename lib/log/{log_linux.c => log_unix.c} (100%)

diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..d86f94d8a85d 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -108,30 +108,3 @@ Memory-related options
 *   ``--match-allocations``
 
     Free hugepages back to system exactly as they were originally allocated.
-
-Other options
-~~~~~~~~~~~~~
-
-*   ``--syslog <syslog facility>``
-
-    Set syslog facility. Valid syslog facilities are::
-
-        auth
-        cron
-        daemon
-        ftp
-        kern
-        lpr
-        mail
-        news
-        syslog
-        user
-        uucp
-        local0
-        local1
-        local2
-        local3
-        local4
-        local5
-        local6
-        local7
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index ff9d1b54a2c8..aacb36c36ce0 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -5,8 +5,8 @@ Log Library
 ===========
 
 The DPDK Log library provides the logging functionality for other DPDK libraries and drivers.
-By default, in a Linux application, logs are sent to syslog and also to the console.
-On FreeBSD and Windows applications, logs are sent only to the console.
+By default, in a Linux (or FreeBSD) application, logs are sent to syslog and also to the console.
+In Windows applications, logs are sent only to the console.
 However, the log function can be overridden by the user to use a different logging mechanism.
 
 Log Levels
@@ -29,6 +29,7 @@ will be emitted by the application to the log output.
 That level can be configured either by the application calling the relevant APIs from the logging library,
 or by the user passing the ``--log-level`` parameter to the EAL via the application.
 
+
 Setting Global Log Level
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -59,6 +60,19 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+
+Setting syslog facility
+~~~~~~~~~~~~~~~~~~~~~~~
+
+On Linux and FreeBSD, where syslog is used a ``facility`` argument can be
+used to specify what type of program is logging.
+The default facility is ``daemon`` but it can be overridden
+by the ``--syslog`` EAL parameter. See ``syslog.3`` man page for full values.
+For example::
+
+	/path/to/app --syslog local0
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bab77118e967..a57ee8406f0c 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -53,6 +53,7 @@
 #include "eal_options.h"
 #include "eal_memcfg.h"
 #include "eal_trace.h"
+#include "log_internal.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
 
@@ -760,6 +761,13 @@ 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;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		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/log/log_freebsd.c b/lib/log/log_freebsd.c
deleted file mode 100644
index 698d3c542337..000000000000
--- a/lib/log/log_freebsd.c
+++ /dev/null
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2023 Intel Corporation
- */
-
-#include <rte_common.h>
-#include "log_internal.h"
-
-int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
-{
-	return 0;
-}
diff --git a/lib/log/log_linux.c b/lib/log/log_unix.c
similarity index 100%
rename from lib/log/log_linux.c
rename to lib/log/log_unix.c
diff --git a/lib/log/meson.build b/lib/log/meson.build
index 0d4319b36f77..60516a0b2a2d 100644
--- a/lib/log/meson.build
+++ b/lib/log/meson.build
@@ -2,8 +2,12 @@
 # Copyright(c) 2023 Intel Corporation
 
 includes += global_inc
-sources = files(
-        'log.c',
-        'log_' + exec_env + '.c',
-)
+sources = files('log.c')
+
+if is_windows
+    sources += files('log_windows.c')
+else
+    sources += files('log_unix.c')
+endif
+
 headers = files('rte_log.h')
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v9 2/5] eal: make eal_log_level_parse common
  2024-03-18 22:02 ` [PATCH v9 0/5] Logging unification and timestamp Stephen Hemminger
  2024-03-18 22:02   ` [PATCH v9 1/5] log: unify logging code Stephen Hemminger
@ 2024-03-18 22:02   ` Stephen Hemminger
  2024-03-18 22:02   ` [PATCH v9 3/5] eal: initialize logging before plugins Stephen Hemminger
                     ` (2 subsequent siblings)
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 22:02 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The code to parse for log-level option should be same on
all OS variants.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 46 +++++++++++++++++++++++++++++
 lib/eal/common/eal_options.h        |  1 +
 lib/eal/freebsd/eal.c               | 42 --------------------------
 lib/eal/linux/eal.c                 | 39 ------------------------
 lib/eal/windows/eal.c               | 35 ----------------------
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index e541f0793964..7310d10dfd78 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -1640,6 +1640,51 @@ eal_parse_huge_unlink(const char *arg, struct hugepage_file_discipline *out)
 	return -1;
 }
 
+/* Parse the all arguments looking for --log-level */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_parse_common_option(int opt, const char *optarg,
 			struct internal_config *conf)
@@ -2173,6 +2218,7 @@ rte_vect_set_max_simd_bitwidth(uint16_t bitwidth)
 	return 0;
 }
 
+
 void
 eal_common_usage(void)
 {
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..f3f2e104f6d7 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -96,6 +96,7 @@ enum {
 extern const char eal_short_options[];
 extern const struct option eal_long_options[];
 
+int eal_log_level_parse(int argc, char * const argv[]);
 int eal_parse_common_option(int opt, const char *argv,
 			    struct internal_config *conf);
 int eal_option_device_parse(void);
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index a57ee8406f0c..94927472edfe 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -364,48 +364,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index fd422f1f6236..bffeb1f34eb9 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 52f0e7462dd8..85171b27687e 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v9 3/5] eal: initialize logging before plugins
  2024-03-18 22:02 ` [PATCH v9 0/5] Logging unification and timestamp Stephen Hemminger
  2024-03-18 22:02   ` [PATCH v9 1/5] log: unify logging code Stephen Hemminger
  2024-03-18 22:02   ` [PATCH v9 2/5] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-18 22:02   ` Stephen Hemminger
  2024-03-18 22:03   ` [PATCH v9 4/5] eal: allow user to set default log stream before init Stephen Hemminger
  2024-03-18 22:03   ` [PATCH v9 5/5] eal: add option to put timestamp on console output Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 22:02 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Want to make sure that as many log messages as possible
get added with the real log stream.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c       | 14 +++++++-------
 lib/eal/linux/eal.c         | 16 ++++++++--------
 lib/eal/unix/eal_unix_log.c |  0
 3 files changed, 15 insertions(+), 15 deletions(-)
 create mode 100644 lib/eal/unix/eal_unix_log.c

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 94927472edfe..6f0080c4d8c6 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -593,6 +593,13 @@ rte_eal_init(int argc, char **argv)
 		internal_conf->in_memory = false;
 	}
 
+	if (eal_log_init(getprogname(), internal_conf->syslog_facility) < 0) {
+		rte_eal_init_alert("Cannot init logging.");
+		rte_errno = ENOMEM;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		return -1;
+	}
+
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
@@ -719,13 +726,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;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		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 bffeb1f34eb9..e24f24b1b0ce 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -973,6 +973,14 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
+	if (eal_log_init(program_invocation_short_name,
+			 internal_conf->syslog_facility) < 0) {
+		rte_eal_init_alert("Cannot init logging.");
+		rte_errno = ENOMEM;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		return -1;
+	}
+
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
@@ -1107,14 +1115,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;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
diff --git a/lib/eal/unix/eal_unix_log.c b/lib/eal/unix/eal_unix_log.c
new file mode 100644
index 000000000000..e69de29bb2d1
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v9 4/5] eal: allow user to set default log stream before init
  2024-03-18 22:02 ` [PATCH v9 0/5] Logging unification and timestamp Stephen Hemminger
                     ` (2 preceding siblings ...)
  2024-03-18 22:02   ` [PATCH v9 3/5] eal: initialize logging before plugins Stephen Hemminger
@ 2024-03-18 22:03   ` Stephen Hemminger
  2024-03-18 22:03   ` [PATCH v9 5/5] eal: add option to put timestamp on console output Stephen Hemminger
  4 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 22:03 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

It is useful for application to be able to set the default log
stream before call rte_eal_init(). This makes all messages go
to the new default.

For example, to skip using syslog; just doing
	rte_openlog_stream(stderr);

There is no reason for helper command line applications to clutter
syslog with messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/dumpcap/main.c     | 3 +++
 app/pdump/main.c       | 3 +++
 app/proc-info/main.c   | 3 +++
 lib/log/log.c          | 6 ++++++
 lib/log/log_internal.h | 2 ++
 lib/log/log_unix.c     | 4 ++++
 lib/log/version.map    | 1 +
 7 files changed, 22 insertions(+)

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index cc0f66b2bc61..27934ca7e688 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -633,6 +633,9 @@ static void dpdk_init(void)
 			rte_panic("No memory\n");
 	}
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	if (rte_eal_init(eal_argc, eal_argv) < 0)
 		rte_exit(EXIT_FAILURE, "EAL init failed: is primary process running?\n");
 }
diff --git a/app/pdump/main.c b/app/pdump/main.c
index a9205e130bb1..7b9ba68b1a14 100644
--- a/app/pdump/main.c
+++ b/app/pdump/main.c
@@ -995,6 +995,9 @@ main(int argc, char **argv)
 
 	argc += 2;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	diag = rte_eal_init(argc, argp);
 	if (diag < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/app/proc-info/main.c b/app/proc-info/main.c
index b672aaefbe99..24ee52c4ac7a 100644
--- a/app/proc-info/main.c
+++ b/app/proc-info/main.c
@@ -2149,6 +2149,9 @@ main(int argc, char **argv)
 
 	argc += 4;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	ret = rte_eal_init(argc, argp);
 	if (ret < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/lib/log/log.c b/lib/log/log.c
index 255f757d94cc..4cc944305057 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -519,6 +519,12 @@ eal_log_set_default(FILE *default_log)
 #endif
 }
 
+FILE *
+eal_log_get_default(void)
+{
+	return default_log_stream;
+}
+
 /*
  * Called by eal_cleanup
  */
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 451629f1c1ba..c77e687e28bc 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -21,6 +21,8 @@ int eal_log_init(const char *id, int facility);
  */
 __rte_internal
 void eal_log_set_default(FILE *default_log);
+__rte_internal
+FILE *eal_log_get_default(void);
 
 /*
  * Save a log option for later.
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index 2dfb0c974b1d..a415bae5774d 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -49,6 +49,10 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	/* skip if user has already setup a log stream */
+	if (eal_log_get_default())
+		return 0;
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
diff --git a/lib/log/version.map b/lib/log/version.map
index 0648f8831aff..6ecc656d1d65 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_get_default;
 	eal_log_init;
 	eal_log_level2str;
 	eal_log_save_pattern;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-18 22:02 ` [PATCH v9 0/5] Logging unification and timestamp Stephen Hemminger
                     ` (3 preceding siblings ...)
  2024-03-18 22:03   ` [PATCH v9 4/5] eal: allow user to set default log stream before init Stephen Hemminger
@ 2024-03-18 22:03   ` Stephen Hemminger
  2024-03-19  7:37     ` Morten Brørup
  4 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-18 22:03 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing. The timestamp format is chosen to look
like the default Linux dmesg timestamp.

The first few lines are not timestamped because the flag is stored
in internal configuration which is stored in shared memory
which is not setup up until a little later in startup process.

This logging skips the unnecessary step of going through stdio,
which makes it more robust against being called in interrupt
handlers etc.

Example:
$ dpdk-testpmd --log-timestamp -- -i
EAL: Detected CPU lcores: 16
EAL: Detected NUMA nodes: 1
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
[       0.112264] testpmd: No probed ethernet devices
Interactive-mode selected
[       0.184573] testpmd: create a new mbuf pool <mb_pool_0>: n=163456, size=2176, socket=0
[       0.184612] testpmd: preferred mempool ops selected: ring_mp_mc

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  9 ++++++++
 doc/guides/prog_guide/log_lib.rst   | 10 +++++++++
 lib/eal/common/eal_common_options.c | 10 +++++++--
 lib/eal/common/eal_options.h        |  2 ++
 lib/log/log_internal.h              |  9 ++++++++
 lib/log/log_unix.c                  | 32 +++++++++++++++++++++++++++--
 lib/log/log_windows.c               |  6 ++++++
 lib/log/version.map                 |  1 +
 8 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 6cb4b0675730..07a038fb6051 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1055,6 +1055,10 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1162,6 +1166,11 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index aacb36c36ce0..1d6b2e3cea5d 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -73,6 +73,16 @@ For example::
 	/path/to/app --syslog local0
 
 
+Console timestamp
+~~~~~~~~~~~~~~~~~
+
+On Linux and FreeBSD, an optional timestamp can be added before each
+message by adding the ``--log-timestamp`` option.
+For example::
+
+	/path/to/app --log-level=lib.*:debug --log-timestamp
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 7310d10dfd78..9bc95433d27c 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -77,6 +77,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     0, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1663,6 +1664,7 @@ eal_log_level_parse(int argc, char * const argv[])
 
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_LOG_TIMESTAMP_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1890,7 +1892,7 @@ eal_parse_common_option(int opt, const char *optarg,
 		break;
 #endif
 
-	case OPT_LOG_LEVEL_NUM: {
+	case OPT_LOG_LEVEL_NUM:
 		if (eal_parse_log_level(optarg) < 0) {
 			EAL_LOG(ERR,
 				"invalid parameters for --"
@@ -1898,7 +1900,10 @@ eal_parse_common_option(int opt, const char *optarg,
 			return -1;
 		}
 		break;
-	}
+
+	case OPT_LOG_TIMESTAMP_NUM:
+		eal_log_enable_timestamp();
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -2261,6 +2266,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index f3f2e104f6d7..e24c9eca53ca 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index c77e687e28bc..1af27fe8f9d3 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -5,8 +5,10 @@
 #ifndef LOG_INTERNAL_H
 #define LOG_INTERNAL_H
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <time.h>
 
 #include <rte_compat.h>
 
@@ -44,4 +46,11 @@ const char *eal_log_level2str(uint32_t level);
 __rte_internal
 void rte_eal_log_cleanup(void);
 
+/*
+ * Add timestamp to console logs
+ */
+__rte_internal
+void eal_log_enable_timestamp(void);
+
+
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index a415bae5774d..71ce366b6825 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -2,24 +2,52 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
 #include "log_internal.h"
 
+static bool timestamp_enabled;
+static struct timespec log_started;
+
+void
+eal_log_enable_timestamp(void)
+{
+	timestamp_enabled = true;
+	clock_gettime(CLOCK_MONOTONIC, &log_started);
+}
+
 /*
  * default log function
  */
 static ssize_t
 console_log_write(__rte_unused void *c, const char *buf, size_t size)
 {
+	struct timespec ts;
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
+	if (timestamp_enabled) {
+		clock_gettime(CLOCK_MONOTONIC, &ts);
+		ts.tv_sec -= log_started.tv_sec;
+		ts.tv_nsec -= log_started.tv_nsec;
+		if (ts.tv_nsec < 0) {
+			--ts.tv_sec;
+			ts.tv_nsec += 1000000000ul;
+		}
+
+		ret = fprintf(stderr, "[%8lu.%06lu] %.*s",
+			      ts.tv_sec, ts.tv_nsec / 1000u,
+			      (int) size, buf);
+	} else {
+		ret = fwrite(buf, 1, size, stderr);
+	}
 	fflush(stderr);
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a6a08895501e..0c471bfb15f6 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -6,6 +6,12 @@
 #include <rte_log.h>
 #include "log_internal.h"
 
+void
+eal_log_enable_timestamp(void)
+{
+	/* not implemented */
+}
+
 /* set the log to default function, called during eal init process. */
 int
 eal_log_init(__rte_unused const char *id, __rte_unused int facility)
diff --git a/lib/log/version.map b/lib/log/version.map
index 6ecc656d1d65..32c555f29e79 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_enable_timestamp;
 	eal_log_get_default;
 	eal_log_init;
 	eal_log_level2str;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-18 22:03   ` [PATCH v9 5/5] eal: add option to put timestamp on console output Stephen Hemminger
@ 2024-03-19  7:37     ` Morten Brørup
  2024-03-19 15:51       ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Morten Brørup @ 2024-03-19  7:37 UTC (permalink / raw)
  To: Stephen Hemminger, dev

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Monday, 18 March 2024 23.03
> 
> When debugging driver or startup issues, it is useful to have
> a timestamp on each message printed. The messages in syslog
> already have a timestamp, but often syslog is not available
> during testing. The timestamp format is chosen to look
> like the default Linux dmesg timestamp.
> 
> The first few lines are not timestamped because the flag is stored
> in internal configuration which is stored in shared memory
> which is not setup up until a little later in startup process.
> 
> This logging skips the unnecessary step of going through stdio,
> which makes it more robust against being called in interrupt
> handlers etc.
> 
> Example:
> $ dpdk-testpmd --log-timestamp -- -i
> EAL: Detected CPU lcores: 16
> EAL: Detected NUMA nodes: 1
> EAL: Detected static linkage of DPDK
> EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
> EAL: Selected IOVA mode 'VA'
> [       0.112264] testpmd: No probed ethernet devices
> Interactive-mode selected
> [       0.184573] testpmd: create a new mbuf pool <mb_pool_0>: n=163456,
> size=2176, socket=0
> [       0.184612] testpmd: preferred mempool ops selected: ring_mp_mc
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---

[...]

>  static ssize_t
>  console_log_write(__rte_unused void *c, const char *buf, size_t size)
>  {
> +	struct timespec ts;
>  	ssize_t ret;
> 
> -	/* write on stderr */
> -	ret = fwrite(buf, 1, size, stderr);
> +	if (timestamp_enabled) {
> +		clock_gettime(CLOCK_MONOTONIC, &ts);
> +		ts.tv_sec -= log_started.tv_sec;
> +		ts.tv_nsec -= log_started.tv_nsec;

Please log the absolute CLOCK_MONOTONIC instead of subtracting log_started, so timestamps can be easily compared with timestamps from other processes.

> +		if (ts.tv_nsec < 0) {
> +			--ts.tv_sec;
> +			ts.tv_nsec += 1000000000ul;
> +		}
> +
> +		ret = fprintf(stderr, "[%8lu.%06lu] %.*s",
> +			      ts.tv_sec, ts.tv_nsec / 1000u,
> +			      (int) size, buf);

With the above change,
For the series,
Acked-by: Morten Brørup <mb@smartsharesystems.com>


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-19  7:37     ` Morten Brørup
@ 2024-03-19 15:51       ` Stephen Hemminger
  2024-03-19 16:13         ` Morten Brørup
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-19 15:51 UTC (permalink / raw)
  To: Morten Brørup; +Cc: dev

On Tue, 19 Mar 2024 08:37:30 +0100
Morten Brørup <mb@smartsharesystems.com> wrote:

> >  static ssize_t
> >  console_log_write(__rte_unused void *c, const char *buf, size_t size)
> >  {
> > +	struct timespec ts;
> >  	ssize_t ret;
> > 
> > -	/* write on stderr */
> > -	ret = fwrite(buf, 1, size, stderr);
> > +	if (timestamp_enabled) {
> > +		clock_gettime(CLOCK_MONOTONIC, &ts);
> > +		ts.tv_sec -= log_started.tv_sec;
> > +		ts.tv_nsec -= log_started.tv_nsec;  
> 
> Please log the absolute CLOCK_MONOTONIC instead of subtracting log_started, so timestamps can be easily compared with timestamps from other processes.


No, was trying to do what kernel dmesg does.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-19 15:51       ` Stephen Hemminger
@ 2024-03-19 16:13         ` Morten Brørup
  2024-03-20  3:33           ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Morten Brørup @ 2024-03-19 16:13 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Tuesday, 19 March 2024 16.52
> 
> On Tue, 19 Mar 2024 08:37:30 +0100
> Morten Brørup <mb@smartsharesystems.com> wrote:
> 
> > >  static ssize_t
> > >  console_log_write(__rte_unused void *c, const char *buf, size_t
> size)
> > >  {
> > > +	struct timespec ts;
> > >  	ssize_t ret;
> > >
> > > -	/* write on stderr */
> > > -	ret = fwrite(buf, 1, size, stderr);
> > > +	if (timestamp_enabled) {
> > > +		clock_gettime(CLOCK_MONOTONIC, &ts);
> > > +		ts.tv_sec -= log_started.tv_sec;
> > > +		ts.tv_nsec -= log_started.tv_nsec;
> >
> > Please log the absolute CLOCK_MONOTONIC instead of subtracting
> log_started, so timestamps can be easily compared with timestamps from
> other processes.
> 
> 
> No, was trying to do what kernel dmesg does.

What do you mean? Doesn't the kernel output CLOCK_MONOTONIC timestamps (without offset)?

And by "timestamps from other processes" I also mean timestamps in log messages from the kernel itself.


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-19 16:13         ` Morten Brørup
@ 2024-03-20  3:33           ` Stephen Hemminger
  2024-03-20  8:34             ` Morten Brørup
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-20  3:33 UTC (permalink / raw)
  To: Morten Brørup; +Cc: dev

On Tue, 19 Mar 2024 17:13:35 +0100
Morten Brørup <mb@smartsharesystems.com> wrote:

> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Tuesday, 19 March 2024 16.52
> > 
> > On Tue, 19 Mar 2024 08:37:30 +0100
> > Morten Brørup <mb@smartsharesystems.com> wrote:
> >   
> > > >  static ssize_t
> > > >  console_log_write(__rte_unused void *c, const char *buf, size_t  
> > size)  
> > > >  {
> > > > +	struct timespec ts;
> > > >  	ssize_t ret;
> > > >
> > > > -	/* write on stderr */
> > > > -	ret = fwrite(buf, 1, size, stderr);
> > > > +	if (timestamp_enabled) {
> > > > +		clock_gettime(CLOCK_MONOTONIC, &ts);
> > > > +		ts.tv_sec -= log_started.tv_sec;
> > > > +		ts.tv_nsec -= log_started.tv_nsec;  
> > >
> > > Please log the absolute CLOCK_MONOTONIC instead of subtracting  
> > log_started, so timestamps can be easily compared with timestamps from
> > other processes.
> > 
> > 
> > No, was trying to do what kernel dmesg does.  
> 
> What do you mean? Doesn't the kernel output CLOCK_MONOTONIC timestamps (without offset)?
> 
> And by "timestamps from other processes" I also mean timestamps in log messages from the kernel itself.
> 

If you look at dmesg command that formats the messages, it has lots of timestamp options.
Next version will support more of these.

       --time-format format
           Print timestamps using the given format, which can be ctime,
           reltime, delta or iso. The first three formats are aliases of
           the time-format-specific options. The iso format is a dmesg
           implementation of the ISO-8601 timestamp format. The purpose
           of this format is to make the comparing of timestamps between
           two systems, and any other parsing, easy. The definition of
           the iso timestamp is:
           YYYY-MM-DD<T>HH:MM:SS,<microseconds>←+><timezone offset from
           UTC>.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-20  3:33           ` Stephen Hemminger
@ 2024-03-20  8:34             ` Morten Brørup
  2024-03-20 14:38               ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Morten Brørup @ 2024-03-20  8:34 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Wednesday, 20 March 2024 04.34
> 
> On Tue, 19 Mar 2024 17:13:35 +0100
> Morten Brørup <mb@smartsharesystems.com> wrote:
> 
> > > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > > Sent: Tuesday, 19 March 2024 16.52
> > >
> > > On Tue, 19 Mar 2024 08:37:30 +0100
> > > Morten Brørup <mb@smartsharesystems.com> wrote:
> > >
> > > > >  static ssize_t
> > > > >  console_log_write(__rte_unused void *c, const char *buf, size_t
> > > size)
> > > > >  {
> > > > > +	struct timespec ts;
> > > > >  	ssize_t ret;
> > > > >
> > > > > -	/* write on stderr */
> > > > > -	ret = fwrite(buf, 1, size, stderr);
> > > > > +	if (timestamp_enabled) {
> > > > > +		clock_gettime(CLOCK_MONOTONIC, &ts);
> > > > > +		ts.tv_sec -= log_started.tv_sec;
> > > > > +		ts.tv_nsec -= log_started.tv_nsec;
> > > >
> > > > Please log the absolute CLOCK_MONOTONIC instead of subtracting
> > > log_started, so timestamps can be easily compared with timestamps from
> > > other processes.
> > >
> > >
> > > No, was trying to do what kernel dmesg does.
> >
> > What do you mean? Doesn't the kernel output CLOCK_MONOTONIC timestamps
> (without offset)?
> >
> > And by "timestamps from other processes" I also mean timestamps in log
> messages from the kernel itself.
> >
> 
> If you look at dmesg command that formats the messages, it has lots of
> timestamp options.
> Next version will support more of these.
> 
>        --time-format format
>            Print timestamps using the given format, which can be ctime,
>            reltime, delta or iso. The first three formats are aliases of
>            the time-format-specific options. The iso format is a dmesg
>            implementation of the ISO-8601 timestamp format. The purpose
>            of this format is to make the comparing of timestamps between
>            two systems, and any other parsing, easy. The definition of
>            the iso timestamp is:
>            YYYY-MM-DD<T>HH:MM:SS,<microseconds>←+><timezone offset from
>            UTC>.

That's formatting.

I'm talking about the initial offset.

The kernel's "log start time" is set to system start timestamp.

Your "log start time" is set to application start timestamp:

+void
+eal_log_enable_timestamp(void)
+{
+	timestamp_enabled = true;
+	clock_gettime(CLOCK_MONOTONIC, &log_started);
+}

So, different offset. I prefer using the same offset.


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-20  8:34             ` Morten Brørup
@ 2024-03-20 14:38               ` Stephen Hemminger
  2024-03-20 17:38                 ` Morten Brørup
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-20 14:38 UTC (permalink / raw)
  To: Morten Brørup; +Cc: dev

On Wed, 20 Mar 2024 09:34:21 +0100
Morten Brørup <mb@smartsharesystems.com> wrote:

> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Wednesday, 20 March 2024 04.34
> > 
> > On Tue, 19 Mar 2024 17:13:35 +0100
> > Morten Brørup <mb@smartsharesystems.com> wrote:
> >   
> > > > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > > > Sent: Tuesday, 19 March 2024 16.52
> > > >
> > > > On Tue, 19 Mar 2024 08:37:30 +0100
> > > > Morten Brørup <mb@smartsharesystems.com> wrote:
> > > >  
> > > > > >  static ssize_t
> > > > > >  console_log_write(__rte_unused void *c, const char *buf, size_t  
> > > > size)  
> > > > > >  {
> > > > > > +	struct timespec ts;
> > > > > >  	ssize_t ret;
> > > > > >
> > > > > > -	/* write on stderr */
> > > > > > -	ret = fwrite(buf, 1, size, stderr);
> > > > > > +	if (timestamp_enabled) {
> > > > > > +		clock_gettime(CLOCK_MONOTONIC, &ts);
> > > > > > +		ts.tv_sec -= log_started.tv_sec;
> > > > > > +		ts.tv_nsec -= log_started.tv_nsec;  
> > > > >
> > > > > Please log the absolute CLOCK_MONOTONIC instead of subtracting  
> > > > log_started, so timestamps can be easily compared with timestamps from
> > > > other processes.
> > > >
> > > >
> > > > No, was trying to do what kernel dmesg does.  
> > >
> > > What do you mean? Doesn't the kernel output CLOCK_MONOTONIC timestamps  
> > (without offset)?  
> > >
> > > And by "timestamps from other processes" I also mean timestamps in log  
> > messages from the kernel itself.  
> > >  
> > 
> > If you look at dmesg command that formats the messages, it has lots of
> > timestamp options.
> > Next version will support more of these.
> > 
> >        --time-format format
> >            Print timestamps using the given format, which can be ctime,
> >            reltime, delta or iso. The first three formats are aliases of
> >            the time-format-specific options. The iso format is a dmesg
> >            implementation of the ISO-8601 timestamp format. The purpose
> >            of this format is to make the comparing of timestamps between
> >            two systems, and any other parsing, easy. The definition of
> >            the iso timestamp is:
> >            YYYY-MM-DD<T>HH:MM:SS,<microseconds>←+><timezone offset from  
> >            UTC>.  
> 
> That's formatting.
> 
> I'm talking about the initial offset.
> 
> The kernel's "log start time" is set to system start timestamp.
> 
> Your "log start time" is set to application start timestamp:
> 
> +void
> +eal_log_enable_timestamp(void)
> +{
> +	timestamp_enabled = true;
> +	clock_gettime(CLOCK_MONOTONIC, &log_started);
> +}
> 
> So, different offset. I prefer using the same offset.


Monotonic has no absolute zero time and can be quite large so offsetting
seemed most logical. Also, the kernel does not make boot time monotonic
clock value available.  Other options are REALTIME or BOOTTIME.
But both of these get quite large.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v9 5/5] eal: add option to put timestamp on console output
  2024-03-20 14:38               ` Stephen Hemminger
@ 2024-03-20 17:38                 ` Morten Brørup
  0 siblings, 0 replies; 277+ messages in thread
From: Morten Brørup @ 2024-03-20 17:38 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Wednesday, 20 March 2024 15.39
> 
> On Wed, 20 Mar 2024 09:34:21 +0100
> Morten Brørup <mb@smartsharesystems.com> wrote:
> 
> > > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > > Sent: Wednesday, 20 March 2024 04.34
> > >
> > > On Tue, 19 Mar 2024 17:13:35 +0100
> > > Morten Brørup <mb@smartsharesystems.com> wrote:
> > >
> > > > > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > > > > Sent: Tuesday, 19 March 2024 16.52
> > > > >
> > > > > On Tue, 19 Mar 2024 08:37:30 +0100
> > > > > Morten Brørup <mb@smartsharesystems.com> wrote:
> > > > >
> > > > > > >  static ssize_t
> > > > > > >  console_log_write(__rte_unused void *c, const char *buf,
> size_t
> > > > > size)
> > > > > > >  {
> > > > > > > +	struct timespec ts;
> > > > > > >  	ssize_t ret;
> > > > > > >
> > > > > > > -	/* write on stderr */
> > > > > > > -	ret = fwrite(buf, 1, size, stderr);
> > > > > > > +	if (timestamp_enabled) {
> > > > > > > +		clock_gettime(CLOCK_MONOTONIC, &ts);
> > > > > > > +		ts.tv_sec -= log_started.tv_sec;
> > > > > > > +		ts.tv_nsec -= log_started.tv_nsec;
> > > > > >
> > > > > > Please log the absolute CLOCK_MONOTONIC instead of subtracting
> > > > > log_started, so timestamps can be easily compared with
> timestamps from
> > > > > other processes.
> > > > >
> > > > >
> > > > > No, was trying to do what kernel dmesg does.
> > > >
> > > > What do you mean? Doesn't the kernel output CLOCK_MONOTONIC
> timestamps
> > > (without offset)?
> > > >
> > > > And by "timestamps from other processes" I also mean timestamps in
> log
> > > messages from the kernel itself.
> > > >
> > >
> > > If you look at dmesg command that formats the messages, it has lots
> of
> > > timestamp options.
> > > Next version will support more of these.
> > >
> > >        --time-format format
> > >            Print timestamps using the given format, which can be
> ctime,
> > >            reltime, delta or iso. The first three formats are
> aliases of
> > >            the time-format-specific options. The iso format is a
> dmesg
> > >            implementation of the ISO-8601 timestamp format. The
> purpose
> > >            of this format is to make the comparing of timestamps
> between
> > >            two systems, and any other parsing, easy. The definition
> of
> > >            the iso timestamp is:
> > >            YYYY-MM-DD<T>HH:MM:SS,<microseconds>←+><timezone offset
> from
> > >            UTC>.
> >
> > That's formatting.
> >
> > I'm talking about the initial offset.
> >
> > The kernel's "log start time" is set to system start timestamp.
> >
> > Your "log start time" is set to application start timestamp:
> >
> > +void
> > +eal_log_enable_timestamp(void)
> > +{
> > +	timestamp_enabled = true;
> > +	clock_gettime(CLOCK_MONOTONIC, &log_started);
> > +}
> >
> > So, different offset. I prefer using the same offset.
> 
> 
> Monotonic has no absolute zero time and can be quite large so offsetting
> seemed most logical. Also, the kernel does not make boot time monotonic
> clock value available.  Other options are REALTIME or BOOTTIME.
> But both of these get quite large.

I don't think that having a large tv_sec is a problem.
I think the ability to correlate log timestamps with other applications is more important.
If we choose one of the standard clocks, applications might choose the same clock. If we subtract the start time of this specific application, it's not in sync with anything else.

The question really is: Which clock is most popular for debug logging to the console? We should use that clock.


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 00/10] Logging enhancements.
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (8 preceding siblings ...)
  2024-03-18 22:02 ` [PATCH v9 0/5] Logging unification and timestamp Stephen Hemminger
@ 2024-03-21 16:00 ` Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 01/10] windows: make getopt functions have const properties Stephen Hemminger
                     ` (9 more replies)
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                   ` (10 subsequent siblings)
  20 siblings, 10 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Improvements and unification of logging library (for 24.07 release).
This is update to earlier patch set. It adds two new features
to the console log output. 

  * Putting a timestamp on console output which is useful for
    analyzing performance of startup codes. Timestamp is optional
    and must be enabled on command line. The message going into
    syslog is already timestamped (by syslog) so no additional
    timestamp is needed.

  * Displaying console output with colors.
    It uses the standard conventions used by many other Linux commands
    for colorized display.  The default is to enable color if the
    console output is going to a terminal. But it can be always
    on or disabled by command line flag. This default was chosen
    based on what dmesg(1) command does.

    I find color helpful because DPDK drivers and libraries print
    lots of not very useful messages. And having error messages
    highlighted in bold face helps. This might also get users to
    pay more attention to error messages. Many bug reports have
    earlier messages that are lost because there are so many
    info messages.

Note: neither of these are possible on Windows because the
fopencookie() function is a GNU libc and not Posix feature.

Will add a release note in next release (after this is merged).

v10 - put windows getopt patch in series since otherwise windows build fails.
    - support multiple timestamp formats
    - support colorized output
    - fix up rte_init_alert() and rte_exit() to match format
      expected by the timestamp and color modes
    - initialize logging much earlier in startup
    - add documentation about logging options

Stephen Hemminger (10):
  windows: make getopt functions have const properties
  log: unify logging code
  eal: make eal_log_level_parse common
  eal: initialize log earlier in startup
  eal: do not duplicate rte_init_alert() messages
  eal: change rte_exit() output to match rte_log()
  eal: allow user to set default log stream before init
  eal: add option to put timestamp on console output
  log: colorize log output
  doc: add documentation of logging options

 app/dumpcap/main.c                            |   3 +
 app/pdump/main.c                              |   3 +
 app/proc-info/main.c                          |   3 +
 app/test/test_eal_flags.c                     |  17 +
 doc/guides/linux_gsg/eal_args.include.rst     |  54 ++-
 doc/guides/linux_gsg/linux_eal_parameters.rst |  27 --
 doc/guides/prog_guide/log_lib.rst             |  28 +-
 lib/eal/common/eal_common_debug.c             |  11 +-
 lib/eal/common/eal_common_options.c           |  70 +++-
 lib/eal/common/eal_options.h                  |   5 +
 lib/eal/freebsd/eal.c                         |  53 +--
 lib/eal/linux/eal.c                           |  58 +---
 lib/eal/unix/eal_unix_log.c                   |   0
 lib/eal/windows/eal.c                         |  35 --
 lib/eal/windows/getopt.c                      |  23 +-
 lib/eal/windows/include/getopt.h              |   8 +-
 lib/log/log.c                                 |   6 +
 lib/log/log_freebsd.c                         |  12 -
 lib/log/log_internal.h                        |  16 +
 lib/log/log_linux.c                           |  61 ----
 lib/log/log_unix.c                            | 321 ++++++++++++++++++
 lib/log/log_windows.c                         |  12 +
 lib/log/meson.build                           |  12 +-
 lib/log/version.map                           |   3 +
 24 files changed, 577 insertions(+), 264 deletions(-)
 create mode 100644 lib/eal/unix/eal_unix_log.c
 delete mode 100644 lib/log/log_freebsd.c
 delete mode 100644 lib/log/log_linux.c
 create mode 100644 lib/log/log_unix.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 01/10] windows: make getopt functions have const properties
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 02/10] log: unify logging code Stephen Hemminger
                     ` (8 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Tyler Retzlaff, Dmitry Kozlyuk

Having different prototypes on different platforms can lead
to lots of unnecessary workarounds.  Looks like the version of
getopt used from windows was based on an older out of date
version from FreeBSD.

This patch changes getopt, getopt_long, etc to have the same const
attributes as Linux and FreeBSD. The changes are derived from
the current FreeBSD version of getopt_long.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 lib/eal/windows/getopt.c         | 23 ++++++++++++-----------
 lib/eal/windows/include/getopt.h |  8 ++++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lib/eal/windows/getopt.c b/lib/eal/windows/getopt.c
index a1f51c6c2318..50ff71b9300d 100644
--- a/lib/eal/windows/getopt.c
+++ b/lib/eal/windows/getopt.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-const char    *optarg;		/* argument associated with option */
+char    *optarg;		/* argument associated with option */
 int	opterr = 1;		/* if error message should be printed */
 int	optind = 1;		/* index into parent argv vector */
 int	optopt = '?';		/* character checked for validity */
@@ -39,9 +39,9 @@ static void pass(const char *a) {(void) a; }
 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
 #define	INORDER		1
 
-#define	EMSG		""
+static char EMSG[] = "";
 
-static const char *place = EMSG; /* option letter processing */
+static char *place = EMSG; /* option letter processing */
 
 /* XXX: set optreset to 1 rather than these two */
 static int nonopt_start = -1; /* first non option argument (for permute) */
@@ -80,7 +80,7 @@ gcd(int a, int b)
  */
 static void
 permute_args(int panonopt_start, int panonopt_end, int opt_end,
-	char **nargv)
+	char * const *nargv)
 {
 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
 	char *swap;
@@ -101,11 +101,12 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
 				pos -= nnonopts;
 			else
 				pos += nopts;
+
 			swap = nargv[pos];
 			/* LINTED const cast */
-			((char **) nargv)[pos] = nargv[cstart];
+			((char **)(uintptr_t)nargv)[pos] = nargv[cstart];
 			/* LINTED const cast */
-			((char **)nargv)[cstart] = swap;
+			((char **)(uintptr_t)nargv)[cstart] = swap;
 		}
 	}
 }
@@ -116,7 +117,7 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
  * Returns -1 if short_too is set and the option does not match long_options.
  */
 static int
-parse_long_options(char **nargv, const char *options,
+parse_long_options(char * const *nargv, const char *options,
 	const struct option *long_options, int *idx, int short_too)
 {
 	const char *current_argv;
@@ -236,7 +237,7 @@ parse_long_options(char **nargv, const char *options,
  *	Parse argc/argv argument vector.  Called by user level routines.
  */
 static int
-getopt_internal(int nargc, char **nargv, const char *options,
+getopt_internal(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx, int flags)
 {
 	char *oli;				/* option letter list index */
@@ -434,7 +435,7 @@ getopt_internal(int nargc, char **nargv, const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt(int nargc, char *nargv[], const char *options)
+getopt(int nargc, char *const nargv[], const char *options)
 {
 	return getopt_internal(nargc, nargv, options, NULL, NULL,
 			       FLAG_PERMUTE);
@@ -445,7 +446,7 @@ getopt(int nargc, char *nargv[], const char *options)
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long(int nargc, char *nargv[], const char *options,
+getopt_long(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
@@ -458,7 +459,7 @@ getopt_long(int nargc, char *nargv[], const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long_only(int nargc, char *nargv[], const char *options,
+getopt_long_only(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
diff --git a/lib/eal/windows/include/getopt.h b/lib/eal/windows/include/getopt.h
index 6f57af454b17..e4cf6873cb0c 100644
--- a/lib/eal/windows/include/getopt.h
+++ b/lib/eal/windows/include/getopt.h
@@ -44,7 +44,7 @@
 
 
 /** argument to current option, or NULL if it has none */
-extern const char *optarg;
+extern char *optarg;
 /** Current position in arg string.  Starts from 1.
  * Setting to 0 resets state.
  */
@@ -80,14 +80,14 @@ struct option {
 };
 
 /** Compat: getopt */
-int getopt(int argc, char *argv[], const char *options);
+int getopt(int argc, char *const argv[], const char *options);
 
 /** Compat: getopt_long */
-int getopt_long(int argc, char *argv[], const char *options,
+int getopt_long(int argc, char *const argv[], const char *options,
 		const struct option *longopts, int *longindex);
 
 /** Compat: getopt_long_only */
-int getopt_long_only(int nargc, char *argv[], const char *options,
+int getopt_long_only(int nargc, char *const argv[], const char *options,
 		     const struct option *long_options, int *idx);
 
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 02/10] log: unify logging code
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 01/10] windows: make getopt functions have const properties Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 03/10] eal: make eal_log_level_parse common Stephen Hemminger
                     ` (7 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

FreeBSD and Linux logging code can use common code. This also
fixes FreeBSD not using syslog.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/linux_gsg/linux_eal_parameters.rst | 27 -------------------
 doc/guides/prog_guide/log_lib.rst             | 18 +++++++++++--
 lib/eal/freebsd/eal.c                         |  8 ++++++
 lib/log/log_freebsd.c                         | 12 ---------
 lib/log/{log_linux.c => log_unix.c}           |  0
 lib/log/meson.build                           | 12 ++++++---
 6 files changed, 32 insertions(+), 45 deletions(-)
 delete mode 100644 lib/log/log_freebsd.c
 rename lib/log/{log_linux.c => log_unix.c} (100%)

diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f38139119..d86f94d8a85d 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -108,30 +108,3 @@ Memory-related options
 *   ``--match-allocations``
 
     Free hugepages back to system exactly as they were originally allocated.
-
-Other options
-~~~~~~~~~~~~~
-
-*   ``--syslog <syslog facility>``
-
-    Set syslog facility. Valid syslog facilities are::
-
-        auth
-        cron
-        daemon
-        ftp
-        kern
-        lpr
-        mail
-        news
-        syslog
-        user
-        uucp
-        local0
-        local1
-        local2
-        local3
-        local4
-        local5
-        local6
-        local7
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index ff9d1b54a2c8..aacb36c36ce0 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -5,8 +5,8 @@ Log Library
 ===========
 
 The DPDK Log library provides the logging functionality for other DPDK libraries and drivers.
-By default, in a Linux application, logs are sent to syslog and also to the console.
-On FreeBSD and Windows applications, logs are sent only to the console.
+By default, in a Linux (or FreeBSD) application, logs are sent to syslog and also to the console.
+In Windows applications, logs are sent only to the console.
 However, the log function can be overridden by the user to use a different logging mechanism.
 
 Log Levels
@@ -29,6 +29,7 @@ will be emitted by the application to the log output.
 That level can be configured either by the application calling the relevant APIs from the logging library,
 or by the user passing the ``--log-level`` parameter to the EAL via the application.
 
+
 Setting Global Log Level
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -59,6 +60,19 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+
+Setting syslog facility
+~~~~~~~~~~~~~~~~~~~~~~~
+
+On Linux and FreeBSD, where syslog is used a ``facility`` argument can be
+used to specify what type of program is logging.
+The default facility is ``daemon`` but it can be overridden
+by the ``--syslog`` EAL parameter. See ``syslog.3`` man page for full values.
+For example::
+
+	/path/to/app --syslog local0
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bab77118e967..a57ee8406f0c 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -53,6 +53,7 @@
 #include "eal_options.h"
 #include "eal_memcfg.h"
 #include "eal_trace.h"
+#include "log_internal.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
 
@@ -760,6 +761,13 @@ 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;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		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/log/log_freebsd.c b/lib/log/log_freebsd.c
deleted file mode 100644
index 698d3c542337..000000000000
--- a/lib/log/log_freebsd.c
+++ /dev/null
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2023 Intel Corporation
- */
-
-#include <rte_common.h>
-#include "log_internal.h"
-
-int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
-{
-	return 0;
-}
diff --git a/lib/log/log_linux.c b/lib/log/log_unix.c
similarity index 100%
rename from lib/log/log_linux.c
rename to lib/log/log_unix.c
diff --git a/lib/log/meson.build b/lib/log/meson.build
index 0d4319b36f77..60516a0b2a2d 100644
--- a/lib/log/meson.build
+++ b/lib/log/meson.build
@@ -2,8 +2,12 @@
 # Copyright(c) 2023 Intel Corporation
 
 includes += global_inc
-sources = files(
-        'log.c',
-        'log_' + exec_env + '.c',
-)
+sources = files('log.c')
+
+if is_windows
+    sources += files('log_windows.c')
+else
+    sources += files('log_unix.c')
+endif
+
 headers = files('rte_log.h')
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 03/10] eal: make eal_log_level_parse common
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 01/10] windows: make getopt functions have const properties Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 02/10] log: unify logging code Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 17:00     ` Tyler Retzlaff
  2024-03-21 16:00   ` [PATCH v10 04/10] eal: initialize log earlier in startup Stephen Hemminger
                     ` (6 subsequent siblings)
  9 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The code to parse for log-level option should be same on
all OS variants.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 46 +++++++++++++++++++++++++++++
 lib/eal/common/eal_options.h        |  1 +
 lib/eal/freebsd/eal.c               | 42 --------------------------
 lib/eal/linux/eal.c                 | 39 ------------------------
 lib/eal/windows/eal.c               | 35 ----------------------
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index e541f0793964..7310d10dfd78 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -1640,6 +1640,51 @@ eal_parse_huge_unlink(const char *arg, struct hugepage_file_discipline *out)
 	return -1;
 }
 
+/* Parse the all arguments looking for --log-level */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_parse_common_option(int opt, const char *optarg,
 			struct internal_config *conf)
@@ -2173,6 +2218,7 @@ rte_vect_set_max_simd_bitwidth(uint16_t bitwidth)
 	return 0;
 }
 
+
 void
 eal_common_usage(void)
 {
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb641284..f3f2e104f6d7 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -96,6 +96,7 @@ enum {
 extern const char eal_short_options[];
 extern const struct option eal_long_options[];
 
+int eal_log_level_parse(int argc, char * const argv[]);
 int eal_parse_common_option(int opt, const char *argv,
 			    struct internal_config *conf);
 int eal_option_device_parse(void);
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index a57ee8406f0c..94927472edfe 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -364,48 +364,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index fd422f1f6236..bffeb1f34eb9 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 52f0e7462dd8..85171b27687e 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 04/10] eal: initialize log earlier in startup
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                     ` (2 preceding siblings ...)
  2024-03-21 16:00   ` [PATCH v10 03/10] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 05/10] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
                     ` (5 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Want to make sure that as many log messages as possible
get added with the real log stream. Despite the comment,
there is no dependency on memzones in current code.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c       | 14 +++++++-------
 lib/eal/linux/eal.c         | 16 ++++++++--------
 lib/eal/unix/eal_unix_log.c |  0
 lib/log/log_unix.c          |  4 ++--
 4 files changed, 17 insertions(+), 17 deletions(-)
 create mode 100644 lib/eal/unix/eal_unix_log.c

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 94927472edfe..777770176339 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -571,6 +571,13 @@ 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;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		return -1;
+	}
+
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
@@ -719,13 +726,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;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		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 bffeb1f34eb9..e515f3cbc8a8 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -959,6 +959,14 @@ rte_eal_init(int argc, char **argv)
 	/* clone argv to report out later in telemetry */
 	eal_save_args(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;
+		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
+		return -1;
+	}
+
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
@@ -1107,14 +1115,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;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
diff --git a/lib/eal/unix/eal_unix_log.c b/lib/eal/unix/eal_unix_log.c
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index 2dfb0c974b1d..ae38b4bf3a59 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -41,8 +41,8 @@ static cookie_io_functions_t console_log_func = {
 };
 
 /*
- * set the log to default function, called during eal init process,
- * once memzones are available.
+ * set the log to default function, called early in eal init process;
+ * before lcore's and memzones are setup.
  */
 int
 eal_log_init(const char *id, int facility)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 05/10] eal: do not duplicate rte_init_alert() messages
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                     ` (3 preceding siblings ...)
  2024-03-21 16:00   ` [PATCH v10 04/10] eal: initialize log earlier in startup Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 06/10] eal: change rte_exit() output to match rte_log() Stephen Hemminger
                     ` (4 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The message already goes through logging, and does not need
to be printed on stderr. Message level should be ALERT
to match function name.

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

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 777770176339..e3f25f619715 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -530,8 +530,7 @@ rte_eal_iopl_init(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /* Launch threads, called at application init(). */
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index e515f3cbc8a8..461f0c093357 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -840,8 +840,7 @@ static int rte_eal_vfio_setup(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /*
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 06/10] eal: change rte_exit() output to match rte_log()
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                     ` (4 preceding siblings ...)
  2024-03-21 16:00   ` [PATCH v10 05/10] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 17:04     ` Tyler Retzlaff
  2024-03-21 16:00   ` [PATCH v10 07/10] eal: allow user to set default log stream before init Stephen Hemminger
                     ` (3 subsequent siblings)
  9 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The rte_exit() output format confuses the timestamp and coloring
options. Change it to use be a single line with proper prefix.

Before:
[     0.006481] EAL: Error - exiting with code: 1
  Cause: [     0.006489] Cannot init EAL: Permission denied

After:
[     0.006238] EAL: Error - exiting with code: 1
[     0.006250] EAL: Cause: Cannot init EAL: Permission denied

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_debug.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/eal/common/eal_common_debug.c b/lib/eal/common/eal_common_debug.c
index 3e77995896d5..568883830f20 100644
--- a/lib/eal/common/eal_common_debug.c
+++ b/lib/eal/common/eal_common_debug.c
@@ -34,17 +34,18 @@ void
 rte_exit(int exit_code, const char *format, ...)
 {
 	va_list ap;
+	char *msg = NULL;
 
 	if (exit_code != 0)
-		RTE_LOG(CRIT, EAL, "Error - exiting with code: %d\n"
-				"  Cause: ", exit_code);
+		EAL_LOG(CRIT, "Error - exiting with code: %d", exit_code);
 
 	va_start(ap, format);
-	rte_vlog(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, format, ap);
+	vasprintf(&msg, format, ap);
 	va_end(ap);
 
+	rte_log(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, "EAL: Cause: %s", msg);
+
 	if (rte_eal_cleanup() != 0 && rte_errno != EALREADY)
-		EAL_LOG(CRIT,
-			"EAL could not release all resources");
+		EAL_LOG(CRIT, "EAL could not release all resources");
 	exit(exit_code);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 07/10] eal: allow user to set default log stream before init
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                     ` (5 preceding siblings ...)
  2024-03-21 16:00   ` [PATCH v10 06/10] eal: change rte_exit() output to match rte_log() Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 17:07     ` Tyler Retzlaff
  2024-03-21 16:00   ` [PATCH v10 08/10] eal: add option to put timestamp on console output Stephen Hemminger
                     ` (2 subsequent siblings)
  9 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

It is useful for application to be able to set the default log
stream before call rte_eal_init(). This makes all messages go
to the new default.

For example, to skip using syslog; just doing
	rte_openlog_stream(stderr);

There is no reason for helper command line applications to clutter
syslog with messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/dumpcap/main.c     | 3 +++
 app/pdump/main.c       | 3 +++
 app/proc-info/main.c   | 3 +++
 lib/log/log.c          | 6 ++++++
 lib/log/log_internal.h | 2 ++
 lib/log/log_unix.c     | 4 ++++
 lib/log/version.map    | 1 +
 7 files changed, 22 insertions(+)

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index cc0f66b2bc61..27934ca7e688 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -633,6 +633,9 @@ static void dpdk_init(void)
 			rte_panic("No memory\n");
 	}
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	if (rte_eal_init(eal_argc, eal_argv) < 0)
 		rte_exit(EXIT_FAILURE, "EAL init failed: is primary process running?\n");
 }
diff --git a/app/pdump/main.c b/app/pdump/main.c
index a9205e130bb1..7b9ba68b1a14 100644
--- a/app/pdump/main.c
+++ b/app/pdump/main.c
@@ -995,6 +995,9 @@ main(int argc, char **argv)
 
 	argc += 2;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	diag = rte_eal_init(argc, argp);
 	if (diag < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/app/proc-info/main.c b/app/proc-info/main.c
index b672aaefbe99..24ee52c4ac7a 100644
--- a/app/proc-info/main.c
+++ b/app/proc-info/main.c
@@ -2149,6 +2149,9 @@ main(int argc, char **argv)
 
 	argc += 4;
 
+	/* keep any logging away from syslog. */
+	rte_openlog_stream(stderr);
+
 	ret = rte_eal_init(argc, argp);
 	if (ret < 0)
 		rte_panic("Cannot init EAL\n");
diff --git a/lib/log/log.c b/lib/log/log.c
index 255f757d94cc..4cc944305057 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -519,6 +519,12 @@ eal_log_set_default(FILE *default_log)
 #endif
 }
 
+FILE *
+eal_log_get_default(void)
+{
+	return default_log_stream;
+}
+
 /*
  * Called by eal_cleanup
  */
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 451629f1c1ba..c77e687e28bc 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -21,6 +21,8 @@ int eal_log_init(const char *id, int facility);
  */
 __rte_internal
 void eal_log_set_default(FILE *default_log);
+__rte_internal
+FILE *eal_log_get_default(void);
 
 /*
  * Save a log option for later.
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index ae38b4bf3a59..c56f80ac6967 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -49,6 +49,10 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	/* skip if user has already setup a log stream */
+	if (eal_log_get_default())
+		return 0;
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
diff --git a/lib/log/version.map b/lib/log/version.map
index 0648f8831aff..6ecc656d1d65 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_get_default;
 	eal_log_init;
 	eal_log_level2str;
 	eal_log_save_pattern;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 08/10] eal: add option to put timestamp on console output
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                     ` (6 preceding siblings ...)
  2024-03-21 16:00   ` [PATCH v10 07/10] eal: allow user to set default log stream before init Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 17:11     ` Tyler Retzlaff
  2024-03-21 16:00   ` [PATCH v10 09/10] log: colorize log output Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 10/10] doc: add documentation of logging options Stephen Hemminger
  9 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing.

There are multiple timestamp formats similar to Linux dmesg.
The default is time relative since startup. Other alternatives
are delta, ctime, reltime and iso formats.

Example:
$ dpdk-testpmd --log-timestamp -- -i
EAL: Detected CPU lcores: 8
EAL: Detected NUMA nodes: 1
[     0.000083] EAL: Detected static linkage of DPDK
[     0.002000] EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
[     0.003507] EAL: Selected IOVA mode 'VA'
[     0.006272] testpmd: No probed ethernet devices
Interactive-mode selected
[     0.029567] testpmd: create a new mbuf pool <mb_pool_0>: n=203456, size=2176, socket=0
[     0.029588] testpmd: preferred mempool ops selected: ring_mp_mc

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  17 ++++
 doc/guides/prog_guide/log_lib.rst   |  10 ++
 lib/eal/common/eal_common_options.c |  14 ++-
 lib/eal/common/eal_options.h        |   2 +
 lib/log/log_internal.h              |   9 ++
 lib/log/log_unix.c                  | 152 +++++++++++++++++++++++++++-
 lib/log/log_windows.c               |   6 ++
 lib/log/version.map                 |   1 +
 8 files changed, 205 insertions(+), 6 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 6cb4b0675730..eeb1799381d7 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1055,6 +1055,14 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
+	/* Try running with --log-timestamp=iso */
+	const char * const argv24[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp=iso" };
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1162,6 +1170,15 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+	if (launch_proc(argv24) != 0) {
+		printf("Error - process did not run ok with --log-timestamp=iso parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index aacb36c36ce0..1d6b2e3cea5d 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -73,6 +73,16 @@ For example::
 	/path/to/app --syslog local0
 
 
+Console timestamp
+~~~~~~~~~~~~~~~~~
+
+On Linux and FreeBSD, an optional timestamp can be added before each
+message by adding the ``--log-timestamp`` option.
+For example::
+
+	/path/to/app --log-level=lib.*:debug --log-timestamp
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 7310d10dfd78..4e2c3d0f255c 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -77,6 +77,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     2, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1663,6 +1664,7 @@ eal_log_level_parse(int argc, char * const argv[])
 
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_LOG_TIMESTAMP_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1890,7 +1892,7 @@ eal_parse_common_option(int opt, const char *optarg,
 		break;
 #endif
 
-	case OPT_LOG_LEVEL_NUM: {
+	case OPT_LOG_LEVEL_NUM:
 		if (eal_parse_log_level(optarg) < 0) {
 			EAL_LOG(ERR,
 				"invalid parameters for --"
@@ -1898,9 +1900,16 @@ eal_parse_common_option(int opt, const char *optarg,
 			return -1;
 		}
 		break;
-	}
 
 #ifndef RTE_EXEC_ENV_WINDOWS
+	case OPT_LOG_TIMESTAMP_NUM:
+		if (eal_log_timestamp(optarg) < 0) {
+			EAL_LOG(ERR, "invalid parameters for --"
+				OPT_LOG_TIMESTAMP);
+			return -1;
+		}
+		break;
+
 	case OPT_TRACE_NUM: {
 		if (eal_trace_args_save(optarg) < 0) {
 			EAL_LOG(ERR, "invalid parameters for --"
@@ -2261,6 +2270,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index f3f2e104f6d7..e24c9eca53ca 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index c77e687e28bc..0e18d147cf98 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -5,8 +5,10 @@
 #ifndef LOG_INTERNAL_H
 #define LOG_INTERNAL_H
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <time.h>
 
 #include <rte_compat.h>
 
@@ -44,4 +46,11 @@ const char *eal_log_level2str(uint32_t level);
 __rte_internal
 void rte_eal_log_cleanup(void);
 
+/*
+ * Add timestamp to console logs
+ */
+__rte_internal
+int eal_log_timestamp(const char *fmt);
+
+
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index c56f80ac6967..cb8f17bc83ef 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -2,28 +2,169 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
+#include <stdbool.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <syslog.h>
+#include <time.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 
 #include "log_internal.h"
 
+enum eal_log_time_format {
+	EAL_LOG_TIMESTAMP_NONE = 0,
+	EAL_LOG_TIMESTAMP_TIME,		/* time since start */
+	EAL_LOG_TIMESTAMP_DELTA,	/* time since last message */
+	EAL_LOG_TIMESTAMP_RELTIME,
+	EAL_LOG_TIMESTAMP_CTIME,
+	EAL_LOG_TIMESTAMP_ISO,
+};
+
+static struct {
+	enum eal_log_time_format time_format;
+	struct timespec start_time;
+	struct timespec last_time;
+	struct tm last_tm;
+} log = {
+	.time_format = EAL_LOG_TIMESTAMP_NONE,
+};
+
+int
+eal_log_timestamp(const char *str)
+{
+	if (str == NULL)
+		log.time_format = EAL_LOG_TIMESTAMP_TIME;
+	else if (strcmp(str, "notime") == 0)
+		log.time_format = EAL_LOG_TIMESTAMP_NONE;
+	else if (strcmp(str, "reltime") == 0)
+		log.time_format = EAL_LOG_TIMESTAMP_RELTIME;
+	else if (strcmp(str, "delta") == 0)
+		log.time_format = EAL_LOG_TIMESTAMP_DELTA;
+	else if (strcmp(str, "ctime") == 0)
+		log.time_format =  EAL_LOG_TIMESTAMP_CTIME;
+	else if (strcmp(str, "iso") == 0)
+		log.time_format = EAL_LOG_TIMESTAMP_ISO;
+	else
+		return -1;
+
+	return 0;
+}
+
+static struct timespec
+timespec_sub(const struct timespec *t0, const struct timespec *t1)
+{
+	struct timespec ts;
+
+	ts.tv_sec = t0->tv_sec - t1->tv_sec;
+	ts.tv_nsec = t0->tv_nsec - t1->tv_nsec;
+	if (ts.tv_nsec < 0) {
+		ts.tv_sec--;
+		ts.tv_nsec += 1000000000L;
+	}
+	return ts;
+}
+
+static ssize_t
+console_log_timestamp(char *tsbuf, size_t tsbuflen)
+{
+	struct timespec now, ts;
+	struct tm *tm, cur;
+
+	switch (log.time_format) {
+	case EAL_LOG_TIMESTAMP_NONE:
+		return 0;
+
+	case EAL_LOG_TIMESTAMP_TIME:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		ts = timespec_sub(&now, &log.start_time);
+
+		return snprintf(tsbuf, tsbuflen, "%6lu.%06lu",
+				ts.tv_sec, ts.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_DELTA:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		ts = timespec_sub(&now, &log.last_time);
+		log.last_time = now;
+
+		return snprintf(tsbuf, tsbuflen, "<%6lu.%06lu>",
+				ts.tv_sec, ts.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_RELTIME:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		tm = localtime_r(&now.tv_sec, &cur);
+		ts = timespec_sub(&now, &log.last_time);
+		log.last_time = now;
+
+		/* if minute, day, hour hasn't changed then print delta */
+		if (cur.tm_min != log.last_tm.tm_min ||
+		    cur.tm_hour != log.last_tm.tm_hour ||
+		    cur.tm_yday != log.last_tm.tm_yday) {
+			log.last_tm = cur;
+			return strftime(tsbuf, tsbuflen, "%b%e %H:%M", tm);
+		} else {
+			return snprintf(tsbuf, tsbuflen, "+%4lu.%06lu",
+					ts.tv_sec, ts.tv_nsec / 1000u);
+		}
+
+	case EAL_LOG_TIMESTAMP_CTIME: {
+		char cbuf[32]; /* "Wed Jun 30 21:49:08 1993\n" */
+
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		/* .24s is to remove newline from ctime result */
+		return snprintf(tsbuf, tsbuflen, "%.24s",
+				ctime_r(&now.tv_sec, cbuf));
+	}
+
+	case EAL_LOG_TIMESTAMP_ISO: {
+		char dbuf[64]; /* "2024-05-01T22:11:00" */
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+		tm = localtime_r(&now.tv_sec, &cur);
+
+		/* make "2024-05-01T22:11:00,123456+0100" */
+		if (strftime(dbuf, sizeof(dbuf), "%Y-%m-%dT%H:%M:%S", tm) == 0)
+			return 0;
+
+		return snprintf(tsbuf, tsbuflen, "%s,%06lu%+03ld:%02ld", dbuf,
+				now.tv_nsec / 1000u,
+				tm->tm_gmtoff / 3600,
+				(tm->tm_gmtoff / 60) % 60);
+	}
+	}
+
+	return 0;
+}
+
 /*
  * default log function
  */
 static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
+console_log_write(__rte_unused void *c, const char *msg, size_t size)
 {
+	char buf[128];
 	ssize_t ret;
 
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
+	ret = console_log_timestamp(buf, sizeof(buf));
+	if (ret == 0)
+		ret = fwrite(msg, 1, size, stderr);
+	else
+		ret = fprintf(stderr, "[%s] %.*s", buf, (int)size, msg);
 	fflush(stderr);
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
-	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
+	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, msg);
 
 	return ret;
 }
@@ -49,6 +190,9 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
+	clock_gettime(CLOCK_MONOTONIC, &log.start_time);
+	log.last_time = log.start_time;
+
 	/* skip if user has already setup a log stream */
 	if (eal_log_get_default())
 		return 0;
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a6a08895501e..8aa68a181bec 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -6,6 +6,12 @@
 #include <rte_log.h>
 #include "log_internal.h"
 
+int
+eal_log_timestamp(__rte_unused const char *fmt)
+{
+	return -1; /* not implemented */
+}
+
 /* set the log to default function, called during eal init process. */
 int
 eal_log_init(__rte_unused const char *id, __rte_unused int facility)
diff --git a/lib/log/version.map b/lib/log/version.map
index 6ecc656d1d65..697cdb3cb1b2 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -31,5 +31,6 @@ INTERNAL {
 	eal_log_save_pattern;
 	eal_log_save_regexp;
 	eal_log_set_default;
+	eal_log_timestamp;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 09/10] log: colorize log output
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                     ` (7 preceding siblings ...)
  2024-03-21 16:00   ` [PATCH v10 08/10] eal: add option to put timestamp on console output Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  2024-03-21 16:00   ` [PATCH v10 10/10] doc: add documentation of logging options Stephen Hemminger
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Like dmesg, colorize the log output (unless redirected to file).
Timestamp is green, the subsystem is in yellow and the message
is red if urgent, boldface if an error, and normal for info and
debug messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c |  10 +++
 lib/eal/common/eal_options.h        |   2 +
 lib/log/log_internal.h              |   5 ++
 lib/log/log_unix.c                  | 132 +++++++++++++++++++++++++---
 lib/log/log_windows.c               |   6 ++
 lib/log/version.map                 |   1 +
 6 files changed, 146 insertions(+), 10 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 4e2c3d0f255c..522634f1dcfa 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -78,6 +78,7 @@ eal_long_options[] = {
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
 	{OPT_LOG_TIMESTAMP,     2, NULL, OPT_LOG_TIMESTAMP_NUM    },
+	{OPT_LOG_COLOR,		1, NULL, OPT_LOG_COLOR_NUM	  },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1910,6 +1911,15 @@ eal_parse_common_option(int opt, const char *optarg,
 		}
 		break;
 
+
+	case OPT_LOG_COLOR_NUM:
+		if (eal_log_color(optarg) < 0) {
+			EAL_LOG(ERR, "invalid parameters for --"
+				OPT_LOG_COLOR);
+			return -1;
+		}
+		break;
+
 	case OPT_TRACE_NUM: {
 		if (eal_trace_args_save(optarg) < 0) {
 			EAL_LOG(ERR, "invalid parameters for --"
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index e24c9eca53ca..5a63c1dd3a32 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -37,6 +37,8 @@ enum {
 	OPT_LOG_LEVEL_NUM,
 #define OPT_LOG_TIMESTAMP     "log-timestamp"
 	OPT_LOG_TIMESTAMP_NUM,
+#define OPT_LOG_COLOR	      "log-color"
+	OPT_LOG_COLOR_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 0e18d147cf98..b90c668f4668 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -52,5 +52,10 @@ void rte_eal_log_cleanup(void);
 __rte_internal
 int eal_log_timestamp(const char *fmt);
 
+/*
+ * Enable or disable color in log messages
+ */
+__rte_internal
+int eal_log_color(const char *mode);
 
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/log_unix.c b/lib/log/log_unix.c
index cb8f17bc83ef..1af93795fec5 100644
--- a/lib/log/log_unix.c
+++ b/lib/log/log_unix.c
@@ -11,6 +11,7 @@
 #include <time.h>
 #include <unistd.h>
 
+#include <rte_common.h>
 #include <rte_log.h>
 
 #include "log_internal.h"
@@ -24,15 +25,113 @@ enum eal_log_time_format {
 	EAL_LOG_TIMESTAMP_ISO,
 };
 
+enum eal_log_color {
+	EAL_LOG_COLOR_AUTO = 0,	/* default */
+	EAL_LOG_COLOR_NEVER,
+	EAL_LOG_COLOR_ALWAYS,
+};
+
 static struct {
+	enum eal_log_color color_mode;
 	enum eal_log_time_format time_format;
+	bool show_color;
 	struct timespec start_time;
 	struct timespec last_time;
 	struct tm last_tm;
 } log = {
+	.color_mode = EAL_LOG_COLOR_AUTO,
 	.time_format = EAL_LOG_TIMESTAMP_NONE,
 };
 
+enum color {
+	COLOR_NONE,
+	COLOR_RED,
+	COLOR_GREEN,
+	COLOR_YELLOW,
+	COLOR_BLUE,
+	COLOR_MAGENTA,
+	COLOR_CYAN,
+	COLOR_WHITE,
+	COLOR_BOLD,
+	COLOR_CLEAR
+};
+
+static const char * const color_code[] = {
+	[COLOR_NONE]	= "",
+	[COLOR_RED]	= "\e[31m",
+	[COLOR_GREEN]	= "\e[32m",
+	[COLOR_YELLOW]	= "\e[33m",
+	[COLOR_BLUE]	= "\e[34m",
+	[COLOR_MAGENTA] = "\e[35m",
+	[COLOR_CYAN]    = "\e[36m",
+	[COLOR_WHITE]	= "\e[37m",
+	[COLOR_BOLD]	= "\e[1m",
+	[COLOR_CLEAR]	= "\e[0m",
+};
+
+__rte_format_printf(3, 4)
+static int color_fprintf(FILE *out, enum color color, const char *fmt, ...)
+{
+	va_list args;
+	int ret = 0;
+
+	va_start(args, fmt);
+	ret = fprintf(out, "%s", color_code[color]);
+	ret += vfprintf(out, fmt, args);
+	ret += fprintf(out, "%s", color_code[COLOR_CLEAR]);
+
+	return ret;
+}
+
+static ssize_t
+color_log_write(FILE *f, int level, const char *msg, size_t size)
+{
+	size_t i;
+	ssize_t ret = 0;
+
+	/* find first : delimiter in message */
+	for (i = 0; i < size; i++) {
+		if (msg[i] == ':') {
+			++i;  /* put colon in the color */
+			ret = color_fprintf(stderr, COLOR_YELLOW, "%.*s", (int)i, msg);
+			msg += i;
+			size -= i;
+			break;
+		}
+	}
+
+	if (level <= 0 || level >= (int)RTE_LOG_INFO)
+		ret += fprintf(f, "%.*s", (int)size, msg);
+	else if (level >= (int)RTE_LOG_ERR)
+		ret += color_fprintf(f, COLOR_BOLD, "%.*s", (int)size, msg);
+	else
+		ret += color_fprintf(f, COLOR_RED, "%.*s", (int)size, msg);
+
+	return ret;
+}
+
+/*
+ * Controls whether color is enabled:
+ * modes are:
+ *   always - enable color output regardless
+ *   auto - enable if stderr is a terminal
+ *   never - color output is disabled.
+ */
+int
+eal_log_color(const char *mode)
+{
+	if (mode == NULL || strcmp(mode, "always") == 0)
+		log.color_mode = EAL_LOG_COLOR_ALWAYS;
+	else if (strcmp(mode, "never") == 0)
+		log.color_mode = EAL_LOG_COLOR_NEVER;
+	else if (strcmp(mode, "auto") == 0)
+		log.color_mode = EAL_LOG_COLOR_AUTO;
+	else
+		return -1;
+
+	return 0;
+}
+
 int
 eal_log_timestamp(const char *str)
 {
@@ -153,18 +252,28 @@ console_log_timestamp(char *tsbuf, size_t tsbuflen)
 static ssize_t
 console_log_write(__rte_unused void *c, const char *msg, size_t size)
 {
-	char buf[128];
+	int level = rte_log_cur_msg_loglevel();
+	char tsbuf[128] = "";
 	ssize_t ret;
 
-	ret = console_log_timestamp(buf, sizeof(buf));
-	if (ret == 0)
-		ret = fwrite(msg, 1, size, stderr);
-	else
-		ret = fprintf(stderr, "[%s] %.*s", buf, (int)size, msg);
+	ret = console_log_timestamp(tsbuf, sizeof(tsbuf));
+	if (ret == 0) {
+		if (log.show_color)
+			ret = color_log_write(stderr, level, msg, size);
+		else
+			ret = fwrite(msg, 1, size, stderr);
+	} else {
+		if (log.show_color) {
+			color_fprintf(stderr, COLOR_GREEN, "[%s] ", tsbuf);
+			ret = color_log_write(stderr, level, msg, size);
+		} else {
+			ret = fprintf(stderr, "[%s] %.*s", tsbuf, (int)size, msg);
+		}
+	}
 	fflush(stderr);
 
 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
-	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, msg);
+	syslog(level - 1, "%.*s", (int)size, msg);
 
 	return ret;
 }
@@ -190,13 +299,16 @@ eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
 
-	clock_gettime(CLOCK_MONOTONIC, &log.start_time);
-	log.last_time = log.start_time;
-
 	/* skip if user has already setup a log stream */
 	if (eal_log_get_default())
 		return 0;
 
+	log.show_color = (log.color_mode == EAL_LOG_COLOR_ALWAYS) ||
+		(log.color_mode == EAL_LOG_COLOR_AUTO && isatty(STDERR_FILENO));
+
+	clock_gettime(CLOCK_MONOTONIC, &log.start_time);
+	log.last_time = log.start_time;
+
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index 8aa68a181bec..1cb72f519a91 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -12,6 +12,12 @@ eal_log_timestamp(__rte_unused const char *fmt)
 	return -1; /* not implemented */
 }
 
+int
+eal_log_color(__rte_unused const char *mode)
+{
+	return -1; /* not implemented */
+}
+
 /* set the log to default function, called during eal init process. */
 int
 eal_log_init(__rte_unused const char *id, __rte_unused int facility)
diff --git a/lib/log/version.map b/lib/log/version.map
index 697cdb3cb1b2..1fe6955427b7 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_color;
 	eal_log_get_default;
 	eal_log_init;
 	eal_log_level2str;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v10 10/10] doc: add documentation of logging options
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
                     ` (8 preceding siblings ...)
  2024-03-21 16:00   ` [PATCH v10 09/10] log: colorize log output Stephen Hemminger
@ 2024-03-21 16:00   ` Stephen Hemminger
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 16:00 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Add documentation of new log-timestamp and log-color options.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/linux_gsg/eal_args.include.rst | 54 +++++++++++++++++++----
 1 file changed, 46 insertions(+), 8 deletions(-)

diff --git a/doc/guides/linux_gsg/eal_args.include.rst b/doc/guides/linux_gsg/eal_args.include.rst
index 9cfbf7de8456..8ba54a62c0f9 100644
--- a/doc/guides/linux_gsg/eal_args.include.rst
+++ b/doc/guides/linux_gsg/eal_args.include.rst
@@ -122,6 +122,52 @@ Memory-related options
     to system pthread stack size unless the optional size (in kbytes) is
     specified.
 
+Logging options
+~~~~~~~~~~~~~~~
+
+*   ``--log-level <type:val>``
+
+    Specify log level for a specific component. For example::
+
+        --log-level lib.eal:debug
+
+    Can be specified multiple times.
+
+*   ``--log-facility=facility``
+
+    Specifies the ``syslog`` facility passed to ``openlog``.
+    The facility can be: ``auth``, ``cron``, ``daemon``, ``ftp``,
+    ``lpr``, ``mail``, ``news``, ``user``, ``uucp``,
+    ``local0``, ``local1``, ``local2``, ``local3``, ``local4``,
+    ``local5``, ``local6``, or ``local7``. The default if
+    no facility is specified is ``daemon``.
+
+*   ``--log-timestamp[=format]``
+
+    Add a time stamp to log messages that are sent to the console (stderr).
+    When format argument is omitted the time stamp is elapsed
+    seconds since the start of the DPDK application.
+    Other formats are:
+
+.. csv-table:: Log time stamp format
+   :header: "Format", "Description", "Example"
+   :widths: 6, 30, 32
+
+   "ctime", "Unix ctime", "``[Wed Mar 20 07:26:12 2024]``"
+   "delta", "Offset since last", "``[<    3.162373>]``"
+   "reltime", "Seconds since last or time if minute changed", "``[  +3.001791]`` or ``[Mar20 07:26:12]``"
+   "iso", "ISO-8601", "``[2024-03-20T07:26:12−07:00]``"
+
+*   ``--log-color[=when]``
+
+    Colorize the console output.
+    The optional argument ``when`` can be ``auto``, ``never`` or ``always``.
+    If the when argument is omitted, it defaults to ``auto``.
+    To disable use of color::
+
+        --log-color=none
+
+
 Debugging options
 ~~~~~~~~~~~~~~~~~
 
@@ -134,14 +180,6 @@ Debugging options
     Use anonymous memory instead of hugepages (implies no secondary process
     support).
 
-*   ``--log-level <type:val>``
-
-    Specify log level for a specific component. For example::
-
-        --log-level lib.eal:debug
-
-    Can be specified multiple times.
-
 *   ``--trace=<regex-match>``
 
     Enable trace based on regular expression trace name. By default, the trace is
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v10 03/10] eal: make eal_log_level_parse common
  2024-03-21 16:00   ` [PATCH v10 03/10] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-21 17:00     ` Tyler Retzlaff
  0 siblings, 0 replies; 277+ messages in thread
From: Tyler Retzlaff @ 2024-03-21 17:00 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Thu, Mar 21, 2024 at 09:00:19AM -0700, Stephen Hemminger wrote:
> The code to parse for log-level option should be same on
> all OS variants.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---

Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v10 06/10] eal: change rte_exit() output to match rte_log()
  2024-03-21 16:00   ` [PATCH v10 06/10] eal: change rte_exit() output to match rte_log() Stephen Hemminger
@ 2024-03-21 17:04     ` Tyler Retzlaff
  0 siblings, 0 replies; 277+ messages in thread
From: Tyler Retzlaff @ 2024-03-21 17:04 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Thu, Mar 21, 2024 at 09:00:22AM -0700, Stephen Hemminger wrote:
> The rte_exit() output format confuses the timestamp and coloring
> options. Change it to use be a single line with proper prefix.
> 
> Before:
> [     0.006481] EAL: Error - exiting with code: 1
>   Cause: [     0.006489] Cannot init EAL: Permission denied
> 
> After:
> [     0.006238] EAL: Error - exiting with code: 1
> [     0.006250] EAL: Cause: Cannot init EAL: Permission denied

it is better but maybe : -> - for Cause?

[     0.006238] EAL: Error - exiting with code: 1
[     0.006250] EAL: Cause - Cannot init EAL: Permission denied


> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---

ACked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>

>  lib/eal/common/eal_common_debug.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/lib/eal/common/eal_common_debug.c b/lib/eal/common/eal_common_debug.c
> index 3e77995896d5..568883830f20 100644
> --- a/lib/eal/common/eal_common_debug.c
> +++ b/lib/eal/common/eal_common_debug.c
> @@ -34,17 +34,18 @@ void
>  rte_exit(int exit_code, const char *format, ...)
>  {
>  	va_list ap;
> +	char *msg = NULL;
>  
>  	if (exit_code != 0)
> -		RTE_LOG(CRIT, EAL, "Error - exiting with code: %d\n"
> -				"  Cause: ", exit_code);
> +		EAL_LOG(CRIT, "Error - exiting with code: %d", exit_code);
>  
>  	va_start(ap, format);
> -	rte_vlog(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, format, ap);
> +	vasprintf(&msg, format, ap);
>  	va_end(ap);
>  
> +	rte_log(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, "EAL: Cause: %s", msg);
> +
>  	if (rte_eal_cleanup() != 0 && rte_errno != EALREADY)
> -		EAL_LOG(CRIT,
> -			"EAL could not release all resources");
> +		EAL_LOG(CRIT, "EAL could not release all resources");
>  	exit(exit_code);
>  }
> -- 
> 2.43.0

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v10 07/10] eal: allow user to set default log stream before init
  2024-03-21 16:00   ` [PATCH v10 07/10] eal: allow user to set default log stream before init Stephen Hemminger
@ 2024-03-21 17:07     ` Tyler Retzlaff
  0 siblings, 0 replies; 277+ messages in thread
From: Tyler Retzlaff @ 2024-03-21 17:07 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Thu, Mar 21, 2024 at 09:00:23AM -0700, Stephen Hemminger wrote:
> It is useful for application to be able to set the default log
> stream before call rte_eal_init(). This makes all messages go
> to the new default.
> 
> For example, to skip using syslog; just doing
> 	rte_openlog_stream(stderr);
> 
> There is no reason for helper command line applications to clutter
> syslog with messages.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---

Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v10 08/10] eal: add option to put timestamp on console output
  2024-03-21 16:00   ` [PATCH v10 08/10] eal: add option to put timestamp on console output Stephen Hemminger
@ 2024-03-21 17:11     ` Tyler Retzlaff
  2024-03-21 17:16       ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Tyler Retzlaff @ 2024-03-21 17:11 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Thu, Mar 21, 2024 at 09:00:24AM -0700, Stephen Hemminger wrote:
> When debugging driver or startup issues, it is useful to have
> a timestamp on each message printed. The messages in syslog
> already have a timestamp, but often syslog is not available
> during testing.
> 
> There are multiple timestamp formats similar to Linux dmesg.
> The default is time relative since startup. Other alternatives
> are delta, ctime, reltime and iso formats.
> 
> Example:
> $ dpdk-testpmd --log-timestamp -- -i
> EAL: Detected CPU lcores: 8
> EAL: Detected NUMA nodes: 1
> [     0.000083] EAL: Detected static linkage of DPDK
> [     0.002000] EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
> [     0.003507] EAL: Selected IOVA mode 'VA'
> [     0.006272] testpmd: No probed ethernet devices
> Interactive-mode selected
> [     0.029567] testpmd: create a new mbuf pool <mb_pool_0>: n=203456, size=2176, socket=0
> [     0.029588] testpmd: preferred mempool ops selected: ring_mp_mc
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  app/test/test_eal_flags.c           |  17 ++++
>  doc/guides/prog_guide/log_lib.rst   |  10 ++
>  lib/eal/common/eal_common_options.c |  14 ++-
>  lib/eal/common/eal_options.h        |   2 +
>  lib/log/log_internal.h              |   9 ++
>  lib/log/log_unix.c                  | 152 +++++++++++++++++++++++++++-
>  lib/log/log_windows.c               |   6 ++
>  lib/log/version.map                 |   1 +
>  8 files changed, 205 insertions(+), 6 deletions(-)
> 
> diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
> index 6cb4b0675730..eeb1799381d7 100644
> --- a/app/test/test_eal_flags.c
> +++ b/app/test/test_eal_flags.c
> @@ -1055,6 +1055,14 @@ test_misc_flags(void)
>  	const char * const argv22[] = {prgname, prefix, mp_flag,
>  				       "--huge-worker-stack=512"};
>  
> +	/* Try running with --log-timestamp */
> +	const char * const argv23[] = {prgname, prefix, mp_flag,
> +				       "--log-timestamp" };
> +
> +	/* Try running with --log-timestamp=iso */
> +	const char * const argv24[] = {prgname, prefix, mp_flag,
> +				       "--log-timestamp=iso" };
> +
>  	/* run all tests also applicable to FreeBSD first */
>  
>  	if (launch_proc(argv0) == 0) {
> @@ -1162,6 +1170,15 @@ test_misc_flags(void)
>  		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
>  		goto fail;
>  	}
> +	if (launch_proc(argv23) != 0) {
> +		printf("Error - process did not run ok with --log-timestamp parameter\n");
> +		goto fail;
> +	}
> +	if (launch_proc(argv24) != 0) {
> +		printf("Error - process did not run ok with --log-timestamp=iso parameter\n");
> +		goto fail;
> +	}
> +
>  
>  	rmdir(hugepath_dir3);
>  	rmdir(hugepath_dir2);
> diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
> index aacb36c36ce0..1d6b2e3cea5d 100644
> --- a/doc/guides/prog_guide/log_lib.rst
> +++ b/doc/guides/prog_guide/log_lib.rst
> @@ -73,6 +73,16 @@ For example::
>  	/path/to/app --syslog local0
>  
>  
> +Console timestamp
> +~~~~~~~~~~~~~~~~~
> +
> +On Linux and FreeBSD, an optional timestamp can be added before each
> +message by adding the ``--log-timestamp`` option.

i don't think Windows should be excluded from the feature, in general
core libraries that get features should cover all platforms.


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v10 08/10] eal: add option to put timestamp on console output
  2024-03-21 17:11     ` Tyler Retzlaff
@ 2024-03-21 17:16       ` Stephen Hemminger
  2024-03-21 17:49         ` Tyler Retzlaff
  0 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-21 17:16 UTC (permalink / raw)
  To: Tyler Retzlaff; +Cc: dev

On Thu, 21 Mar 2024 10:11:31 -0700
Tyler Retzlaff <roretzla@linux.microsoft.com> wrote:

> >  
> >  
> > +Console timestamp
> > +~~~~~~~~~~~~~~~~~
> > +
> > +On Linux and FreeBSD, an optional timestamp can be added before each
> > +message by adding the ``--log-timestamp`` option.  
> 
> i don't think Windows should be excluded from the feature, in general
> core libraries that get features should cover all platforms.

The problem is that current Linux/FreeBSD log code depends on fopencookie()
to work. I could not find a way to do that on Windows.

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [dpdk-dev] [PATCH] eal: add option to put timestamp on console output
  2020-10-19 15:25               ` Stephen Hemminger
@ 2024-03-21 17:22                 ` Thomas Monjalon
  0 siblings, 0 replies; 277+ messages in thread
From: Thomas Monjalon @ 2024-03-21 17:22 UTC (permalink / raw)
  To: Stephen Hemminger
  Cc: Bruce Richardson, Dmitry Kozlyuk, dev, david.marchand, olivier.matz

19/10/2020 17:25, Stephen Hemminger:
> On Mon, 19 Oct 2020 16:11:19 +0200
> Thomas Monjalon <thomas@monjalon.net> wrote:
> 
> > There was no ack or conclusion for this patch.
> > 
> > 17/08/2020 17:11, Stephen Hemminger:
> > > On Mon, 17 Aug 2020 11:37:40 +0100
> > > Bruce Richardson <bruce.richardson@intel.com> wrote:
> > >   
> > > > On Fri, Aug 14, 2020 at 12:20:28PM -0700, Stephen Hemminger wrote:  
> > > > > On Fri, 14 Aug 2020 22:09:11 +0300
> > > > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:
> > > > >     
> > > > > > On Fri, 14 Aug 2020 11:45:49 -0700, Stephen Hemminger wrote:    
> > > > > > > On Fri, 14 Aug 2020 21:39:29 +0300
> > > > > > > Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:      
> > > > > > [...]    
> > > > > > > FreeBsd doesn't go through the same log code.      
> > > > > > 
> > > > > > Then --log-timestamp shouldn't appear on FreeBSD if it's unsupported.    
> > > > > 
> > > > > Was using same ifdef that is already there. There are several options already
> > > > > (like syslog facility) which do nothing on FreeBSD anyway.
> > > > >     
> > > > While that is correct, I think that we really should support this on
> > > > FreeBSD also, since I don't think there is any technical reason we can't do
> > > > so, right? Even a sub-optimal solution where the timestamp write and log
> > > > write are non-atomic would be better than nothing.
> > > > 
> > > > /Bruce  
> > > 
> > > FreeBSD now just uses default log stream (stdout).
> > > Linux has log stream to syslog and stdout.
> > > 
> > > There is nothing Linux specific in the old or new code.
> > > The Linux code could just be moved to common, and both could use it.
> > > That is a separate patch, and someone with expertise and build setup for FreeBSD needs to do it.
> > >   
> 
> Lets fix FreeBSD to use common code for logging, then timestamp option can be added?

OK




^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v10 08/10] eal: add option to put timestamp on console output
  2024-03-21 17:16       ` Stephen Hemminger
@ 2024-03-21 17:49         ` Tyler Retzlaff
  2024-03-22  0:30           ` Stephen Hemminger
  0 siblings, 1 reply; 277+ messages in thread
From: Tyler Retzlaff @ 2024-03-21 17:49 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

On Thu, Mar 21, 2024 at 10:16:36AM -0700, Stephen Hemminger wrote:
> On Thu, 21 Mar 2024 10:11:31 -0700
> Tyler Retzlaff <roretzla@linux.microsoft.com> wrote:
> 
> > >  
> > >  
> > > +Console timestamp
> > > +~~~~~~~~~~~~~~~~~
> > > +
> > > +On Linux and FreeBSD, an optional timestamp can be added before each
> > > +message by adding the ``--log-timestamp`` option.  
> > 
> > i don't think Windows should be excluded from the feature, in general
> > core libraries that get features should cover all platforms.
> 
> The problem is that current Linux/FreeBSD log code depends on fopencookie()
> to work. I could not find a way to do that on Windows.

but that looks like it is part of general log initialization. surely
when we are talking about logging a timestamp to a console we're just
talking about a standard stream and a format string for Windows?

i'll try to study more to gain a better understanding.

thanks

^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v10 08/10] eal: add option to put timestamp on console output
  2024-03-21 17:49         ` Tyler Retzlaff
@ 2024-03-22  0:30           ` Stephen Hemminger
  0 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-22  0:30 UTC (permalink / raw)
  To: Tyler Retzlaff; +Cc: dev

On Thu, 21 Mar 2024 10:49:06 -0700
Tyler Retzlaff <roretzla@linux.microsoft.com> wrote:

> On Thu, Mar 21, 2024 at 10:16:36AM -0700, Stephen Hemminger wrote:
> > On Thu, 21 Mar 2024 10:11:31 -0700
> > Tyler Retzlaff <roretzla@linux.microsoft.com> wrote:
> >   
> > > >  
> > > >  
> > > > +Console timestamp
> > > > +~~~~~~~~~~~~~~~~~
> > > > +
> > > > +On Linux and FreeBSD, an optional timestamp can be added before each
> > > > +message by adding the ``--log-timestamp`` option.    
> > > 
> > > i don't think Windows should be excluded from the feature, in general
> > > core libraries that get features should cover all platforms.  
> > 
> > The problem is that current Linux/FreeBSD log code depends on fopencookie()
> > to work. I could not find a way to do that on Windows.  
> 
> but that looks like it is part of general log initialization. surely
> when we are talking about logging a timestamp to a console we're just
> talking about a standard stream and a format string for Windows?
> 
> i'll try to study more to gain a better understanding.
> 
> thanks

What the code does is take what rte_log created and add more data before
actual printing it. The other option is doing this at rte_log() but
that will break the syslog side.

I am not sure that having log go directly to syslog makes sense anyway
on a modern Linux system. If DPDK is being used by a daemon, that daemon
will be a systemd service. And systemd has simple way to make stderr go
to syslog.  So if we drop the syslog support, then the whole thing gets
more generic and simpler.


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 0/9] Logging unification and enhancements
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (9 preceding siblings ...)
  2024-03-21 16:00 ` [PATCH v10 00/10] Logging enhancements Stephen Hemminger
@ 2024-03-24  2:33 ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 1/9] windows: make getopt functions have const properties Stephen Hemminger
                     ` (9 more replies)
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                   ` (9 subsequent siblings)
  20 siblings, 10 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Improvements and unification of logging library (for 24.07 release).
This version works on all platforms: Linux, Windows and FreeBSD.

This is update to rework patch set. It adds several new features
to the console log output.

  * Putting a timestamp on console output which is useful for
    analyzing performance of startup codes. Timestamp is optional
    and must be enabled on command line.

  * Displaying console output with colors.
    It uses the standard conventions used by many other Linux commands
    for colorized display.  The default is to enable color if the
    console output is going to a terminal. But it can be always
    on or disabled by command line flag. This default was chosen
    based on what dmesg(1) command does.

    I find color helpful because DPDK drivers and libraries print
    lots of not very useful messages. And having error messages
    highlighted in bold face helps. This might also get users to
    pay more attention to error messages. Many bug reports have
    earlier messages that are lost because there are so many
    info messages.

  * Use of syslog on Linux is replaced by using journal
    API (over unix domain socket). This replaces legacy syslog;
    (almost) all Linux distros have switched to journal.

Will add a release note in next release (after this is merged)

v11 - rework to be portable to Windows
      drop syslog support and support journal instead

Stephen Hemminger (9):
  windows: make getopt functions have const properties
  eal: make eal_log_level_parse common
  eal: do not duplicate rte_init_alert() messages
  eal: change rte_exit() output to match rte_log()
  log: drop syslog support, and make code common
  log: add hook for printing log messages
  log: add timestamp option
  log: add support for systemd journal
  log: colorize log output

 app/test/test_eal_flags.c                     |  38 +-
 doc/guides/linux_gsg/linux_eal_parameters.rst |  27 -
 .../prog_guide/env_abstraction_layer.rst      |   5 +-
 doc/guides/prog_guide/log_lib.rst             |  41 +-
 lib/eal/common/eal_common_debug.c             |  11 +-
 lib/eal/common/eal_common_options.c           | 123 ++---
 lib/eal/common/eal_internal_cfg.h             |   2 +-
 lib/eal/common/eal_options.h                  |   7 +-
 lib/eal/freebsd/eal.c                         |  54 +-
 lib/eal/linux/eal.c                           |  52 +-
 lib/eal/unix/eal_debug.c                      |   3 +-
 lib/eal/windows/eal.c                         |  39 +-
 lib/eal/windows/getopt.c                      |  23 +-
 lib/eal/windows/include/getopt.h              |   8 +-
 lib/log/log.c                                 | 513 ++++++++++++++++--
 lib/log/log_freebsd.c                         |  12 -
 lib/log/log_internal.h                        |  16 +-
 lib/log/log_linux.c                           |  61 ---
 lib/log/log_windows.c                         |  18 -
 lib/log/meson.build                           |   5 +-
 lib/log/version.map                           |   2 +
 21 files changed, 656 insertions(+), 404 deletions(-)
 delete mode 100644 lib/log/log_freebsd.c
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 1/9] windows: make getopt functions have const properties
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 2/9] eal: make eal_log_level_parse common Stephen Hemminger
                     ` (8 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Tyler Retzlaff, Dmitry Kozlyuk, Pallavi Kadam

Having different prototypes on different platforms can lead
to lots of unnecessary workarounds.  Looks like the version of
getopt used from windows was based on an older out of date
version from FreeBSD.

This patch changes getopt, getopt_long, etc to have the same const
attributes as Linux and FreeBSD. The changes are derived from
the current FreeBSD version of getopt_long.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 lib/eal/windows/getopt.c         | 23 ++++++++++++-----------
 lib/eal/windows/include/getopt.h |  8 ++++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lib/eal/windows/getopt.c b/lib/eal/windows/getopt.c
index a1f51c6c23..50ff71b930 100644
--- a/lib/eal/windows/getopt.c
+++ b/lib/eal/windows/getopt.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-const char    *optarg;		/* argument associated with option */
+char    *optarg;		/* argument associated with option */
 int	opterr = 1;		/* if error message should be printed */
 int	optind = 1;		/* index into parent argv vector */
 int	optopt = '?';		/* character checked for validity */
@@ -39,9 +39,9 @@ static void pass(const char *a) {(void) a; }
 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
 #define	INORDER		1
 
-#define	EMSG		""
+static char EMSG[] = "";
 
-static const char *place = EMSG; /* option letter processing */
+static char *place = EMSG; /* option letter processing */
 
 /* XXX: set optreset to 1 rather than these two */
 static int nonopt_start = -1; /* first non option argument (for permute) */
@@ -80,7 +80,7 @@ gcd(int a, int b)
  */
 static void
 permute_args(int panonopt_start, int panonopt_end, int opt_end,
-	char **nargv)
+	char * const *nargv)
 {
 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
 	char *swap;
@@ -101,11 +101,12 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
 				pos -= nnonopts;
 			else
 				pos += nopts;
+
 			swap = nargv[pos];
 			/* LINTED const cast */
-			((char **) nargv)[pos] = nargv[cstart];
+			((char **)(uintptr_t)nargv)[pos] = nargv[cstart];
 			/* LINTED const cast */
-			((char **)nargv)[cstart] = swap;
+			((char **)(uintptr_t)nargv)[cstart] = swap;
 		}
 	}
 }
@@ -116,7 +117,7 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
  * Returns -1 if short_too is set and the option does not match long_options.
  */
 static int
-parse_long_options(char **nargv, const char *options,
+parse_long_options(char * const *nargv, const char *options,
 	const struct option *long_options, int *idx, int short_too)
 {
 	const char *current_argv;
@@ -236,7 +237,7 @@ parse_long_options(char **nargv, const char *options,
  *	Parse argc/argv argument vector.  Called by user level routines.
  */
 static int
-getopt_internal(int nargc, char **nargv, const char *options,
+getopt_internal(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx, int flags)
 {
 	char *oli;				/* option letter list index */
@@ -434,7 +435,7 @@ getopt_internal(int nargc, char **nargv, const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt(int nargc, char *nargv[], const char *options)
+getopt(int nargc, char *const nargv[], const char *options)
 {
 	return getopt_internal(nargc, nargv, options, NULL, NULL,
 			       FLAG_PERMUTE);
@@ -445,7 +446,7 @@ getopt(int nargc, char *nargv[], const char *options)
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long(int nargc, char *nargv[], const char *options,
+getopt_long(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
@@ -458,7 +459,7 @@ getopt_long(int nargc, char *nargv[], const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long_only(int nargc, char *nargv[], const char *options,
+getopt_long_only(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
diff --git a/lib/eal/windows/include/getopt.h b/lib/eal/windows/include/getopt.h
index 6f57af454b..e4cf6873cb 100644
--- a/lib/eal/windows/include/getopt.h
+++ b/lib/eal/windows/include/getopt.h
@@ -44,7 +44,7 @@
 
 
 /** argument to current option, or NULL if it has none */
-extern const char *optarg;
+extern char *optarg;
 /** Current position in arg string.  Starts from 1.
  * Setting to 0 resets state.
  */
@@ -80,14 +80,14 @@ struct option {
 };
 
 /** Compat: getopt */
-int getopt(int argc, char *argv[], const char *options);
+int getopt(int argc, char *const argv[], const char *options);
 
 /** Compat: getopt_long */
-int getopt_long(int argc, char *argv[], const char *options,
+int getopt_long(int argc, char *const argv[], const char *options,
 		const struct option *longopts, int *longindex);
 
 /** Compat: getopt_long_only */
-int getopt_long_only(int nargc, char *argv[], const char *options,
+int getopt_long_only(int nargc, char *const argv[], const char *options,
 		     const struct option *long_options, int *idx);
 
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 2/9] eal: make eal_log_level_parse common
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 1/9] windows: make getopt functions have const properties Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 3/9] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
                     ` (7 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

The code to parse for log-level option should be same on
all OS variants.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 46 +++++++++++++++++++++++++++++
 lib/eal/common/eal_options.h        |  1 +
 lib/eal/freebsd/eal.c               | 42 --------------------------
 lib/eal/linux/eal.c                 | 39 ------------------------
 lib/eal/windows/eal.c               | 35 ----------------------
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index e541f07939..5435399b85 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -1640,6 +1640,51 @@ eal_parse_huge_unlink(const char *arg, struct hugepage_file_discipline *out)
 	return -1;
 }
 
+/* Parse the all arguments looking for log related ones */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_parse_common_option(int opt, const char *optarg,
 			struct internal_config *conf)
@@ -2173,6 +2218,7 @@ rte_vect_set_max_simd_bitwidth(uint16_t bitwidth)
 	return 0;
 }
 
+
 void
 eal_common_usage(void)
 {
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb6412..f3f2e104f6 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -96,6 +96,7 @@ enum {
 extern const char eal_short_options[];
 extern const struct option eal_long_options[];
 
+int eal_log_level_parse(int argc, char * const argv[]);
 int eal_parse_common_option(int opt, const char *argv,
 			    struct internal_config *conf);
 int eal_option_device_parse(void);
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bab77118e9..9825bcea0b 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -363,48 +363,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index fd422f1f62..bffeb1f34e 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 52f0e7462d..85171b2768 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 3/9] eal: do not duplicate rte_init_alert() messages
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 1/9] windows: make getopt functions have const properties Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 2/9] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 4/9] eal: change rte_exit() output to match rte_log() Stephen Hemminger
                     ` (6 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

The message already goes through logging, and does not need
to be printed on stderr. Message level should be ALERT
to match function name.

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

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 9825bcea0b..17b56f38aa 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -529,8 +529,7 @@ rte_eal_iopl_init(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /* Launch threads, called at application init(). */
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index bffeb1f34e..23dc26b124 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -840,8 +840,7 @@ static int rte_eal_vfio_setup(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /*
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 4/9] eal: change rte_exit() output to match rte_log()
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                     ` (2 preceding siblings ...)
  2024-03-24  2:33   ` [PATCH v11 3/9] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 5/9] log: drop syslog support, and make code common Stephen Hemminger
                     ` (5 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The rte_exit() output format confuses the timestamp and coloring
options. Change it to use be a single line with proper prefix.

Before:
[     0.006481] EAL: Error - exiting with code: 1
  Cause: [     0.006489] Cannot init EAL: Permission denied

After:
[     0.006238] EAL: Error - exiting with code: 1
[     0.006250] EAL: Cause - Cannot init EAL: Permission denied

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_debug.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/eal/common/eal_common_debug.c b/lib/eal/common/eal_common_debug.c
index 3e77995896..3f37879144 100644
--- a/lib/eal/common/eal_common_debug.c
+++ b/lib/eal/common/eal_common_debug.c
@@ -34,17 +34,18 @@ void
 rte_exit(int exit_code, const char *format, ...)
 {
 	va_list ap;
+	char *msg = NULL;
 
 	if (exit_code != 0)
-		RTE_LOG(CRIT, EAL, "Error - exiting with code: %d\n"
-				"  Cause: ", exit_code);
+		EAL_LOG(CRIT, "Error - exiting with code: %d", exit_code);
 
 	va_start(ap, format);
-	rte_vlog(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, format, ap);
+	vasprintf(&msg, format, ap);
 	va_end(ap);
 
+	rte_log(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, "EAL: Cause - %s", msg);
+
 	if (rte_eal_cleanup() != 0 && rte_errno != EALREADY)
-		EAL_LOG(CRIT,
-			"EAL could not release all resources");
+		EAL_LOG(CRIT, "EAL could not release all resources");
 	exit(exit_code);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 5/9] log: drop syslog support, and make code common
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                     ` (3 preceding siblings ...)
  2024-03-24  2:33   ` [PATCH v11 4/9] eal: change rte_exit() output to match rte_log() Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 6/9] log: add hook for printing log messages Stephen Hemminger
                     ` (4 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Anatoly Burakov, Bruce Richardson,
	Dmitry Kozlyuk, Tyler Retzlaff, Pallavi Kadam

This patch removes syslog support from logging. Which allows the
logging code to all be common across Linux, Windows and FreeBSD.
It also initializes log subsystem much earlier in the init
process so that all messages get processed.

It drops syslog was only used on Linux. Modern Linux systems have
systemd and journal support. A later patch will add direct
native journal support. Removing syslog means lots of other
parts of the log system can be simplified and made portable.
Remove the syslog tests and documentation as well.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c                     | 21 +------
 doc/guides/linux_gsg/linux_eal_parameters.rst | 27 --------
 .../prog_guide/env_abstraction_layer.rst      |  5 +-
 doc/guides/prog_guide/log_lib.rst             |  3 +-
 lib/eal/common/eal_common_options.c           | 62 -------------------
 lib/eal/common/eal_internal_cfg.h             |  2 +-
 lib/eal/common/eal_options.h                  |  2 -
 lib/eal/freebsd/eal.c                         |  9 +--
 lib/eal/linux/eal.c                           | 10 +--
 lib/eal/unix/eal_debug.c                      |  3 +-
 lib/eal/windows/eal.c                         |  4 +-
 lib/log/log.c                                 | 46 +++++---------
 lib/log/log_freebsd.c                         | 12 ----
 lib/log/log_internal.h                        |  2 +-
 lib/log/log_linux.c                           | 61 ------------------
 lib/log/log_windows.c                         | 18 ------
 lib/log/meson.build                           |  5 +-
 17 files changed, 34 insertions(+), 258 deletions(-)
 delete mode 100644 lib/log/log_freebsd.c
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 6cb4b06757..bea5465168 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -984,17 +984,10 @@ test_misc_flags(void)
 	const char *argv1[] = {prgname, prefix, mp_flag, "--no-pci"};
 	/* With -v */
 	const char *argv2[] = {prgname, prefix, mp_flag, "-v"};
-	/* With valid --syslog */
-	const char *argv3[] = {prgname, prefix, mp_flag,
-			"--syslog", "syslog"};
-	/* With empty --syslog (should fail) */
-	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog"};
-	/* With invalid --syslog */
-	const char *argv5[] = {prgname, prefix, mp_flag, "--syslog", "error"};
+
 	/* With no-sh-conf, also use no-huge to ensure this test runs on BSD */
 	const char *argv6[] = {prgname, "-m", DEFAULT_MEM_SIZE,
 			no_shconf, nosh_prefix, no_huge};
-
 	/* With --huge-dir */
 	const char *argv7[] = {prgname, "-m", DEFAULT_MEM_SIZE,
 			"--file-prefix=hugedir", "--huge-dir", hugepath};
@@ -1079,18 +1072,6 @@ test_misc_flags(void)
 	return 0;
 #endif
 
-	if (launch_proc(argv3) != 0) {
-		printf("Error - process did not run ok with --syslog flag\n");
-		goto fail;
-	}
-	if (launch_proc(argv4) == 0) {
-		printf("Error - process run ok with empty --syslog flag\n");
-		goto fail;
-	}
-	if (launch_proc(argv5) == 0) {
-		printf("Error - process run ok with invalid --syslog flag\n");
-		goto fail;
-	}
 	if (launch_proc(argv7) != 0) {
 		printf("Error - process did not run ok with --huge-dir flag\n");
 		goto fail;
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f381391..d86f94d8a8 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -108,30 +108,3 @@ Memory-related options
 *   ``--match-allocations``
 
     Free hugepages back to system exactly as they were originally allocated.
-
-Other options
-~~~~~~~~~~~~~
-
-*   ``--syslog <syslog facility>``
-
-    Set syslog facility. Valid syslog facilities are::
-
-        auth
-        cron
-        daemon
-        ftp
-        kern
-        lpr
-        mail
-        news
-        syslog
-        user
-        uucp
-        local0
-        local1
-        local2
-        local3
-        local4
-        local5
-        local6
-        local7
diff --git a/doc/guides/prog_guide/env_abstraction_layer.rst b/doc/guides/prog_guide/env_abstraction_layer.rst
index 9559c12a98..9a73628907 100644
--- a/doc/guides/prog_guide/env_abstraction_layer.rst
+++ b/doc/guides/prog_guide/env_abstraction_layer.rst
@@ -855,9 +855,8 @@ Signal Safety
   Other functions are not signal safe because they use one or more
   library routines that are not themselves signal safe.
   For example, calling ``rte_panic()`` is not safe in a signal handler
-  because it uses ``rte_log()`` and ``rte_log()`` calls the
-  ``syslog()`` library function which is in the list of
-  signal safe functions in
+  because it uses ``rte_log()`` and ``rte_log()`` calls library functions
+  that are not all signal safe.
   `Signal-Safety manual page <https://man7.org/linux/man-pages/man7/signal-safety.7.html>`_.
 
   The set of functions that are expected to be async-signal-safe in DPDK
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index ff9d1b54a2..17ed8426b2 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -5,8 +5,7 @@ Log Library
 ===========
 
 The DPDK Log library provides the logging functionality for other DPDK libraries and drivers.
-By default, in a Linux application, logs are sent to syslog and also to the console.
-On FreeBSD and Windows applications, logs are sent only to the console.
+By default, log messages are sent to the stderr.
 However, the log function can be overridden by the user to use a different logging mechanism.
 
 Log Levels
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 5435399b85..73167d4603 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -6,9 +6,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#ifndef RTE_EXEC_ENV_WINDOWS
-#include <syslog.h>
-#endif
 #include <ctype.h>
 #include <limits.h>
 #include <errno.h>
@@ -93,7 +90,6 @@ eal_long_options[] = {
 	{OPT_PROC_TYPE,         1, NULL, OPT_PROC_TYPE_NUM        },
 	{OPT_SOCKET_MEM,        1, NULL, OPT_SOCKET_MEM_NUM       },
 	{OPT_SOCKET_LIMIT,      1, NULL, OPT_SOCKET_LIMIT_NUM     },
-	{OPT_SYSLOG,            1, NULL, OPT_SYSLOG_NUM           },
 	{OPT_VDEV,              1, NULL, OPT_VDEV_NUM             },
 	{OPT_VFIO_INTR,         1, NULL, OPT_VFIO_INTR_NUM        },
 	{OPT_VFIO_VF_TOKEN,     1, NULL, OPT_VFIO_VF_TOKEN_NUM    },
@@ -349,10 +345,6 @@ eal_reset_internal_config(struct internal_config *internal_cfg)
 	}
 	internal_cfg->base_virtaddr = 0;
 
-#ifdef LOG_DAEMON
-	internal_cfg->syslog_facility = LOG_DAEMON;
-#endif
-
 	/* if set to NONE, interrupt mode is determined automatically */
 	internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
 	memset(internal_cfg->vfio_vf_token, 0,
@@ -1297,47 +1289,6 @@ eal_parse_lcores(const char *lcores)
 	return ret;
 }
 
-#ifndef RTE_EXEC_ENV_WINDOWS
-static int
-eal_parse_syslog(const char *facility, struct internal_config *conf)
-{
-	int i;
-	static const struct {
-		const char *name;
-		int value;
-	} map[] = {
-		{ "auth", LOG_AUTH },
-		{ "cron", LOG_CRON },
-		{ "daemon", LOG_DAEMON },
-		{ "ftp", LOG_FTP },
-		{ "kern", LOG_KERN },
-		{ "lpr", LOG_LPR },
-		{ "mail", LOG_MAIL },
-		{ "news", LOG_NEWS },
-		{ "syslog", LOG_SYSLOG },
-		{ "user", LOG_USER },
-		{ "uucp", LOG_UUCP },
-		{ "local0", LOG_LOCAL0 },
-		{ "local1", LOG_LOCAL1 },
-		{ "local2", LOG_LOCAL2 },
-		{ "local3", LOG_LOCAL3 },
-		{ "local4", LOG_LOCAL4 },
-		{ "local5", LOG_LOCAL5 },
-		{ "local6", LOG_LOCAL6 },
-		{ "local7", LOG_LOCAL7 },
-		{ NULL, 0 }
-	};
-
-	for (i = 0; map[i].name; i++) {
-		if (!strcmp(facility, map[i].name)) {
-			conf->syslog_facility = map[i].value;
-			return 0;
-		}
-	}
-	return -1;
-}
-#endif
-
 static void
 eal_log_usage(void)
 {
@@ -1880,16 +1831,6 @@ eal_parse_common_option(int opt, const char *optarg,
 		}
 		break;
 
-#ifndef RTE_EXEC_ENV_WINDOWS
-	case OPT_SYSLOG_NUM:
-		if (eal_parse_syslog(optarg, conf) < 0) {
-			EAL_LOG(ERR, "invalid parameters for --"
-					OPT_SYSLOG);
-			return -1;
-		}
-		break;
-#endif
-
 	case OPT_LOG_LEVEL_NUM: {
 		if (eal_parse_log_level(optarg) < 0) {
 			EAL_LOG(ERR,
@@ -2259,9 +2200,6 @@ eal_common_usage(void)
 	       "                      (can be used multiple times)\n"
 	       "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
-#ifndef RTE_EXEC_ENV_WINDOWS
-	       "  --"OPT_SYSLOG"            Set syslog facility\n"
-#endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
 	       "                      Set specific log level\n"
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index 167ec501fa..ed56a58b8b 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -84,7 +84,7 @@ struct internal_config {
 	/**< true if storing all pages within single files (per-page-size,
 	 * per-node) non-legacy mode only.
 	 */
-	volatile int syslog_facility;	  /**< facility passed to openlog() */
+
 	/** default interrupt mode for VFIO */
 	volatile enum rte_intr_mode vfio_intr_mode;
 	/** the shared VF token for VFIO-PCI bound PF and VFs devices */
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index f3f2e104f6..6b204d6698 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -63,8 +63,6 @@ enum {
 	OPT_SOCKET_MEM_NUM,
 #define OPT_SOCKET_LIMIT        "socket-limit"
 	OPT_SOCKET_LIMIT_NUM,
-#define OPT_SYSLOG            "syslog"
-	OPT_SYSLOG_NUM,
 #define OPT_VDEV              "vdev"
 	OPT_VDEV_NUM,
 #define OPT_VFIO_INTR         "vfio-intr"
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 17b56f38aa..d8628ba632 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -11,7 +11,6 @@
 #include <stdarg.h>
 #include <unistd.h>
 #include <pthread.h>
-#include <syslog.h>
 #include <getopt.h>
 #include <sys/file.h>
 #include <stddef.h>
@@ -563,12 +562,14 @@ rte_eal_init(int argc, char **argv)
 
 	eal_reset_internal_config(internal_conf);
 
-	/* clone argv to report out later in telemetry */
-	eal_save_args(argc, argv);
-
 	/* set log level as early as possible */
 	eal_log_level_parse(argc, argv);
 
+	eal_log_init(getprogname());
+
+	/* clone argv to report out later in telemetry */
+	eal_save_args(argc, argv);
+
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 23dc26b124..913ee62fc6 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -955,6 +955,8 @@ rte_eal_init(int argc, char **argv)
 	/* set log level as early as possible */
 	eal_log_level_parse(argc, argv);
 
+	eal_log_init(program_invocation_short_name);
+
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
@@ -1106,14 +1108,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;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
diff --git a/lib/eal/unix/eal_debug.c b/lib/eal/unix/eal_debug.c
index 69ba3758c2..c830cf7f05 100644
--- a/lib/eal/unix/eal_debug.c
+++ b/lib/eal/unix/eal_debug.c
@@ -43,8 +43,7 @@ static char *safe_itoa(long val, char *buf, size_t len, unsigned int radix)
  * used in this code since may be called from inside libc or
  * when malloc poll is corrupt.
  *
- * Most of libc is therefore not safe, include RTE_LOG (calls syslog);
- * backtrace_symbols (calls malloc), etc.
+ * Most of libc is therefore not safe including backtrace_symbols (calls malloc), etc.
  */
 void rte_dump_stack(void)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 85171b2768..10f7cb840a 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -250,10 +250,10 @@ rte_eal_init(int argc, char **argv)
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_THREAD_NAME_SIZE];
 
-	eal_log_init(NULL, 0);
-
 	eal_log_level_parse(argc, argv);
 
+	eal_log_init(NULL);
+
 	if (eal_create_cpu_map() < 0) {
 		rte_eal_init_alert("Cannot discover CPU and NUMA.");
 		/* rte_errno is set */
diff --git a/lib/log/log.c b/lib/log/log.c
index 255f757d94..4c8666ac93 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -55,9 +55,6 @@ TAILQ_HEAD(rte_eal_opt_loglevel_list, rte_eal_opt_loglevel);
 static struct rte_eal_opt_loglevel_list opt_loglevel_list =
 	TAILQ_HEAD_INITIALIZER(opt_loglevel_list);
 
-/* Stream to use for logging if rte_logs.file is NULL */
-static FILE *default_log_stream;
-
 /**
  * This global structure stores some information about the message
  * that is currently being processed by one lcore
@@ -76,6 +73,9 @@ static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
 int
 rte_openlog_stream(FILE *f)
 {
+	if (rte_logs.file != NULL)
+		fclose(rte_logs.file);
+
 	rte_logs.file = f;
 	return 0;
 }
@@ -85,17 +85,7 @@ rte_log_get_stream(void)
 {
 	FILE *f = rte_logs.file;
 
-	if (f == NULL) {
-		/*
-		 * Grab the current value of stderr here, rather than
-		 * just initializing default_log_stream to stderr. This
-		 * ensures that we will always use the current value
-		 * of stderr, even if the application closes and
-		 * reopens it.
-		 */
-		return default_log_stream != NULL ? default_log_stream : stderr;
-	}
-	return f;
+	return (f == NULL) ? stderr : f;
 }
 
 /* Set global log level */
@@ -506,27 +496,25 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
 }
 
 /*
- * Called by environment-specific initialization functions.
+ * Called by eal_cleanup
  */
 void
-eal_log_set_default(FILE *default_log)
+rte_eal_log_cleanup(void)
 {
-	default_log_stream = default_log;
+	FILE *f = rte_logs.file;
 
-#if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
-	RTE_LOG(NOTICE, EAL,
-		"Debug dataplane logs available - lower performance\n");
-#endif
+	if (f != NULL) {
+		fclose(f);
+		rte_logs.file = NULL;
+	}
 }
 
-/*
- * Called by eal_cleanup
- */
+/* initialize logging */
 void
-rte_eal_log_cleanup(void)
+eal_log_init(const char *id __rte_unused)
 {
-	if (default_log_stream) {
-		fclose(default_log_stream);
-		default_log_stream = NULL;
-	}
+#if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
+	RTE_LOG(NOTICE, EAL,
+		"Debug dataplane logs available - lower performance\n");
+#endif
 }
diff --git a/lib/log/log_freebsd.c b/lib/log/log_freebsd.c
deleted file mode 100644
index 698d3c5423..0000000000
--- a/lib/log/log_freebsd.c
+++ /dev/null
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2023 Intel Corporation
- */
-
-#include <rte_common.h>
-#include "log_internal.h"
-
-int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
-{
-	return 0;
-}
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 451629f1c1..4f1ffe999e 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -14,7 +14,7 @@
  * Initialize the default log stream.
  */
 __rte_internal
-int eal_log_init(const char *id, int facility);
+void eal_log_init(const char *id);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
deleted file mode 100644
index 2dfb0c974b..0000000000
--- a/lib/log/log_linux.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <syslog.h>
-
-#include <rte_log.h>
-
-#include "log_internal.h"
-
-/*
- * default log function
- */
-static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
-{
-	ssize_t ret;
-
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
-
-	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
-	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
-
-	return ret;
-}
-
-static int
-console_log_close(__rte_unused void *c)
-{
-	closelog();
-	return 0;
-}
-
-static cookie_io_functions_t console_log_func = {
-	.write = console_log_write,
-	.close = console_log_close,
-};
-
-/*
- * set the log to default function, called during eal init process,
- * once memzones are available.
- */
-int
-eal_log_init(const char *id, int facility)
-{
-	FILE *log_stream;
-
-	log_stream = fopencookie(NULL, "w+", console_log_func);
-	if (log_stream == NULL)
-		return -1;
-
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
-
-	eal_log_set_default(log_stream);
-
-	return 0;
-}
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
deleted file mode 100644
index a6a0889550..0000000000
--- a/lib/log/log_windows.c
+++ /dev/null
@@ -1,18 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2017-2018 Intel Corporation
- */
-
-#include <rte_common.h>
-#include <rte_log.h>
-#include "log_internal.h"
-
-/* set the log to default function, called during eal init process. */
-int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
-{
-	rte_openlog_stream(stderr);
-
-	eal_log_set_default(stderr);
-
-	return 0;
-}
diff --git a/lib/log/meson.build b/lib/log/meson.build
index 0d4319b36f..891f77a237 100644
--- a/lib/log/meson.build
+++ b/lib/log/meson.build
@@ -2,8 +2,5 @@
 # Copyright(c) 2023 Intel Corporation
 
 includes += global_inc
-sources = files(
-        'log.c',
-        'log_' + exec_env + '.c',
-)
+sources = files('log.c')
 headers = files('rte_log.h')
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 6/9] log: add hook for printing log messages
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                     ` (4 preceding siblings ...)
  2024-03-24  2:33   ` [PATCH v11 5/9] log: drop syslog support, and make code common Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 7/9] log: add timestamp option Stephen Hemminger
                     ` (3 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This is useful for when decorating log output for console
or journal. Provide basic version in this patch.
No functional changes in this patch, just infrastructure addition.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/log/log.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/lib/log/log.c b/lib/log/log.c
index 4c8666ac93..4cc871911c 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -26,16 +26,21 @@ struct rte_log_dynamic_type {
 	uint32_t loglevel;
 };
 
+typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
+static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
+
 /** The rte_log structure. */
 static struct rte_logs {
 	uint32_t type;  /**< Bitfield with enabled logs. */
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
+	log_print_t print_func;
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
 	.type = UINT32_MAX,
 	.level = RTE_LOG_DEBUG,
+	.print_func = log_print,
 };
 
 struct rte_eal_opt_loglevel {
@@ -67,8 +72,6 @@ struct log_cur_msg {
  /* per core log */
 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
 
-/* default logs */
-
 /* Change the stream that will be used by logging system */
 int
 rte_openlog_stream(FILE *f)
@@ -77,6 +80,7 @@ rte_openlog_stream(FILE *f)
 		fclose(rte_logs.file);
 
 	rte_logs.file = f;
+	rte_logs.print_func = log_print;
 	return 0;
 }
 
@@ -473,7 +477,7 @@ rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
 	RTE_PER_LCORE(log_cur_msg).loglevel = level;
 	RTE_PER_LCORE(log_cur_msg).logtype = logtype;
 
-	ret = vfprintf(f, format, ap);
+	ret = (*rte_logs.print_func)(f, level, format, ap);
 	fflush(f);
 	return ret;
 }
@@ -509,6 +513,15 @@ rte_eal_log_cleanup(void)
 	}
 }
 
+/* default log print function */
+__rte_format_printf(3, 0)
+static int
+log_print(FILE *f, uint32_t level __rte_unused,
+	  const char *format, va_list ap)
+{
+	return vfprintf(f, format, ap);
+}
+
 /* initialize logging */
 void
 eal_log_init(const char *id __rte_unused)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 7/9] log: add timestamp option
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                     ` (5 preceding siblings ...)
  2024-03-24  2:33   ` [PATCH v11 6/9] log: add hook for printing log messages Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 8/9] log: add support for systemd journal Stephen Hemminger
                     ` (2 subsequent siblings)
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing.

There are multiple timestamp formats similar to Linux dmesg.
The default is time relative since startup (when first
step of logging initialization is done by constructor).
Other alternative formats are delta, ctime, reltime and iso formats.

Example:
$ dpdk-testpmd --log-timestamp -- -i
[     0.008610] EAL: Detected CPU lcores: 8
[     0.008634] EAL: Detected NUMA nodes: 1
[     0.008792] EAL: Detected static linkage of DPDK
[     0.010620] EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
[     0.012618] EAL: Selected IOVA mode 'VA'
[     0.016675] testpmd: No probed ethernet devices
Interactive-mode selected

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  17 +++
 doc/guides/prog_guide/log_lib.rst   |  26 +++++
 lib/eal/common/eal_common_options.c |  14 ++-
 lib/eal/common/eal_options.h        |   2 +
 lib/log/log.c                       | 168 +++++++++++++++++++++++++++-
 lib/log/log_internal.h              |   9 ++
 lib/log/version.map                 |   1 +
 7 files changed, 234 insertions(+), 3 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index bea5465168..def74e34d5 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1048,6 +1048,14 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
+	/* Try running with --log-timestamp=iso */
+	const char * const argv24[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp=iso" };
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1143,6 +1151,15 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+	if (launch_proc(argv24) != 0) {
+		printf("Error - process did not run ok with --log-timestamp=iso parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index 17ed8426b2..83949cce35 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -58,6 +58,32 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+Console timestamp
+~~~~~~~~~~~~~~~~~
+
+An optional timestamp can be added before each message when printing on the console
+by adding the ``--log-timestamp`` option.
+For example::
+
+	/path/to/app --log-level=lib.*:debug --log-timestamp
+
+Multiple timestamp alternative timestamp formats are available:
+
+.. csv-table:: Log time stamp format
+   :header: "Format", "Description", "Example"
+   :widths: 6, 30, 32
+
+   "ctime", "Unix ctime", "``[Wed Mar 20 07:26:12 2024]``"
+   "delta", "Offset since last", "``[<    3.162373>]``"
+   "reltime", "Seconds since last or time if minute changed", "``[  +3.001791]`` or ``[Mar20 07:26:12]``"
+   "iso", "ISO-8601", "``[2024-03-20T07:26:12−07:00]``"
+
+To prefix all console messages with ISO format time the syntax is::
+
+	/path/to/app --log-timestamp=iso
+
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 73167d4603..bc6d87266d 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -74,6 +74,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     2, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1614,6 +1615,7 @@ eal_log_level_parse(int argc, char * const argv[])
 
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_LOG_TIMESTAMP_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1831,7 +1833,7 @@ eal_parse_common_option(int opt, const char *optarg,
 		}
 		break;
 
-	case OPT_LOG_LEVEL_NUM: {
+	case OPT_LOG_LEVEL_NUM:
 		if (eal_parse_log_level(optarg) < 0) {
 			EAL_LOG(ERR,
 				"invalid parameters for --"
@@ -1839,7 +1841,14 @@ eal_parse_common_option(int opt, const char *optarg,
 			return -1;
 		}
 		break;
-	}
+
+	case OPT_LOG_TIMESTAMP_NUM:
+		if (eal_log_timestamp(optarg) < 0) {
+			EAL_LOG(ERR, "invalid parameters for --"
+				OPT_LOG_TIMESTAMP);
+			return -1;
+		}
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -2204,6 +2213,7 @@ eal_common_usage(void)
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
 	       "                      Set specific log level\n"
 	       "  --"OPT_LOG_LEVEL"=help    Show log types and levels\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_TRACE"=<regex-match>\n"
 	       "                      Enable trace based on regular expression trace name.\n"
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 6b204d6698..197b7b25ef 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/log/log.c b/lib/log/log.c
index 4cc871911c..bd488dff1b 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -11,6 +11,7 @@
 #include <regex.h>
 #include <fnmatch.h>
 #include <sys/queue.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 #include <rte_per_lcore.h>
@@ -18,7 +19,7 @@
 #include "log_internal.h"
 
 #ifdef RTE_EXEC_ENV_WINDOWS
-#define strdup _strdup
+#include <rte_os_shim.h>
 #endif
 
 struct rte_log_dynamic_type {
@@ -26,6 +27,15 @@ struct rte_log_dynamic_type {
 	uint32_t loglevel;
 };
 
+enum eal_log_time_format {
+	EAL_LOG_TIMESTAMP_NONE = 0,
+	EAL_LOG_TIMESTAMP_TIME,		/* time since start */
+	EAL_LOG_TIMESTAMP_DELTA,	/* time since last message */
+	EAL_LOG_TIMESTAMP_RELTIME,
+	EAL_LOG_TIMESTAMP_CTIME,
+	EAL_LOG_TIMESTAMP_ISO,
+};
+
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
@@ -35,6 +45,12 @@ static struct rte_logs {
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
 	log_print_t print_func;
+
+	enum eal_log_time_format time_format;
+	struct timespec started;   /* when log was initialized */
+	struct timespec previous;  /* when last msg was printed */
+	struct tm last_tm;	   /*  in local time format */
+
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
@@ -362,6 +378,9 @@ RTE_INIT_PRIO(log_init, LOG)
 {
 	uint32_t i;
 
+	clock_gettime(CLOCK_MONOTONIC, &rte_logs.started);
+	rte_logs.previous = rte_logs.started;
+
 	rte_log_set_global_level(RTE_LOG_DEBUG);
 
 	rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
@@ -513,6 +532,132 @@ rte_eal_log_cleanup(void)
 	}
 }
 
+/* Set the log timestamp format */
+int
+eal_log_timestamp(const char *str)
+{
+	if (str == NULL)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_TIME;
+	else if (strcmp(str, "notime") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_NONE;
+	else if (strcmp(str, "reltime") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_RELTIME;
+	else if (strcmp(str, "delta") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_DELTA;
+	else if (strcmp(str, "ctime") == 0)
+		rte_logs.time_format =  EAL_LOG_TIMESTAMP_CTIME;
+	else if (strcmp(str, "iso") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_ISO;
+	else
+		return -1;
+
+	return 0;
+}
+
+/* Subtract two timespec values and handle wraparound */
+static struct timespec
+timespec_sub(const struct timespec *t0, const struct timespec *t1)
+{
+	struct timespec ts;
+
+	ts.tv_sec = t0->tv_sec - t1->tv_sec;
+	ts.tv_nsec = t0->tv_nsec - t1->tv_nsec;
+	if (ts.tv_nsec < 0) {
+		ts.tv_sec--;
+		ts.tv_nsec += 1000000000L;
+	}
+	return ts;
+}
+
+/* Format up a timestamp based on current format */
+static ssize_t
+format_timestamp(char *tsbuf, size_t tsbuflen)
+{
+	struct timespec now, delta;
+	struct tm *tm;
+
+	switch (rte_logs.time_format) {
+	case EAL_LOG_TIMESTAMP_NONE:
+		return 0;
+
+	case EAL_LOG_TIMESTAMP_TIME:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		delta = timespec_sub(&now, &rte_logs.started);
+
+		return snprintf(tsbuf, tsbuflen, "%6lu.%06lu",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_DELTA:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		delta = timespec_sub(&now, &rte_logs.previous);
+		rte_logs.previous = now;
+
+		return snprintf(tsbuf, tsbuflen, "<%6lu.%06lu>",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_RELTIME:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		tm = localtime(&now.tv_sec);
+		delta = timespec_sub(&now, &rte_logs.previous);
+		rte_logs.previous = now;
+
+		/* if minute, day, hour hasn't changed then print delta */
+		if (tm->tm_min != rte_logs
+.last_tm.tm_min ||
+		    tm->tm_hour != rte_logs.last_tm.tm_hour ||
+		    tm->tm_yday != rte_logs.last_tm.tm_yday) {
+			rte_logs.last_tm = *tm;
+			return strftime(tsbuf, tsbuflen, "%b%d %H:%M", tm);
+		} else {
+			return snprintf(tsbuf, tsbuflen, "+%4lu.%06lu",
+					(unsigned long)delta.tv_sec,
+					(unsigned long)delta.tv_nsec / 1000u);
+		}
+
+	case EAL_LOG_TIMESTAMP_CTIME:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		/* trncate to remove newline from ctime result */
+		return snprintf(tsbuf, tsbuflen, "%.24s", ctime(&now.tv_sec));
+
+	case EAL_LOG_TIMESTAMP_ISO: {
+		char dbuf[64]; /* "2024-05-01T22:11:00" */
+		char zbuf[16]; /* "+0800" */
+
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		tm = localtime(&now.tv_sec);
+
+		/* make "2024-05-01T22:11:00,123456+0100" */
+		if (strftime(dbuf, sizeof(dbuf), "%Y-%m-%dT%H:%M:%S", tm) == 0)
+			return 0;
+
+		/* convert timezone to +HH:MM */
+		if (strftime(zbuf, sizeof(zbuf), "%z", tm) == 0)
+			return 0;
+		/* insert : required in ISO */
+		memmove(zbuf + 3, zbuf + 4, 2);
+		zbuf[3] = ':';
+		zbuf[6] = '\0';
+
+		return snprintf(tsbuf, tsbuflen, "%s,%06lu%s",
+				dbuf, now.tv_nsec / 1000u, zbuf);
+		}
+	}
+
+	return 0;
+}
+
 /* default log print function */
 __rte_format_printf(3, 0)
 static int
@@ -522,10 +667,31 @@ log_print(FILE *f, uint32_t level __rte_unused,
 	return vfprintf(f, format, ap);
 }
 
+/* print timestamp before message */
+__rte_format_printf(3, 0)
+static int
+log_print_with_timestamp(FILE *f, uint32_t level,
+			 const char *format, va_list ap)
+{
+	char tsbuf[128];
+
+	if (format_timestamp(tsbuf, sizeof(tsbuf)) > 0)
+		fprintf(f, "[%s] ", tsbuf);
+
+	return log_print(f, level, format, ap);
+}
+
 /* initialize logging */
 void
 eal_log_init(const char *id __rte_unused)
 {
+	/* skip if user has already setup a log stream */
+	if (rte_logs.file != NULL)
+		return;
+
+	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
+		rte_logs.print_func = log_print_with_timestamp;
+
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 	RTE_LOG(NOTICE, EAL,
 		"Debug dataplane logs available - lower performance\n");
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 4f1ffe999e..3544ae49ac 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -5,8 +5,10 @@
 #ifndef LOG_INTERNAL_H
 #define LOG_INTERNAL_H
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <time.h>
 
 #include <rte_compat.h>
 
@@ -42,4 +44,11 @@ const char *eal_log_level2str(uint32_t level);
 __rte_internal
 void rte_eal_log_cleanup(void);
 
+/*
+ * Add timestamp to console logs
+ */
+__rte_internal
+int eal_log_timestamp(const char *fmt);
+
+
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/version.map b/lib/log/version.map
index 0648f8831a..7fd5b39e3a 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -30,5 +30,6 @@ INTERNAL {
 	eal_log_save_pattern;
 	eal_log_save_regexp;
 	eal_log_set_default;
+	eal_log_timestamp;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 8/9] log: add support for systemd journal
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                     ` (6 preceding siblings ...)
  2024-03-24  2:33   ` [PATCH v11 7/9] log: add timestamp option Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24  2:33   ` [PATCH v11 9/9] log: colorize log output Stephen Hemminger
  2024-03-24 11:18   ` [PATCH v11 0/9] Logging unification and enhancements Mattias Rönnblom
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

If DPDK application is being run as a systemd service, then
it can use the journal protocol which allows putting more information
in the log such as priority and other information.

The use of journal protocol is automatically detected and
handled.  Rather than having a dependency on libsystemd,
just use the protocol directly as defined in:
	https://systemd.io/JOURNAL_NATIVE_PROTOCOL/

The journal protocol supports more information that could
be added later.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/log/log.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 136 insertions(+), 5 deletions(-)

diff --git a/lib/log/log.c b/lib/log/log.c
index bd488dff1b..4cfa160e3b 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -13,15 +13,20 @@
 #include <sys/queue.h>
 #include <unistd.h>
 
+#ifdef RTE_EXEC_ENV_WINDOWS
+#include <rte_os_shim.h>
+#else
+#include <sys/uio.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#endif
+
 #include <rte_log.h>
 #include <rte_per_lcore.h>
 
 #include "log_internal.h"
 
-#ifdef RTE_EXEC_ENV_WINDOWS
-#include <rte_os_shim.h>
-#endif
-
 struct rte_log_dynamic_type {
 	const char *name;
 	uint32_t loglevel;
@@ -39,11 +44,13 @@ enum eal_log_time_format {
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
+
 /** The rte_log structure. */
 static struct rte_logs {
 	uint32_t type;  /**< Bitfield with enabled logs. */
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
+	int journal_fd;	/**< Journal file descriptor if using */
 	log_print_t print_func;
 
 	enum eal_log_time_format time_format;
@@ -681,6 +688,116 @@ log_print_with_timestamp(FILE *f, uint32_t level,
 	return log_print(f, level, format, ap);
 }
 
+#ifdef RTE_EXEC_ENV_LINUX
+/*
+ * send message using journal protocol to journald
+ */
+__rte_format_printf(3, 0)
+static int
+journal_print(FILE *f __rte_unused, uint32_t level, const char *format, va_list ap)
+{
+	struct iovec iov[3];
+	char *buf = NULL;
+	size_t len;
+	char msg[] = "MESSAGE=";
+	char *prio;
+
+	iov[0].iov_base = msg;
+	iov[0].iov_len = strlen(msg);
+
+	len = vasprintf(&buf, format, ap);
+	if (len == 0)
+		return 0;
+
+	/* check that message ends with newline */
+	if (buf[len - 1] != '\n') {
+		char *clone  = alloca(len + 1);
+		if (clone == NULL)
+			return 0;
+		memcpy(clone, buf, len);
+		clone[len++] = '\n';
+		buf = clone;
+	}
+
+	iov[1].iov_base = buf;
+	iov[1].iov_len = len;
+
+	/* priority value between 0 ("emerg") and 7 ("debug") */
+	len = asprintf(&prio, "PRIORITY=%i\n", level - 1);
+	iov[2].iov_base = prio;
+	iov[2].iov_len = len;
+
+	return writev(rte_logs.journal_fd, iov, 3);
+}
+
+static void
+journal_send_id(int fd, const char *id)
+{
+	char *syslog_id = NULL;
+	size_t len;
+
+	len = asprintf(&syslog_id, "SYSLOG_IDENTIFIER=%s\n", id);
+	if (len > 0)
+		write(fd, syslog_id, len);
+
+}
+
+/*
+ * Check if stderr is going to system journal.
+ * This is the documented way to handle systemd journal
+ *
+ * See: https://systemd.io/JOURNAL_NATIVE_PROTOCOL/
+ *
+ */
+static bool
+using_journal(void)
+{
+	char *jenv, *endp = NULL;
+	struct stat st;
+	unsigned long dev, ino;
+
+	jenv = getenv("JOURNAL_STREAM");
+	if (jenv == NULL)
+		return false;
+
+	if (fstat(STDERR_FILENO, &st) < 0)
+		return false;
+
+	/* systemd sets colon-separated list of device and inode number */
+	dev = strtoul(jenv, &endp, 10);
+	if (endp == NULL || *endp != ':')
+		return false;	/* missing colon */
+
+	ino = strtoul(endp + 1, NULL, 10);
+
+	return dev == st.st_dev && ino == st.st_ino;
+}
+
+/*
+ * If we are being run as systemd service and stderr is going to journal
+ * then upgrade to use journal protocol.
+ */
+static int
+open_journal(void)
+{
+	struct sockaddr_un sun = {
+		.sun_family = AF_UNIX,
+		.sun_path = "/run/systemd/journal/socket",
+	};
+	int s;
+
+	s = socket(AF_UNIX, SOCK_DGRAM, 0);
+	if (s < 0)
+		return -1;
+
+	if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
+		close(s);
+		return -1;
+	}
+	return s;
+}
+#endif
+
 /* initialize logging */
 void
 eal_log_init(const char *id __rte_unused)
@@ -689,8 +806,22 @@ eal_log_init(const char *id __rte_unused)
 	if (rte_logs.file != NULL)
 		return;
 
-	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
+#ifdef RTE_EXEC_ENV_LINUX
+	if (using_journal()) {
+		int jfd = open_journal();
+
+		if (jfd < 0) {
+			RTE_LOG_LINE(NOTICE, EAL, "Cannot connect to journal");
+		} else {
+			rte_logs.journal_fd = jfd;
+			rte_logs.print_func = journal_print;
+			journal_send_id(jfd, id);
+		}
+	} else
+#endif
+	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE) {
 		rte_logs.print_func = log_print_with_timestamp;
+	}
 
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 	RTE_LOG(NOTICE, EAL,
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v11 9/9] log: colorize log output
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                     ` (7 preceding siblings ...)
  2024-03-24  2:33   ` [PATCH v11 8/9] log: add support for systemd journal Stephen Hemminger
@ 2024-03-24  2:33   ` Stephen Hemminger
  2024-03-24 11:18   ` [PATCH v11 0/9] Logging unification and enhancements Mattias Rönnblom
  9 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-24  2:33 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Like dmesg, colorize the log output (unless redirected to file).
Timestamp is green, the subsystem is in yellow and the message
is red if urgent, boldface if an error, and normal for info and
debug messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/prog_guide/log_lib.rst   |  14 ++-
 lib/eal/common/eal_common_options.c |   1 +
 lib/eal/common/eal_options.h        |   2 +
 lib/log/log.c                       | 153 +++++++++++++++++++++++++++-
 lib/log/log_internal.h              |   5 +
 lib/log/version.map                 |   1 +
 6 files changed, 173 insertions(+), 3 deletions(-)

diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index 83949cce35..e0cb9bb9ed 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -58,6 +58,19 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+Color output
+~~~~~~~~~~~~
+
+It is useful ot be able see important messages highlighted. This is controlled by the
+``--log-color`` option.  The optional argument ``when`` can be ``auto``, ``never``,
+or ``always``.The default setting is ``auto`` which enables color when the output to
+``stderr`` is a terminal. If the ``when`` argument is omitted, it defaults to ``always``.
+
+For example to turn off all coloring::
+
+	/path/to/app --log-color=none
+
+
 Console timestamp
 ~~~~~~~~~~~~~~~~~
 
@@ -83,7 +96,6 @@ To prefix all console messages with ISO format time the syntax is::
 	/path/to/app --log-timestamp=iso
 
 
-
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index bc6d87266d..0da6729c64 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -75,6 +75,7 @@ eal_long_options[] = {
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
 	{OPT_LOG_TIMESTAMP,     2, NULL, OPT_LOG_TIMESTAMP_NUM    },
+	{OPT_LOG_COLOR,		1, NULL, OPT_LOG_COLOR_NUM	  },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 197b7b25ef..c1696d75e5 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -37,6 +37,8 @@ enum {
 	OPT_LOG_LEVEL_NUM,
 #define OPT_LOG_TIMESTAMP     "log-timestamp"
 	OPT_LOG_TIMESTAMP_NUM,
+#define OPT_LOG_COLOR	      "log-color"
+	OPT_LOG_COLOR_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/log/log.c b/lib/log/log.c
index 4cfa160e3b..e75ed804ec 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -22,6 +22,7 @@
 #include <sys/un.h>
 #endif
 
+#include <rte_common.h>
 #include <rte_log.h>
 #include <rte_per_lcore.h>
 
@@ -41,6 +42,12 @@ enum eal_log_time_format {
 	EAL_LOG_TIMESTAMP_ISO,
 };
 
+enum eal_log_color {
+	EAL_LOG_COLOR_AUTO = 0,	/* default */
+	EAL_LOG_COLOR_NEVER,
+	EAL_LOG_COLOR_ALWAYS,
+};
+
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
@@ -53,6 +60,7 @@ static struct rte_logs {
 	int journal_fd;	/**< Journal file descriptor if using */
 	log_print_t print_func;
 
+	enum eal_log_color color_mode;
 	enum eal_log_time_format time_format;
 	struct timespec started;   /* when log was initialized */
 	struct timespec previous;  /* when last msg was printed */
@@ -665,6 +673,74 @@ format_timestamp(char *tsbuf, size_t tsbuflen)
 	return 0;
 }
 
+enum color {
+	COLOR_NONE,
+	COLOR_RED,
+	COLOR_GREEN,
+	COLOR_YELLOW,
+	COLOR_BLUE,
+	COLOR_MAGENTA,
+	COLOR_CYAN,
+	COLOR_WHITE,
+	COLOR_BOLD,
+	COLOR_CLEAR
+};
+
+static const char * const color_code[] = {
+	[COLOR_NONE]	= "",
+	[COLOR_RED]	= "\e[31m",
+	[COLOR_GREEN]	= "\e[32m",
+	[COLOR_YELLOW]	= "\e[33m",
+	[COLOR_BLUE]	= "\e[34m",
+	[COLOR_MAGENTA] = "\e[35m",
+	[COLOR_CYAN]    = "\e[36m",
+	[COLOR_WHITE]	= "\e[37m",
+	[COLOR_BOLD]	= "\e[1m",
+	[COLOR_CLEAR]	= "\e[0m",
+};
+
+__rte_format_printf(3, 4)
+static int color_fprintf(FILE *out, enum color color, const char *fmt, ...)
+{
+	va_list args;
+	int ret = 0;
+
+	va_start(args, fmt);
+	ret = fprintf(out, "%s", color_code[color]);
+	ret += vfprintf(out, fmt, args);
+	ret += fprintf(out, "%s", color_code[COLOR_CLEAR]);
+
+	return ret;
+}
+
+static ssize_t
+color_log_write(FILE *f, int level, char *msg)
+{
+	char *cp;
+	ssize_t ret = 0;
+
+	/*
+	 * use convention that first part of message (up to the ':' character)
+	 * is the subsystem id and should be highlighted.
+	 */
+	cp = strchr(msg, ':');
+	if (cp) {
+		/* print first part in yellow */
+		ret = color_fprintf(stderr, COLOR_YELLOW, "%.*s",
+				    (int)(cp - msg + 1), msg);
+		msg = cp + 1;
+	}
+
+	if (level <= 0 || level >= (int)RTE_LOG_INFO)
+		ret += fprintf(f, "%s", msg);
+	else if (level >= (int)RTE_LOG_ERR)
+		ret += color_fprintf(f, COLOR_BOLD, "%s", msg);
+	else
+		ret += color_fprintf(f, COLOR_RED, "%s", msg);
+
+	return ret;
+}
+
 /* default log print function */
 __rte_format_printf(3, 0)
 static int
@@ -688,6 +764,70 @@ log_print_with_timestamp(FILE *f, uint32_t level,
 	return log_print(f, level, format, ap);
 }
 
+__rte_format_printf(3, 0)
+static int
+color_print(FILE *f, uint32_t level, const char *format, va_list ap)
+{
+	char *buf = NULL;
+
+	/* need to make temporary buffer for color scan */
+	if (vasprintf(&buf, format, ap) > 0)
+		return color_log_write(f, level, buf);
+
+	/* if vasprintf fails, print without color */
+	return log_print(f, level, format, ap);
+}
+
+__rte_format_printf(3, 0)
+static int
+color_print_with_timestamp(FILE *f, uint32_t level,
+			   const char *format, va_list ap)
+{
+	char tsbuf[128];
+
+	if (format_timestamp(tsbuf, sizeof(tsbuf)) > 0)
+		color_fprintf(f, COLOR_GREEN, "[%s] ", tsbuf);
+
+	return color_print(f, level, format, ap);
+}
+
+/*
+ * Controls whether color is enabled:
+ * modes are:
+ *   always - enable color output regardless
+ *   auto - enable if stderr is a terminal
+ *   never - color output is disabled.
+ */
+int
+eal_log_color(const char *mode)
+{
+	if (mode == NULL || strcmp(mode, "always") == 0)
+		rte_logs.color_mode = EAL_LOG_COLOR_ALWAYS;
+	else if (strcmp(mode, "never") == 0)
+		rte_logs.color_mode = EAL_LOG_COLOR_NEVER;
+	else if (strcmp(mode, "auto") == 0)
+		rte_logs.color_mode = EAL_LOG_COLOR_AUTO;
+	else
+		return -1;
+
+	return 0;
+}
+
+static bool
+use_color(int out_fd)
+{
+	switch (rte_logs.color_mode) {
+	default:
+	case EAL_LOG_COLOR_NEVER:
+		return false;
+	case EAL_LOG_COLOR_ALWAYS:
+		return true;
+	case EAL_LOG_COLOR_AUTO:
+		return !!isatty(out_fd);
+	}
+
+}
+
 #ifdef RTE_EXEC_ENV_LINUX
 /*
  * send message using journal protocol to journald
@@ -817,10 +957,19 @@ eal_log_init(const char *id __rte_unused)
 			rte_logs.print_func = journal_print;
 			journal_send_id(jfd, id);
 		}
+
 	} else
 #endif
-	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE) {
-		rte_logs.print_func = log_print_with_timestamp;
+	if (use_color(STDERR_FILENO)) {
+		if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
+			rte_logs.print_func = color_print_with_timestamp;
+		else
+			rte_logs.print_func = color_print;
+	} else {
+		if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
+			rte_logs.print_func = log_print_with_timestamp;
+		else
+			rte_logs.print_func = log_print;
 	}
 
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 3544ae49ac..5b8bb6ffc9 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -50,5 +50,10 @@ void rte_eal_log_cleanup(void);
 __rte_internal
 int eal_log_timestamp(const char *fmt);
 
+/*
+ * Enable or disable color in log messages
+ */
+__rte_internal
+int eal_log_color(const char *mode);
 
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/version.map b/lib/log/version.map
index 7fd5b39e3a..9ca561b05b 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_color;
 	eal_log_init;
 	eal_log_level2str;
 	eal_log_save_pattern;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* Re: [PATCH v11 0/9] Logging unification and enhancements
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
                     ` (8 preceding siblings ...)
  2024-03-24  2:33   ` [PATCH v11 9/9] log: colorize log output Stephen Hemminger
@ 2024-03-24 11:18   ` Mattias Rönnblom
  9 siblings, 0 replies; 277+ messages in thread
From: Mattias Rönnblom @ 2024-03-24 11:18 UTC (permalink / raw)
  To: Stephen Hemminger, dev

On 2024-03-24 03:33, Stephen Hemminger wrote:
> Improvements and unification of logging library (for 24.07 release).
> This version works on all platforms: Linux, Windows and FreeBSD.
> 
> This is update to rework patch set. It adds several new features
> to the console log output.
> 
>    * Putting a timestamp on console output which is useful for
>      analyzing performance of startup codes. Timestamp is optional
>      and must be enabled on command line.
> 
>    * Displaying console output with colors.
>      It uses the standard conventions used by many other Linux commands
>      for colorized display.  The default is to enable color if the
>      console output is going to a terminal. But it can be always
>      on or disabled by command line flag. This default was chosen
>      based on what dmesg(1) command does.
> 
>      I find color helpful because DPDK drivers and libraries print
>      lots of not very useful messages. And having error messages
>      highlighted in bold face helps. This might also get users to
>      pay more attention to error messages. Many bug reports have
>      earlier messages that are lost because there are so many
>      info messages.
> 
>    * Use of syslog on Linux is replaced by using journal
>      API (over unix domain socket). This replaces legacy syslog;
>      (almost) all Linux distros have switched to journal.
> 

Is "switched to journal" the same as "switched to systemd", or are there 
non-systemd journal API implementations?

I'm guessing there are quite a few Linux-based embedded systems that 
doesn't use systemd.

Ideally, you would like to have journal as the Linux default, and then 
syslog as a build-time option.

> Will add a release note in next release (after this is merged)
> 
> v11 - rework to be portable to Windows
>        drop syslog support and support journal instead
> 
> Stephen Hemminger (9):
>    windows: make getopt functions have const properties
>    eal: make eal_log_level_parse common
>    eal: do not duplicate rte_init_alert() messages
>    eal: change rte_exit() output to match rte_log()
>    log: drop syslog support, and make code common
>    log: add hook for printing log messages
>    log: add timestamp option
>    log: add support for systemd journal
>    log: colorize log output
> 
>   app/test/test_eal_flags.c                     |  38 +-
>   doc/guides/linux_gsg/linux_eal_parameters.rst |  27 -
>   .../prog_guide/env_abstraction_layer.rst      |   5 +-
>   doc/guides/prog_guide/log_lib.rst             |  41 +-
>   lib/eal/common/eal_common_debug.c             |  11 +-
>   lib/eal/common/eal_common_options.c           | 123 ++---
>   lib/eal/common/eal_internal_cfg.h             |   2 +-
>   lib/eal/common/eal_options.h                  |   7 +-
>   lib/eal/freebsd/eal.c                         |  54 +-
>   lib/eal/linux/eal.c                           |  52 +-
>   lib/eal/unix/eal_debug.c                      |   3 +-
>   lib/eal/windows/eal.c                         |  39 +-
>   lib/eal/windows/getopt.c                      |  23 +-
>   lib/eal/windows/include/getopt.h              |   8 +-
>   lib/log/log.c                                 | 513 ++++++++++++++++--
>   lib/log/log_freebsd.c                         |  12 -
>   lib/log/log_internal.h                        |  16 +-
>   lib/log/log_linux.c                           |  61 ---
>   lib/log/log_windows.c                         |  18 -
>   lib/log/meson.build                           |   5 +-
>   lib/log/version.map                           |   2 +
>   21 files changed, 656 insertions(+), 404 deletions(-)
>   delete mode 100644 lib/log/log_freebsd.c
>   delete mode 100644 lib/log/log_linux.c
>   delete mode 100644 lib/log/log_windows.c
> 

^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 00/14] Logging unification and enhancements
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (10 preceding siblings ...)
  2024-03-24  2:33 ` [PATCH v11 0/9] Logging unification and enhancements Stephen Hemminger
@ 2024-03-25 20:46 ` Stephen Hemminger
  2024-03-25 20:46   ` [PATCH v12 01/14] windows: make getopt functions have const properties Stephen Hemminger
                     ` (13 more replies)
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                   ` (8 subsequent siblings)
  20 siblings, 14 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:46 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Improvements and unification of logging library (for 24.07 release).
This version works on all platforms: Linux, Windows and FreeBSD.

This is update to rework patch set. It adds several new features
to the console log output.

  * Putting a timestamp on console output which is useful for
    analyzing performance of startup codes. Timestamp is optional
    and must be enabled on command line.

  * Displaying console output with colors.
    It uses the standard conventions used by many other Linux commands
    for colorized display.  The default is to enable color if the
    console output is going to a terminal. But it can be always
    on or disabled by command line flag. This default was chosen
    based on what dmesg(1) command does.

    I find color helpful because DPDK drivers and libraries print
    lots of not very useful messages. And having error messages
    highlighted in bold face helps. This might also get users to
    pay more attention to error messages. Many bug reports have
    earlier messages that are lost because there are so many
    info messages.

  * Add support for automatic detection of systemd journal
    protocol. If running as systemd service will get enhanced
    logging.

  * Use of syslog is optional and the meaning of the
    --syslog flag has changed. The default is *not* to use
    syslog. 

Add myself as maintainer for log because by now have added
more than previous authors...
Will add a release note in next release (after this is merged)

v12 - add back syslog but make it optional.
      better shims for windows (thread safe)
      split out more of the eal core bits
      fix build warnings on FreeBSD and Ubuntu

Stephen Hemminger (14):
  windows: make getopt functions have const properties
  windows: add os shim for localtime_r
  eal: make eal_log_level_parse common
  eal: do not duplicate rte_init_alert() messages
  eal: change rte_exit() output to match rte_log()
  log: move handling of syslog facility out of eal
  eal: initialize log before everything else
  log: drop syslog support, and make code common
  log: add hook for printing log messages
  log: add timestamp option
  log: add optional support of syslog
  log: add support for systemd journal
  log: colorize log output
  maintainers: add for log library

 MAINTAINERS                                   |   1 +
 app/test/test_eal_flags.c                     |  17 +
 doc/guides/linux_gsg/linux_eal_parameters.rst |  27 -
 doc/guides/prog_guide/log_lib.rst             |  57 ++
 lib/eal/common/eal_common_debug.c             |  11 +-
 lib/eal/common/eal_common_options.c           | 114 ++--
 lib/eal/common/eal_options.h                  |   5 +
 lib/eal/freebsd/eal.c                         |  60 +-
 lib/eal/linux/eal.c                           |  64 +-
 lib/eal/windows/eal.c                         |  44 +-
 lib/eal/windows/getopt.c                      |  23 +-
 lib/eal/windows/include/getopt.h              |   8 +-
 lib/eal/windows/include/rte_os_shim.h         |  10 +
 lib/log/log.c                                 | 594 +++++++++++++++++-
 lib/log/log_freebsd.c                         |   5 +-
 lib/log/log_internal.h                        |  25 +-
 lib/log/log_linux.c                           |  61 --
 lib/log/log_windows.c                         |  18 -
 lib/log/meson.build                           |   5 +-
 lib/log/version.map                           |   4 +-
 20 files changed, 803 insertions(+), 350 deletions(-)
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 01/14] windows: make getopt functions have const properties
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
@ 2024-03-25 20:46   ` Stephen Hemminger
  2024-03-25 20:46   ` [PATCH v12 02/14] windows: add os shim for localtime_r Stephen Hemminger
                     ` (12 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:46 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Tyler Retzlaff, Dmitry Kozlyuk, Pallavi Kadam

Having different prototypes on different platforms can lead
to lots of unnecessary workarounds.  Looks like the version of
getopt used from windows was based on an older out of date
version from FreeBSD.

This patch changes getopt, getopt_long, etc to have the same const
attributes as Linux and FreeBSD. The changes are derived from
the current FreeBSD version of getopt_long.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 lib/eal/windows/getopt.c         | 23 ++++++++++++-----------
 lib/eal/windows/include/getopt.h |  8 ++++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lib/eal/windows/getopt.c b/lib/eal/windows/getopt.c
index a1f51c6c23..50ff71b930 100644
--- a/lib/eal/windows/getopt.c
+++ b/lib/eal/windows/getopt.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-const char    *optarg;		/* argument associated with option */
+char    *optarg;		/* argument associated with option */
 int	opterr = 1;		/* if error message should be printed */
 int	optind = 1;		/* index into parent argv vector */
 int	optopt = '?';		/* character checked for validity */
@@ -39,9 +39,9 @@ static void pass(const char *a) {(void) a; }
 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
 #define	INORDER		1
 
-#define	EMSG		""
+static char EMSG[] = "";
 
-static const char *place = EMSG; /* option letter processing */
+static char *place = EMSG; /* option letter processing */
 
 /* XXX: set optreset to 1 rather than these two */
 static int nonopt_start = -1; /* first non option argument (for permute) */
@@ -80,7 +80,7 @@ gcd(int a, int b)
  */
 static void
 permute_args(int panonopt_start, int panonopt_end, int opt_end,
-	char **nargv)
+	char * const *nargv)
 {
 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
 	char *swap;
@@ -101,11 +101,12 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
 				pos -= nnonopts;
 			else
 				pos += nopts;
+
 			swap = nargv[pos];
 			/* LINTED const cast */
-			((char **) nargv)[pos] = nargv[cstart];
+			((char **)(uintptr_t)nargv)[pos] = nargv[cstart];
 			/* LINTED const cast */
-			((char **)nargv)[cstart] = swap;
+			((char **)(uintptr_t)nargv)[cstart] = swap;
 		}
 	}
 }
@@ -116,7 +117,7 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
  * Returns -1 if short_too is set and the option does not match long_options.
  */
 static int
-parse_long_options(char **nargv, const char *options,
+parse_long_options(char * const *nargv, const char *options,
 	const struct option *long_options, int *idx, int short_too)
 {
 	const char *current_argv;
@@ -236,7 +237,7 @@ parse_long_options(char **nargv, const char *options,
  *	Parse argc/argv argument vector.  Called by user level routines.
  */
 static int
-getopt_internal(int nargc, char **nargv, const char *options,
+getopt_internal(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx, int flags)
 {
 	char *oli;				/* option letter list index */
@@ -434,7 +435,7 @@ getopt_internal(int nargc, char **nargv, const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt(int nargc, char *nargv[], const char *options)
+getopt(int nargc, char *const nargv[], const char *options)
 {
 	return getopt_internal(nargc, nargv, options, NULL, NULL,
 			       FLAG_PERMUTE);
@@ -445,7 +446,7 @@ getopt(int nargc, char *nargv[], const char *options)
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long(int nargc, char *nargv[], const char *options,
+getopt_long(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
@@ -458,7 +459,7 @@ getopt_long(int nargc, char *nargv[], const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long_only(int nargc, char *nargv[], const char *options,
+getopt_long_only(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
diff --git a/lib/eal/windows/include/getopt.h b/lib/eal/windows/include/getopt.h
index 6f57af454b..e4cf6873cb 100644
--- a/lib/eal/windows/include/getopt.h
+++ b/lib/eal/windows/include/getopt.h
@@ -44,7 +44,7 @@
 
 
 /** argument to current option, or NULL if it has none */
-extern const char *optarg;
+extern char *optarg;
 /** Current position in arg string.  Starts from 1.
  * Setting to 0 resets state.
  */
@@ -80,14 +80,14 @@ struct option {
 };
 
 /** Compat: getopt */
-int getopt(int argc, char *argv[], const char *options);
+int getopt(int argc, char *const argv[], const char *options);
 
 /** Compat: getopt_long */
-int getopt_long(int argc, char *argv[], const char *options,
+int getopt_long(int argc, char *const argv[], const char *options,
 		const struct option *longopts, int *longindex);
 
 /** Compat: getopt_long_only */
-int getopt_long_only(int nargc, char *argv[], const char *options,
+int getopt_long_only(int nargc, char *const argv[], const char *options,
 		     const struct option *long_options, int *idx);
 
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 02/14] windows: add os shim for localtime_r
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
  2024-03-25 20:46   ` [PATCH v12 01/14] windows: make getopt functions have const properties Stephen Hemminger
@ 2024-03-25 20:46   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 03/14] eal: make eal_log_level_parse common Stephen Hemminger
                     ` (11 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:46 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Dmitry Kozlyuk, Tyler Retzlaff, Pallavi Kadam

Windows does not have localtime_r but it does have a similar
function that can be used instead.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/windows/include/rte_os_shim.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index eda8113662..e9741a9df2 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -110,4 +110,14 @@ rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
 }
 #define clock_gettime(clock_id, tp) rte_clock_gettime(clock_id, tp)
 
+static inline struct tm *
+rte_localtime_r(const time_t *timer, struct tm *buf)
+{
+	if (localtime_s(buf, timer) == 0)
+		return buf;
+	else
+		return NULL;
+}
+#define localtime_r(timer, buf) rte_localtime_r(timer, buf)
+
 #endif /* _RTE_OS_SHIM_ */
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 03/14] eal: make eal_log_level_parse common
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
  2024-03-25 20:46   ` [PATCH v12 01/14] windows: make getopt functions have const properties Stephen Hemminger
  2024-03-25 20:46   ` [PATCH v12 02/14] windows: add os shim for localtime_r Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 04/14] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
                     ` (10 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

The code to parse for log-level option should be same on
all OS variants.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 46 +++++++++++++++++++++++++++++
 lib/eal/common/eal_options.h        |  1 +
 lib/eal/freebsd/eal.c               | 42 --------------------------
 lib/eal/linux/eal.c                 | 39 ------------------------
 lib/eal/windows/eal.c               | 35 ----------------------
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index e541f07939..5435399b85 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -1640,6 +1640,51 @@ eal_parse_huge_unlink(const char *arg, struct hugepage_file_discipline *out)
 	return -1;
 }
 
+/* Parse the all arguments looking for log related ones */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_parse_common_option(int opt, const char *optarg,
 			struct internal_config *conf)
@@ -2173,6 +2218,7 @@ rte_vect_set_max_simd_bitwidth(uint16_t bitwidth)
 	return 0;
 }
 
+
 void
 eal_common_usage(void)
 {
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb6412..f3f2e104f6 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -96,6 +96,7 @@ enum {
 extern const char eal_short_options[];
 extern const struct option eal_long_options[];
 
+int eal_log_level_parse(int argc, char * const argv[]);
 int eal_parse_common_option(int opt, const char *argv,
 			    struct internal_config *conf);
 int eal_option_device_parse(void);
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bab77118e9..9825bcea0b 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -363,48 +363,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index fd422f1f62..bffeb1f34e 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 52f0e7462d..85171b2768 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 04/14] eal: do not duplicate rte_init_alert() messages
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (2 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 03/14] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 05/14] eal: change rte_exit() output to match rte_log() Stephen Hemminger
                     ` (9 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

The message already goes through logging, and does not need
to be printed on stderr. Message level should be ALERT
to match function name.

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

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 9825bcea0b..17b56f38aa 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -529,8 +529,7 @@ rte_eal_iopl_init(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /* Launch threads, called at application init(). */
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index bffeb1f34e..23dc26b124 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -840,8 +840,7 @@ static int rte_eal_vfio_setup(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /*
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 05/14] eal: change rte_exit() output to match rte_log()
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (3 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 04/14] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-27  7:17     ` Tyler Retzlaff
  2024-03-25 20:47   ` [PATCH v12 06/14] log: move handling of syslog facility out of eal Stephen Hemminger
                     ` (8 subsequent siblings)
  13 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The rte_exit() output format confuses the timestamp and coloring
options. Change it to use be a single line with proper prefix.

Before:
[     0.006481] EAL: Error - exiting with code: 1
  Cause: [     0.006489] Cannot init EAL: Permission denied

After:
[     0.006238] EAL: Error - exiting with code: 1
[     0.006250] EAL: Cause - Cannot init EAL: Permission denied

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_debug.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/eal/common/eal_common_debug.c b/lib/eal/common/eal_common_debug.c
index 3e77995896..3f37879144 100644
--- a/lib/eal/common/eal_common_debug.c
+++ b/lib/eal/common/eal_common_debug.c
@@ -34,17 +34,18 @@ void
 rte_exit(int exit_code, const char *format, ...)
 {
 	va_list ap;
+	char *msg = NULL;
 
 	if (exit_code != 0)
-		RTE_LOG(CRIT, EAL, "Error - exiting with code: %d\n"
-				"  Cause: ", exit_code);
+		EAL_LOG(CRIT, "Error - exiting with code: %d", exit_code);
 
 	va_start(ap, format);
-	rte_vlog(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, format, ap);
+	vasprintf(&msg, format, ap);
 	va_end(ap);
 
+	rte_log(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, "EAL: Cause - %s", msg);
+
 	if (rte_eal_cleanup() != 0 && rte_errno != EALREADY)
-		EAL_LOG(CRIT,
-			"EAL could not release all resources");
+		EAL_LOG(CRIT, "EAL could not release all resources");
 	exit(exit_code);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 06/14] log: move handling of syslog facility out of eal
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (4 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 05/14] eal: change rte_exit() output to match rte_log() Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 07/14] eal: initialize log before everything else Stephen Hemminger
                     ` (7 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

The syslog facility property is better handled in lib/log
rather than in eal. This also allows for changes to what
syslog flag means in later steps.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 51 ++---------------------------
 lib/eal/freebsd/eal.c               |  5 ++-
 lib/eal/linux/eal.c                 |  7 ++--
 lib/eal/windows/eal.c               |  6 ++--
 lib/log/log_freebsd.c               |  2 +-
 lib/log/log_internal.h              |  5 ++-
 lib/log/log_linux.c                 | 47 ++++++++++++++++++++++++--
 lib/log/log_windows.c               |  8 ++++-
 lib/log/version.map                 |  1 +
 9 files changed, 68 insertions(+), 64 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 5435399b85..661b2db211 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -6,9 +6,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#ifndef RTE_EXEC_ENV_WINDOWS
-#include <syslog.h>
-#endif
 #include <ctype.h>
 #include <limits.h>
 #include <errno.h>
@@ -349,10 +346,6 @@ eal_reset_internal_config(struct internal_config *internal_cfg)
 	}
 	internal_cfg->base_virtaddr = 0;
 
-#ifdef LOG_DAEMON
-	internal_cfg->syslog_facility = LOG_DAEMON;
-#endif
-
 	/* if set to NONE, interrupt mode is determined automatically */
 	internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
 	memset(internal_cfg->vfio_vf_token, 0,
@@ -1297,47 +1290,6 @@ eal_parse_lcores(const char *lcores)
 	return ret;
 }
 
-#ifndef RTE_EXEC_ENV_WINDOWS
-static int
-eal_parse_syslog(const char *facility, struct internal_config *conf)
-{
-	int i;
-	static const struct {
-		const char *name;
-		int value;
-	} map[] = {
-		{ "auth", LOG_AUTH },
-		{ "cron", LOG_CRON },
-		{ "daemon", LOG_DAEMON },
-		{ "ftp", LOG_FTP },
-		{ "kern", LOG_KERN },
-		{ "lpr", LOG_LPR },
-		{ "mail", LOG_MAIL },
-		{ "news", LOG_NEWS },
-		{ "syslog", LOG_SYSLOG },
-		{ "user", LOG_USER },
-		{ "uucp", LOG_UUCP },
-		{ "local0", LOG_LOCAL0 },
-		{ "local1", LOG_LOCAL1 },
-		{ "local2", LOG_LOCAL2 },
-		{ "local3", LOG_LOCAL3 },
-		{ "local4", LOG_LOCAL4 },
-		{ "local5", LOG_LOCAL5 },
-		{ "local6", LOG_LOCAL6 },
-		{ "local7", LOG_LOCAL7 },
-		{ NULL, 0 }
-	};
-
-	for (i = 0; map[i].name; i++) {
-		if (!strcmp(facility, map[i].name)) {
-			conf->syslog_facility = map[i].value;
-			return 0;
-		}
-	}
-	return -1;
-}
-#endif
-
 static void
 eal_log_usage(void)
 {
@@ -1663,6 +1615,7 @@ eal_log_level_parse(int argc, char * const argv[])
 
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_SYSLOG_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1882,7 +1835,7 @@ eal_parse_common_option(int opt, const char *optarg,
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_SYSLOG_NUM:
-		if (eal_parse_syslog(optarg, conf) < 0) {
+		if (eal_log_syslog(optarg) < 0) {
 			EAL_LOG(ERR, "invalid parameters for --"
 					OPT_SYSLOG);
 			return -1;
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 17b56f38aa..6552f9c138 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -11,7 +11,6 @@
 #include <stdarg.h>
 #include <unistd.h>
 #include <pthread.h>
-#include <syslog.h>
 #include <getopt.h>
 #include <sys/file.h>
 #include <stddef.h>
@@ -392,8 +391,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_LOG_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 23dc26b124..3d0c34063e 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -610,8 +610,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 options */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -1106,8 +1106,7 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(program_invocation_short_name,
-			 internal_conf->syslog_facility) < 0) {
+	if (eal_log_init(program_invocation_short_name) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 85171b2768..2519a30017 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -120,8 +120,8 @@ eal_parse_args(int argc, char **argv)
 			return -1;
 		}
 
-		/* eal_log_level_parse() already handled this option */
-		if (opt == OPT_LOG_LEVEL_NUM)
+		/* eal_log_level_parse() already handled these options */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -250,7 +250,7 @@ rte_eal_init(int argc, char **argv)
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_THREAD_NAME_SIZE];
 
-	eal_log_init(NULL, 0);
+	eal_log_init(NULL);
 
 	eal_log_level_parse(argc, argv);
 
diff --git a/lib/log/log_freebsd.c b/lib/log/log_freebsd.c
index 698d3c5423..953e371bee 100644
--- a/lib/log/log_freebsd.c
+++ b/lib/log/log_freebsd.c
@@ -6,7 +6,7 @@
 #include "log_internal.h"
 
 int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
+eal_log_init(__rte_unused const char *id)
 {
 	return 0;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 451629f1c1..cb15cdff08 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -14,7 +14,7 @@
  * Initialize the default log stream.
  */
 __rte_internal
-int eal_log_init(const char *id, int facility);
+int eal_log_init(const char *id);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
@@ -30,6 +30,9 @@ int eal_log_save_regexp(const char *regexp, uint32_t level);
 __rte_internal
 int eal_log_save_pattern(const char *pattern, uint32_t level);
 
+__rte_internal
+int eal_log_syslog(const char *name);
+
 /*
  * Convert log level to string.
  */
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
index 2dfb0c974b..47aa074da2 100644
--- a/lib/log/log_linux.c
+++ b/lib/log/log_linux.c
@@ -3,13 +3,56 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <syslog.h>
 
+#include <rte_common.h>
 #include <rte_log.h>
 
 #include "log_internal.h"
 
+static int log_facility = LOG_DAEMON;
+
+static const struct {
+	const char *name;
+	int value;
+} facilitys[] = {
+	{ "auth", LOG_AUTH },
+	{ "cron", LOG_CRON },
+	{ "daemon", LOG_DAEMON },
+	{ "ftp", LOG_FTP },
+	{ "kern", LOG_KERN },
+	{ "lpr", LOG_LPR },
+	{ "mail", LOG_MAIL },
+	{ "news", LOG_NEWS },
+	{ "syslog", LOG_SYSLOG },
+	{ "user", LOG_USER },
+	{ "uucp", LOG_UUCP },
+	{ "local0", LOG_LOCAL0 },
+	{ "local1", LOG_LOCAL1 },
+	{ "local2", LOG_LOCAL2 },
+	{ "local3", LOG_LOCAL3 },
+	{ "local4", LOG_LOCAL4 },
+	{ "local5", LOG_LOCAL5 },
+	{ "local6", LOG_LOCAL6 },
+	{ "local7", LOG_LOCAL7 },
+};
+
+int
+eal_log_syslog(const char *name)
+{
+	unsigned int i;
+
+	for (i = 0; i < RTE_DIM(facilitys); i++) {
+		if (!strcmp(name, facilitys[i].name)) {
+			log_facility = facilitys[i].value;
+			return 0;
+		}
+	}
+	return -1;
+}
+
 /*
  * default log function
  */
@@ -45,7 +88,7 @@ static cookie_io_functions_t console_log_func = {
  * once memzones are available.
  */
 int
-eal_log_init(const char *id, int facility)
+eal_log_init(const char *id)
 {
 	FILE *log_stream;
 
@@ -53,7 +96,7 @@ eal_log_init(const char *id, int facility)
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
 
 	eal_log_set_default(log_stream);
 
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a6a0889550..a3a756351d 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -6,9 +6,15 @@
 #include <rte_log.h>
 #include "log_internal.h"
 
+int
+eal_log_syslog(const char *name __rte_unused)
+{
+	return -1; /* not used */
+}
+
 /* set the log to default function, called during eal init process. */
 int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
+eal_log_init(__rte_unused const char *id)
 {
 	rte_openlog_stream(stderr);
 
diff --git a/lib/log/version.map b/lib/log/version.map
index 0648f8831a..9c6c49bf06 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -30,5 +30,6 @@ INTERNAL {
 	eal_log_save_pattern;
 	eal_log_save_regexp;
 	eal_log_set_default;
+	eal_log_syslog;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 07/14] eal: initialize log before everything else
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (5 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 06/14] log: move handling of syslog facility out of eal Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 08/14] log: drop syslog support, and make code common Stephen Hemminger
                     ` (6 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

In order for all log messages (including CPU mismatch) to
come out through the logging library, it must be initialized
as early in rte_eal_init() as possible on all platforms.

Where it was done before was likely historical based on
the support of non-OS isolated CPU's which required a shared
memory buffer; that support was dropped before DPDK was
publicly released.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c  |  8 +++++---
 lib/eal/linux/eal.c    | 15 +++++----------
 lib/eal/windows/eal.c  |  3 +--
 lib/log/log_freebsd.c  |  3 +--
 lib/log/log_internal.h |  2 +-
 lib/log/log_linux.c    | 14 ++++++--------
 lib/log/log_windows.c  |  4 +---
 7 files changed, 20 insertions(+), 29 deletions(-)

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 6552f9c138..ec3a0c3244 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -52,6 +52,7 @@
 #include "eal_options.h"
 #include "eal_memcfg.h"
 #include "eal_trace.h"
+#include "log_internal.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
 
@@ -546,6 +547,10 @@ rte_eal_init(int argc, char **argv)
 	bool has_phys_addr;
 	enum rte_iova_mode iova_mode;
 
+	/* setup log as early as possible */
+	eal_log_level_parse(argc, argv);
+	eal_log_init(getprogname());
+
 	/* checks if the machine is adequate */
 	if (!rte_cpu_is_supported()) {
 		rte_eal_init_alert("unsupported cpu type.");
@@ -565,9 +570,6 @@ rte_eal_init(int argc, char **argv)
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
-	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
-
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 3d0c34063e..0a488ee567 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -936,6 +936,11 @@ rte_eal_init(int argc, char **argv)
 	struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
+
+	/* setup log as early as possible */
+	eal_log_level_parse(argc, argv);
+	eal_log_init(program_invocation_short_name);
+
 	/* checks if the machine is adequate */
 	if (!rte_cpu_is_supported()) {
 		rte_eal_init_alert("unsupported cpu type.");
@@ -952,9 +957,6 @@ rte_eal_init(int argc, char **argv)
 
 	eal_reset_internal_config(internal_conf);
 
-	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
-
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
@@ -1106,13 +1108,6 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(program_invocation_short_name) < 0) {
-		rte_eal_init_alert("Cannot init logging.");
-		rte_errno = ENOMEM;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 2519a30017..fb2920e1b8 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -250,9 +250,8 @@ rte_eal_init(int argc, char **argv)
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_THREAD_NAME_SIZE];
 
-	eal_log_init(NULL);
-
 	eal_log_level_parse(argc, argv);
+	eal_log_init(NULL);
 
 	if (eal_create_cpu_map() < 0) {
 		rte_eal_init_alert("Cannot discover CPU and NUMA.");
diff --git a/lib/log/log_freebsd.c b/lib/log/log_freebsd.c
index 953e371bee..33a0925c43 100644
--- a/lib/log/log_freebsd.c
+++ b/lib/log/log_freebsd.c
@@ -5,8 +5,7 @@
 #include <rte_common.h>
 #include "log_internal.h"
 
-int
+void
 eal_log_init(__rte_unused const char *id)
 {
-	return 0;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index cb15cdff08..d5fabd7ef7 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -14,7 +14,7 @@
  * Initialize the default log stream.
  */
 __rte_internal
-int eal_log_init(const char *id);
+void eal_log_init(const char *id);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
index 47aa074da2..6d7dc8f3ab 100644
--- a/lib/log/log_linux.c
+++ b/lib/log/log_linux.c
@@ -87,18 +87,16 @@ static cookie_io_functions_t console_log_func = {
  * set the log to default function, called during eal init process,
  * once memzones are available.
  */
-int
+void
 eal_log_init(const char *id)
 {
 	FILE *log_stream;
 
-	log_stream = fopencookie(NULL, "w+", console_log_func);
-	if (log_stream == NULL)
-		return -1;
-
 	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
 
-	eal_log_set_default(log_stream);
-
-	return 0;
+	log_stream = fopencookie(NULL, "w+", console_log_func);
+	if (log_stream != NULL)
+		eal_log_set_default(log_stream);
+	else
+		eal_log_set_default(stderr);
 }
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a3a756351d..d7c30e4cfa 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -13,12 +13,10 @@ eal_log_syslog(const char *name __rte_unused)
 }
 
 /* set the log to default function, called during eal init process. */
-int
+void
 eal_log_init(__rte_unused const char *id)
 {
 	rte_openlog_stream(stderr);
 
 	eal_log_set_default(stderr);
-
-	return 0;
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 08/14] log: drop syslog support, and make code common
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (6 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 07/14] eal: initialize log before everything else Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 09/14] log: add hook for printing log messages Stephen Hemminger
                     ` (5 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This patch makes the log setup code common across all platforms.

Drops syslog support for now, will come back in later patch.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/log/log.c          |  29 ++++++++----
 lib/log/log_internal.h |   6 ---
 lib/log/log_linux.c    | 102 -----------------------------------------
 lib/log/log_windows.c  |  22 ---------
 lib/log/meson.build    |   5 +-
 lib/log/version.map    |   1 -
 6 files changed, 21 insertions(+), 144 deletions(-)
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

diff --git a/lib/log/log.c b/lib/log/log.c
index 255f757d94..f597da2e39 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -70,12 +70,13 @@ struct log_cur_msg {
  /* per core log */
 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
 
-/* default logs */
-
 /* Change the stream that will be used by logging system */
 int
 rte_openlog_stream(FILE *f)
 {
+	if (rte_logs.file != NULL)
+		fclose(rte_logs.file);
+
 	rte_logs.file = f;
 	return 0;
 }
@@ -505,13 +506,20 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
 	return ret;
 }
 
+/* Placeholder */
+int
+eal_log_syslog(const char *mode __rte_unused)
+{
+	return -1;
+}
+
 /*
- * Called by environment-specific initialization functions.
+ * Called by rte_eal_init
  */
 void
-eal_log_set_default(FILE *default_log)
+eal_log_init(const char *id __rte_unused)
 {
-	default_log_stream = default_log;
+	default_log_stream = stderr;
 
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 	RTE_LOG(NOTICE, EAL,
@@ -525,8 +533,11 @@ eal_log_set_default(FILE *default_log)
 void
 rte_eal_log_cleanup(void)
 {
-	if (default_log_stream) {
-		fclose(default_log_stream);
-		default_log_stream = NULL;
-	}
+	FILE *log_stream = rte_log_get_stream();
+
+	/* don't close stderr on the application */
+	if (log_stream != stderr)
+		fclose(log_stream);
+
+	rte_logs.file = NULL;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index d5fabd7ef7..3c46328e7b 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -16,12 +16,6 @@
 __rte_internal
 void eal_log_init(const char *id);
 
-/*
- * Determine where log data is written when no call to rte_openlog_stream.
- */
-__rte_internal
-void eal_log_set_default(FILE *default_log);
-
 /*
  * Save a log option for later.
  */
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
deleted file mode 100644
index 6d7dc8f3ab..0000000000
--- a/lib/log/log_linux.c
+++ /dev/null
@@ -1,102 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <syslog.h>
-
-#include <rte_common.h>
-#include <rte_log.h>
-
-#include "log_internal.h"
-
-static int log_facility = LOG_DAEMON;
-
-static const struct {
-	const char *name;
-	int value;
-} facilitys[] = {
-	{ "auth", LOG_AUTH },
-	{ "cron", LOG_CRON },
-	{ "daemon", LOG_DAEMON },
-	{ "ftp", LOG_FTP },
-	{ "kern", LOG_KERN },
-	{ "lpr", LOG_LPR },
-	{ "mail", LOG_MAIL },
-	{ "news", LOG_NEWS },
-	{ "syslog", LOG_SYSLOG },
-	{ "user", LOG_USER },
-	{ "uucp", LOG_UUCP },
-	{ "local0", LOG_LOCAL0 },
-	{ "local1", LOG_LOCAL1 },
-	{ "local2", LOG_LOCAL2 },
-	{ "local3", LOG_LOCAL3 },
-	{ "local4", LOG_LOCAL4 },
-	{ "local5", LOG_LOCAL5 },
-	{ "local6", LOG_LOCAL6 },
-	{ "local7", LOG_LOCAL7 },
-};
-
-int
-eal_log_syslog(const char *name)
-{
-	unsigned int i;
-
-	for (i = 0; i < RTE_DIM(facilitys); i++) {
-		if (!strcmp(name, facilitys[i].name)) {
-			log_facility = facilitys[i].value;
-			return 0;
-		}
-	}
-	return -1;
-}
-
-/*
- * default log function
- */
-static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
-{
-	ssize_t ret;
-
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
-
-	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
-	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
-
-	return ret;
-}
-
-static int
-console_log_close(__rte_unused void *c)
-{
-	closelog();
-	return 0;
-}
-
-static cookie_io_functions_t console_log_func = {
-	.write = console_log_write,
-	.close = console_log_close,
-};
-
-/*
- * set the log to default function, called during eal init process,
- * once memzones are available.
- */
-void
-eal_log_init(const char *id)
-{
-	FILE *log_stream;
-
-	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
-
-	log_stream = fopencookie(NULL, "w+", console_log_func);
-	if (log_stream != NULL)
-		eal_log_set_default(log_stream);
-	else
-		eal_log_set_default(stderr);
-}
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
deleted file mode 100644
index d7c30e4cfa..0000000000
--- a/lib/log/log_windows.c
+++ /dev/null
@@ -1,22 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2017-2018 Intel Corporation
- */
-
-#include <rte_common.h>
-#include <rte_log.h>
-#include "log_internal.h"
-
-int
-eal_log_syslog(const char *name __rte_unused)
-{
-	return -1; /* not used */
-}
-
-/* set the log to default function, called during eal init process. */
-void
-eal_log_init(__rte_unused const char *id)
-{
-	rte_openlog_stream(stderr);
-
-	eal_log_set_default(stderr);
-}
diff --git a/lib/log/meson.build b/lib/log/meson.build
index 0d4319b36f..891f77a237 100644
--- a/lib/log/meson.build
+++ b/lib/log/meson.build
@@ -2,8 +2,5 @@
 # Copyright(c) 2023 Intel Corporation
 
 includes += global_inc
-sources = files(
-        'log.c',
-        'log_' + exec_env + '.c',
-)
+sources = files('log.c')
 headers = files('rte_log.h')
diff --git a/lib/log/version.map b/lib/log/version.map
index 9c6c49bf06..32b9680c31 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -29,7 +29,6 @@ INTERNAL {
 	eal_log_level2str;
 	eal_log_save_pattern;
 	eal_log_save_regexp;
-	eal_log_set_default;
 	eal_log_syslog;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 09/14] log: add hook for printing log messages
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (7 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 08/14] log: drop syslog support, and make code common Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 10/14] log: add timestamp option Stephen Hemminger
                     ` (4 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This is useful for when decorating log output for console
or journal. Provide basic version in this patch.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/log/log.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/log/log.c b/lib/log/log.c
index f597da2e39..acd4c320b6 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -26,16 +26,21 @@ struct rte_log_dynamic_type {
 	uint32_t loglevel;
 };
 
+typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
+static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
+
 /** The rte_log structure. */
 static struct rte_logs {
 	uint32_t type;  /**< Bitfield with enabled logs. */
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
+	log_print_t print_func;
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
 	.type = UINT32_MAX,
 	.level = RTE_LOG_DEBUG,
+	.print_func = log_print,
 };
 
 struct rte_eal_opt_loglevel {
@@ -78,6 +83,7 @@ rte_openlog_stream(FILE *f)
 		fclose(rte_logs.file);
 
 	rte_logs.file = f;
+	rte_logs.print_func = log_print;
 	return 0;
 }
 
@@ -484,7 +490,7 @@ rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
 	RTE_PER_LCORE(log_cur_msg).loglevel = level;
 	RTE_PER_LCORE(log_cur_msg).logtype = logtype;
 
-	ret = vfprintf(f, format, ap);
+	ret = (*rte_logs.print_func)(f, level, format, ap);
 	fflush(f);
 	return ret;
 }
@@ -513,6 +519,15 @@ eal_log_syslog(const char *mode __rte_unused)
 	return -1;
 }
 
+/* default log print function */
+__rte_format_printf(3, 0)
+static int
+log_print(FILE *f, uint32_t level __rte_unused,
+	  const char *format, va_list ap)
+{
+	return vfprintf(f, format, ap);
+}
+
 /*
  * Called by rte_eal_init
  */
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 10/14] log: add timestamp option
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (8 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 09/14] log: add hook for printing log messages Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 11/14] log: add optional support of syslog Stephen Hemminger
                     ` (3 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing.

There are multiple timestamp formats similar to Linux dmesg.
The default is time relative since startup (when first
step of logging initialization is done by constructor).
Other alternative formats are delta, ctime, reltime and iso formats.

Example:
$ dpdk-testpmd --log-timestamp -- -i
[     0.008610] EAL: Detected CPU lcores: 8
[     0.008634] EAL: Detected NUMA nodes: 1
[     0.008792] EAL: Detected static linkage of DPDK
[     0.010620] EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
[     0.012618] EAL: Selected IOVA mode 'VA'
[     0.016675] testpmd: No probed ethernet devices
Interactive-mode selected

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  17 +++
 doc/guides/prog_guide/log_lib.rst   |  26 ++++
 lib/eal/common/eal_common_options.c |  14 ++-
 lib/eal/common/eal_options.h        |   2 +
 lib/eal/freebsd/eal.c               |   6 +-
 lib/eal/linux/eal.c                 |   4 +-
 lib/eal/windows/eal.c               |   4 +-
 lib/log/log.c                       | 183 +++++++++++++++++++++++++++-
 lib/log/log_internal.h              |   9 ++
 lib/log/version.map                 |   1 +
 10 files changed, 259 insertions(+), 7 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 6cb4b06757..eeb1799381 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1055,6 +1055,14 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
+	/* Try running with --log-timestamp=iso */
+	const char * const argv24[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp=iso" };
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1162,6 +1170,15 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+	if (launch_proc(argv24) != 0) {
+		printf("Error - process did not run ok with --log-timestamp=iso parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index ff9d1b54a2..504eefe1d2 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -59,6 +59,32 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+Log timestamp
+~~~~~~~~~~~~~
+
+An optional timestamp can be added before each message
+by adding the ``--log-timestamp`` option.
+For example::
+
+	/path/to/app --log-level=lib.*:debug --log-timestamp
+
+Multiple timestamp alternative timestamp formats are available:
+
+.. csv-table:: Log time stamp format
+   :header: "Format", "Description", "Example"
+   :widths: 6, 30, 32
+
+   "ctime", "Unix ctime", "``[Wed Mar 20 07:26:12 2024]``"
+   "delta", "Offset since last", "``[<    3.162373>]``"
+   "reltime", "Seconds since last or time if minute changed", "``[  +3.001791]`` or ``[Mar20 07:26:12]``"
+   "iso", "ISO-8601", "``[2024-03-20T07:26:12−07:00]``"
+
+To prefix all console messages with ISO format time the syntax is::
+
+	/path/to/app --log-timestamp=iso
+
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 661b2db211..aa1faad45c 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -74,6 +74,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     2, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1616,6 +1617,7 @@ eal_log_level_parse(int argc, char * const argv[])
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
 		case OPT_SYSLOG_NUM:
+		case OPT_LOG_TIMESTAMP_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1843,7 +1845,7 @@ eal_parse_common_option(int opt, const char *optarg,
 		break;
 #endif
 
-	case OPT_LOG_LEVEL_NUM: {
+	case OPT_LOG_LEVEL_NUM:
 		if (eal_parse_log_level(optarg) < 0) {
 			EAL_LOG(ERR,
 				"invalid parameters for --"
@@ -1851,7 +1853,14 @@ eal_parse_common_option(int opt, const char *optarg,
 			return -1;
 		}
 		break;
-	}
+
+	case OPT_LOG_TIMESTAMP_NUM:
+		if (eal_log_timestamp(optarg) < 0) {
+			EAL_LOG(ERR, "invalid parameters for --"
+				OPT_LOG_TIMESTAMP);
+			return -1;
+		}
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -2219,6 +2228,7 @@ eal_common_usage(void)
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
 	       "                      Set specific log level\n"
 	       "  --"OPT_LOG_LEVEL"=help    Show log types and levels\n"
+	       "  --"OPT_LOG_TIMESTAMP"     Timestamp log output\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_TRACE"=<regex-match>\n"
 	       "                      Enable trace based on regular expression trace name.\n"
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index f3f2e104f6..e24c9eca53 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index ec3a0c3244..8f41c47f47 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -392,8 +392,10 @@ eal_parse_args(int argc, char **argv)
 			goto out;
 		}
 
-		/* eal_log_level_parse() already handled these */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_LOG_SYSLOG_NUM)
+		/* eal_log_level_parse() already handled these options */
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 0a488ee567..16aaefe355 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -611,7 +611,9 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these options */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index fb2920e1b8..bff1316e0b 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -121,7 +121,9 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these options */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/log/log.c b/lib/log/log.c
index acd4c320b6..2dca91306e 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -11,6 +11,7 @@
 #include <regex.h>
 #include <fnmatch.h>
 #include <sys/queue.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 #include <rte_per_lcore.h>
@@ -18,7 +19,7 @@
 #include "log_internal.h"
 
 #ifdef RTE_EXEC_ENV_WINDOWS
-#define strdup _strdup
+#include <rte_os_shim.h>
 #endif
 
 struct rte_log_dynamic_type {
@@ -26,6 +27,15 @@ struct rte_log_dynamic_type {
 	uint32_t loglevel;
 };
 
+enum eal_log_time_format {
+	EAL_LOG_TIMESTAMP_NONE = 0,
+	EAL_LOG_TIMESTAMP_TIME,		/* time since start */
+	EAL_LOG_TIMESTAMP_DELTA,	/* time since last message */
+	EAL_LOG_TIMESTAMP_RELTIME,
+	EAL_LOG_TIMESTAMP_CTIME,
+	EAL_LOG_TIMESTAMP_ISO,
+};
+
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
@@ -35,6 +45,11 @@ static struct rte_logs {
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
 	log_print_t print_func;
+
+	enum eal_log_time_format time_format;
+	struct timespec started;   /* when log was initialized */
+	struct timespec previous;  /* when last msg was printed */
+
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
@@ -375,6 +390,9 @@ RTE_INIT_PRIO(log_init, LOG)
 {
 	uint32_t i;
 
+	clock_gettime(CLOCK_MONOTONIC, &rte_logs.started);
+	rte_logs.previous = rte_logs.started;
+
 	rte_log_set_global_level(RTE_LOG_DEBUG);
 
 	rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
@@ -519,6 +537,152 @@ eal_log_syslog(const char *mode __rte_unused)
 	return -1;
 }
 
+/* Set the log timestamp format */
+int
+eal_log_timestamp(const char *str)
+{
+	if (str == NULL)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_TIME;
+	else if (strcmp(str, "notime") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_NONE;
+	else if (strcmp(str, "reltime") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_RELTIME;
+	else if (strcmp(str, "delta") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_DELTA;
+	else if (strcmp(str, "ctime") == 0)
+		rte_logs.time_format =  EAL_LOG_TIMESTAMP_CTIME;
+	else if (strcmp(str, "iso") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_ISO;
+	else
+		return -1;
+
+	return 0;
+}
+
+/* Subtract two timespec values and handle wraparound */
+static struct timespec
+timespec_sub(const struct timespec *t0, const struct timespec *t1)
+{
+	struct timespec ts;
+
+	ts.tv_sec = t0->tv_sec - t1->tv_sec;
+	ts.tv_nsec = t0->tv_nsec - t1->tv_nsec;
+	if (ts.tv_nsec < 0) {
+		ts.tv_sec--;
+		ts.tv_nsec += 1000000000L;
+	}
+	return ts;
+}
+
+
+/* Format current timespec into ISO8601 format */
+static ssize_t
+format_iso8601(char *tsbuf, size_t tsbuflen, const struct timespec *now)
+{
+	struct tm *tm, tbuf;
+	char dbuf[64]; /* "2024-05-01T22:11:00" */
+	char zbuf[16] = { }; /* "+0800" */
+
+	tm = localtime_r(&now->tv_sec, &tbuf);
+
+	/* make "2024-05-01T22:11:00,123456+0100" */
+	if (strftime(dbuf, sizeof(dbuf), "%Y-%m-%dT%H:%M:%S", tm) == 0)
+		return 0;
+
+	/* convert timezone to +hhmm */
+	if (strftime(zbuf, sizeof(zbuf), "%z", tm) == 0)
+		return 0;
+
+	/* the result for strftime is "+hhmm" but ISO wants "+hh:mm" */
+	return snprintf(tsbuf, tsbuflen, "%s,%06lu%.3s:%.2s",
+			dbuf, now->tv_nsec / 1000u,
+			zbuf, zbuf + 3);
+}
+
+/*
+ * Make a timestamp where if the minute, hour or day has
+ * changed from the last message, then print abbreviated
+ * "Month day hour:minute" format.
+ * Otherwise print delta from last printed message as +sec.usec
+ */
+static ssize_t
+format_reltime(char *tsbuf, size_t tsbuflen, const struct timespec *now)
+{
+	struct tm *tm, tbuf;
+	static struct tm last_tm;
+	struct timespec delta;
+
+	tm = localtime_r(&now->tv_sec, &tbuf);
+	delta = timespec_sub(now, &rte_logs.previous);
+	rte_logs.previous = *now;
+
+	/* if minute, day, hour hasn't changed then print delta */
+	if (tm->tm_min != last_tm.tm_min ||
+	    tm->tm_hour != last_tm.tm_hour ||
+	    tm->tm_yday != last_tm.tm_yday) {
+		last_tm = *tm;
+		return strftime(tsbuf, tsbuflen, "%b%d %H:%M", tm);
+	} else {
+		return snprintf(tsbuf, tsbuflen, "+%3lu.%06lu",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+	}
+}
+
+/* Format up a timestamp based on current format */
+static ssize_t
+format_timestamp(char *tsbuf, size_t tsbuflen)
+{
+	struct timespec now, delta;
+
+	switch (rte_logs.time_format) {
+	case EAL_LOG_TIMESTAMP_NONE:
+		return 0;
+
+	case EAL_LOG_TIMESTAMP_TIME:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		delta = timespec_sub(&now, &rte_logs.started);
+
+		return snprintf(tsbuf, tsbuflen, "%6lu.%06lu",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_DELTA:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		delta = timespec_sub(&now, &rte_logs.previous);
+		rte_logs.previous = now;
+
+		return snprintf(tsbuf, tsbuflen, "<%6lu.%06lu>",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_RELTIME:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		return format_reltime(tsbuf, tsbuflen, &now);
+
+	case EAL_LOG_TIMESTAMP_CTIME:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		/* trncate to remove newline from ctime result */
+		return snprintf(tsbuf, tsbuflen, "%.24s", ctime(&now.tv_sec));
+
+	case EAL_LOG_TIMESTAMP_ISO:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		return format_iso8601(tsbuf, tsbuflen, &now);
+	}
+
+	return 0;
+}
+
 /* default log print function */
 __rte_format_printf(3, 0)
 static int
@@ -528,12 +692,29 @@ log_print(FILE *f, uint32_t level __rte_unused,
 	return vfprintf(f, format, ap);
 }
 
+/* print timestamp before message */
+__rte_format_printf(3, 0)
+static int
+log_print_with_timestamp(FILE *f, uint32_t level,
+			 const char *format, va_list ap)
+{
+	char tsbuf[128];
+
+	if (format_timestamp(tsbuf, sizeof(tsbuf)) > 0)
+		fprintf(f, "[%s] ", tsbuf);
+
+	return log_print(f, level, format, ap);
+}
+
 /*
  * Called by rte_eal_init
  */
 void
 eal_log_init(const char *id __rte_unused)
 {
+	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
+		rte_logs.print_func = log_print_with_timestamp;
+
 	default_log_stream = stderr;
 
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 3c46328e7b..7c7d44eed2 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -5,8 +5,10 @@
 #ifndef LOG_INTERNAL_H
 #define LOG_INTERNAL_H
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <time.h>
 
 #include <rte_compat.h>
 
@@ -39,4 +41,11 @@ const char *eal_log_level2str(uint32_t level);
 __rte_internal
 void rte_eal_log_cleanup(void);
 
+/*
+ * Add timestamp to console logs
+ */
+__rte_internal
+int eal_log_timestamp(const char *fmt);
+
+
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/version.map b/lib/log/version.map
index 32b9680c31..14d6681a5f 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -30,5 +30,6 @@ INTERNAL {
 	eal_log_save_pattern;
 	eal_log_save_regexp;
 	eal_log_syslog;
+	eal_log_timestamp;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 11/14] log: add optional support of syslog
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (9 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 10/14] log: add timestamp option Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 12/14] log: add support for systemd journal Stephen Hemminger
                     ` (2 subsequent siblings)
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Log to syslog only if option is specified. And if syslog is used
then normally only log to syslog, don't duplicate output.
Also enables syslog support on FreeBSD.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/linux_gsg/linux_eal_parameters.rst |  27 -----
 doc/guides/prog_guide/log_lib.rst             |  17 +++
 lib/eal/common/eal_common_options.c           |   2 +-
 lib/log/log.c                                 | 101 ++++++++++++++++--
 4 files changed, 111 insertions(+), 36 deletions(-)

diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f381391..d86f94d8a8 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -108,30 +108,3 @@ Memory-related options
 *   ``--match-allocations``
 
     Free hugepages back to system exactly as they were originally allocated.
-
-Other options
-~~~~~~~~~~~~~
-
-*   ``--syslog <syslog facility>``
-
-    Set syslog facility. Valid syslog facilities are::
-
-        auth
-        cron
-        daemon
-        ftp
-        kern
-        lpr
-        mail
-        news
-        syslog
-        user
-        uucp
-        local0
-        local1
-        local2
-        local3
-        local4
-        local5
-        local6
-        local7
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index 504eefe1d2..abaedc7212 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -83,6 +83,23 @@ To prefix all console messages with ISO format time the syntax is::
 
 	/path/to/app --log-timestamp=iso
 
+Log output
+~~~~~~~~~~
+
+If desired, messages can be redirected to syslog (on Linux and FreeBSD) with the ``--syslog``
+option. There are three possible settings for this option:
+
+*always*
+    Redirect all log output to syslog.
+
+*auto*
+    Use console if it is a terminal, and use syslog if is not.
+
+*both*
+    Print to both console and syslog.
+
+If ``--syslog`` option is not specified, then only console (stderr) will be used.
+
 
 
 Using Logging APIs to Generate Log Messages
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index aa1faad45c..6f0fd151c1 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -91,7 +91,7 @@ eal_long_options[] = {
 	{OPT_PROC_TYPE,         1, NULL, OPT_PROC_TYPE_NUM        },
 	{OPT_SOCKET_MEM,        1, NULL, OPT_SOCKET_MEM_NUM       },
 	{OPT_SOCKET_LIMIT,      1, NULL, OPT_SOCKET_LIMIT_NUM     },
-	{OPT_SYSLOG,            1, NULL, OPT_SYSLOG_NUM           },
+	{OPT_SYSLOG,            2, NULL, OPT_SYSLOG_NUM           },
 	{OPT_VDEV,              1, NULL, OPT_VDEV_NUM             },
 	{OPT_VFIO_INTR,         1, NULL, OPT_VFIO_INTR_NUM        },
 	{OPT_VFIO_VF_TOKEN,     1, NULL, OPT_VFIO_VF_TOKEN_NUM    },
diff --git a/lib/log/log.c b/lib/log/log.c
index 2dca91306e..d8974c66db 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -13,15 +13,17 @@
 #include <sys/queue.h>
 #include <unistd.h>
 
+#ifdef RTE_EXEC_ENV_WINDOWS
+#include <rte_os_shim.h>
+#else
+#include <syslog.h>
+#endif
+
 #include <rte_log.h>
 #include <rte_per_lcore.h>
 
 #include "log_internal.h"
 
-#ifdef RTE_EXEC_ENV_WINDOWS
-#include <rte_os_shim.h>
-#endif
-
 struct rte_log_dynamic_type {
 	const char *name;
 	uint32_t loglevel;
@@ -36,14 +38,25 @@ enum eal_log_time_format {
 	EAL_LOG_TIMESTAMP_ISO,
 };
 
+enum eal_log_syslog {
+	EAL_LOG_SYSLOG_NONE = 0,	/* do not use syslog */
+	EAL_LOG_SYSLOG_AUTO,		/* use syslog only if not a terminal */
+	EAL_LOG_SYSLOG_ALWAYS,		/* always use syslog */
+	EAL_LOG_SYSLOG_BOTH,		/* log to both syslog and stderr */
+};
+
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
+
 /** The rte_log structure. */
 static struct rte_logs {
 	uint32_t type;  /**< Bitfield with enabled logs. */
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
+#ifndef RTE_EXEC_ENV_WINDOWS
+	enum eal_log_syslog syslog_opt;
+#endif
 	log_print_t print_func;
 
 	enum eal_log_time_format time_format;
@@ -532,9 +545,23 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
 
 /* Placeholder */
 int
-eal_log_syslog(const char *mode __rte_unused)
+eal_log_syslog(const char *str)
 {
+#ifdef RTE_EXEC_ENV_WINDOWS
+	RTE_SET_USED(str);
 	return -1;
+#else
+	if (str == NULL || strcmp(str, "auto") == 0)
+		/* log to syslog only if stderr is not a terminal */
+		rte_logs.syslog_opt = EAL_LOG_SYSLOG_AUTO;
+	else if (strcmp(str, "both") == 0)
+		rte_logs.syslog_opt = EAL_LOG_SYSLOG_BOTH;
+	else if (strcmp(str, "always") == 0)
+		rte_logs.syslog_opt = EAL_LOG_SYSLOG_ALWAYS;
+	else
+		return -1;
+	return 0;
+#endif
 }
 
 /* Set the log timestamp format */
@@ -706,17 +733,75 @@ log_print_with_timestamp(FILE *f, uint32_t level,
 	return log_print(f, level, format, ap);
 }
 
+#ifndef RTE_EXEC_ENV_WINDOWS
 /*
- * Called by rte_eal_init
+ * wrapper for log stream to put messages into syslog
+ * useful for cases like:
+ *   rte_hex_dump(rte_get_log_stream(), ...)
  */
-void
-eal_log_init(const char *id __rte_unused)
+static ssize_t
+syslog_log_write(__rte_unused void *c, const char *buf, size_t size)
+{
+	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
+	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
+	return size;
+}
+
+static int
+syslog_log_close(__rte_unused void *c)
 {
+	closelog();
+	return 0;
+}
+
+static cookie_io_functions_t syslog_log_func = {
+	.write = syslog_log_write,
+	.close = syslog_log_close,
+};
+#endif
+
+
+/* Choose how log output is directed */
+static void
+log_output_selection(const char *id)
+{
+	RTE_SET_USED(id);
+
+#ifndef RTE_EXEC_ENV_WINDOWS
+	bool is_terminal = isatty(STDERR_FILENO);
+
+	if (!(rte_logs.syslog_opt == EAL_LOG_SYSLOG_NONE ||
+	      (rte_logs.syslog_opt == EAL_LOG_SYSLOG_AUTO && is_terminal))) {
+		int flags = LOG_NDELAY | LOG_PID;
+
+#ifdef LOG_PERROR
+		if (rte_logs.syslog_opt == EAL_LOG_SYSLOG_BOTH)
+			flags |= LOG_PERROR;
+#endif
+		openlog(id, flags, is_terminal ? LOG_USER : LOG_DAEMON);
+
+		/* redirect other log messages to syslog as well */
+		FILE *log_stream = fopencookie(NULL, "w", syslog_log_func);
+		if (log_stream != NULL)
+			default_log_stream = log_stream;
+		return;
+	}
+#endif
 	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
 		rte_logs.print_func = log_print_with_timestamp;
+}
 
+/*
+ * Called by rte_eal_init
+ */
+void
+eal_log_init(const char *id)
+{
+	rte_logs.print_func = log_print;
 	default_log_stream = stderr;
 
+	log_output_selection(id);
+
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 	RTE_LOG(NOTICE, EAL,
 		"Debug dataplane logs available - lower performance\n");
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 12/14] log: add support for systemd journal
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (10 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 11/14] log: add optional support of syslog Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 13/14] log: colorize log output Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 14/14] maintainers: add for log library Stephen Hemminger
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

If DPDK application is being run as a systemd service, then
it can use the journal protocol which allows putting more information
in the log such as priority and other information.

The use of journal protocol is automatically detected and
handled.  Rather than having a dependency on libsystemd,
just use the protocol directly as defined in:
	https://systemd.io/JOURNAL_NATIVE_PROTOCOL/

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/log/log.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 126 insertions(+), 2 deletions(-)

diff --git a/lib/log/log.c b/lib/log/log.c
index d8974c66db..0f7bdb3f25 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -17,6 +17,10 @@
 #include <rte_os_shim.h>
 #else
 #include <syslog.h>
+#include <sys/uio.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 #endif
 
 #include <rte_log.h>
@@ -56,6 +60,7 @@ static struct rte_logs {
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
 #ifndef RTE_EXEC_ENV_WINDOWS
 	enum eal_log_syslog syslog_opt;
+	int journal_fd;
 #endif
 	log_print_t print_func;
 
@@ -758,6 +763,112 @@ static cookie_io_functions_t syslog_log_func = {
 	.write = syslog_log_write,
 	.close = syslog_log_close,
 };
+
+
+/*
+ * send message using journal protocol to journald
+ */
+__rte_format_printf(3, 0)
+static int
+journal_print(FILE *f __rte_unused, uint32_t level, const char *format, va_list ap)
+{
+	struct iovec iov[3];
+	char *buf = NULL;
+	size_t len;
+	char msg[] = "MESSAGE=";
+	char *prio;
+
+	iov[0].iov_base = msg;
+	iov[0].iov_len = strlen(msg);
+
+	len = vasprintf(&buf, format, ap);
+	if (len == 0)
+		return 0;
+
+	/* check that message ends with newline, if not add one */
+	if (buf[len - 1] != '\n') {
+		char *clone  = alloca(len + 1);
+		if (clone == NULL)
+			return 0;
+		memcpy(clone, buf, len);
+		clone[len++] = '\n';
+		buf = clone;
+	}
+
+	iov[1].iov_base = buf;
+	iov[1].iov_len = len;
+
+	/* priority value between 0 ("emerg") and 7 ("debug") */
+	len = asprintf(&prio, "PRIORITY=%i\n", level - 1);
+	iov[2].iov_base = prio;
+	iov[2].iov_len = len;
+
+	return writev(rte_logs.journal_fd, iov, 3);
+}
+
+/*
+ * Check if stderr is going to system journal.
+ * This is the documented way to handle systemd journal
+ *
+ * See: https://systemd.io/JOURNAL_NATIVE_PROTOCOL/
+ *
+ */
+static bool
+using_journal(void)
+{
+	char *jenv, *endp = NULL;
+	struct stat st;
+	unsigned long dev, ino;
+
+	jenv = getenv("JOURNAL_STREAM");
+	if (jenv == NULL)
+		return false;
+
+	if (fstat(STDERR_FILENO, &st) < 0)
+		return false;
+
+	/* systemd sets colon-separated list of device and inode number */
+	dev = strtoul(jenv, &endp, 10);
+	if (endp == NULL || *endp != ':')
+		return false;	/* missing colon */
+
+	ino = strtoul(endp + 1, NULL, 10);
+
+	return dev == st.st_dev && ino == st.st_ino;
+}
+
+/* Connect to systemd's journal service */
+static int
+open_journal(const char *id)
+{
+	char *syslog_id = NULL;
+	struct sockaddr_un sun = {
+		.sun_family = AF_UNIX,
+		.sun_path = "/run/systemd/journal/socket",
+	};
+	ssize_t len;
+	int s;
+
+	s = socket(AF_UNIX, SOCK_DGRAM, 0);
+	if (s < 0)
+		return -1;
+
+	if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
+		goto error;
+
+	/* Send syslog identifier as first message */
+	len = asprintf(&syslog_id, "SYSLOG_IDENTIFIER=%s\n", id);
+	if (len == 0)
+		goto error;
+
+	if (write(s, syslog_id, len) != len)
+		goto error;
+
+	return s;
+error:
+	close(s);
+	return -1;
+}
 #endif
 
 
@@ -765,11 +876,24 @@ static cookie_io_functions_t syslog_log_func = {
 static void
 log_output_selection(const char *id)
 {
+#ifdef RTE_EXEC_ENV_WINDOWS
 	RTE_SET_USED(id);
-
-#ifndef RTE_EXEC_ENV_WINDOWS
+#else
 	bool is_terminal = isatty(STDERR_FILENO);
 
+	/* If stderr is redirected to systemd journal then upgrade */
+	if (using_journal()) {
+		int jfd = open_journal(id);
+
+		if (jfd < 0) {
+			RTE_LOG_LINE(NOTICE, EAL, "Cannot connect to journal: %s",
+				     strerror(errno));
+		} else {
+			rte_logs.print_func = journal_print;
+			return;
+		}
+	}
+
 	if (!(rte_logs.syslog_opt == EAL_LOG_SYSLOG_NONE ||
 	      (rte_logs.syslog_opt == EAL_LOG_SYSLOG_AUTO && is_terminal))) {
 		int flags = LOG_NDELAY | LOG_PID;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 13/14] log: colorize log output
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (11 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 12/14] log: add support for systemd journal Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  2024-03-25 20:47   ` [PATCH v12 14/14] maintainers: add for log library Stephen Hemminger
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Like dmesg, colorize the log output (unless redirected to file).
Timestamp is green, the subsystem is in yellow and the message
is red if urgent, boldface if an error, and normal for info and
debug messages.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 doc/guides/prog_guide/log_lib.rst   |  16 ++-
 lib/eal/common/eal_common_options.c |   1 +
 lib/eal/common/eal_options.h        |   2 +
 lib/log/log.c                       | 158 +++++++++++++++++++++++++++-
 lib/log/log_internal.h              |   5 +
 lib/log/version.map                 |   1 +
 6 files changed, 178 insertions(+), 5 deletions(-)

diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index abaedc7212..40727ebaae 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -59,6 +59,21 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+Color output
+~~~~~~~~~~~~
+
+The log library will highlight important messages.
+This is controlled by the ``--log-color`` option.
+he optional argument ``when`` can be ``auto``, ``never``, or ``always``.
+The default setting is ``auto`` which enables color when the output to
+``stderr`` is a terminal.
+If the ``when`` argument is omitted, it defaults to ``always``.
+
+For example to turn off all coloring::
+
+	/path/to/app --log-color=none
+
+
 Log timestamp
 ~~~~~~~~~~~~~
 
@@ -101,7 +116,6 @@ option. There are three possible settings for this option:
 If ``--syslog`` option is not specified, then only console (stderr) will be used.
 
 
-
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 6f0fd151c1..23b536b7a0 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -75,6 +75,7 @@ eal_long_options[] = {
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
 	{OPT_LOG_TIMESTAMP,     2, NULL, OPT_LOG_TIMESTAMP_NUM    },
+	{OPT_LOG_COLOR,		1, NULL, OPT_LOG_COLOR_NUM	  },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index e24c9eca53..5a63c1dd3a 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -37,6 +37,8 @@ enum {
 	OPT_LOG_LEVEL_NUM,
 #define OPT_LOG_TIMESTAMP     "log-timestamp"
 	OPT_LOG_TIMESTAMP_NUM,
+#define OPT_LOG_COLOR	      "log-color"
+	OPT_LOG_COLOR_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/log/log.c b/lib/log/log.c
index 0f7bdb3f25..26a63024be 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -23,6 +23,7 @@
 #include <sys/un.h>
 #endif
 
+#include <rte_common.h>
 #include <rte_log.h>
 #include <rte_per_lcore.h>
 
@@ -49,6 +50,12 @@ enum eal_log_syslog {
 	EAL_LOG_SYSLOG_BOTH,		/* log to both syslog and stderr */
 };
 
+enum eal_log_color {
+	EAL_LOG_COLOR_AUTO = 0,	/* default */
+	EAL_LOG_COLOR_NEVER,
+	EAL_LOG_COLOR_ALWAYS,
+};
+
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
@@ -64,6 +71,7 @@ static struct rte_logs {
 #endif
 	log_print_t print_func;
 
+	enum eal_log_color color_mode;
 	enum eal_log_time_format time_format;
 	struct timespec started;   /* when log was initialized */
 	struct timespec previous;  /* when last msg was printed */
@@ -715,6 +723,74 @@ format_timestamp(char *tsbuf, size_t tsbuflen)
 	return 0;
 }
 
+enum color {
+	COLOR_NONE,
+	COLOR_RED,
+	COLOR_GREEN,
+	COLOR_YELLOW,
+	COLOR_BLUE,
+	COLOR_MAGENTA,
+	COLOR_CYAN,
+	COLOR_WHITE,
+	COLOR_BOLD,
+	COLOR_CLEAR
+};
+
+static const char * const color_code[] = {
+	[COLOR_NONE]	= "",
+	[COLOR_RED]	= "\e[31m",
+	[COLOR_GREEN]	= "\e[32m",
+	[COLOR_YELLOW]	= "\e[33m",
+	[COLOR_BLUE]	= "\e[34m",
+	[COLOR_MAGENTA] = "\e[35m",
+	[COLOR_CYAN]    = "\e[36m",
+	[COLOR_WHITE]	= "\e[37m",
+	[COLOR_BOLD]	= "\e[1m",
+	[COLOR_CLEAR]	= "\e[0m",
+};
+
+__rte_format_printf(3, 4)
+static int color_fprintf(FILE *out, enum color color, const char *fmt, ...)
+{
+	va_list args;
+	int ret = 0;
+
+	va_start(args, fmt);
+	ret = fprintf(out, "%s", color_code[color]);
+	ret += vfprintf(out, fmt, args);
+	ret += fprintf(out, "%s", color_code[COLOR_CLEAR]);
+
+	return ret;
+}
+
+static ssize_t
+color_log_write(FILE *f, int level, char *msg)
+{
+	char *cp;
+	ssize_t ret = 0;
+
+	/*
+	 * use convention that first part of message (up to the ':' character)
+	 * is the subsystem id and should be highlighted.
+	 */
+	cp = strchr(msg, ':');
+	if (cp) {
+		/* print first part in yellow */
+		ret = color_fprintf(stderr, COLOR_YELLOW, "%.*s",
+				    (int)(cp - msg + 1), msg);
+		msg = cp + 1;
+	}
+
+	if (level <= 0 || level >= (int)RTE_LOG_INFO)
+		ret += fprintf(f, "%s", msg);
+	else if (level >= (int)RTE_LOG_ERR)
+		ret += color_fprintf(f, COLOR_BOLD, "%s", msg);
+	else
+		ret += color_fprintf(f, COLOR_RED, "%s", msg);
+
+	return ret;
+}
+
 /* default log print function */
 __rte_format_printf(3, 0)
 static int
@@ -871,16 +947,79 @@ open_journal(const char *id)
 }
 #endif
 
+__rte_format_printf(3, 0)
+static int
+color_print(FILE *f, uint32_t level, const char *format, va_list ap)
+{
+	char *buf = NULL;
+
+	/* need to make temporary buffer for color scan */
+	if (vasprintf(&buf, format, ap) > 0)
+		return color_log_write(f, level, buf);
+
+	/* if vasprintf fails, print without color */
+	return log_print(f, level, format, ap);
+}
+
+__rte_format_printf(3, 0)
+static int
+color_print_with_timestamp(FILE *f, uint32_t level,
+			   const char *format, va_list ap)
+{
+	char tsbuf[128];
+
+	if (format_timestamp(tsbuf, sizeof(tsbuf)) > 0)
+		color_fprintf(f, COLOR_GREEN, "[%s] ", tsbuf);
+
+	return color_print(f, level, format, ap);
+}
+
+/*
+ * Controls whether color is enabled:
+ * modes are:
+ *   always - enable color output regardless
+ *   auto - enable if stderr is a terminal
+ *   never - color output is disabled.
+ */
+int
+eal_log_color(const char *mode)
+{
+	if (mode == NULL || strcmp(mode, "always") == 0)
+		rte_logs.color_mode = EAL_LOG_COLOR_ALWAYS;
+	else if (strcmp(mode, "never") == 0)
+		rte_logs.color_mode = EAL_LOG_COLOR_NEVER;
+	else if (strcmp(mode, "auto") == 0)
+		rte_logs.color_mode = EAL_LOG_COLOR_AUTO;
+	else
+		return -1;
+
+	return 0;
+}
+
+static inline bool
+use_color(bool is_terminal)
+{
+	switch (rte_logs.color_mode) {
+	default:
+	case EAL_LOG_COLOR_NEVER:
+		return false;
+	case EAL_LOG_COLOR_ALWAYS:
+		return true;
+	case EAL_LOG_COLOR_AUTO:
+		return is_terminal;
+	}
+
+}
 
 /* Choose how log output is directed */
 static void
 log_output_selection(const char *id)
 {
+	bool is_terminal = isatty(STDERR_FILENO);
+
 #ifdef RTE_EXEC_ENV_WINDOWS
 	RTE_SET_USED(id);
 #else
-	bool is_terminal = isatty(STDERR_FILENO);
-
 	/* If stderr is redirected to systemd journal then upgrade */
 	if (using_journal()) {
 		int jfd = open_journal(id);
@@ -911,8 +1050,19 @@ log_output_selection(const char *id)
 		return;
 	}
 #endif
-	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
-		rte_logs.print_func = log_print_with_timestamp;
+
+	if (use_color(is_terminal)) {
+		if (rte_logs.time_format == EAL_LOG_TIMESTAMP_NONE)
+			rte_logs.print_func = color_print;
+		else
+			rte_logs.print_func = color_print_with_timestamp;
+	} else {
+		if (rte_logs.time_format == EAL_LOG_TIMESTAMP_NONE)
+			rte_logs.print_func = log_print;
+		else
+			rte_logs.print_func = log_print_with_timestamp;
+	}
+
 }
 
 /*
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 7c7d44eed2..525e1397fd 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -47,5 +47,10 @@ void rte_eal_log_cleanup(void);
 __rte_internal
 int eal_log_timestamp(const char *fmt);
 
+/*
+ * Enable or disable color in log messages
+ */
+__rte_internal
+int eal_log_color(const char *mode);
 
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/version.map b/lib/log/version.map
index 14d6681a5f..7fa79b20ff 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -25,6 +25,7 @@ DPDK_24 {
 INTERNAL {
 	global:
 
+	eal_log_color;
 	eal_log_init;
 	eal_log_level2str;
 	eal_log_save_pattern;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v12 14/14] maintainers: add for log library
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
                     ` (12 preceding siblings ...)
  2024-03-25 20:47   ` [PATCH v12 13/14] log: colorize log output Stephen Hemminger
@ 2024-03-25 20:47   ` Stephen Hemminger
  13 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-25 20:47 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Thomas Monjalon

"You touch it you own it"
Add myself as maintainer for log library.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 7abb3aee49..54c28a601d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -180,6 +180,7 @@ F: app/test/test_threads.c
 F: app/test/test_version.c
 
 Logging
+M: Stephen Hemminger <stephen@networkplumber.org>
 F: lib/log/
 F: doc/guides/prog_guide/log_lib.rst
 F: app/test/test_logs.c
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 00/11] Logging unification and improvements
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (11 preceding siblings ...)
  2024-03-25 20:46 ` [PATCH v12 00/14] " Stephen Hemminger
@ 2024-03-26  1:56 ` Stephen Hemminger
  2024-03-26  1:56   ` [PATCH v13 01/11] windows: make getopt functions have const properties Stephen Hemminger
                     ` (10 more replies)
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                   ` (7 subsequent siblings)
  20 siblings, 11 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:56 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Improvements and unification of logging library (for 24.07 release).
This version works on all platforms: Linux, Windows and FreeBSD.

This is update to rework patch set. It adds several new features
to the console log output.

  * Putting a timestamp on console output which is useful for
    analyzing performance of startup codes. Timestamp is optional
    and must be enabled on command line.

  * Displaying console output with colors.
    It uses the standard conventions used by many other Linux commands
    for colorized display.  The default is to enable color if the
    console output is going to a terminal. But it can be always
    on or disabled by command line flag. This default was chosen
    based on what dmesg(1) command does.

    I find color helpful because DPDK drivers and libraries print
    lots of not very useful messages. And having error messages
    highlighted in bold face helps. This might also get users to
    pay more attention to error messages. Many bug reports have
    earlier messages that are lost because there are so many
    info messages.

  * Add support for automatic detection of systemd journal
    protocol. If running as systemd service will get enhanced
    logging.

  * Use of syslog is optional and the meaning of the
    --syslog flag has changed. The default is *not* to use
    syslog. 

Add myself as maintainer for log because by now have added
more than previous authors...
Will add a release note in next release (after this is merged)

v13 - fix functional tests and warnings about unused result

v12 - add back syslog but make it optional.
      better shims for windows (thread safe)
      split out more of the eal core bits
      fix build warnings on FreeBSD and Ubuntu

Stephen Hemminger (11):
  windows: make getopt functions have const properties
  windows: add os shim for localtime_r
  eal: make eal_log_level_parse common
  eal: do not duplicate rte_init_alert() messages
  eal: change rte_exit() output to match rte_log()
  log: move handling of syslog facility out of eal
  eal: initialize log before everything else
  log: drop syslog support, and make code common
  log: add hook for printing log messages
  log: add timestamp option
  log: add optional support of syslog

 app/test/test_eal_flags.c                     |  40 ++-
 doc/guides/linux_gsg/linux_eal_parameters.rst |  27 --
 doc/guides/prog_guide/log_lib.rst             |  43 +++
 lib/eal/common/eal_common_debug.c             |  10 +-
 lib/eal/common/eal_common_options.c           | 116 ++++---
 lib/eal/common/eal_options.h                  |   3 +
 lib/eal/freebsd/eal.c                         |  64 +---
 lib/eal/linux/eal.c                           |  68 +---
 lib/eal/windows/eal.c                         |  49 +--
 lib/eal/windows/getopt.c                      |  23 +-
 lib/eal/windows/include/getopt.h              |   8 +-
 lib/eal/windows/include/rte_os_shim.h         |  10 +
 lib/log/log.c                                 | 320 +++++++++++++++++-
 lib/log/log_freebsd.c                         |   5 +-
 lib/log/log_internal.h                        |  20 +-
 lib/log/log_linux.c                           |  61 ----
 lib/log/log_windows.c                         |  18 -
 lib/log/meson.build                           |   5 +-
 lib/log/version.map                           |   3 +-
 19 files changed, 533 insertions(+), 360 deletions(-)
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 01/11] windows: make getopt functions have const properties
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
@ 2024-03-26  1:56   ` Stephen Hemminger
  2024-03-26  9:35     ` Morten Brørup
  2024-03-26  1:56   ` [PATCH v13 02/11] windows: add os shim for localtime_r Stephen Hemminger
                     ` (9 subsequent siblings)
  10 siblings, 1 reply; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:56 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Tyler Retzlaff, Dmitry Kozlyuk, Pallavi Kadam

Having different prototypes on different platforms can lead
to lots of unnecessary workarounds.  Looks like the version of
getopt used from windows was based on an older out of date
version from FreeBSD.

This patch changes getopt, getopt_long, etc to have the same const
attributes as Linux and FreeBSD. The changes are derived from
the current FreeBSD version of getopt_long.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 lib/eal/windows/getopt.c         | 23 ++++++++++++-----------
 lib/eal/windows/include/getopt.h |  8 ++++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lib/eal/windows/getopt.c b/lib/eal/windows/getopt.c
index a1f51c6c23..50ff71b930 100644
--- a/lib/eal/windows/getopt.c
+++ b/lib/eal/windows/getopt.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-const char    *optarg;		/* argument associated with option */
+char    *optarg;		/* argument associated with option */
 int	opterr = 1;		/* if error message should be printed */
 int	optind = 1;		/* index into parent argv vector */
 int	optopt = '?';		/* character checked for validity */
@@ -39,9 +39,9 @@ static void pass(const char *a) {(void) a; }
 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
 #define	INORDER		1
 
-#define	EMSG		""
+static char EMSG[] = "";
 
-static const char *place = EMSG; /* option letter processing */
+static char *place = EMSG; /* option letter processing */
 
 /* XXX: set optreset to 1 rather than these two */
 static int nonopt_start = -1; /* first non option argument (for permute) */
@@ -80,7 +80,7 @@ gcd(int a, int b)
  */
 static void
 permute_args(int panonopt_start, int panonopt_end, int opt_end,
-	char **nargv)
+	char * const *nargv)
 {
 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
 	char *swap;
@@ -101,11 +101,12 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
 				pos -= nnonopts;
 			else
 				pos += nopts;
+
 			swap = nargv[pos];
 			/* LINTED const cast */
-			((char **) nargv)[pos] = nargv[cstart];
+			((char **)(uintptr_t)nargv)[pos] = nargv[cstart];
 			/* LINTED const cast */
-			((char **)nargv)[cstart] = swap;
+			((char **)(uintptr_t)nargv)[cstart] = swap;
 		}
 	}
 }
@@ -116,7 +117,7 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
  * Returns -1 if short_too is set and the option does not match long_options.
  */
 static int
-parse_long_options(char **nargv, const char *options,
+parse_long_options(char * const *nargv, const char *options,
 	const struct option *long_options, int *idx, int short_too)
 {
 	const char *current_argv;
@@ -236,7 +237,7 @@ parse_long_options(char **nargv, const char *options,
  *	Parse argc/argv argument vector.  Called by user level routines.
  */
 static int
-getopt_internal(int nargc, char **nargv, const char *options,
+getopt_internal(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx, int flags)
 {
 	char *oli;				/* option letter list index */
@@ -434,7 +435,7 @@ getopt_internal(int nargc, char **nargv, const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt(int nargc, char *nargv[], const char *options)
+getopt(int nargc, char *const nargv[], const char *options)
 {
 	return getopt_internal(nargc, nargv, options, NULL, NULL,
 			       FLAG_PERMUTE);
@@ -445,7 +446,7 @@ getopt(int nargc, char *nargv[], const char *options)
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long(int nargc, char *nargv[], const char *options,
+getopt_long(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
@@ -458,7 +459,7 @@ getopt_long(int nargc, char *nargv[], const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long_only(int nargc, char *nargv[], const char *options,
+getopt_long_only(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
diff --git a/lib/eal/windows/include/getopt.h b/lib/eal/windows/include/getopt.h
index 6f57af454b..e4cf6873cb 100644
--- a/lib/eal/windows/include/getopt.h
+++ b/lib/eal/windows/include/getopt.h
@@ -44,7 +44,7 @@
 
 
 /** argument to current option, or NULL if it has none */
-extern const char *optarg;
+extern char *optarg;
 /** Current position in arg string.  Starts from 1.
  * Setting to 0 resets state.
  */
@@ -80,14 +80,14 @@ struct option {
 };
 
 /** Compat: getopt */
-int getopt(int argc, char *argv[], const char *options);
+int getopt(int argc, char *const argv[], const char *options);
 
 /** Compat: getopt_long */
-int getopt_long(int argc, char *argv[], const char *options,
+int getopt_long(int argc, char *const argv[], const char *options,
 		const struct option *longopts, int *longindex);
 
 /** Compat: getopt_long_only */
-int getopt_long_only(int nargc, char *argv[], const char *options,
+int getopt_long_only(int nargc, char *const argv[], const char *options,
 		     const struct option *long_options, int *idx);
 
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 02/11] windows: add os shim for localtime_r
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
  2024-03-26  1:56   ` [PATCH v13 01/11] windows: make getopt functions have const properties Stephen Hemminger
@ 2024-03-26  1:56   ` Stephen Hemminger
  2024-03-26  1:56   ` [PATCH v13 03/11] eal: make eal_log_level_parse common Stephen Hemminger
                     ` (8 subsequent siblings)
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:56 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Dmitry Kozlyuk, Tyler Retzlaff, Pallavi Kadam

Windows does not have localtime_r but it does have a similar
function that can be used instead.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/windows/include/rte_os_shim.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index eda8113662..e9741a9df2 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -110,4 +110,14 @@ rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
 }
 #define clock_gettime(clock_id, tp) rte_clock_gettime(clock_id, tp)
 
+static inline struct tm *
+rte_localtime_r(const time_t *timer, struct tm *buf)
+{
+	if (localtime_s(buf, timer) == 0)
+		return buf;
+	else
+		return NULL;
+}
+#define localtime_r(timer, buf) rte_localtime_r(timer, buf)
+
 #endif /* _RTE_OS_SHIM_ */
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 03/11] eal: make eal_log_level_parse common
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
  2024-03-26  1:56   ` [PATCH v13 01/11] windows: make getopt functions have const properties Stephen Hemminger
  2024-03-26  1:56   ` [PATCH v13 02/11] windows: add os shim for localtime_r Stephen Hemminger
@ 2024-03-26  1:56   ` Stephen Hemminger
  2024-03-26  1:56   ` [PATCH v13 04/11] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
                     ` (7 subsequent siblings)
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:56 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

The code to parse for log-level option should be same on
all OS variants.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 46 +++++++++++++++++++++++++++++
 lib/eal/common/eal_options.h        |  1 +
 lib/eal/freebsd/eal.c               | 42 --------------------------
 lib/eal/linux/eal.c                 | 39 ------------------------
 lib/eal/windows/eal.c               | 35 ----------------------
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index e541f07939..5435399b85 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -1640,6 +1640,51 @@ eal_parse_huge_unlink(const char *arg, struct hugepage_file_discipline *out)
 	return -1;
 }
 
+/* Parse the all arguments looking for log related ones */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_parse_common_option(int opt, const char *optarg,
 			struct internal_config *conf)
@@ -2173,6 +2218,7 @@ rte_vect_set_max_simd_bitwidth(uint16_t bitwidth)
 	return 0;
 }
 
+
 void
 eal_common_usage(void)
 {
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb6412..f3f2e104f6 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -96,6 +96,7 @@ enum {
 extern const char eal_short_options[];
 extern const struct option eal_long_options[];
 
+int eal_log_level_parse(int argc, char * const argv[]);
 int eal_parse_common_option(int opt, const char *argv,
 			    struct internal_config *conf);
 int eal_option_device_parse(void);
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bab77118e9..9825bcea0b 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -363,48 +363,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index fd422f1f62..bffeb1f34e 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 52f0e7462d..85171b2768 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 04/11] eal: do not duplicate rte_init_alert() messages
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (2 preceding siblings ...)
  2024-03-26  1:56   ` [PATCH v13 03/11] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-26  1:56   ` Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 05/11] eal: change rte_exit() output to match rte_log() Stephen Hemminger
                     ` (6 subsequent siblings)
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:56 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

The message already goes through logging, and does not need
to be printed on stderr. Message level should be ALERT
to match function name.

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

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 9825bcea0b..17b56f38aa 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -529,8 +529,7 @@ rte_eal_iopl_init(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /* Launch threads, called at application init(). */
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index bffeb1f34e..23dc26b124 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -840,8 +840,7 @@ static int rte_eal_vfio_setup(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /*
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 05/11] eal: change rte_exit() output to match rte_log()
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (3 preceding siblings ...)
  2024-03-26  1:56   ` [PATCH v13 04/11] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
@ 2024-03-26  1:57   ` Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 06/11] log: move handling of syslog facility out of eal Stephen Hemminger
                     ` (5 subsequent siblings)
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:57 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The rte_exit() output format confuses the timestamp and coloring
options. Change it to use be a single line with proper prefix.

Before:
[     0.006481] EAL: Error - exiting with code: 1
  Cause: [     0.006489] Cannot init EAL: Permission denied

After:
[     0.006238] EAL: Error - exiting with code: 1
[     0.006250] EAL: Cause - Cannot init EAL: Permission denied

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_debug.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/eal/common/eal_common_debug.c b/lib/eal/common/eal_common_debug.c
index 3e77995896..71bb5a7d34 100644
--- a/lib/eal/common/eal_common_debug.c
+++ b/lib/eal/common/eal_common_debug.c
@@ -34,17 +34,17 @@ void
 rte_exit(int exit_code, const char *format, ...)
 {
 	va_list ap;
+	char *msg = NULL;
 
 	if (exit_code != 0)
-		RTE_LOG(CRIT, EAL, "Error - exiting with code: %d\n"
-				"  Cause: ", exit_code);
+		EAL_LOG(CRIT, "Error - exiting with code: %d", exit_code);
 
 	va_start(ap, format);
-	rte_vlog(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, format, ap);
+	if (vasprintf(&msg, format, ap) > 0)
+		rte_log(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, "EAL: Cause - %s", msg);
 	va_end(ap);
 
 	if (rte_eal_cleanup() != 0 && rte_errno != EALREADY)
-		EAL_LOG(CRIT,
-			"EAL could not release all resources");
+		EAL_LOG(CRIT, "EAL could not release all resources");
 	exit(exit_code);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 06/11] log: move handling of syslog facility out of eal
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (4 preceding siblings ...)
  2024-03-26  1:57   ` [PATCH v13 05/11] eal: change rte_exit() output to match rte_log() Stephen Hemminger
@ 2024-03-26  1:57   ` Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 07/11] eal: initialize log before everything else Stephen Hemminger
                     ` (4 subsequent siblings)
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:57 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

The syslog facility property is better handled in lib/log
rather than in eal. This also allows for changes to what
syslog flag means in later steps.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 51 ++---------------------------
 lib/eal/freebsd/eal.c               |  5 ++-
 lib/eal/linux/eal.c                 |  7 ++--
 lib/eal/windows/eal.c               |  6 ++--
 lib/log/log_freebsd.c               |  2 +-
 lib/log/log_internal.h              |  5 ++-
 lib/log/log_linux.c                 | 47 ++++++++++++++++++++++++--
 lib/log/log_windows.c               |  8 ++++-
 lib/log/version.map                 |  1 +
 9 files changed, 68 insertions(+), 64 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 5435399b85..661b2db211 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -6,9 +6,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#ifndef RTE_EXEC_ENV_WINDOWS
-#include <syslog.h>
-#endif
 #include <ctype.h>
 #include <limits.h>
 #include <errno.h>
@@ -349,10 +346,6 @@ eal_reset_internal_config(struct internal_config *internal_cfg)
 	}
 	internal_cfg->base_virtaddr = 0;
 
-#ifdef LOG_DAEMON
-	internal_cfg->syslog_facility = LOG_DAEMON;
-#endif
-
 	/* if set to NONE, interrupt mode is determined automatically */
 	internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
 	memset(internal_cfg->vfio_vf_token, 0,
@@ -1297,47 +1290,6 @@ eal_parse_lcores(const char *lcores)
 	return ret;
 }
 
-#ifndef RTE_EXEC_ENV_WINDOWS
-static int
-eal_parse_syslog(const char *facility, struct internal_config *conf)
-{
-	int i;
-	static const struct {
-		const char *name;
-		int value;
-	} map[] = {
-		{ "auth", LOG_AUTH },
-		{ "cron", LOG_CRON },
-		{ "daemon", LOG_DAEMON },
-		{ "ftp", LOG_FTP },
-		{ "kern", LOG_KERN },
-		{ "lpr", LOG_LPR },
-		{ "mail", LOG_MAIL },
-		{ "news", LOG_NEWS },
-		{ "syslog", LOG_SYSLOG },
-		{ "user", LOG_USER },
-		{ "uucp", LOG_UUCP },
-		{ "local0", LOG_LOCAL0 },
-		{ "local1", LOG_LOCAL1 },
-		{ "local2", LOG_LOCAL2 },
-		{ "local3", LOG_LOCAL3 },
-		{ "local4", LOG_LOCAL4 },
-		{ "local5", LOG_LOCAL5 },
-		{ "local6", LOG_LOCAL6 },
-		{ "local7", LOG_LOCAL7 },
-		{ NULL, 0 }
-	};
-
-	for (i = 0; map[i].name; i++) {
-		if (!strcmp(facility, map[i].name)) {
-			conf->syslog_facility = map[i].value;
-			return 0;
-		}
-	}
-	return -1;
-}
-#endif
-
 static void
 eal_log_usage(void)
 {
@@ -1663,6 +1615,7 @@ eal_log_level_parse(int argc, char * const argv[])
 
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_SYSLOG_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1882,7 +1835,7 @@ eal_parse_common_option(int opt, const char *optarg,
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_SYSLOG_NUM:
-		if (eal_parse_syslog(optarg, conf) < 0) {
+		if (eal_log_syslog(optarg) < 0) {
 			EAL_LOG(ERR, "invalid parameters for --"
 					OPT_SYSLOG);
 			return -1;
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 17b56f38aa..6552f9c138 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -11,7 +11,6 @@
 #include <stdarg.h>
 #include <unistd.h>
 #include <pthread.h>
-#include <syslog.h>
 #include <getopt.h>
 #include <sys/file.h>
 #include <stddef.h>
@@ -392,8 +391,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_LOG_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 23dc26b124..3d0c34063e 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -610,8 +610,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 options */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -1106,8 +1106,7 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(program_invocation_short_name,
-			 internal_conf->syslog_facility) < 0) {
+	if (eal_log_init(program_invocation_short_name) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 85171b2768..2519a30017 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -120,8 +120,8 @@ eal_parse_args(int argc, char **argv)
 			return -1;
 		}
 
-		/* eal_log_level_parse() already handled this option */
-		if (opt == OPT_LOG_LEVEL_NUM)
+		/* eal_log_level_parse() already handled these options */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -250,7 +250,7 @@ rte_eal_init(int argc, char **argv)
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_THREAD_NAME_SIZE];
 
-	eal_log_init(NULL, 0);
+	eal_log_init(NULL);
 
 	eal_log_level_parse(argc, argv);
 
diff --git a/lib/log/log_freebsd.c b/lib/log/log_freebsd.c
index 698d3c5423..953e371bee 100644
--- a/lib/log/log_freebsd.c
+++ b/lib/log/log_freebsd.c
@@ -6,7 +6,7 @@
 #include "log_internal.h"
 
 int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
+eal_log_init(__rte_unused const char *id)
 {
 	return 0;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 451629f1c1..cb15cdff08 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -14,7 +14,7 @@
  * Initialize the default log stream.
  */
 __rte_internal
-int eal_log_init(const char *id, int facility);
+int eal_log_init(const char *id);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
@@ -30,6 +30,9 @@ int eal_log_save_regexp(const char *regexp, uint32_t level);
 __rte_internal
 int eal_log_save_pattern(const char *pattern, uint32_t level);
 
+__rte_internal
+int eal_log_syslog(const char *name);
+
 /*
  * Convert log level to string.
  */
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
index 2dfb0c974b..47aa074da2 100644
--- a/lib/log/log_linux.c
+++ b/lib/log/log_linux.c
@@ -3,13 +3,56 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <syslog.h>
 
+#include <rte_common.h>
 #include <rte_log.h>
 
 #include "log_internal.h"
 
+static int log_facility = LOG_DAEMON;
+
+static const struct {
+	const char *name;
+	int value;
+} facilitys[] = {
+	{ "auth", LOG_AUTH },
+	{ "cron", LOG_CRON },
+	{ "daemon", LOG_DAEMON },
+	{ "ftp", LOG_FTP },
+	{ "kern", LOG_KERN },
+	{ "lpr", LOG_LPR },
+	{ "mail", LOG_MAIL },
+	{ "news", LOG_NEWS },
+	{ "syslog", LOG_SYSLOG },
+	{ "user", LOG_USER },
+	{ "uucp", LOG_UUCP },
+	{ "local0", LOG_LOCAL0 },
+	{ "local1", LOG_LOCAL1 },
+	{ "local2", LOG_LOCAL2 },
+	{ "local3", LOG_LOCAL3 },
+	{ "local4", LOG_LOCAL4 },
+	{ "local5", LOG_LOCAL5 },
+	{ "local6", LOG_LOCAL6 },
+	{ "local7", LOG_LOCAL7 },
+};
+
+int
+eal_log_syslog(const char *name)
+{
+	unsigned int i;
+
+	for (i = 0; i < RTE_DIM(facilitys); i++) {
+		if (!strcmp(name, facilitys[i].name)) {
+			log_facility = facilitys[i].value;
+			return 0;
+		}
+	}
+	return -1;
+}
+
 /*
  * default log function
  */
@@ -45,7 +88,7 @@ static cookie_io_functions_t console_log_func = {
  * once memzones are available.
  */
 int
-eal_log_init(const char *id, int facility)
+eal_log_init(const char *id)
 {
 	FILE *log_stream;
 
@@ -53,7 +96,7 @@ eal_log_init(const char *id, int facility)
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
 
 	eal_log_set_default(log_stream);
 
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a6a0889550..a3a756351d 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -6,9 +6,15 @@
 #include <rte_log.h>
 #include "log_internal.h"
 
+int
+eal_log_syslog(const char *name __rte_unused)
+{
+	return -1; /* not used */
+}
+
 /* set the log to default function, called during eal init process. */
 int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
+eal_log_init(__rte_unused const char *id)
 {
 	rte_openlog_stream(stderr);
 
diff --git a/lib/log/version.map b/lib/log/version.map
index 0648f8831a..9c6c49bf06 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -30,5 +30,6 @@ INTERNAL {
 	eal_log_save_pattern;
 	eal_log_save_regexp;
 	eal_log_set_default;
+	eal_log_syslog;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 07/11] eal: initialize log before everything else
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (5 preceding siblings ...)
  2024-03-26  1:57   ` [PATCH v13 06/11] log: move handling of syslog facility out of eal Stephen Hemminger
@ 2024-03-26  1:57   ` Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 08/11] log: drop syslog support, and make code common Stephen Hemminger
                     ` (3 subsequent siblings)
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:57 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

In order for all log messages (including CPU mismatch) to
come out through the logging library, it must be initialized
as early in rte_eal_init() as possible on all platforms.

Where it was done before was likely historical based on
the support of non-OS isolated CPU's which required a shared
memory buffer; that support was dropped before DPDK was
publicly released.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c  | 12 +++++++++---
 lib/eal/linux/eal.c    | 19 +++++++++----------
 lib/eal/windows/eal.c  |  8 ++++++--
 lib/log/log_freebsd.c  |  3 +--
 lib/log/log_internal.h |  2 +-
 lib/log/log_linux.c    | 14 ++++++--------
 lib/log/log_windows.c  |  4 +---
 7 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 6552f9c138..55ff27a4da 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -52,6 +52,7 @@
 #include "eal_options.h"
 #include "eal_memcfg.h"
 #include "eal_trace.h"
+#include "log_internal.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
 
@@ -546,6 +547,14 @@ rte_eal_init(int argc, char **argv)
 	bool has_phys_addr;
 	enum rte_iova_mode iova_mode;
 
+	/* setup log as early as possible */
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log arguments.");
+		rte_errno = EINVAL;
+		return -1;
+	}
+	eal_log_init(getprogname());
+
 	/* checks if the machine is adequate */
 	if (!rte_cpu_is_supported()) {
 		rte_eal_init_alert("unsupported cpu type.");
@@ -565,9 +574,6 @@ rte_eal_init(int argc, char **argv)
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
-	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
-
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 3d0c34063e..b9a0fb1742 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -936,6 +936,15 @@ rte_eal_init(int argc, char **argv)
 	struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
+	/* setup log as early as possible */
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log arguments.");
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	eal_log_init(program_invocation_short_name);
+
 	/* checks if the machine is adequate */
 	if (!rte_cpu_is_supported()) {
 		rte_eal_init_alert("unsupported cpu type.");
@@ -952,9 +961,6 @@ rte_eal_init(int argc, char **argv)
 
 	eal_reset_internal_config(internal_conf);
 
-	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
-
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
@@ -1106,13 +1112,6 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(program_invocation_short_name) < 0) {
-		rte_eal_init_alert("Cannot init logging.");
-		rte_errno = ENOMEM;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 2519a30017..74b3ece30c 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -250,9 +250,13 @@ rte_eal_init(int argc, char **argv)
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_THREAD_NAME_SIZE];
 
-	eal_log_init(NULL);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log arguments.");
+		rte_errno = EINVAL;
+		return -1;
+	}
 
-	eal_log_level_parse(argc, argv);
+	eal_log_init(NULL);
 
 	if (eal_create_cpu_map() < 0) {
 		rte_eal_init_alert("Cannot discover CPU and NUMA.");
diff --git a/lib/log/log_freebsd.c b/lib/log/log_freebsd.c
index 953e371bee..33a0925c43 100644
--- a/lib/log/log_freebsd.c
+++ b/lib/log/log_freebsd.c
@@ -5,8 +5,7 @@
 #include <rte_common.h>
 #include "log_internal.h"
 
-int
+void
 eal_log_init(__rte_unused const char *id)
 {
-	return 0;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index cb15cdff08..d5fabd7ef7 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -14,7 +14,7 @@
  * Initialize the default log stream.
  */
 __rte_internal
-int eal_log_init(const char *id);
+void eal_log_init(const char *id);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
index 47aa074da2..6d7dc8f3ab 100644
--- a/lib/log/log_linux.c
+++ b/lib/log/log_linux.c
@@ -87,18 +87,16 @@ static cookie_io_functions_t console_log_func = {
  * set the log to default function, called during eal init process,
  * once memzones are available.
  */
-int
+void
 eal_log_init(const char *id)
 {
 	FILE *log_stream;
 
-	log_stream = fopencookie(NULL, "w+", console_log_func);
-	if (log_stream == NULL)
-		return -1;
-
 	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
 
-	eal_log_set_default(log_stream);
-
-	return 0;
+	log_stream = fopencookie(NULL, "w+", console_log_func);
+	if (log_stream != NULL)
+		eal_log_set_default(log_stream);
+	else
+		eal_log_set_default(stderr);
 }
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a3a756351d..d7c30e4cfa 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -13,12 +13,10 @@ eal_log_syslog(const char *name __rte_unused)
 }
 
 /* set the log to default function, called during eal init process. */
-int
+void
 eal_log_init(__rte_unused const char *id)
 {
 	rte_openlog_stream(stderr);
 
 	eal_log_set_default(stderr);
-
-	return 0;
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 08/11] log: drop syslog support, and make code common
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (6 preceding siblings ...)
  2024-03-26  1:57   ` [PATCH v13 07/11] eal: initialize log before everything else Stephen Hemminger
@ 2024-03-26  1:57   ` Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 09/11] log: add hook for printing log messages Stephen Hemminger
                     ` (2 subsequent siblings)
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:57 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This patch makes the log setup code common across all platforms.

Drops syslog support for now, will come back in later patch.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  11 ++-
 lib/eal/common/eal_common_options.c |   3 -
 lib/log/log.c                       |  29 +++++---
 lib/log/log_internal.h              |   6 --
 lib/log/log_linux.c                 | 102 ----------------------------
 lib/log/log_windows.c               |  22 ------
 lib/log/meson.build                 |   5 +-
 lib/log/version.map                 |   1 -
 8 files changed, 26 insertions(+), 153 deletions(-)
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 6cb4b06757..36e3185a10 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -984,11 +984,10 @@ test_misc_flags(void)
 	const char *argv1[] = {prgname, prefix, mp_flag, "--no-pci"};
 	/* With -v */
 	const char *argv2[] = {prgname, prefix, mp_flag, "-v"};
+	/* With empty --syslog */
+	const char *argv3[] = {prgname, prefix, mp_flag, "--syslog"};
 	/* With valid --syslog */
-	const char *argv3[] = {prgname, prefix, mp_flag,
-			"--syslog", "syslog"};
-	/* With empty --syslog (should fail) */
-	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog"};
+	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog", "always"};
 	/* With invalid --syslog */
 	const char *argv5[] = {prgname, prefix, mp_flag, "--syslog", "error"};
 	/* With no-sh-conf, also use no-huge to ensure this test runs on BSD */
@@ -1083,8 +1082,8 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --syslog flag\n");
 		goto fail;
 	}
-	if (launch_proc(argv4) == 0) {
-		printf("Error - process run ok with empty --syslog flag\n");
+	if (launch_proc(argv4) != 0) {
+		printf("Error - process did not with --syslog always flag\n");
 		goto fail;
 	}
 	if (launch_proc(argv5) == 0) {
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 661b2db211..9ab512e8a1 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -2212,9 +2212,6 @@ eal_common_usage(void)
 	       "                      (can be used multiple times)\n"
 	       "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
-#ifndef RTE_EXEC_ENV_WINDOWS
-	       "  --"OPT_SYSLOG"            Set syslog facility\n"
-#endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
 	       "                      Set specific log level\n"
diff --git a/lib/log/log.c b/lib/log/log.c
index 255f757d94..f597da2e39 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -70,12 +70,13 @@ struct log_cur_msg {
  /* per core log */
 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
 
-/* default logs */
-
 /* Change the stream that will be used by logging system */
 int
 rte_openlog_stream(FILE *f)
 {
+	if (rte_logs.file != NULL)
+		fclose(rte_logs.file);
+
 	rte_logs.file = f;
 	return 0;
 }
@@ -505,13 +506,20 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
 	return ret;
 }
 
+/* Placeholder */
+int
+eal_log_syslog(const char *mode __rte_unused)
+{
+	return -1;
+}
+
 /*
- * Called by environment-specific initialization functions.
+ * Called by rte_eal_init
  */
 void
-eal_log_set_default(FILE *default_log)
+eal_log_init(const char *id __rte_unused)
 {
-	default_log_stream = default_log;
+	default_log_stream = stderr;
 
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 	RTE_LOG(NOTICE, EAL,
@@ -525,8 +533,11 @@ eal_log_set_default(FILE *default_log)
 void
 rte_eal_log_cleanup(void)
 {
-	if (default_log_stream) {
-		fclose(default_log_stream);
-		default_log_stream = NULL;
-	}
+	FILE *log_stream = rte_log_get_stream();
+
+	/* don't close stderr on the application */
+	if (log_stream != stderr)
+		fclose(log_stream);
+
+	rte_logs.file = NULL;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index d5fabd7ef7..3c46328e7b 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -16,12 +16,6 @@
 __rte_internal
 void eal_log_init(const char *id);
 
-/*
- * Determine where log data is written when no call to rte_openlog_stream.
- */
-__rte_internal
-void eal_log_set_default(FILE *default_log);
-
 /*
  * Save a log option for later.
  */
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
deleted file mode 100644
index 6d7dc8f3ab..0000000000
--- a/lib/log/log_linux.c
+++ /dev/null
@@ -1,102 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <syslog.h>
-
-#include <rte_common.h>
-#include <rte_log.h>
-
-#include "log_internal.h"
-
-static int log_facility = LOG_DAEMON;
-
-static const struct {
-	const char *name;
-	int value;
-} facilitys[] = {
-	{ "auth", LOG_AUTH },
-	{ "cron", LOG_CRON },
-	{ "daemon", LOG_DAEMON },
-	{ "ftp", LOG_FTP },
-	{ "kern", LOG_KERN },
-	{ "lpr", LOG_LPR },
-	{ "mail", LOG_MAIL },
-	{ "news", LOG_NEWS },
-	{ "syslog", LOG_SYSLOG },
-	{ "user", LOG_USER },
-	{ "uucp", LOG_UUCP },
-	{ "local0", LOG_LOCAL0 },
-	{ "local1", LOG_LOCAL1 },
-	{ "local2", LOG_LOCAL2 },
-	{ "local3", LOG_LOCAL3 },
-	{ "local4", LOG_LOCAL4 },
-	{ "local5", LOG_LOCAL5 },
-	{ "local6", LOG_LOCAL6 },
-	{ "local7", LOG_LOCAL7 },
-};
-
-int
-eal_log_syslog(const char *name)
-{
-	unsigned int i;
-
-	for (i = 0; i < RTE_DIM(facilitys); i++) {
-		if (!strcmp(name, facilitys[i].name)) {
-			log_facility = facilitys[i].value;
-			return 0;
-		}
-	}
-	return -1;
-}
-
-/*
- * default log function
- */
-static ssize_t
-console_log_write(__rte_unused void *c, const char *buf, size_t size)
-{
-	ssize_t ret;
-
-	/* write on stderr */
-	ret = fwrite(buf, 1, size, stderr);
-	fflush(stderr);
-
-	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
-	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
-
-	return ret;
-}
-
-static int
-console_log_close(__rte_unused void *c)
-{
-	closelog();
-	return 0;
-}
-
-static cookie_io_functions_t console_log_func = {
-	.write = console_log_write,
-	.close = console_log_close,
-};
-
-/*
- * set the log to default function, called during eal init process,
- * once memzones are available.
- */
-void
-eal_log_init(const char *id)
-{
-	FILE *log_stream;
-
-	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
-
-	log_stream = fopencookie(NULL, "w+", console_log_func);
-	if (log_stream != NULL)
-		eal_log_set_default(log_stream);
-	else
-		eal_log_set_default(stderr);
-}
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
deleted file mode 100644
index d7c30e4cfa..0000000000
--- a/lib/log/log_windows.c
+++ /dev/null
@@ -1,22 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2017-2018 Intel Corporation
- */
-
-#include <rte_common.h>
-#include <rte_log.h>
-#include "log_internal.h"
-
-int
-eal_log_syslog(const char *name __rte_unused)
-{
-	return -1; /* not used */
-}
-
-/* set the log to default function, called during eal init process. */
-void
-eal_log_init(__rte_unused const char *id)
-{
-	rte_openlog_stream(stderr);
-
-	eal_log_set_default(stderr);
-}
diff --git a/lib/log/meson.build b/lib/log/meson.build
index 0d4319b36f..891f77a237 100644
--- a/lib/log/meson.build
+++ b/lib/log/meson.build
@@ -2,8 +2,5 @@
 # Copyright(c) 2023 Intel Corporation
 
 includes += global_inc
-sources = files(
-        'log.c',
-        'log_' + exec_env + '.c',
-)
+sources = files('log.c')
 headers = files('rte_log.h')
diff --git a/lib/log/version.map b/lib/log/version.map
index 9c6c49bf06..32b9680c31 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -29,7 +29,6 @@ INTERNAL {
 	eal_log_level2str;
 	eal_log_save_pattern;
 	eal_log_save_regexp;
-	eal_log_set_default;
 	eal_log_syslog;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 09/11] log: add hook for printing log messages
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (7 preceding siblings ...)
  2024-03-26  1:57   ` [PATCH v13 08/11] log: drop syslog support, and make code common Stephen Hemminger
@ 2024-03-26  1:57   ` Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 10/11] log: add timestamp option Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 11/11] log: add optional support of syslog Stephen Hemminger
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:57 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This is useful for when decorating log output for console
or journal. Provide basic version in this patch.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/log/log.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/log/log.c b/lib/log/log.c
index f597da2e39..acd4c320b6 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -26,16 +26,21 @@ struct rte_log_dynamic_type {
 	uint32_t loglevel;
 };
 
+typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
+static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
+
 /** The rte_log structure. */
 static struct rte_logs {
 	uint32_t type;  /**< Bitfield with enabled logs. */
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
+	log_print_t print_func;
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
 	.type = UINT32_MAX,
 	.level = RTE_LOG_DEBUG,
+	.print_func = log_print,
 };
 
 struct rte_eal_opt_loglevel {
@@ -78,6 +83,7 @@ rte_openlog_stream(FILE *f)
 		fclose(rte_logs.file);
 
 	rte_logs.file = f;
+	rte_logs.print_func = log_print;
 	return 0;
 }
 
@@ -484,7 +490,7 @@ rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
 	RTE_PER_LCORE(log_cur_msg).loglevel = level;
 	RTE_PER_LCORE(log_cur_msg).logtype = logtype;
 
-	ret = vfprintf(f, format, ap);
+	ret = (*rte_logs.print_func)(f, level, format, ap);
 	fflush(f);
 	return ret;
 }
@@ -513,6 +519,15 @@ eal_log_syslog(const char *mode __rte_unused)
 	return -1;
 }
 
+/* default log print function */
+__rte_format_printf(3, 0)
+static int
+log_print(FILE *f, uint32_t level __rte_unused,
+	  const char *format, va_list ap)
+{
+	return vfprintf(f, format, ap);
+}
+
 /*
  * Called by rte_eal_init
  */
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 10/11] log: add timestamp option
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (8 preceding siblings ...)
  2024-03-26  1:57   ` [PATCH v13 09/11] log: add hook for printing log messages Stephen Hemminger
@ 2024-03-26  1:57   ` Stephen Hemminger
  2024-03-26  1:57   ` [PATCH v13 11/11] log: add optional support of syslog Stephen Hemminger
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:57 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

When debugging driver or startup issues, it is useful to have
a timestamp on each message printed. The messages in syslog
already have a timestamp, but often syslog is not available
during testing.

There are multiple timestamp formats similar to Linux dmesg.
The default is time relative since startup (when first
step of logging initialization is done by constructor).
Other alternative formats are delta, ctime, reltime and iso formats.

Example:
$ dpdk-testpmd --log-timestamp -- -i
[     0.008610] EAL: Detected CPU lcores: 8
[     0.008634] EAL: Detected NUMA nodes: 1
[     0.008792] EAL: Detected static linkage of DPDK
[     0.010620] EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
[     0.012618] EAL: Selected IOVA mode 'VA'
[     0.016675] testpmd: No probed ethernet devices
Interactive-mode selected

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  26 ++++
 doc/guides/prog_guide/log_lib.rst   |  26 ++++
 lib/eal/common/eal_common_options.c |  14 ++-
 lib/eal/common/eal_options.h        |   2 +
 lib/eal/freebsd/eal.c               |   6 +-
 lib/eal/linux/eal.c                 |   4 +-
 lib/eal/windows/eal.c               |   4 +-
 lib/log/log.c                       | 183 +++++++++++++++++++++++++++-
 lib/log/log_internal.h              |   9 ++
 lib/log/version.map                 |   1 +
 10 files changed, 268 insertions(+), 7 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 36e3185a10..e54f6e8b7f 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -1054,6 +1054,19 @@ test_misc_flags(void)
 	const char * const argv22[] = {prgname, prefix, mp_flag,
 				       "--huge-worker-stack=512"};
 
+	/* Try running with --log-timestamp */
+	const char * const argv23[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp" };
+
+	/* Try running with --log-timestamp=iso */
+	const char * const argv24[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp=iso" };
+
+	/* Try running with invalid timestamp */
+	const char * const argv25[] = {prgname, prefix, mp_flag,
+				       "--log-timestamp=invalid" };
+
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1161,6 +1174,19 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --huge-worker-stack=size parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv23) != 0) {
+		printf("Error - process did not run ok with --log-timestamp parameter\n");
+		goto fail;
+	}
+	if (launch_proc(argv24) != 0) {
+		printf("Error - process did not run ok with --log-timestamp=iso parameter\n");
+		goto fail;
+	}
+	if (launch_proc(argv25) == 0) {
+		printf("Error - process did run ok with --log-timestamp=invalid parameter\n");
+		goto fail;
+	}
+
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index ff9d1b54a2..504eefe1d2 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -59,6 +59,32 @@ For example::
 
 Within an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
 
+Log timestamp
+~~~~~~~~~~~~~
+
+An optional timestamp can be added before each message
+by adding the ``--log-timestamp`` option.
+For example::
+
+	/path/to/app --log-level=lib.*:debug --log-timestamp
+
+Multiple timestamp alternative timestamp formats are available:
+
+.. csv-table:: Log time stamp format
+   :header: "Format", "Description", "Example"
+   :widths: 6, 30, 32
+
+   "ctime", "Unix ctime", "``[Wed Mar 20 07:26:12 2024]``"
+   "delta", "Offset since last", "``[<    3.162373>]``"
+   "reltime", "Seconds since last or time if minute changed", "``[  +3.001791]`` or ``[Mar20 07:26:12]``"
+   "iso", "ISO-8601", "``[2024-03-20T07:26:12−07:00]``"
+
+To prefix all console messages with ISO format time the syntax is::
+
+	/path/to/app --log-timestamp=iso
+
+
+
 Using Logging APIs to Generate Log Messages
 -------------------------------------------
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 9ab512e8a1..5173835c2c 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -74,6 +74,7 @@ eal_long_options[] = {
 	{OPT_IOVA_MODE,	        1, NULL, OPT_IOVA_MODE_NUM        },
 	{OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
 	{OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
+	{OPT_LOG_TIMESTAMP,     2, NULL, OPT_LOG_TIMESTAMP_NUM    },
 	{OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
 	{OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
 	{OPT_TRACE_BUF_SIZE,    1, NULL, OPT_TRACE_BUF_SIZE_NUM   },
@@ -1616,6 +1617,7 @@ eal_log_level_parse(int argc, char * const argv[])
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
 		case OPT_SYSLOG_NUM:
+		case OPT_LOG_TIMESTAMP_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1843,7 +1845,7 @@ eal_parse_common_option(int opt, const char *optarg,
 		break;
 #endif
 
-	case OPT_LOG_LEVEL_NUM: {
+	case OPT_LOG_LEVEL_NUM:
 		if (eal_parse_log_level(optarg) < 0) {
 			EAL_LOG(ERR,
 				"invalid parameters for --"
@@ -1851,7 +1853,14 @@ eal_parse_common_option(int opt, const char *optarg,
 			return -1;
 		}
 		break;
-	}
+
+	case OPT_LOG_TIMESTAMP_NUM:
+		if (eal_log_timestamp(optarg) < 0) {
+			EAL_LOG(ERR, "invalid parameters for --"
+				OPT_LOG_TIMESTAMP);
+			return -1;
+		}
+		break;
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_TRACE_NUM: {
@@ -2216,6 +2225,7 @@ eal_common_usage(void)
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
 	       "                      Set specific log level\n"
 	       "  --"OPT_LOG_LEVEL"=help    Show log types and levels\n"
+	       "  --"OPT_LOG_TIMESTAMP"[=<format>]  Timestamp log output\n"
 #ifndef RTE_EXEC_ENV_WINDOWS
 	       "  --"OPT_TRACE"=<regex-match>\n"
 	       "                      Enable trace based on regular expression trace name.\n"
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index f3f2e104f6..e24c9eca53 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -35,6 +35,8 @@ enum {
 	OPT_LCORES_NUM,
 #define OPT_LOG_LEVEL         "log-level"
 	OPT_LOG_LEVEL_NUM,
+#define OPT_LOG_TIMESTAMP     "log-timestamp"
+	OPT_LOG_TIMESTAMP_NUM,
 #define OPT_TRACE             "trace"
 	OPT_TRACE_NUM,
 #define OPT_TRACE_DIR         "trace-dir"
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 55ff27a4da..662a829ce8 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -392,8 +392,10 @@ eal_parse_args(int argc, char **argv)
 			goto out;
 		}
 
-		/* eal_log_level_parse() already handled these */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_LOG_SYSLOG_NUM)
+		/* eal_log_level_parse() already handled these options */
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index b9a0fb1742..8cbea480e0 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -611,7 +611,9 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these options */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 74b3ece30c..4283c920c8 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -121,7 +121,9 @@ eal_parse_args(int argc, char **argv)
 		}
 
 		/* eal_log_level_parse() already handled these options */
-		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
+		if (opt == OPT_LOG_LEVEL_NUM ||
+		    opt == OPT_SYSLOG_NUM ||
+		    opt == OPT_LOG_TIMESTAMP_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/log/log.c b/lib/log/log.c
index acd4c320b6..2dca91306e 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -11,6 +11,7 @@
 #include <regex.h>
 #include <fnmatch.h>
 #include <sys/queue.h>
+#include <unistd.h>
 
 #include <rte_log.h>
 #include <rte_per_lcore.h>
@@ -18,7 +19,7 @@
 #include "log_internal.h"
 
 #ifdef RTE_EXEC_ENV_WINDOWS
-#define strdup _strdup
+#include <rte_os_shim.h>
 #endif
 
 struct rte_log_dynamic_type {
@@ -26,6 +27,15 @@ struct rte_log_dynamic_type {
 	uint32_t loglevel;
 };
 
+enum eal_log_time_format {
+	EAL_LOG_TIMESTAMP_NONE = 0,
+	EAL_LOG_TIMESTAMP_TIME,		/* time since start */
+	EAL_LOG_TIMESTAMP_DELTA,	/* time since last message */
+	EAL_LOG_TIMESTAMP_RELTIME,
+	EAL_LOG_TIMESTAMP_CTIME,
+	EAL_LOG_TIMESTAMP_ISO,
+};
+
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
@@ -35,6 +45,11 @@ static struct rte_logs {
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
 	log_print_t print_func;
+
+	enum eal_log_time_format time_format;
+	struct timespec started;   /* when log was initialized */
+	struct timespec previous;  /* when last msg was printed */
+
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
@@ -375,6 +390,9 @@ RTE_INIT_PRIO(log_init, LOG)
 {
 	uint32_t i;
 
+	clock_gettime(CLOCK_MONOTONIC, &rte_logs.started);
+	rte_logs.previous = rte_logs.started;
+
 	rte_log_set_global_level(RTE_LOG_DEBUG);
 
 	rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
@@ -519,6 +537,152 @@ eal_log_syslog(const char *mode __rte_unused)
 	return -1;
 }
 
+/* Set the log timestamp format */
+int
+eal_log_timestamp(const char *str)
+{
+	if (str == NULL)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_TIME;
+	else if (strcmp(str, "notime") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_NONE;
+	else if (strcmp(str, "reltime") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_RELTIME;
+	else if (strcmp(str, "delta") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_DELTA;
+	else if (strcmp(str, "ctime") == 0)
+		rte_logs.time_format =  EAL_LOG_TIMESTAMP_CTIME;
+	else if (strcmp(str, "iso") == 0)
+		rte_logs.time_format = EAL_LOG_TIMESTAMP_ISO;
+	else
+		return -1;
+
+	return 0;
+}
+
+/* Subtract two timespec values and handle wraparound */
+static struct timespec
+timespec_sub(const struct timespec *t0, const struct timespec *t1)
+{
+	struct timespec ts;
+
+	ts.tv_sec = t0->tv_sec - t1->tv_sec;
+	ts.tv_nsec = t0->tv_nsec - t1->tv_nsec;
+	if (ts.tv_nsec < 0) {
+		ts.tv_sec--;
+		ts.tv_nsec += 1000000000L;
+	}
+	return ts;
+}
+
+
+/* Format current timespec into ISO8601 format */
+static ssize_t
+format_iso8601(char *tsbuf, size_t tsbuflen, const struct timespec *now)
+{
+	struct tm *tm, tbuf;
+	char dbuf[64]; /* "2024-05-01T22:11:00" */
+	char zbuf[16] = { }; /* "+0800" */
+
+	tm = localtime_r(&now->tv_sec, &tbuf);
+
+	/* make "2024-05-01T22:11:00,123456+0100" */
+	if (strftime(dbuf, sizeof(dbuf), "%Y-%m-%dT%H:%M:%S", tm) == 0)
+		return 0;
+
+	/* convert timezone to +hhmm */
+	if (strftime(zbuf, sizeof(zbuf), "%z", tm) == 0)
+		return 0;
+
+	/* the result for strftime is "+hhmm" but ISO wants "+hh:mm" */
+	return snprintf(tsbuf, tsbuflen, "%s,%06lu%.3s:%.2s",
+			dbuf, now->tv_nsec / 1000u,
+			zbuf, zbuf + 3);
+}
+
+/*
+ * Make a timestamp where if the minute, hour or day has
+ * changed from the last message, then print abbreviated
+ * "Month day hour:minute" format.
+ * Otherwise print delta from last printed message as +sec.usec
+ */
+static ssize_t
+format_reltime(char *tsbuf, size_t tsbuflen, const struct timespec *now)
+{
+	struct tm *tm, tbuf;
+	static struct tm last_tm;
+	struct timespec delta;
+
+	tm = localtime_r(&now->tv_sec, &tbuf);
+	delta = timespec_sub(now, &rte_logs.previous);
+	rte_logs.previous = *now;
+
+	/* if minute, day, hour hasn't changed then print delta */
+	if (tm->tm_min != last_tm.tm_min ||
+	    tm->tm_hour != last_tm.tm_hour ||
+	    tm->tm_yday != last_tm.tm_yday) {
+		last_tm = *tm;
+		return strftime(tsbuf, tsbuflen, "%b%d %H:%M", tm);
+	} else {
+		return snprintf(tsbuf, tsbuflen, "+%3lu.%06lu",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+	}
+}
+
+/* Format up a timestamp based on current format */
+static ssize_t
+format_timestamp(char *tsbuf, size_t tsbuflen)
+{
+	struct timespec now, delta;
+
+	switch (rte_logs.time_format) {
+	case EAL_LOG_TIMESTAMP_NONE:
+		return 0;
+
+	case EAL_LOG_TIMESTAMP_TIME:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		delta = timespec_sub(&now, &rte_logs.started);
+
+		return snprintf(tsbuf, tsbuflen, "%6lu.%06lu",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_DELTA:
+		if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+			return 0;
+
+		delta = timespec_sub(&now, &rte_logs.previous);
+		rte_logs.previous = now;
+
+		return snprintf(tsbuf, tsbuflen, "<%6lu.%06lu>",
+				(unsigned long)delta.tv_sec,
+				(unsigned long)delta.tv_nsec / 1000u);
+
+	case EAL_LOG_TIMESTAMP_RELTIME:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		return format_reltime(tsbuf, tsbuflen, &now);
+
+	case EAL_LOG_TIMESTAMP_CTIME:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		/* trncate to remove newline from ctime result */
+		return snprintf(tsbuf, tsbuflen, "%.24s", ctime(&now.tv_sec));
+
+	case EAL_LOG_TIMESTAMP_ISO:
+		if (clock_gettime(CLOCK_REALTIME, &now) < 0)
+			return 0;
+
+		return format_iso8601(tsbuf, tsbuflen, &now);
+	}
+
+	return 0;
+}
+
 /* default log print function */
 __rte_format_printf(3, 0)
 static int
@@ -528,12 +692,29 @@ log_print(FILE *f, uint32_t level __rte_unused,
 	return vfprintf(f, format, ap);
 }
 
+/* print timestamp before message */
+__rte_format_printf(3, 0)
+static int
+log_print_with_timestamp(FILE *f, uint32_t level,
+			 const char *format, va_list ap)
+{
+	char tsbuf[128];
+
+	if (format_timestamp(tsbuf, sizeof(tsbuf)) > 0)
+		fprintf(f, "[%s] ", tsbuf);
+
+	return log_print(f, level, format, ap);
+}
+
 /*
  * Called by rte_eal_init
  */
 void
 eal_log_init(const char *id __rte_unused)
 {
+	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
+		rte_logs.print_func = log_print_with_timestamp;
+
 	default_log_stream = stderr;
 
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 3c46328e7b..7c7d44eed2 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -5,8 +5,10 @@
 #ifndef LOG_INTERNAL_H
 #define LOG_INTERNAL_H
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <time.h>
 
 #include <rte_compat.h>
 
@@ -39,4 +41,11 @@ const char *eal_log_level2str(uint32_t level);
 __rte_internal
 void rte_eal_log_cleanup(void);
 
+/*
+ * Add timestamp to console logs
+ */
+__rte_internal
+int eal_log_timestamp(const char *fmt);
+
+
 #endif /* LOG_INTERNAL_H */
diff --git a/lib/log/version.map b/lib/log/version.map
index 32b9680c31..14d6681a5f 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -30,5 +30,6 @@ INTERNAL {
 	eal_log_save_pattern;
 	eal_log_save_regexp;
 	eal_log_syslog;
+	eal_log_timestamp;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v13 11/11] log: add optional support of syslog
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
                     ` (9 preceding siblings ...)
  2024-03-26  1:57   ` [PATCH v13 10/11] log: add timestamp option Stephen Hemminger
@ 2024-03-26  1:57   ` Stephen Hemminger
  10 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26  1:57 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Log to syslog only if option is specified. And if syslog is used
then normally only log to syslog, don't duplicate output.
Also enables syslog support on FreeBSD.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c                     |   5 +-
 doc/guides/linux_gsg/linux_eal_parameters.rst |  27 -----
 doc/guides/prog_guide/log_lib.rst             |  17 +++
 lib/eal/common/eal_common_options.c           |   2 +-
 lib/log/log.c                                 | 101 ++++++++++++++++--
 5 files changed, 114 insertions(+), 38 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index e54f6e8b7f..08f4866461 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -987,9 +987,10 @@ test_misc_flags(void)
 	/* With empty --syslog */
 	const char *argv3[] = {prgname, prefix, mp_flag, "--syslog"};
 	/* With valid --syslog */
-	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog", "always"};
+	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog=both"};
 	/* With invalid --syslog */
-	const char *argv5[] = {prgname, prefix, mp_flag, "--syslog", "error"};
+	const char *argv5[] = {prgname, prefix, mp_flag, "--syslog=invalid"};
+
 	/* With no-sh-conf, also use no-huge to ensure this test runs on BSD */
 	const char *argv6[] = {prgname, "-m", DEFAULT_MEM_SIZE,
 			no_shconf, nosh_prefix, no_huge};
diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst
index ea8f381391..d86f94d8a8 100644
--- a/doc/guides/linux_gsg/linux_eal_parameters.rst
+++ b/doc/guides/linux_gsg/linux_eal_parameters.rst
@@ -108,30 +108,3 @@ Memory-related options
 *   ``--match-allocations``
 
     Free hugepages back to system exactly as they were originally allocated.
-
-Other options
-~~~~~~~~~~~~~
-
-*   ``--syslog <syslog facility>``
-
-    Set syslog facility. Valid syslog facilities are::
-
-        auth
-        cron
-        daemon
-        ftp
-        kern
-        lpr
-        mail
-        news
-        syslog
-        user
-        uucp
-        local0
-        local1
-        local2
-        local3
-        local4
-        local5
-        local6
-        local7
diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst
index 504eefe1d2..abaedc7212 100644
--- a/doc/guides/prog_guide/log_lib.rst
+++ b/doc/guides/prog_guide/log_lib.rst
@@ -83,6 +83,23 @@ To prefix all console messages with ISO format time the syntax is::
 
 	/path/to/app --log-timestamp=iso
 
+Log output
+~~~~~~~~~~
+
+If desired, messages can be redirected to syslog (on Linux and FreeBSD) with the ``--syslog``
+option. There are three possible settings for this option:
+
+*always*
+    Redirect all log output to syslog.
+
+*auto*
+    Use console if it is a terminal, and use syslog if is not.
+
+*both*
+    Print to both console and syslog.
+
+If ``--syslog`` option is not specified, then only console (stderr) will be used.
+
 
 
 Using Logging APIs to Generate Log Messages
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 5173835c2c..16884c5aa3 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -91,7 +91,7 @@ eal_long_options[] = {
 	{OPT_PROC_TYPE,         1, NULL, OPT_PROC_TYPE_NUM        },
 	{OPT_SOCKET_MEM,        1, NULL, OPT_SOCKET_MEM_NUM       },
 	{OPT_SOCKET_LIMIT,      1, NULL, OPT_SOCKET_LIMIT_NUM     },
-	{OPT_SYSLOG,            1, NULL, OPT_SYSLOG_NUM           },
+	{OPT_SYSLOG,            2, NULL, OPT_SYSLOG_NUM           },
 	{OPT_VDEV,              1, NULL, OPT_VDEV_NUM             },
 	{OPT_VFIO_INTR,         1, NULL, OPT_VFIO_INTR_NUM        },
 	{OPT_VFIO_VF_TOKEN,     1, NULL, OPT_VFIO_VF_TOKEN_NUM    },
diff --git a/lib/log/log.c b/lib/log/log.c
index 2dca91306e..d8974c66db 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -13,15 +13,17 @@
 #include <sys/queue.h>
 #include <unistd.h>
 
+#ifdef RTE_EXEC_ENV_WINDOWS
+#include <rte_os_shim.h>
+#else
+#include <syslog.h>
+#endif
+
 #include <rte_log.h>
 #include <rte_per_lcore.h>
 
 #include "log_internal.h"
 
-#ifdef RTE_EXEC_ENV_WINDOWS
-#include <rte_os_shim.h>
-#endif
-
 struct rte_log_dynamic_type {
 	const char *name;
 	uint32_t loglevel;
@@ -36,14 +38,25 @@ enum eal_log_time_format {
 	EAL_LOG_TIMESTAMP_ISO,
 };
 
+enum eal_log_syslog {
+	EAL_LOG_SYSLOG_NONE = 0,	/* do not use syslog */
+	EAL_LOG_SYSLOG_AUTO,		/* use syslog only if not a terminal */
+	EAL_LOG_SYSLOG_ALWAYS,		/* always use syslog */
+	EAL_LOG_SYSLOG_BOTH,		/* log to both syslog and stderr */
+};
+
 typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
 static int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
 
+
 /** The rte_log structure. */
 static struct rte_logs {
 	uint32_t type;  /**< Bitfield with enabled logs. */
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
+#ifndef RTE_EXEC_ENV_WINDOWS
+	enum eal_log_syslog syslog_opt;
+#endif
 	log_print_t print_func;
 
 	enum eal_log_time_format time_format;
@@ -532,9 +545,23 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
 
 /* Placeholder */
 int
-eal_log_syslog(const char *mode __rte_unused)
+eal_log_syslog(const char *str)
 {
+#ifdef RTE_EXEC_ENV_WINDOWS
+	RTE_SET_USED(str);
 	return -1;
+#else
+	if (str == NULL || strcmp(str, "auto") == 0)
+		/* log to syslog only if stderr is not a terminal */
+		rte_logs.syslog_opt = EAL_LOG_SYSLOG_AUTO;
+	else if (strcmp(str, "both") == 0)
+		rte_logs.syslog_opt = EAL_LOG_SYSLOG_BOTH;
+	else if (strcmp(str, "always") == 0)
+		rte_logs.syslog_opt = EAL_LOG_SYSLOG_ALWAYS;
+	else
+		return -1;
+	return 0;
+#endif
 }
 
 /* Set the log timestamp format */
@@ -706,17 +733,75 @@ log_print_with_timestamp(FILE *f, uint32_t level,
 	return log_print(f, level, format, ap);
 }
 
+#ifndef RTE_EXEC_ENV_WINDOWS
 /*
- * Called by rte_eal_init
+ * wrapper for log stream to put messages into syslog
+ * useful for cases like:
+ *   rte_hex_dump(rte_get_log_stream(), ...)
  */
-void
-eal_log_init(const char *id __rte_unused)
+static ssize_t
+syslog_log_write(__rte_unused void *c, const char *buf, size_t size)
+{
+	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
+	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
+	return size;
+}
+
+static int
+syslog_log_close(__rte_unused void *c)
 {
+	closelog();
+	return 0;
+}
+
+static cookie_io_functions_t syslog_log_func = {
+	.write = syslog_log_write,
+	.close = syslog_log_close,
+};
+#endif
+
+
+/* Choose how log output is directed */
+static void
+log_output_selection(const char *id)
+{
+	RTE_SET_USED(id);
+
+#ifndef RTE_EXEC_ENV_WINDOWS
+	bool is_terminal = isatty(STDERR_FILENO);
+
+	if (!(rte_logs.syslog_opt == EAL_LOG_SYSLOG_NONE ||
+	      (rte_logs.syslog_opt == EAL_LOG_SYSLOG_AUTO && is_terminal))) {
+		int flags = LOG_NDELAY | LOG_PID;
+
+#ifdef LOG_PERROR
+		if (rte_logs.syslog_opt == EAL_LOG_SYSLOG_BOTH)
+			flags |= LOG_PERROR;
+#endif
+		openlog(id, flags, is_terminal ? LOG_USER : LOG_DAEMON);
+
+		/* redirect other log messages to syslog as well */
+		FILE *log_stream = fopencookie(NULL, "w", syslog_log_func);
+		if (log_stream != NULL)
+			default_log_stream = log_stream;
+		return;
+	}
+#endif
 	if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE)
 		rte_logs.print_func = log_print_with_timestamp;
+}
 
+/*
+ * Called by rte_eal_init
+ */
+void
+eal_log_init(const char *id)
+{
+	rte_logs.print_func = log_print;
 	default_log_stream = stderr;
 
+	log_output_selection(id);
+
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 	RTE_LOG(NOTICE, EAL,
 		"Debug dataplane logs available - lower performance\n");
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* RE: [PATCH v13 01/11] windows: make getopt functions have const properties
  2024-03-26  1:56   ` [PATCH v13 01/11] windows: make getopt functions have const properties Stephen Hemminger
@ 2024-03-26  9:35     ` Morten Brørup
  0 siblings, 0 replies; 277+ messages in thread
From: Morten Brørup @ 2024-03-26  9:35 UTC (permalink / raw)
  To: Stephen Hemminger, dev; +Cc: Tyler Retzlaff, Dmitry Kozlyuk, Pallavi Kadam

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Tuesday, 26 March 2024 02.57
> 
> Having different prototypes on different platforms can lead
> to lots of unnecessary workarounds.  Looks like the version of
> getopt used from windows was based on an older out of date
> version from FreeBSD.
> 
> This patch changes getopt, getopt_long, etc to have the same const
> attributes as Linux and FreeBSD. The changes are derived from
> the current FreeBSD version of getopt_long.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
> Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> ---

For the series,
Acked-by: Morten Brørup <mb@smartsharesystems.com>


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 00/15] Logging unification and improvments
  2020-08-14 17:34 [dpdk-dev] [PATCH] eal: add option to put timestamp on console output Stephen Hemminger
                   ` (12 preceding siblings ...)
  2024-03-26  1:56 ` [PATCH v13 00/11] Logging unification and improvements Stephen Hemminger
@ 2024-03-26 17:34 ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 01/15] maintainers: add for log library Stephen Hemminger
                     ` (14 more replies)
  2024-03-27  0:26 ` [PATCH v15 00/15] Logging unification and improvements Stephen Hemminger
                   ` (6 subsequent siblings)
  20 siblings, 15 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Improvements and unification of logging library (for 24.07 release).
This version works on all platforms: Linux, Windows and FreeBSD.

This is update to rework patch set. It adds several new features
to the console log output.

  * Putting a timestamp on console output which is useful for
    analyzing performance of startup codes. Timestamp is optional
    and must be enabled on command line.

  * Displaying console output with colors.
    It uses the standard conventions used by many other Linux commands
    for colorized display.  The default is to enable color if the
    console output is going to a terminal. But it can be always
    on or disabled by command line flag. This default was chosen
    based on what dmesg(1) command does.

    I find color helpful because DPDK drivers and libraries print
    lots of not very useful messages. And having error messages
    highlighted in bold face helps. This might also get users to
    pay more attention to error messages. Many bug reports have
    earlier messages that are lost because there are so many
    info messages.

  * Add support for automatic detection of systemd journal
    protocol. If running as systemd service will get enhanced
    logging.

  * Use of syslog is optional and the meaning of the
    --syslog flag has changed. The default is *not* to use
    syslog. 

Add myself as maintainer for log because by now have added
more than previous authors...
Will add a release note in next release (after this is merged)

v14 - fix Windows build, by having common asprintf shim
      fix asprintf memory leaks
      add log stream when using journal

Stephen Hemminger (15):
  maintainers: add for log library
  windows: make getopt functions have const properties
  windows: add os shim for localtime_r
  windows: common wrapper for vasprintf and asprintf
  eal: make eal_log_level_parse common
  eal: do not duplicate rte_init_alert() messages
  eal: change rte_exit() output to match rte_log()
  log: move handling of syslog facility out of eal
  eal: initialize log before everything else
  log: drop syslog support, and make code common
  log: add hook for printing log messages
  log: add timestamp option
  log: add optional support of syslog
  log: add support for systemd journal
  log: colorize log output

 MAINTAINERS                                   |   1 +
 app/test/test_eal_flags.c                     |  64 +-
 doc/guides/linux_gsg/linux_eal_parameters.rst |  27 -
 doc/guides/prog_guide/log_lib.rst             |  57 ++
 drivers/bus/pci/pci_common.c                  |  32 -
 lib/eal/common/eal_common_debug.c             |  10 +-
 lib/eal/common/eal_common_options.c           | 117 ++--
 lib/eal/common/eal_options.h                  |   5 +
 lib/eal/common/eal_private.h                  |  10 -
 lib/eal/freebsd/eal.c                         |  64 +-
 lib/eal/linux/eal.c                           |  68 +-
 lib/eal/windows/eal.c                         |  77 +--
 lib/eal/windows/getopt.c                      |  23 +-
 lib/eal/windows/include/getopt.h              |   8 +-
 lib/eal/windows/include/rte_os_shim.h         |  56 ++
 lib/log/log.c                                 | 633 +++++++++++++++++-
 lib/log/log_freebsd.c                         |   5 +-
 lib/log/log_internal.h                        |  25 +-
 lib/log/log_linux.c                           |  61 --
 lib/log/log_windows.c                         |  18 -
 lib/log/meson.build                           |   5 +-
 lib/log/version.map                           |   4 +-
 22 files changed, 942 insertions(+), 428 deletions(-)
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 01/15] maintainers: add for log library
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 02/15] windows: make getopt functions have const properties Stephen Hemminger
                     ` (13 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Thomas Monjalon

"You touch it you own it"
Add myself as maintainer for log library.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 7abb3aee49..54c28a601d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -180,6 +180,7 @@ F: app/test/test_threads.c
 F: app/test/test_version.c
 
 Logging
+M: Stephen Hemminger <stephen@networkplumber.org>
 F: lib/log/
 F: doc/guides/prog_guide/log_lib.rst
 F: app/test/test_logs.c
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 02/15] windows: make getopt functions have const properties
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 01/15] maintainers: add for log library Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 03/15] windows: add os shim for localtime_r Stephen Hemminger
                     ` (12 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Tyler Retzlaff, Dmitry Kozlyuk, Pallavi Kadam

Having different prototypes on different platforms can lead
to lots of unnecessary workarounds.  Looks like the version of
getopt used from windows was based on an older out of date
version from FreeBSD.

This patch changes getopt, getopt_long, etc to have the same const
attributes as Linux and FreeBSD. The changes are derived from
the current FreeBSD version of getopt_long.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 lib/eal/windows/getopt.c         | 23 ++++++++++++-----------
 lib/eal/windows/include/getopt.h |  8 ++++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lib/eal/windows/getopt.c b/lib/eal/windows/getopt.c
index a1f51c6c23..50ff71b930 100644
--- a/lib/eal/windows/getopt.c
+++ b/lib/eal/windows/getopt.c
@@ -20,7 +20,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-const char    *optarg;		/* argument associated with option */
+char    *optarg;		/* argument associated with option */
 int	opterr = 1;		/* if error message should be printed */
 int	optind = 1;		/* index into parent argv vector */
 int	optopt = '?';		/* character checked for validity */
@@ -39,9 +39,9 @@ static void pass(const char *a) {(void) a; }
 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
 #define	INORDER		1
 
-#define	EMSG		""
+static char EMSG[] = "";
 
-static const char *place = EMSG; /* option letter processing */
+static char *place = EMSG; /* option letter processing */
 
 /* XXX: set optreset to 1 rather than these two */
 static int nonopt_start = -1; /* first non option argument (for permute) */
@@ -80,7 +80,7 @@ gcd(int a, int b)
  */
 static void
 permute_args(int panonopt_start, int panonopt_end, int opt_end,
-	char **nargv)
+	char * const *nargv)
 {
 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
 	char *swap;
@@ -101,11 +101,12 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
 				pos -= nnonopts;
 			else
 				pos += nopts;
+
 			swap = nargv[pos];
 			/* LINTED const cast */
-			((char **) nargv)[pos] = nargv[cstart];
+			((char **)(uintptr_t)nargv)[pos] = nargv[cstart];
 			/* LINTED const cast */
-			((char **)nargv)[cstart] = swap;
+			((char **)(uintptr_t)nargv)[cstart] = swap;
 		}
 	}
 }
@@ -116,7 +117,7 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
  * Returns -1 if short_too is set and the option does not match long_options.
  */
 static int
-parse_long_options(char **nargv, const char *options,
+parse_long_options(char * const *nargv, const char *options,
 	const struct option *long_options, int *idx, int short_too)
 {
 	const char *current_argv;
@@ -236,7 +237,7 @@ parse_long_options(char **nargv, const char *options,
  *	Parse argc/argv argument vector.  Called by user level routines.
  */
 static int
-getopt_internal(int nargc, char **nargv, const char *options,
+getopt_internal(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx, int flags)
 {
 	char *oli;				/* option letter list index */
@@ -434,7 +435,7 @@ getopt_internal(int nargc, char **nargv, const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt(int nargc, char *nargv[], const char *options)
+getopt(int nargc, char *const nargv[], const char *options)
 {
 	return getopt_internal(nargc, nargv, options, NULL, NULL,
 			       FLAG_PERMUTE);
@@ -445,7 +446,7 @@ getopt(int nargc, char *nargv[], const char *options)
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long(int nargc, char *nargv[], const char *options,
+getopt_long(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
@@ -458,7 +459,7 @@ getopt_long(int nargc, char *nargv[], const char *options,
  *	Parse argc/argv argument vector.
  */
 int
-getopt_long_only(int nargc, char *nargv[], const char *options,
+getopt_long_only(int nargc, char *const nargv[], const char *options,
 	const struct option *long_options, int *idx)
 {
 
diff --git a/lib/eal/windows/include/getopt.h b/lib/eal/windows/include/getopt.h
index 6f57af454b..e4cf6873cb 100644
--- a/lib/eal/windows/include/getopt.h
+++ b/lib/eal/windows/include/getopt.h
@@ -44,7 +44,7 @@
 
 
 /** argument to current option, or NULL if it has none */
-extern const char *optarg;
+extern char *optarg;
 /** Current position in arg string.  Starts from 1.
  * Setting to 0 resets state.
  */
@@ -80,14 +80,14 @@ struct option {
 };
 
 /** Compat: getopt */
-int getopt(int argc, char *argv[], const char *options);
+int getopt(int argc, char *const argv[], const char *options);
 
 /** Compat: getopt_long */
-int getopt_long(int argc, char *argv[], const char *options,
+int getopt_long(int argc, char *const argv[], const char *options,
 		const struct option *longopts, int *longindex);
 
 /** Compat: getopt_long_only */
-int getopt_long_only(int nargc, char *argv[], const char *options,
+int getopt_long_only(int nargc, char *const argv[], const char *options,
 		     const struct option *long_options, int *idx);
 
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 03/15] windows: add os shim for localtime_r
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 01/15] maintainers: add for log library Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 02/15] windows: make getopt functions have const properties Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 04/15] windows: common wrapper for vasprintf and asprintf Stephen Hemminger
                     ` (11 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Dmitry Kozlyuk, Tyler Retzlaff, Pallavi Kadam

Windows does not have localtime_r but it does have a similar
function that can be used instead.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/windows/include/rte_os_shim.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index eda8113662..e9741a9df2 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -110,4 +110,14 @@ rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
 }
 #define clock_gettime(clock_id, tp) rte_clock_gettime(clock_id, tp)
 
+static inline struct tm *
+rte_localtime_r(const time_t *timer, struct tm *buf)
+{
+	if (localtime_s(buf, timer) == 0)
+		return buf;
+	else
+		return NULL;
+}
+#define localtime_r(timer, buf) rte_localtime_r(timer, buf)
+
 #endif /* _RTE_OS_SHIM_ */
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 04/15] windows: common wrapper for vasprintf and asprintf
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                     ` (2 preceding siblings ...)
  2024-03-26 17:34   ` [PATCH v14 03/15] windows: add os shim for localtime_r Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 05/15] eal: make eal_log_level_parse common Stephen Hemminger
                     ` (10 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Chenbo Xia, Nipun Gupta, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam, David Marchand, Bruce Richardson,
	Nick Connolly, Khoa To, Ranjit Menon

Replace the windows version of asprintf() that was only usable
in eal. With a more generic one that supports both vasprintf()
and asprintf().  This also eliminates duplicate code.

Fixes: 8f4de2dba9b9 ("bus/pci: fill bus specific information")
Fixes: 9ec521006db0 ("eal/windows: hide asprintf shim")

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 drivers/bus/pci/pci_common.c          | 32 -------------------
 lib/eal/common/eal_private.h          | 10 ------
 lib/eal/windows/eal.c                 | 28 ----------------
 lib/eal/windows/include/rte_os_shim.h | 46 +++++++++++++++++++++++++++
 4 files changed, 46 insertions(+), 70 deletions(-)

diff --git a/drivers/bus/pci/pci_common.c b/drivers/bus/pci/pci_common.c
index 889a48d2af..80691c75a3 100644
--- a/drivers/bus/pci/pci_common.c
+++ b/drivers/bus/pci/pci_common.c
@@ -45,38 +45,6 @@ const char *rte_pci_get_sysfs_path(void)
 	return path;
 }
 
-#ifdef RTE_EXEC_ENV_WINDOWS
-#define asprintf pci_asprintf
-
-static int
-__rte_format_printf(2, 3)
-pci_asprintf(char **buffer, const char *format, ...)
-{
-	int size, ret;
-	va_list arg;
-
-	va_start(arg, format);
-	size = vsnprintf(NULL, 0, format, arg);
-	va_end(arg);
-	if (size < 0)
-		return -1;
-	size++;
-
-	*buffer = malloc(size);
-	if (*buffer == NULL)
-		return -1;
-
-	va_start(arg, format);
-	ret = vsnprintf(*buffer, size, format, arg);
-	va_end(arg);
-	if (ret != size - 1) {
-		free(*buffer);
-		return -1;
-	}
-	return ret;
-}
-#endif /* RTE_EXEC_ENV_WINDOWS */
-
 static struct rte_devargs *
 pci_devargs_lookup(const struct rte_pci_addr *pci_addr)
 {
diff --git a/lib/eal/common/eal_private.h b/lib/eal/common/eal_private.h
index 71523cfdb8..da8d77a134 100644
--- a/lib/eal/common/eal_private.h
+++ b/lib/eal/common/eal_private.h
@@ -737,16 +737,6 @@ void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
  */
 void __rte_thread_uninit(void);
 
-/**
- * asprintf(3) replacement for Windows.
- */
-#ifdef RTE_EXEC_ENV_WINDOWS
-__rte_format_printf(2, 3)
-int eal_asprintf(char **buffer, const char *format, ...);
-
-#define asprintf(buffer, format, ...) \
-		eal_asprintf(buffer, format, ##__VA_ARGS__)
-#endif
 
 #define EAL_LOG(level, ...) \
 	RTE_LOG_LINE(level, EAL, "" __VA_ARGS__)
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 52f0e7462d..8ca00c0f95 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -503,34 +503,6 @@ rte_eal_init(int argc, char **argv)
 	return fctret;
 }
 
-/* Don't use MinGW asprintf() to have identical code with all toolchains. */
-int
-eal_asprintf(char **buffer, const char *format, ...)
-{
-	int size, ret;
-	va_list arg;
-
-	va_start(arg, format);
-	size = vsnprintf(NULL, 0, format, arg);
-	va_end(arg);
-	if (size < 0)
-		return -1;
-	size++;
-
-	*buffer = malloc(size);
-	if (*buffer == NULL)
-		return -1;
-
-	va_start(arg, format);
-	ret = vsnprintf(*buffer, size, format, arg);
-	va_end(arg);
-	if (ret != size - 1) {
-		free(*buffer);
-		return -1;
-	}
-	return ret;
-}
-
 int
 rte_vfio_container_dma_map(__rte_unused int container_fd,
 			__rte_unused uint64_t vaddr,
diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index e9741a9df2..b941476fe0 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -3,6 +3,7 @@
 #ifndef _RTE_OS_SHIM_
 #define _RTE_OS_SHIM_
 
+#include <stdio.h>
 #include <time.h>
 
 #include <rte_os.h>
@@ -120,4 +121,49 @@ rte_localtime_r(const time_t *timer, struct tm *buf)
 }
 #define localtime_r(timer, buf) rte_localtime_r(timer, buf)
 
+/* print to allocated string */
+static inline int
+rte_vasprintf(char **strp, const char *fmt, va_list ap)
+{
+	char *str;
+	int len, ret;
+
+	*strp = NULL;
+
+	/* determine size of buffer needed */
+	len = _vscprintf(fmt, ap);
+	if (len < 0)
+		return -1;
+
+	len += 1;	/* for nul termination */
+	str = malloc(len);
+	if (str == NULL)
+		return -1;
+
+	ret = vsnprintf(str, len, fmt, ap);
+	if (ret < 0) {
+		free(str);
+		return -1;
+	} else {
+		*strp = str;
+		return ret;
+	}
+}
+#define vasprintf(strp, fmt, ap) rte_vasprintf(strp, fmt, ap)
+
+static inline int
+rte_asprintf(char **strp, const char *fmt, ...)
+{
+	int ret;
+
+	va_list ap;
+
+	va_start(ap, fmt);
+	ret = rte_vasprintf(strp, fmt, ap);
+	va_end(ap);
+
+	return ret;
+}
+
+#define asprintf(strp, fmt, ...) rte_asprintf(strp, fmt, __VA_ARGS__)
 #endif /* _RTE_OS_SHIM_ */
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 05/15] eal: make eal_log_level_parse common
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                     ` (3 preceding siblings ...)
  2024-03-26 17:34   ` [PATCH v14 04/15] windows: common wrapper for vasprintf and asprintf Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 06/15] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
                     ` (9 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

The code to parse for log-level option should be same on
all OS variants.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 46 +++++++++++++++++++++++++++++
 lib/eal/common/eal_options.h        |  1 +
 lib/eal/freebsd/eal.c               | 42 --------------------------
 lib/eal/linux/eal.c                 | 39 ------------------------
 lib/eal/windows/eal.c               | 35 ----------------------
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index e541f07939..5435399b85 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -1640,6 +1640,51 @@ eal_parse_huge_unlink(const char *arg, struct hugepage_file_discipline *out)
 	return -1;
 }
 
+/* Parse the all arguments looking for log related ones */
+int
+eal_log_level_parse(int argc, char * const argv[])
+{
+	struct internal_config *internal_conf = eal_get_internal_configuration();
+	int option_index, opt;
+	const int old_optind = optind;
+	const int old_optopt = optopt;
+	const int old_opterr = opterr;
+	char *old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+	opterr = 0;
+
+	while ((opt = getopt_long(argc, argv, eal_short_options,
+				  eal_long_options, &option_index)) != EOF) {
+
+		switch (opt) {
+		case OPT_LOG_LEVEL_NUM:
+			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
+				return -1;
+			break;
+		case '?':
+			/* getopt is not happy, stop right now */
+			goto out;
+		default:
+			continue;
+		}
+	}
+out:
+	/* restore getopt lib */
+	optind = old_optind;
+	optopt = old_optopt;
+	optarg = old_optarg;
+	opterr = old_opterr;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	optreset = old_optreset;
+#endif
+	return 0;
+}
+
 int
 eal_parse_common_option(int opt, const char *optarg,
 			struct internal_config *conf)
@@ -2173,6 +2218,7 @@ rte_vect_set_max_simd_bitwidth(uint16_t bitwidth)
 	return 0;
 }
 
+
 void
 eal_common_usage(void)
 {
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 3cc9cb6412..f3f2e104f6 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -96,6 +96,7 @@ enum {
 extern const char eal_short_options[];
 extern const struct option eal_long_options[];
 
+int eal_log_level_parse(int argc, char * const argv[]);
 int eal_parse_common_option(int opt, const char *argv,
 			    struct internal_config *conf);
 int eal_option_device_parse(void);
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index bab77118e9..9825bcea0b 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -363,48 +363,6 @@ eal_get_hugepage_mem_size(void)
 	return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	const int old_optreset = optreset;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-	optreset = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optreset = old_optreset;
-	optarg = old_optarg;
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index fd422f1f62..bffeb1f34e 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token)
 	return -1;
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	const int old_optind = optind;
-	const int old_optopt = optopt;
-	char * const old_optarg = optarg;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-	optind = 1;
-
-	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)
-			break;
-	}
-
-	/* restore getopt lib */
-	optind = old_optind;
-	optopt = old_optopt;
-	optarg = old_optarg;
-}
-
 static int
 eal_parse_huge_worker_stack(const char *arg)
 {
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 8ca00c0f95..44e20643f2 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -96,41 +96,6 @@ eal_usage(const char *prgname)
 	}
 }
 
-/* Parse the arguments for --log-level only */
-static void
-eal_log_level_parse(int argc, char **argv)
-{
-	int opt;
-	char **argvopt;
-	int option_index;
-	struct internal_config *internal_conf =
-		eal_get_internal_configuration();
-
-	argvopt = argv;
-
-	eal_reset_internal_config(internal_conf);
-
-	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)
-			break;
-	}
-
-	optind = 0; /* reset getopt lib */
-}
-
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 06/15] eal: do not duplicate rte_init_alert() messages
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                     ` (4 preceding siblings ...)
  2024-03-26 17:34   ` [PATCH v14 05/15] eal: make eal_log_level_parse common Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 07/15] eal: change rte_exit() output to match rte_log() Stephen Hemminger
                     ` (8 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Bruce Richardson

The message already goes through logging, and does not need
to be printed on stderr. Message level should be ALERT
to match function name.

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

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 9825bcea0b..17b56f38aa 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -529,8 +529,7 @@ rte_eal_iopl_init(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /* Launch threads, called at application init(). */
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index bffeb1f34e..23dc26b124 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -840,8 +840,7 @@ static int rte_eal_vfio_setup(void)
 
 static void rte_eal_init_alert(const char *msg)
 {
-	fprintf(stderr, "EAL: FATAL: %s\n", msg);
-	EAL_LOG(ERR, "%s", msg);
+	EAL_LOG(ALERT, "%s", msg);
 }
 
 /*
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 07/15] eal: change rte_exit() output to match rte_log()
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                     ` (5 preceding siblings ...)
  2024-03-26 17:34   ` [PATCH v14 06/15] eal: do not duplicate rte_init_alert() messages Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 08/15] log: move handling of syslog facility out of eal Stephen Hemminger
                     ` (7 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

The rte_exit() output format confuses the timestamp and coloring
options. Change it to use be a single line with proper prefix.

Before:
[     0.006481] EAL: Error - exiting with code: 1
  Cause: [     0.006489] Cannot init EAL: Permission denied

After:
[     0.006238] EAL: Error - exiting with code: 1
[     0.006250] EAL: Cause - Cannot init EAL: Permission denied

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_debug.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/eal/common/eal_common_debug.c b/lib/eal/common/eal_common_debug.c
index 3e77995896..71bb5a7d34 100644
--- a/lib/eal/common/eal_common_debug.c
+++ b/lib/eal/common/eal_common_debug.c
@@ -34,17 +34,17 @@ void
 rte_exit(int exit_code, const char *format, ...)
 {
 	va_list ap;
+	char *msg = NULL;
 
 	if (exit_code != 0)
-		RTE_LOG(CRIT, EAL, "Error - exiting with code: %d\n"
-				"  Cause: ", exit_code);
+		EAL_LOG(CRIT, "Error - exiting with code: %d", exit_code);
 
 	va_start(ap, format);
-	rte_vlog(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, format, ap);
+	if (vasprintf(&msg, format, ap) > 0)
+		rte_log(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, "EAL: Cause - %s", msg);
 	va_end(ap);
 
 	if (rte_eal_cleanup() != 0 && rte_errno != EALREADY)
-		EAL_LOG(CRIT,
-			"EAL could not release all resources");
+		EAL_LOG(CRIT, "EAL could not release all resources");
 	exit(exit_code);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 08/15] log: move handling of syslog facility out of eal
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                     ` (6 preceding siblings ...)
  2024-03-26 17:34   ` [PATCH v14 07/15] eal: change rte_exit() output to match rte_log() Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 09/15] eal: initialize log before everything else Stephen Hemminger
                     ` (6 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

The syslog facility property is better handled in lib/log
rather than in eal. This also allows for changes to what
syslog flag means in later steps.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/eal_common_options.c | 51 ++---------------------------
 lib/eal/freebsd/eal.c               |  5 ++-
 lib/eal/linux/eal.c                 |  7 ++--
 lib/eal/windows/eal.c               |  6 ++--
 lib/log/log_freebsd.c               |  2 +-
 lib/log/log_internal.h              |  5 ++-
 lib/log/log_linux.c                 | 47 ++++++++++++++++++++++++--
 lib/log/log_windows.c               |  8 ++++-
 lib/log/version.map                 |  1 +
 9 files changed, 68 insertions(+), 64 deletions(-)

diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 5435399b85..661b2db211 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -6,9 +6,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#ifndef RTE_EXEC_ENV_WINDOWS
-#include <syslog.h>
-#endif
 #include <ctype.h>
 #include <limits.h>
 #include <errno.h>
@@ -349,10 +346,6 @@ eal_reset_internal_config(struct internal_config *internal_cfg)
 	}
 	internal_cfg->base_virtaddr = 0;
 
-#ifdef LOG_DAEMON
-	internal_cfg->syslog_facility = LOG_DAEMON;
-#endif
-
 	/* if set to NONE, interrupt mode is determined automatically */
 	internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
 	memset(internal_cfg->vfio_vf_token, 0,
@@ -1297,47 +1290,6 @@ eal_parse_lcores(const char *lcores)
 	return ret;
 }
 
-#ifndef RTE_EXEC_ENV_WINDOWS
-static int
-eal_parse_syslog(const char *facility, struct internal_config *conf)
-{
-	int i;
-	static const struct {
-		const char *name;
-		int value;
-	} map[] = {
-		{ "auth", LOG_AUTH },
-		{ "cron", LOG_CRON },
-		{ "daemon", LOG_DAEMON },
-		{ "ftp", LOG_FTP },
-		{ "kern", LOG_KERN },
-		{ "lpr", LOG_LPR },
-		{ "mail", LOG_MAIL },
-		{ "news", LOG_NEWS },
-		{ "syslog", LOG_SYSLOG },
-		{ "user", LOG_USER },
-		{ "uucp", LOG_UUCP },
-		{ "local0", LOG_LOCAL0 },
-		{ "local1", LOG_LOCAL1 },
-		{ "local2", LOG_LOCAL2 },
-		{ "local3", LOG_LOCAL3 },
-		{ "local4", LOG_LOCAL4 },
-		{ "local5", LOG_LOCAL5 },
-		{ "local6", LOG_LOCAL6 },
-		{ "local7", LOG_LOCAL7 },
-		{ NULL, 0 }
-	};
-
-	for (i = 0; map[i].name; i++) {
-		if (!strcmp(facility, map[i].name)) {
-			conf->syslog_facility = map[i].value;
-			return 0;
-		}
-	}
-	return -1;
-}
-#endif
-
 static void
 eal_log_usage(void)
 {
@@ -1663,6 +1615,7 @@ eal_log_level_parse(int argc, char * const argv[])
 
 		switch (opt) {
 		case OPT_LOG_LEVEL_NUM:
+		case OPT_SYSLOG_NUM:
 			if (eal_parse_common_option(opt, optarg, internal_conf) < 0)
 				return -1;
 			break;
@@ -1882,7 +1835,7 @@ eal_parse_common_option(int opt, const char *optarg,
 
 #ifndef RTE_EXEC_ENV_WINDOWS
 	case OPT_SYSLOG_NUM:
-		if (eal_parse_syslog(optarg, conf) < 0) {
+		if (eal_log_syslog(optarg) < 0) {
 			EAL_LOG(ERR, "invalid parameters for --"
 					OPT_SYSLOG);
 			return -1;
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 17b56f38aa..6552f9c138 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -11,7 +11,6 @@
 #include <stdarg.h>
 #include <unistd.h>
 #include <pthread.h>
-#include <syslog.h>
 #include <getopt.h>
 #include <sys/file.h>
 #include <stddef.h>
@@ -392,8 +391,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_LOG_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 23dc26b124..3d0c34063e 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -610,8 +610,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 options */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -1106,8 +1106,7 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(program_invocation_short_name,
-			 internal_conf->syslog_facility) < 0) {
+	if (eal_log_init(program_invocation_short_name) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 44e20643f2..14e498a643 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -120,8 +120,8 @@ eal_parse_args(int argc, char **argv)
 			return -1;
 		}
 
-		/* eal_log_level_parse() already handled this option */
-		if (opt == OPT_LOG_LEVEL_NUM)
+		/* eal_log_level_parse() already handled these options */
+		if (opt == OPT_LOG_LEVEL_NUM || opt == OPT_SYSLOG_NUM)
 			continue;
 
 		ret = eal_parse_common_option(opt, optarg, internal_conf);
@@ -250,7 +250,7 @@ rte_eal_init(int argc, char **argv)
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_THREAD_NAME_SIZE];
 
-	eal_log_init(NULL, 0);
+	eal_log_init(NULL);
 
 	eal_log_level_parse(argc, argv);
 
diff --git a/lib/log/log_freebsd.c b/lib/log/log_freebsd.c
index 698d3c5423..953e371bee 100644
--- a/lib/log/log_freebsd.c
+++ b/lib/log/log_freebsd.c
@@ -6,7 +6,7 @@
 #include "log_internal.h"
 
 int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
+eal_log_init(__rte_unused const char *id)
 {
 	return 0;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index 451629f1c1..cb15cdff08 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -14,7 +14,7 @@
  * Initialize the default log stream.
  */
 __rte_internal
-int eal_log_init(const char *id, int facility);
+int eal_log_init(const char *id);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
@@ -30,6 +30,9 @@ int eal_log_save_regexp(const char *regexp, uint32_t level);
 __rte_internal
 int eal_log_save_pattern(const char *pattern, uint32_t level);
 
+__rte_internal
+int eal_log_syslog(const char *name);
+
 /*
  * Convert log level to string.
  */
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
index 2dfb0c974b..47aa074da2 100644
--- a/lib/log/log_linux.c
+++ b/lib/log/log_linux.c
@@ -3,13 +3,56 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <syslog.h>
 
+#include <rte_common.h>
 #include <rte_log.h>
 
 #include "log_internal.h"
 
+static int log_facility = LOG_DAEMON;
+
+static const struct {
+	const char *name;
+	int value;
+} facilitys[] = {
+	{ "auth", LOG_AUTH },
+	{ "cron", LOG_CRON },
+	{ "daemon", LOG_DAEMON },
+	{ "ftp", LOG_FTP },
+	{ "kern", LOG_KERN },
+	{ "lpr", LOG_LPR },
+	{ "mail", LOG_MAIL },
+	{ "news", LOG_NEWS },
+	{ "syslog", LOG_SYSLOG },
+	{ "user", LOG_USER },
+	{ "uucp", LOG_UUCP },
+	{ "local0", LOG_LOCAL0 },
+	{ "local1", LOG_LOCAL1 },
+	{ "local2", LOG_LOCAL2 },
+	{ "local3", LOG_LOCAL3 },
+	{ "local4", LOG_LOCAL4 },
+	{ "local5", LOG_LOCAL5 },
+	{ "local6", LOG_LOCAL6 },
+	{ "local7", LOG_LOCAL7 },
+};
+
+int
+eal_log_syslog(const char *name)
+{
+	unsigned int i;
+
+	for (i = 0; i < RTE_DIM(facilitys); i++) {
+		if (!strcmp(name, facilitys[i].name)) {
+			log_facility = facilitys[i].value;
+			return 0;
+		}
+	}
+	return -1;
+}
+
 /*
  * default log function
  */
@@ -45,7 +88,7 @@ static cookie_io_functions_t console_log_func = {
  * once memzones are available.
  */
 int
-eal_log_init(const char *id, int facility)
+eal_log_init(const char *id)
 {
 	FILE *log_stream;
 
@@ -53,7 +96,7 @@ eal_log_init(const char *id, int facility)
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
 
 	eal_log_set_default(log_stream);
 
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a6a0889550..a3a756351d 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -6,9 +6,15 @@
 #include <rte_log.h>
 #include "log_internal.h"
 
+int
+eal_log_syslog(const char *name __rte_unused)
+{
+	return -1; /* not used */
+}
+
 /* set the log to default function, called during eal init process. */
 int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
+eal_log_init(__rte_unused const char *id)
 {
 	rte_openlog_stream(stderr);
 
diff --git a/lib/log/version.map b/lib/log/version.map
index 0648f8831a..9c6c49bf06 100644
--- a/lib/log/version.map
+++ b/lib/log/version.map
@@ -30,5 +30,6 @@ INTERNAL {
 	eal_log_save_pattern;
 	eal_log_save_regexp;
 	eal_log_set_default;
+	eal_log_syslog;
 	rte_eal_log_cleanup;
 };
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 09/15] eal: initialize log before everything else
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                     ` (7 preceding siblings ...)
  2024-03-26 17:34   ` [PATCH v14 08/15] log: move handling of syslog facility out of eal Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 10/15] log: drop syslog support, and make code common Stephen Hemminger
                     ` (5 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev
  Cc: Stephen Hemminger, Bruce Richardson, Dmitry Kozlyuk,
	Tyler Retzlaff, Pallavi Kadam

In order for all log messages (including CPU mismatch) to
come out through the logging library, it must be initialized
as early in rte_eal_init() as possible on all platforms.

Where it was done before was likely historical based on
the support of non-OS isolated CPU's which required a shared
memory buffer; that support was dropped before DPDK was
publicly released.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c  | 12 +++++++++---
 lib/eal/linux/eal.c    | 19 +++++++++----------
 lib/eal/windows/eal.c  |  8 ++++++--
 lib/log/log_freebsd.c  |  3 +--
 lib/log/log_internal.h |  2 +-
 lib/log/log_linux.c    | 14 ++++++--------
 lib/log/log_windows.c  |  4 +---
 7 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 6552f9c138..55ff27a4da 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -52,6 +52,7 @@
 #include "eal_options.h"
 #include "eal_memcfg.h"
 #include "eal_trace.h"
+#include "log_internal.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
 
@@ -546,6 +547,14 @@ rte_eal_init(int argc, char **argv)
 	bool has_phys_addr;
 	enum rte_iova_mode iova_mode;
 
+	/* setup log as early as possible */
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log arguments.");
+		rte_errno = EINVAL;
+		return -1;
+	}
+	eal_log_init(getprogname());
+
 	/* checks if the machine is adequate */
 	if (!rte_cpu_is_supported()) {
 		rte_eal_init_alert("unsupported cpu type.");
@@ -565,9 +574,6 @@ rte_eal_init(int argc, char **argv)
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
-	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
-
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 3d0c34063e..b9a0fb1742 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -936,6 +936,15 @@ rte_eal_init(int argc, char **argv)
 	struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
+	/* setup log as early as possible */
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log arguments.");
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	eal_log_init(program_invocation_short_name);
+
 	/* checks if the machine is adequate */
 	if (!rte_cpu_is_supported()) {
 		rte_eal_init_alert("unsupported cpu type.");
@@ -952,9 +961,6 @@ rte_eal_init(int argc, char **argv)
 
 	eal_reset_internal_config(internal_conf);
 
-	/* set log level as early as possible */
-	eal_log_level_parse(argc, argv);
-
 	/* clone argv to report out later in telemetry */
 	eal_save_args(argc, argv);
 
@@ -1106,13 +1112,6 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(program_invocation_short_name) < 0) {
-		rte_eal_init_alert("Cannot init logging.");
-		rte_errno = ENOMEM;
-		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
-		return -1;
-	}
-
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c
index 14e498a643..e59aba954e 100644
--- a/lib/eal/windows/eal.c
+++ b/lib/eal/windows/eal.c
@@ -250,9 +250,13 @@ rte_eal_init(int argc, char **argv)
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_THREAD_NAME_SIZE];
 
-	eal_log_init(NULL);
+	if (eal_log_level_parse(argc, argv) < 0) {
+		rte_eal_init_alert("invalid log arguments.");
+		rte_errno = EINVAL;
+		return -1;
+	}
 
-	eal_log_level_parse(argc, argv);
+	eal_log_init(NULL);
 
 	if (eal_create_cpu_map() < 0) {
 		rte_eal_init_alert("Cannot discover CPU and NUMA.");
diff --git a/lib/log/log_freebsd.c b/lib/log/log_freebsd.c
index 953e371bee..33a0925c43 100644
--- a/lib/log/log_freebsd.c
+++ b/lib/log/log_freebsd.c
@@ -5,8 +5,7 @@
 #include <rte_common.h>
 #include "log_internal.h"
 
-int
+void
 eal_log_init(__rte_unused const char *id)
 {
-	return 0;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index cb15cdff08..d5fabd7ef7 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -14,7 +14,7 @@
  * Initialize the default log stream.
  */
 __rte_internal
-int eal_log_init(const char *id);
+void eal_log_init(const char *id);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
index 47aa074da2..6d7dc8f3ab 100644
--- a/lib/log/log_linux.c
+++ b/lib/log/log_linux.c
@@ -87,18 +87,16 @@ static cookie_io_functions_t console_log_func = {
  * set the log to default function, called during eal init process,
  * once memzones are available.
  */
-int
+void
 eal_log_init(const char *id)
 {
 	FILE *log_stream;
 
-	log_stream = fopencookie(NULL, "w+", console_log_func);
-	if (log_stream == NULL)
-		return -1;
-
 	openlog(id, LOG_NDELAY | LOG_PID, log_facility);
 
-	eal_log_set_default(log_stream);
-
-	return 0;
+	log_stream = fopencookie(NULL, "w+", console_log_func);
+	if (log_stream != NULL)
+		eal_log_set_default(log_stream);
+	else
+		eal_log_set_default(stderr);
 }
diff --git a/lib/log/log_windows.c b/lib/log/log_windows.c
index a3a756351d..d7c30e4cfa 100644
--- a/lib/log/log_windows.c
+++ b/lib/log/log_windows.c
@@ -13,12 +13,10 @@ eal_log_syslog(const char *name __rte_unused)
 }
 
 /* set the log to default function, called during eal init process. */
-int
+void
 eal_log_init(__rte_unused const char *id)
 {
 	rte_openlog_stream(stderr);
 
 	eal_log_set_default(stderr);
-
-	return 0;
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 277+ messages in thread

* [PATCH v14 10/15] log: drop syslog support, and make code common
  2024-03-26 17:34 ` [PATCH v14 00/15] Logging unification and improvments Stephen Hemminger
                     ` (8 preceding siblings ...)
  2024-03-26 17:34   ` [PATCH v14 09/15] eal: initialize log before everything else Stephen Hemminger
@ 2024-03-26 17:34   ` Stephen Hemminger
  2024-03-26 17:34   ` [PATCH v14 11/15] log: add hook for printing log messages Stephen Hemminger
                     ` (4 subsequent siblings)
  14 siblings, 0 replies; 277+ messages in thread
From: Stephen Hemminger @ 2024-03-26 17:34 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

This patch makes the log setup code common across all platforms.

Drops syslog support for now, will come back in later patch.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c           |  11 ++-
 lib/eal/common/eal_common_options.c |   3 -
 lib/log/log.c                       |  29 +++++---
 lib/log/log_internal.h              |   6 --
 lib/log/log_linux.c                 | 102 ----------------------------
 lib/log/log_windows.c               |  22 ------
 lib/log/meson.build                 |   5 +-
 lib/log/version.map                 |   1 -
 8 files changed, 26 insertions(+), 153 deletions(-)
 delete mode 100644 lib/log/log_linux.c
 delete mode 100644 lib/log/log_windows.c

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 6cb4b06757..36e3185a10 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -984,11 +984,10 @@ test_misc_flags(void)
 	const char *argv1[] = {prgname, prefix, mp_flag, "--no-pci"};
 	/* With -v */
 	const char *argv2[] = {prgname, prefix, mp_flag, "-v"};
+	/* With empty --syslog */
+	const char *argv3[] = {prgname, prefix, mp_flag, "--syslog"};
 	/* With valid --syslog */
-	const char *argv3[] = {prgname, prefix, mp_flag,
-			"--syslog", "syslog"};
-	/* With empty --syslog (should fail) */
-	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog"};
+	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog", "always"};
 	/* With invalid --syslog */
 	const char *argv5[] = {prgname, prefix, mp_flag, "--syslog", "error"};
 	/* With no-sh-conf, also use no-huge to ensure this test runs on BSD */
@@ -1083,8 +1082,8 @@ test_misc_flags(void)
 		printf("Error - process did not run ok with --syslog flag\n");
 		goto fail;
 	}
-	if (launch_proc(argv4) == 0) {
-		printf("Error - process run ok with empty --syslog flag\n");
+	if (launch_proc(argv4) != 0) {
+		printf("Error - process did not with --syslog always flag\n");
 		goto fail;
 	}
 	if (launch_proc(argv5) == 0) {
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index 661b2db211..9ab512e8a1 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -2212,9 +2212,6 @@ eal_common_usage(void)
 	       "                      (can be used multiple times)\n"
 	       "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
-#ifndef RTE_EXEC_ENV_WINDOWS
-	       "  --"OPT_SYSLOG"            Set syslog facility\n"
-#endif
 	       "  --"OPT_LOG_LEVEL"=<level> Set global log level\n"
 	       "  --"OPT_LOG_LEVEL"=<type-match>:<level>\n"
 	       "                      Set specific log level\n"
diff --git a/lib/log/log.c b/lib/log/log.c
index 255f757d94..f597da2e39 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -70,12 +70,13 @@ struct log_cur_msg {
  /* per core log */
 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
 
-/* default logs */
-
 /* Change the stream that will be used by logging system */
 int
 rte_openlog_stream(FILE *f)
 {
+	if (rte_logs.file != NULL)
+		fclose(rte_logs.file);
+
 	rte_logs.file = f;
 	return 0;
 }
@@ -505,13 +506,20 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
 	return ret;
 }
 
+/* Placeholder */
+int
+eal_log_syslog(const char *mode __rte_unused)
+{
+	return -1;
+}
+
 /*
- * Called by environment-specific initialization functions.
+ * Called by rte_eal_init
  */
 void
-eal_log_set_default(FILE *default_log)
+eal_log_init(const char *id __rte_unused)
 {
-	default_log_stream = default_log;
+	default_log_stream = stderr;
 
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 	RTE_LOG(NOTICE, EAL,
@@ -525,8 +533,11 @@ eal_log_set_default(FILE *default_log)
 void
 rte_eal_log_cleanup(void)
 {
-	if (default_log_stream) {
-		fclose(default_log_stream);
-		default_log_stream = NULL;
-	}
+	FILE *log_stream = rte_log_get_stream();
+
+	/* don't close stderr on the application */
+	if (log_stream != stderr)
+		fclose(log_stream);
+
+	rte_logs.file = NULL;
 }
diff --git a/lib/log/log_internal.h b/lib/log/log_internal.h
index d5fabd7ef7..3c46328e7b 100644
--- a/lib/log/log_internal.h
+++ b/lib/log/log_internal.h
@@ -16,12 +16,6 @@
 __rte_internal
 void eal_log_init(const char *id);
 
-/*
- * Determine where log data is written when no call to rte_openlog_stream.
- */
-__rte_internal
-void eal_log_set_default(FILE *default_log);
-
 /*
  * Save a log option for later.
  */
diff --git a/lib/log/log_linux.c b/lib/log/log_linux.c
deleted file mode 100644
index 6d7dc8f3ab..0000000000
--- a/lib/log/log_linux.c
+++ /dev/null
@@ -1,102 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <syslog.h>
-
-#include <rte_common.h>
-#include <rte_log.h>
-
-#include "log_internal.h"
-
-static int log_facility = LOG_DAEMON;
-
-static const struct {
-	const char *name;
-	int value;
-} facilitys[] = {
-	{ "auth", LOG_AUTH },
-	{ "cron", LOG_CRON },
-	{ "daemon", LOG_DAEMON },
-	{ "ftp", LOG_FTP },
-	{ "kern", LOG_KERN },
-	{ "lpr", LOG_LPR },
-	{ "mail", LOG_MAIL },
-	{ "news", LOG_NEWS },
-	{ "syslog", LOG_SYSLOG },
-	{ "user", LOG_USER },
-	{ "uucp", LOG_UUCP },
-	{ "local0", LOG_LOCAL0 },
-	{ "local1"