From 9dd6c7f8e0e33dac5141945a712311b65837874b Mon Sep 17 00:00:00 2001
From: Dmitry Ivanov <d.ivanov@picodata.io>
Date: Thu, 26 Oct 2023 11:54:17 +0000
Subject: [PATCH] feat: Add new methods to `say` for FFI use cases

This patch helps us create and write to non-default
loggers provided by `say.h`. We'll use this mainly
for audit log in picodata.

NO_DOC=internal
NO_TEST=internal
NO_CHANGELOG=internal
---
 extra/exports      |  6 ++++++
 src/lib/core/say.c | 18 ++++++++++++++++++
 src/lib/core/say.h | 22 ++++++++++++----------
 3 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/extra/exports b/extra/exports
index c8f814b33a..2962b4e0a8 100644
--- a/extra/exports
+++ b/extra/exports
@@ -653,3 +653,9 @@ fiber_set_name_n
 authenticate
 user_auth_method_name
 port_c_create
+
+# picodata audit log
+log_create
+log_destroy
+log_new
+log_say
diff --git a/src/lib/core/say.c b/src/lib/core/say.c
index e441418fae..a12d811146 100644
--- a/src/lib/core/say.c
+++ b/src/lib/core/say.c
@@ -660,6 +660,12 @@ log_syslog_init(struct log *log, const char *init_str)
 	return 0;
 }
 
+struct log *
+log_new(void)
+{
+	return xmalloc(sizeof(struct log));
+}
+
 /**
  * Initialize logging subsystem to use in daemon mode.
  */
@@ -1401,6 +1407,18 @@ log_vsay(struct log *log, int level, bool check_level, const char *module,
 	return total;
 }
 
+int
+log_say(struct log *log, int level, const char *filename,
+	int line, const char *error, const char *format, ...)
+{
+	va_list ap;
+	va_start(ap, format);
+	int total = log_vsay(log, level, true, NULL, filename, line, error,
+			     format, ap);
+	va_end(ap);
+	return total;
+}
+
 void
 say_set_stderr_callback(say_stderr_callback_t before,
 			say_stderr_callback_t after)
diff --git a/src/lib/core/say.h b/src/lib/core/say.h
index 235aec751a..bcd70f23ee 100644
--- a/src/lib/core/say.h
+++ b/src/lib/core/say.h
@@ -186,6 +186,16 @@ struct log {
 	struct rlist in_log_list;
 };
 
+/**
+ * Allocate a new uninitialized log object.
+ * To initialize the object one must call log_create()
+ * before doing log_say(). To deallocate the object use
+ * log_destroy() and free().
+ * @return pointer to log object (allocation failure is fatal)
+ */
+struct log *
+log_new(void);
+
 /**
  * Create a new log object.
  * @param log		log to initialize
@@ -207,17 +217,9 @@ log_vsay(struct log *log, int level, bool check_level, const char *module,
 	 va_list ap);
 
 /** Perform log write. */
-static inline int
+int
 log_say(struct log *log, int level, const char *filename,
-	int line, const char *error, const char *format, ...)
-{
-	va_list ap;
-	va_start(ap, format);
-	int total = log_vsay(log, level, true, NULL, filename, line, error,
-			     format, ap);
-	va_end(ap);
-	return total;
-}
+	int line, const char *error, const char *format, ...);
 
 /**
  * Default logger type info.
-- 
GitLab