[v3] log: support custom log function

Message ID 20210210052019.802387-1-fengli@smartx.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series [v3] log: support custom log function |

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/intel-Testing success Testing PASS
ci/iol-testing warning Testing issues
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-mellanox-Functional success Functional Testing PASS
ci/checkpatch warning coding style issues

Commit Message

Li Feng Feb. 10, 2021, 5:20 a.m. UTC
By default, the dpdk log is out to stdout/stderr and syslog.
The rte_openlog_stream could set an external FILE* stream, but it asks
the
consumer to give it a FILE* pointer.
For C++ or other languages, it's hard to get a libc FILE*.

Support to set a hook is another choice for this scenario.

Signed-off-by: Li Feng <fengli@smartx.com>
---
v3: Rename the func, change the comments, add funcs in version.map.
v2: Simplify the code.

 lib/librte_eal/include/rte_log.h | 29 +++++++++++++++++++++++++++++
 lib/librte_eal/linux/eal_log.c   | 21 +++++++++++++++++++++
 lib/librte_eal/version.map       |  2 ++
 lib/librte_eal/windows/eal_log.c | 19 +++++++++++++++++++
 4 files changed, 71 insertions(+)
  

Comments

Feng Li Feb. 18, 2021, 2:55 a.m. UTC | #1
Ping……
Any comments about this?

Thanks,

On Wed, Feb 10, 2021 at 1:20 PM Li Feng <fengli@smartx.com> wrote:
>
> By default, the dpdk log is out to stdout/stderr and syslog.
> The rte_openlog_stream could set an external FILE* stream, but it asks
> the
> consumer to give it a FILE* pointer.
> For C++ or other languages, it's hard to get a libc FILE*.
>
> Support to set a hook is another choice for this scenario.
>
> Signed-off-by: Li Feng <fengli@smartx.com>
> ---
> v3: Rename the func, change the comments, add funcs in version.map.
> v2: Simplify the code.
>
>  lib/librte_eal/include/rte_log.h | 29 +++++++++++++++++++++++++++++
>  lib/librte_eal/linux/eal_log.c   | 21 +++++++++++++++++++++
>  lib/librte_eal/version.map       |  2 ++
>  lib/librte_eal/windows/eal_log.c | 19 +++++++++++++++++++
>  4 files changed, 71 insertions(+)
>
> diff --git a/lib/librte_eal/include/rte_log.h b/lib/librte_eal/include/rte_log.h
> index 173004fd7..566319cd4 100644
> --- a/lib/librte_eal/include/rte_log.h
> +++ b/lib/librte_eal/include/rte_log.h
> @@ -97,6 +97,35 @@ int rte_openlog_stream(FILE *f);
>   */
>  FILE *rte_log_get_stream(void);
>
> +/**
> + * Define a logging write function.
> + */
> +typedef ssize_t rte_log_write_function(void *cookie, const char *buf, size_t size);
> +
> +/**
> + * Change the default stream's write action that will be used by the logging system.
> + *
> + * This should be done before the 'rte_eal_init' call. And the 'rte_openlog_stream'
> + * call will override this action.
> + *
> + * @param logf
> + *   Pointer to the log write function.
> + */
> +__rte_experimental
> +void
> +rte_log_sink_set(rte_log_write_function* logf);
> +
> +/**
> + * Retrieve the log function used by the logging system (see rte_log_sink_set()
> + * to change it).
> + *
> + * @return
> + *   Pointer to the log function.
> + */
> +__rte_experimental
> +rte_log_write_function*
> +rte_log_sink_get(void);
> +
>  /**
>   * Set the global log level.
>   *
> diff --git a/lib/librte_eal/linux/eal_log.c b/lib/librte_eal/linux/eal_log.c
> index 43c8460bf..2192b43f6 100644
> --- a/lib/librte_eal/linux/eal_log.c
> +++ b/lib/librte_eal/linux/eal_log.c
> @@ -60,3 +60,24 @@ rte_eal_log_init(const char *id, int facility)
>
>         return 0;
>  }
> +
> +/**
> + * Change the default stream's write action that will be used by the logging system.
> + *
> + * This should be done before the 'rte_eal_init' call. And the 'rte_openlog_stream'
> + * call will override this action.
> + */
> +void
> +rte_log_sink_set(rte_log_write_function* logf)
> +{
> +    console_log_func.write = logf;
> +}
> +
> +/**
> + * Retrieve the log function used by the logging system (see rte_log_sink_set()
> + * to change it).
> + */
> +rte_log_write_function*
> +rte_log_sink_get(void) {
> +    return console_log_func.write;
> +}
> diff --git a/lib/librte_eal/version.map b/lib/librte_eal/version.map
> index fce90a112..3cc8691a3 100644
> --- a/lib/librte_eal/version.map
> +++ b/lib/librte_eal/version.map
> @@ -412,6 +412,8 @@ EXPERIMENTAL {
>         rte_thread_tls_key_delete;
>         rte_thread_tls_value_get;
>         rte_thread_tls_value_set;
> +    rte_log_sink_set;
> +    rte_log_sink_get;
>  };
>
>  INTERNAL {
> diff --git a/lib/librte_eal/windows/eal_log.c b/lib/librte_eal/windows/eal_log.c
> index 875981f13..42c2d5ff3 100644
> --- a/lib/librte_eal/windows/eal_log.c
> +++ b/lib/librte_eal/windows/eal_log.c
> @@ -14,3 +14,22 @@ rte_eal_log_init(__rte_unused const char *id, __rte_unused int facility)
>
>         return 0;
>  }
> +
> +/*
> + * Set the customized logger, it will override the default stream write action,
> + * which is writing to syslog and stdout.
> + */
> +void
> +rte_log_sink_set(rte_log_write_function* logf)
> +{
> +       RTE_SET_USED(logf);
> +       return;
> +}
> +
> +/*
> + * Retrieve the default log write function.
> + */
> +rte_log_write_function*
> +rte_log_sink_get(void) {
> +    return NULL;
> +}
> --
> 2.29.2
>
  
Stephen Hemminger June 15, 2023, 4:12 p.m. UTC | #2
On Wed, 10 Feb 2021 13:20:19 +0800
Li Feng <fengli@smartx.com> wrote:

> By default, the dpdk log is out to stdout/stderr and syslog.
> The rte_openlog_stream could set an external FILE* stream, but it asks
> the
> consumer to give it a FILE* pointer.
> For C++ or other languages, it's hard to get a libc FILE*.
> 
> Support to set a hook is another choice for this scenario.
> 
> Signed-off-by: Li Feng <fengli@smartx.com>
> ---

Rejecting this patch.
You can do the same thing with a small bit of C called during application
init that creates a standard I/O stream. Like fopencookie().

The added overhead of adding callback here adds unnecessary complexity.
  

Patch

diff --git a/lib/librte_eal/include/rte_log.h b/lib/librte_eal/include/rte_log.h
index 173004fd7..566319cd4 100644
--- a/lib/librte_eal/include/rte_log.h
+++ b/lib/librte_eal/include/rte_log.h
@@ -97,6 +97,35 @@  int rte_openlog_stream(FILE *f);
  */
 FILE *rte_log_get_stream(void);
 
+/**
+ * Define a logging write function.
+ */
+typedef ssize_t rte_log_write_function(void *cookie, const char *buf, size_t size);
+
+/**
+ * Change the default stream's write action that will be used by the logging system.
+ *
+ * This should be done before the 'rte_eal_init' call. And the 'rte_openlog_stream'
+ * call will override this action.
+ *
+ * @param logf
+ *   Pointer to the log write function.
+ */
+__rte_experimental
+void
+rte_log_sink_set(rte_log_write_function* logf);
+
+/**
+ * Retrieve the log function used by the logging system (see rte_log_sink_set()
+ * to change it).
+ *
+ * @return
+ *   Pointer to the log function.
+ */
+__rte_experimental
+rte_log_write_function*
+rte_log_sink_get(void);
+
 /**
  * Set the global log level.
  *
diff --git a/lib/librte_eal/linux/eal_log.c b/lib/librte_eal/linux/eal_log.c
index 43c8460bf..2192b43f6 100644
--- a/lib/librte_eal/linux/eal_log.c
+++ b/lib/librte_eal/linux/eal_log.c
@@ -60,3 +60,24 @@  rte_eal_log_init(const char *id, int facility)
 
 	return 0;
 }
+
+/**
+ * Change the default stream's write action that will be used by the logging system.
+ *
+ * This should be done before the 'rte_eal_init' call. And the 'rte_openlog_stream'
+ * call will override this action.
+ */
+void
+rte_log_sink_set(rte_log_write_function* logf)
+{
+    console_log_func.write = logf;
+}
+
+/**
+ * Retrieve the log function used by the logging system (see rte_log_sink_set()
+ * to change it).
+ */
+rte_log_write_function*
+rte_log_sink_get(void) {
+    return console_log_func.write;
+}
diff --git a/lib/librte_eal/version.map b/lib/librte_eal/version.map
index fce90a112..3cc8691a3 100644
--- a/lib/librte_eal/version.map
+++ b/lib/librte_eal/version.map
@@ -412,6 +412,8 @@  EXPERIMENTAL {
 	rte_thread_tls_key_delete;
 	rte_thread_tls_value_get;
 	rte_thread_tls_value_set;
+    rte_log_sink_set;
+    rte_log_sink_get;
 };
 
 INTERNAL {
diff --git a/lib/librte_eal/windows/eal_log.c b/lib/librte_eal/windows/eal_log.c
index 875981f13..42c2d5ff3 100644
--- a/lib/librte_eal/windows/eal_log.c
+++ b/lib/librte_eal/windows/eal_log.c
@@ -14,3 +14,22 @@  rte_eal_log_init(__rte_unused const char *id, __rte_unused int facility)
 
 	return 0;
 }
+
+/*
+ * Set the customized logger, it will override the default stream write action,
+ * which is writing to syslog and stdout.
+ */
+void
+rte_log_sink_set(rte_log_write_function* logf)
+{
+	RTE_SET_USED(logf);
+	return;
+}
+
+/*
+ * Retrieve the default log write function.
+ */
+rte_log_write_function*
+rte_log_sink_get(void) {
+    return NULL;
+}