From 2cee45f3a6273141c177dff7d869c66e6fad01e4 Mon Sep 17 00:00:00 2001
From: Ronan Dunklau <ronan.dunklau@aiven.io>
Date: Fri, 20 Aug 2021 10:46:20 +0200
Subject: [PATCH v2 2/3] Add test module for the new tag functionality.

This test module consists of an extension exposing a function which just
logs things, and a hook which adds the tags to the detail field in order
to be tested in "regular" output.
---
 src/test/modules/test_logging/Makefile        |  20 +++
 .../test_logging/expected/test_logging.out    |  18 +++
 .../modules/test_logging/sql/test_logging.sql |   5 +
 .../test_logging/test_logging--1.0.sql        |   4 +
 src/test/modules/test_logging/test_logging.c  | 121 ++++++++++++++++++
 .../modules/test_logging/test_logging.control |   5 +
 6 files changed, 173 insertions(+)
 create mode 100644 src/test/modules/test_logging/Makefile
 create mode 100644 src/test/modules/test_logging/expected/test_logging.out
 create mode 100644 src/test/modules/test_logging/sql/test_logging.sql
 create mode 100644 src/test/modules/test_logging/test_logging--1.0.sql
 create mode 100644 src/test/modules/test_logging/test_logging.c
 create mode 100644 src/test/modules/test_logging/test_logging.control

diff --git a/src/test/modules/test_logging/Makefile b/src/test/modules/test_logging/Makefile
new file mode 100644
index 0000000000..1191886e43
--- /dev/null
+++ b/src/test/modules/test_logging/Makefile
@@ -0,0 +1,20 @@
+# src/test/modules/test_logging/Makefile
+
+MODULES = test_logging
+
+EXTENSION = test_logging
+DATA = test_logging--1.0.sql
+PGFILEDESC = "test_logging - simple log hook test"
+
+REGRESS = test_logging
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_logging
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_logging/expected/test_logging.out b/src/test/modules/test_logging/expected/test_logging.out
new file mode 100644
index 0000000000..41423da535
--- /dev/null
+++ b/src/test/modules/test_logging/expected/test_logging.out
@@ -0,0 +1,18 @@
+CREATE EXTENSION test_logging;
+SET log_min_messages = NOTICE;
+SELECT test_logging(18, 'This is a test message', NULL);
+NOTICE:  This is a test message
+DETAIL:  NB TAGS: 0 TAGS: 
+ test_logging 
+--------------
+ 
+(1 row)
+
+SELECT test_logging(18, 'This is a test message', '{"tag1": "value1", "tag2": "value2"}');
+NOTICE:  This is a test message
+DETAIL:  NB TAGS: 2 TAGS: tag1: value1 tag2: value2 
+ test_logging 
+--------------
+ 
+(1 row)
+
diff --git a/src/test/modules/test_logging/sql/test_logging.sql b/src/test/modules/test_logging/sql/test_logging.sql
new file mode 100644
index 0000000000..bb964b4b63
--- /dev/null
+++ b/src/test/modules/test_logging/sql/test_logging.sql
@@ -0,0 +1,5 @@
+CREATE EXTENSION test_logging;
+SET log_min_messages = NOTICE;
+SELECT test_logging(18, 'This is a test message', NULL);
+SELECT test_logging(18, 'This is a test message', '{"tag1": "value1", "tag2": "value2"}');
+
diff --git a/src/test/modules/test_logging/test_logging--1.0.sql b/src/test/modules/test_logging/test_logging--1.0.sql
new file mode 100644
index 0000000000..663af2db8f
--- /dev/null
+++ b/src/test/modules/test_logging/test_logging--1.0.sql
@@ -0,0 +1,4 @@
+CREATE FUNCTION test_logging(level int, message text, tags jsonb)
+RETURNS VOID
+AS 'MODULE_PATHNAME'
+LANGUAGE C;
diff --git a/src/test/modules/test_logging/test_logging.c b/src/test/modules/test_logging/test_logging.c
new file mode 100644
index 0000000000..f92eff5150
--- /dev/null
+++ b/src/test/modules/test_logging/test_logging.c
@@ -0,0 +1,121 @@
+/*--------------------------------------------------------------------------
+ *
+ * test_logging.c
+ *		Test logging functions
+ *
+ * Copyright (c) 2021, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		src/test/modules/test_logging/test_logging.c
+ *
+ * -------------------------------------------------------------------------
+ */
+
+#include <unistd.h>
+#include "postgres.h"
+
+#include "fmgr.h"
+#include "utils/builtins.h"
+#include "utils/elog.h"
+#include "utils/jsonb.h"
+
+PG_MODULE_MAGIC;
+
+static emit_log_hook_type prev_log_hook = NULL;
+
+void		_PG_init(void);
+void		_PG_fini(void);
+
+static void move_tags_to_detail(ErrorData *edata);
+
+static char *
+jsonb_value_to_tagvalue(JsonbValue *v)
+{
+	switch (v->type)
+	{
+		case jbvNull:
+			return "";
+		case jbvString:
+			return pnstrdup(v->val.string.val, v->val.string.len);
+		default:
+			elog(ERROR, "jsonb type not allowed here: %d", (int) v->type);
+	}
+}
+
+static void
+jsonb_tags_to_key_value_text_pairs(Jsonb *tags, List **keys, List **values)
+{
+	JsonbValue	v;
+	JsonbIterator *it;
+	JsonbIteratorToken r;
+	bool		skipNested = false;
+
+	it = JsonbIteratorInit(&tags->root);
+	*keys = *values = NIL;
+	while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
+	{
+		skipNested = true;
+		if (r == WJB_KEY)
+		{
+			*keys = lappend(*keys, pnstrdup(v.val.string.val, v.val.string.len));
+			r = JsonbIteratorNext(&it, &v, skipNested);
+			Assert(r != WJB_DONE);
+			*values = lappend(*values, jsonb_value_to_tagvalue(&v));
+		}
+	}
+}
+
+PG_FUNCTION_INFO_V1(test_logging);
+Datum
+test_logging(PG_FUNCTION_ARGS)
+{
+	int			level = PG_GETARG_INT32(0);
+	char	   *message = "";
+	List	   *keys = NIL,
+			   *values = NIL;
+	ListCell   *lk,
+			   *lv;
+
+	if (!PG_ARGISNULL(1))
+	{
+		message = text_to_cstring(PG_GETARG_TEXT_PP(1));
+	}
+	if (!PG_ARGISNULL(2))
+	{
+		jsonb_tags_to_key_value_text_pairs(PG_GETARG_JSONB_P(2), &keys, &values);
+	}
+	ereport(level,
+	 (errmsg("%s", message),
+	  ({
+		forboth(lk, keys, lv, values)
+		{
+			(errtag(lfirst(lk), "%s", (char *) lfirst(lv)));
+		}})
+	));
+
+	PG_RETURN_VOID();
+}
+
+void
+move_tags_to_detail(ErrorData *edata)
+{
+	StringInfoData buf;
+	ListCell   *lc;
+
+	initStringInfo(&buf);
+	foreach(lc, edata->tags)
+	{
+		ErrorTag   *tag = (ErrorTag *) lfirst(lc);
+
+		appendStringInfo(&buf, "%s: %s ", tag->tagname, tag->tagvalue);
+	}
+	errdetail("NB TAGS: %d TAGS: %s", list_length(edata->tags), buf.data);
+	pfree(buf.data);
+}
+
+void
+_PG_init(void)
+{
+	prev_log_hook = emit_log_hook;
+	emit_log_hook = move_tags_to_detail;
+}
diff --git a/src/test/modules/test_logging/test_logging.control b/src/test/modules/test_logging/test_logging.control
new file mode 100644
index 0000000000..b1596b4b1d
--- /dev/null
+++ b/src/test/modules/test_logging/test_logging.control
@@ -0,0 +1,5 @@
+# test_logging extension
+comment = 'test_logging - simple extension for emitting logs'
+default_version = '1.0'
+module_pathname = '$libdir/test_logging'
+relocatable = true
-- 
2.32.0

