[v25,10/15] log: add hook for printing log messages

Message ID 20240919150609.5281-11-stephen@networkplumber.org (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series Logging enhancements for 24.11 |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Sept. 19, 2024, 3:04 p.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>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
 lib/log/log.c         | 6 +++++-
 lib/log/log_private.h | 9 +++++++++
 2 files changed, 14 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..48543ee941 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
@@ -33,11 +34,13 @@  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 = vfprintf,
 };
 
 struct rte_eal_opt_loglevel {
@@ -74,6 +77,7 @@  int
 rte_openlog_stream(FILE *f)
 {
 	rte_logs.file = f;
+	rte_logs.print_func = vfprintf;
 	return 0;
 }
 
@@ -470,7 +474,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, format, ap);
 	fflush(f);
 	return ret;
 }
diff --git a/lib/log/log_private.h b/lib/log/log_private.h
new file mode 100644
index 0000000000..67d2463b2f
--- /dev/null
+++ b/lib/log/log_private.h
@@ -0,0 +1,9 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause */
+
+#ifndef LOG_PRIVATE_H
+#define LOG_PRIVATE_H
+
+/* Note: same as vfprintf() */
+typedef int (*log_print_t)(FILE *f, const char *fmt, va_list ap);
+
+#endif /* LOG_PRIVATE_H */