[v19,11/15] log: add hook for printing log messages

Message ID 20240330030429.4630-12-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series Logging unification and improvements |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger March 30, 2024, 3 a.m. UTC
  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         | 14 +++++++++++++-
 lib/log/log_private.h | 11 +++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 lib/log/log_private.h
  

Patch

diff --git a/lib/log/log.c b/lib/log/log.c
index 3fe86ddcd7..71b00528d0 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -18,6 +18,7 @@ 
 #include <rte_per_lcore.h>
 
 #include "log_internal.h"
+#include "log_private.h"
 
 #ifdef RTE_EXEC_ENV_WINDOWS
 #define strdup _strdup
@@ -28,16 +29,19 @@  struct rte_log_dynamic_type {
 	uint32_t loglevel;
 };
 
+
 /** 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 {
@@ -74,6 +78,7 @@  int
 rte_openlog_stream(FILE *f)
 {
 	rte_logs.file = f;
+	rte_logs.print_func = log_print;
 	return 0;
 }
 
@@ -470,7 +475,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;
 }
@@ -499,6 +504,13 @@  eal_log_syslog(const char *mode __rte_unused)
 	return -1;
 }
 
+/* default log print function */
+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
  */
diff --git a/lib/log/log_private.h b/lib/log/log_private.h
new file mode 100644
index 0000000000..afd833c3bd
--- /dev/null
+++ b/lib/log/log_private.h
@@ -0,0 +1,11 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause */
+
+#ifndef LOG_PRIVATE_H
+#define LOG_PRIVATE_H
+
+typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap);
+
+__rte_format_printf(3, 0)
+int log_print(FILE *f, uint32_t level, const char *format, va_list ap);
+
+#endif /* LOG_PRIVATE_H */