summaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
authorMarek Vavruša <mvavrusa@cloudflare.com>2017-11-28 00:02:35 +0100
committerMarek Vavruša <mvavrusa@cloudflare.com>2017-12-21 07:32:56 +0100
commit393b738acc10dc808bbc7f6bfbd0e5ead3d6df73 (patch)
tree04f067de2c1d008aba781cd5f138148adc88f4ee /contrib
parentlib/rplan: remember request context in each query (diff)
downloadknot-resolver-393b738acc10dc808bbc7f6bfbd0e5ead3d6df73.tar.xz
knot-resolver-393b738acc10dc808bbc7f6bfbd0e5ead3d6df73.zip
lib: added support for trace_log for verbose messages
The `QRVERBOSE` macro uses the `query` pointer to find out whether the request has trace log enabled. If it does, it uses trace log to log verbose messages using that callback (regardless of whether verbose mode is set or not). This required changing of structure printing functions to formatting functions returning textual representation of the objects (dname, rrset, pkt). This is potentially slower as creates heap objects, but it doesn't happen in the hotpath so it doesn't really matter for verbose logs.
Diffstat (limited to 'contrib')
-rw-r--r--contrib/contrib.mk1
-rw-r--r--contrib/ucw/lib.h1
-rw-r--r--contrib/ucw/mempool-fmt.c98
3 files changed, 100 insertions, 0 deletions
diff --git a/contrib/contrib.mk b/contrib/contrib.mk
index 1753b530..b311c974 100644
--- a/contrib/contrib.mk
+++ b/contrib/contrib.mk
@@ -4,6 +4,7 @@ contrib_SOURCES := \
contrib/ccan/isaac/isaac.c \
contrib/ccan/json/json.c \
contrib/ucw/mempool.c \
+ contrib/ucw/mempool-fmt.c \
contrib/murmurhash3/murmurhash3.c \
contrib/base32hex.c \
contrib/base64.c
diff --git a/contrib/ucw/lib.h b/contrib/ucw/lib.h
index ad0f02d9..77187933 100644
--- a/contrib/ucw/lib.h
+++ b/contrib/ucw/lib.h
@@ -15,6 +15,7 @@
#include <stdarg.h>
#include <stdbool.h>
+#include <stdlib.h>
#ifdef CONFIG_UCW_CLEAN_ABI
#define assert_failed ucw_assert_failed
diff --git a/contrib/ucw/mempool-fmt.c b/contrib/ucw/mempool-fmt.c
new file mode 100644
index 00000000..e9db0ab5
--- /dev/null
+++ b/contrib/ucw/mempool-fmt.c
@@ -0,0 +1,98 @@
+/*
+ * UCW Library -- Memory Pools (Formatting)
+ *
+ * (c) 2005 Martin Mares <mj@ucw.cz>
+ * (c) 2007 Pavel Charvat <pchar@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
+ */
+
+#include <ucw/lib.h>
+#include <ucw/mempool.h>
+
+#include <alloca.h>
+#include <stdio.h>
+#include <string.h>
+
+static char *
+mp_vprintf_at(struct mempool *mp, size_t ofs, const char *fmt, va_list args)
+{
+ char *ret = mp_grow(mp, ofs + 1) + ofs;
+ va_list args2;
+ va_copy(args2, args);
+ int cnt = vsnprintf(ret, mp_avail(mp) - ofs, fmt, args2);
+ va_end(args2);
+ if (cnt < 0)
+ {
+ /* Our C library doesn't support C99 return value of vsnprintf, so we need to iterate */
+ do
+ {
+ ret = mp_expand(mp) + ofs;
+ va_copy(args2, args);
+ cnt = vsnprintf(ret, mp_avail(mp) - ofs, fmt, args2);
+ va_end(args2);
+ }
+ while (cnt < 0);
+ }
+ else if ((uint)cnt >= mp_avail(mp) - ofs)
+ {
+ ret = mp_grow(mp, ofs + cnt + 1) + ofs;
+ va_copy(args2, args);
+ vsnprintf(ret, cnt + 1, fmt, args2);
+ va_end(args2);
+ }
+ mp_end(mp, ret + cnt + 1);
+ return ret - ofs;
+}
+
+char *
+mp_vprintf(struct mempool *mp, const char *fmt, va_list args)
+{
+ mp_start(mp, 1);
+ return mp_vprintf_at(mp, 0, fmt, args);
+}
+
+char *
+mp_printf(struct mempool *p, const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ char *res = mp_vprintf(p, fmt, args);
+ va_end(args);
+ return res;
+}
+
+char *
+mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args)
+{
+ size_t ofs = mp_open(mp, ptr);
+ ASSERT(ofs && !ptr[ofs - 1]);
+ return mp_vprintf_at(mp, ofs - 1, fmt, args);
+}
+
+char *
+mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ char *res = mp_vprintf_append(mp, ptr, fmt, args);
+ va_end(args);
+ return res;
+}
+
+#ifdef TEST
+
+int main(void)
+{
+ struct mempool *mp = mp_new(64);
+ char *x = mp_printf(mp, "<Hello, %s!>", "World");
+ fputs(x, stdout);
+ x = mp_printf_append(mp, x, "<Appended>");
+ fputs(x, stdout);
+ x = mp_printf(mp, "<Hello, %50s!>\n", "World");
+ fputs(x, stdout);
+ return 0;
+}
+
+#endif \ No newline at end of file