From c5a90fd1bf475b4596afc5af83906f3dbb791411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Thu, 22 Nov 2018 16:15:06 +0100 Subject: [PATCH] QRVERBOSE: move more code into a function, add docs There's only very little that makes sense to "inline". My understanding that it makes sense to have such heavier optimization only for the case when no verbose logging is done. This might actually help due to decreasing code size. --- lib/layer.h | 33 +++++++++++++++------------------ lib/layer/iterate.c | 2 +- lib/utils.c | 17 +++++++++++++++++ lib/utils.h | 4 ++++ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/lib/layer.h b/lib/layer.h index f94c2d1e..0909cb75 100644 --- a/lib/layer.h +++ b/lib/layer.h @@ -19,25 +19,22 @@ #include "lib/defines.h" #include "lib/utils.h" -#ifndef NOVERBOSELOG - /** @internal Print a debug message related to resolution. */ - #define QRVERBOSE(query, cls, ...) { \ - const struct kr_query *q = (query); \ - if (kr_log_trace_enabled(q)) { \ - kr_log_trace(q, cls, __VA_ARGS__); \ - } else if (VERBOSE_STATUS) { \ - unsigned _ind = 0; \ - uint32_t _q_uid = q ? q->uid : 0; \ - uint32_t _req_uid = q && q->request ? q->request->uid : 0; \ - for (; q; q = q->parent, _ind += 2); \ - /* simplified kr_log_verbose() calls */ \ - printf("[%05u.%02u][%s] %*s", _req_uid, _q_uid, cls, _ind, ""); \ - printf(__VA_ARGS__); \ - fflush(stdout); \ - } \ -} +#ifdef NOVERBOSELOG + #define QRVERBOSE(query, cls, ...) #else - #define QRVERBOSE(query, cls, ...) + /** Print a debug message related to resolution. + * \param _query associated kr_query, may be NULL + * \param _cls identifying string, typically of length exactly four (padded) + * \param ... printf-compatible list of parameters + */ + #define QRVERBOSE(_query, _cls, ...) do { \ + const struct kr_query *_qry = (_query); \ + if (kr_log_trace_enabled(_qry)) { \ + kr_log_trace(_qry, (_cls), __VA_ARGS__); \ + } else if (VERBOSE_STATUS) { \ + kr_log_qverbose_impl(_qry, (_cls), __VA_ARGS__); \ + } \ + } while (false) #endif /** Layer processing states. Only one value at a time (but see TODO). diff --git a/lib/layer/iterate.c b/lib/layer/iterate.c index 0c4e2a3c..570ca824 100644 --- a/lib/layer/iterate.c +++ b/lib/layer/iterate.c @@ -690,7 +690,7 @@ static int process_answer(knot_pkt_t *pkt, struct kr_request *req) const knot_pktsection_t *ans = knot_pkt_section(pkt, KNOT_ANSWER); if ((pkt_class & (PKT_NOERROR)) && ans->count > 0 && knot_dname_is_equal(pkt_qname, query->zone_cut.name)) { - VERBOSE_MSG("<= continuing with qname minimization\n") + VERBOSE_MSG("<= continuing with qname minimization\n"); } else { /* fall back to disabling minimization */ VERBOSE_MSG("<= retrying with non-minimized name\n"); diff --git a/lib/utils.c b/lib/utils.c index b136c8ee..5c737803 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -133,6 +133,23 @@ void kr_log_verbose(const char *fmt, ...) } } +void kr_log_qverbose_impl(const struct kr_query *qry, const char *cls, const char *fmt, ...) +{ + unsigned ind = 0; + for (const struct kr_query *q = qry; q; q = q->parent) + ind += 2; + uint32_t qry_uid = qry ? qry->uid : 0; + uint32_t req_uid = qry && qry->request ? qry->request->uid : 0; + /* Simplified kr_log_verbose() calls, first prefix then passed fmt... + * Calling it would take about the same amount of code. */ + printf("[%05u.%02u][%s] %*s", req_uid, qry_uid, cls, ind, ""); + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + fflush(stdout); +} + bool kr_log_trace(const struct kr_query *query, const char *source, const char *fmt, ...) { if (!kr_log_trace_enabled(query)) { diff --git a/lib/utils.h b/lib/utils.h index 41bb8b9e..2a846c77 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -56,6 +56,10 @@ KR_EXPORT bool kr_verbose_set(bool status); KR_EXPORT KR_PRINTF(1) void kr_log_verbose(const char *fmt, ...); +/** Utility for QRVERBOSE - use that instead. */ +KR_EXPORT KR_PRINTF(3) +void kr_log_qverbose_impl(const struct kr_query *qry, const char *cls, const char *fmt, ...); + /** * @brief Return true if the query has request log handler installed. */ -- 2.22.0